Skip to content

Commit

Permalink
cleaning and keepalive start
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jun 24, 2019
1 parent 3cb4442 commit 4376dfd
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 9 deletions.
@@ -1,7 +1,9 @@
package com.denizenscript.depenizen.bukkit.bungee;

import com.denizenscript.depenizen.bukkit.Depenizen;
import com.denizenscript.depenizen.bukkit.bungee.packets.in.*;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.ControlsProxyPingPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.KeepAlivePacketOut;
import com.denizenscript.depenizen.bukkit.commands.bungee.BungeeCommand;
import com.denizenscript.depenizen.bukkit.commands.bungee.BungeeExecuteCommand;
import com.denizenscript.depenizen.bukkit.commands.bungee.BungeeRunCommand;
Expand All @@ -23,6 +25,7 @@
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.ReplaceableTagEvent;
import net.aufdemrand.denizencore.tags.TagManager;
import org.bukkit.Bukkit;

import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -52,6 +55,10 @@ public class BungeeBridge {

public boolean controlsProxyPing = false;

public int keepAliveTickRate = 10;

public int ticksTilKeepalive = 0;

public void checkBroadcastProxyPing() {
if (connected) {
sendPacket(new ControlsProxyPingPacketOut(controlsProxyPing));
Expand All @@ -67,6 +74,7 @@ public void registerPackets() {
packets.put(55, new PlayerSwitchServerPacketIn());
packets.put(56, new ProxyPingPacketIn());
packets.put(57, new RunScriptPacketIn());
packets.put(58, new RunCommandsPacketIn());
}

public void sendPacket(PacketOut packet) {
Expand All @@ -75,8 +83,9 @@ public void sendPacket(PacketOut packet) {
ByteBuf header = channel.alloc().buffer();
header.writeInt(buf.writerIndex());
header.writeInt(packet.getPacketId());
channel.write(header);
channel.writeAndFlush(header);
channel.writeAndFlush(buf);
ticksTilKeepalive = 0;
}

public void init(String address, int port) {
Expand Down Expand Up @@ -127,6 +136,19 @@ public void run(ReplaceableTagEvent event) {
tagEvent(event);
}
}, "bungee");
Bukkit.getScheduler().scheduleSyncRepeatingTask(Depenizen.instance, new Runnable() {
@Override
public void run() {
if (!connected) {
return;
}
ticksTilKeepalive--;
if (ticksTilKeepalive <= 0) {
sendPacket(new KeepAlivePacketOut());
ticksTilKeepalive = keepAliveTickRate;
}
}
}, 1, 1);
}

public void tagEvent(ReplaceableTagEvent event) {
Expand Down
Expand Up @@ -23,9 +23,7 @@ public void process(ByteBuf data) {
BungeeBridge.instance.handler.fail("Invalid YourInfoPacket (name bytes requested: " + yourNameLength + ")");
return;
}
byte[] serverNameBytes = new byte[yourNameLength];
data.readBytes(serverNameBytes, 0, yourNameLength);
String serverName = new String(serverNameBytes, Charsets.UTF_8);
String serverName = readString(data, yourNameLength);
BungeeBridge.instance.serverName = serverName;
}
}
@@ -0,0 +1,17 @@
package com.denizenscript.depenizen.bukkit.bungee.packets.out;

import com.denizenscript.depenizen.bukkit.bungee.PacketOut;
import io.netty.buffer.ByteBuf;

public class KeepAlivePacketOut extends PacketOut {

@Override
public int getPacketId() {
return 0;
}

@Override
public void writeTo(ByteBuf buf) {
// No data to write
}
}
Expand Up @@ -24,12 +24,9 @@ public void writeTo(ByteBuf buf) {
writeString(buf, server);
ByteBuf nbuf = buf.alloc().buffer();
toSend.writeTo(nbuf);
ByteBuf header = buf.alloc().buffer();
header.writeInt(nbuf.writerIndex());
header.writeInt(toSend.getPacketId());
buf.writeBytes(header);
buf.writeInt(nbuf.writerIndex());
buf.writeInt(toSend.getPacketId());
buf.writeBytes(nbuf);
header.release();
nbuf.release();
}
}
Expand Up @@ -2,6 +2,7 @@

import com.denizenscript.depenizen.bukkit.bungee.BungeeBridge;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.ExecuteCommandPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.KeepAlivePacketOut;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
Expand Down Expand Up @@ -58,5 +59,6 @@ public void execute(ScriptEntry scriptEntry) {
}
ExecuteCommandPacketOut packet = new ExecuteCommandPacketOut(command.asString());
BungeeBridge.instance.sendPacket(packet);
BungeeBridge.instance.sendPacket(new KeepAlivePacketOut());
}
}
@@ -1,6 +1,7 @@
package com.denizenscript.depenizen.bukkit.commands.bungee;

import com.denizenscript.depenizen.bukkit.bungee.BungeeBridge;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.KeepAlivePacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.RedirectPacketOut;
import com.denizenscript.depenizen.bukkit.bungee.packets.out.redirectable.RunScriptPacketOut;
import net.aufdemrand.denizen.BukkitScriptEntryData;
Expand Down Expand Up @@ -83,6 +84,7 @@ public void execute(ScriptEntry scriptEntry) {
for (String server : servers) {
RedirectPacketOut packet = new RedirectPacketOut(server, packetScript);
BungeeBridge.instance.sendPacket(packet);
BungeeBridge.instance.sendPacket(new KeepAlivePacketOut());
}
}
}

0 comments on commit 4376dfd

Please sign in to comment.