forked from BLOCKFANTASY/LOOHP-Limbo
Fixed keepalive packet issue
This commit is contained in:
parent
9d507f684f
commit
224e12a6a8
Binary file not shown.
2
pom.xml
2
pom.xml
|
|
@ -5,7 +5,7 @@
|
||||||
<groupId>com.loohp</groupId>
|
<groupId>com.loohp</groupId>
|
||||||
<artifactId>Limbo</artifactId>
|
<artifactId>Limbo</artifactId>
|
||||||
<name>Limbo</name>
|
<name>Limbo</name>
|
||||||
<version>0.6.7-ALPHA</version>
|
<version>0.6.8-ALPHA</version>
|
||||||
|
|
||||||
<description>Standalone Limbo Minecraft Server.</description>
|
<description>Standalone Limbo Minecraft Server.</description>
|
||||||
<url>https://github.com/LOOHP/Limbo</url>
|
<url>https://github.com/LOOHP/Limbo</url>
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.Timer;
|
||||||
|
import java.util.TimerTask;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
@ -55,6 +57,7 @@ import com.loohp.limbo.server.packets.PacketPlayOutDisconnect;
|
||||||
import com.loohp.limbo.server.packets.PacketPlayOutEntityMetadata;
|
import com.loohp.limbo.server.packets.PacketPlayOutEntityMetadata;
|
||||||
import com.loohp.limbo.server.packets.PacketPlayOutGameState;
|
import com.loohp.limbo.server.packets.PacketPlayOutGameState;
|
||||||
import com.loohp.limbo.server.packets.PacketPlayOutHeldItemChange;
|
import com.loohp.limbo.server.packets.PacketPlayOutHeldItemChange;
|
||||||
|
import com.loohp.limbo.server.packets.PacketPlayOutKeepAlive;
|
||||||
import com.loohp.limbo.server.packets.PacketPlayOutLogin;
|
import com.loohp.limbo.server.packets.PacketPlayOutLogin;
|
||||||
import com.loohp.limbo.server.packets.PacketPlayOutPlayerAbilities;
|
import com.loohp.limbo.server.packets.PacketPlayOutPlayerAbilities;
|
||||||
import com.loohp.limbo.server.packets.PacketPlayOutPlayerAbilities.PlayerAbilityFlags;
|
import com.loohp.limbo.server.packets.PacketPlayOutPlayerAbilities.PlayerAbilityFlags;
|
||||||
|
|
@ -104,7 +107,9 @@ public class ClientConnection extends Thread {
|
||||||
private boolean running;
|
private boolean running;
|
||||||
private ClientState state;
|
private ClientState state;
|
||||||
|
|
||||||
private Player player;
|
private Player player;
|
||||||
|
private TimerTask keepAliveTask;
|
||||||
|
private AtomicLong lastPacketTimestamp;
|
||||||
private AtomicLong lastKeepAlivePayLoad;
|
private AtomicLong lastKeepAlivePayLoad;
|
||||||
|
|
||||||
private DataOutputStream output;
|
private DataOutputStream output;
|
||||||
|
|
@ -117,7 +122,8 @@ public class ClientConnection extends Thread {
|
||||||
public ClientConnection(Socket client_socket) {
|
public ClientConnection(Socket client_socket) {
|
||||||
this.client_socket = client_socket;
|
this.client_socket = client_socket;
|
||||||
this.inetAddress = client_socket.getInetAddress();
|
this.inetAddress = client_socket.getInetAddress();
|
||||||
this.lastKeepAlivePayLoad = new AtomicLong();
|
this.lastPacketTimestamp = new AtomicLong(-1);
|
||||||
|
this.lastKeepAlivePayLoad = new AtomicLong(-1);
|
||||||
this.running = false;
|
this.running = false;
|
||||||
this.ready = false;
|
this.ready = false;
|
||||||
}
|
}
|
||||||
|
|
@ -133,6 +139,18 @@ public class ClientConnection extends Thread {
|
||||||
public void setLastKeepAlivePayLoad(long payLoad) {
|
public void setLastKeepAlivePayLoad(long payLoad) {
|
||||||
this.lastKeepAlivePayLoad.set(payLoad);
|
this.lastKeepAlivePayLoad.set(payLoad);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getLastPacketTimestamp() {
|
||||||
|
return lastPacketTimestamp.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastPacketTimestamp(long payLoad) {
|
||||||
|
this.lastPacketTimestamp.set(payLoad);
|
||||||
|
}
|
||||||
|
|
||||||
|
public TimerTask getKeepAliveTask() {
|
||||||
|
return this.keepAliveTask;
|
||||||
|
}
|
||||||
|
|
||||||
public Player getPlayer() {
|
public Player getPlayer() {
|
||||||
return player;
|
return player;
|
||||||
|
|
@ -159,6 +177,7 @@ public class ClientConnection extends Thread {
|
||||||
DataTypeIO.writeVarInt(output, packetByte.length);
|
DataTypeIO.writeVarInt(output, packetByte.length);
|
||||||
output.write(packetByte);
|
output.write(packetByte);
|
||||||
output.flush();
|
output.flush();
|
||||||
|
setLastPacketTimestamp(System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect(BaseComponent[] reason) {
|
public void disconnect(BaseComponent[] reason) {
|
||||||
|
|
@ -436,6 +455,25 @@ public class ClientConnection extends Thread {
|
||||||
player.setPlayerListHeaderFooter(properties.getTabHeader(), properties.getTabFooter());
|
player.setPlayerListHeaderFooter(properties.getTabHeader(), properties.getTabFooter());
|
||||||
|
|
||||||
ready = true;
|
ready = true;
|
||||||
|
|
||||||
|
keepAliveTask = new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
if (ready) {
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
if (now - getLastPacketTimestamp() > 15000) {
|
||||||
|
PacketPlayOutKeepAlive keepAlivePacket = new PacketPlayOutKeepAlive(now);
|
||||||
|
try {
|
||||||
|
sendPacket(keepAlivePacket);
|
||||||
|
setLastKeepAlivePayLoad(now);
|
||||||
|
} catch (Exception e) {}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
new Timer().schedule(keepAliveTask, 5000, 10000);
|
||||||
|
|
||||||
while (client_socket.isConnected()) {
|
while (client_socket.isConnected()) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -1,40 +0,0 @@
|
||||||
package com.loohp.limbo.server;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import com.loohp.limbo.Limbo;
|
|
||||||
import com.loohp.limbo.server.ClientConnection.ClientState;
|
|
||||||
import com.loohp.limbo.server.packets.PacketPlayOutKeepAlive;
|
|
||||||
|
|
||||||
public class KeepAliveSender extends Thread {
|
|
||||||
|
|
||||||
private Random random;
|
|
||||||
|
|
||||||
public KeepAliveSender() {
|
|
||||||
random = new Random();
|
|
||||||
start();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
for (ClientConnection client : Limbo.getInstance().getServerConnection().getClients()) {
|
|
||||||
if (client.getClientState() != null && client.getClientState().equals(ClientState.PLAY)) {
|
|
||||||
try {
|
|
||||||
PacketPlayOutKeepAlive packet = new PacketPlayOutKeepAlive(random.nextLong());
|
|
||||||
client.setLastKeepAlivePayLoad(packet.getPayload());
|
|
||||||
client.sendPacket(packet);
|
|
||||||
} catch (IOException ignore) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TimeUnit.SECONDS.sleep(5);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -15,14 +15,12 @@ public class ServerConnection extends Thread {
|
||||||
private List<ClientConnection> clients;
|
private List<ClientConnection> clients;
|
||||||
private String ip;
|
private String ip;
|
||||||
private int port;
|
private int port;
|
||||||
private KeepAliveSender keepAliveSender;
|
|
||||||
|
|
||||||
public ServerConnection(String ip, int port) {
|
public ServerConnection(String ip, int port) {
|
||||||
clients = new ArrayList<ClientConnection>();
|
clients = new ArrayList<ClientConnection>();
|
||||||
this.ip = ip;
|
this.ip = ip;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
start();
|
start();
|
||||||
keepAliveSender = new KeepAliveSender();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -32,8 +30,6 @@ public class ServerConnection extends Thread {
|
||||||
Limbo.getInstance().getConsole().sendMessage("Limbo server listening on /" + serverSocket.getInetAddress().getHostName() + ":" + serverSocket.getLocalPort());
|
Limbo.getInstance().getConsole().sendMessage("Limbo server listening on /" + serverSocket.getInetAddress().getHostName() + ":" + serverSocket.getLocalPort());
|
||||||
while (true) {
|
while (true) {
|
||||||
Socket connection = serverSocket.accept();
|
Socket connection = serverSocket.accept();
|
||||||
//String str = connection.getInetAddress().getHostName() + ":" + connection.getPort();
|
|
||||||
//Limbo.getInstance().getConsole().sendMessage("[/127.0.0.1:57310] <-> InitialHandler has pinged);
|
|
||||||
ClientConnection sc = new ClientConnection(connection);
|
ClientConnection sc = new ClientConnection(connection);
|
||||||
clients.add(sc);
|
clients.add(sc);
|
||||||
sc.start();
|
sc.start();
|
||||||
|
|
@ -42,10 +38,6 @@ public class ServerConnection extends Thread {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public KeepAliveSender getKeepAliveSender() {
|
|
||||||
return keepAliveSender;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServerSocket getServerSocket() {
|
public ServerSocket getServerSocket() {
|
||||||
return serverSocket;
|
return serverSocket;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue