diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java index f1bb69c56..ae569dd4c 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java @@ -34,16 +34,6 @@ public class Connection { network = new Network(this); handlingQueue = new ArrayList<>(); handler = new PacketHandler(this); - Thread handleThread = new Thread(() -> { - while (getConnectionState() != ConnectionState.DISCONNECTING) { - while (handlingQueue.size() > 0) { - handlingQueue.get(0).handle(getHandler()); - handlingQueue.remove(0); - } - Util.sleep(1); - } - }); - handleThread.start(); } /** @@ -83,7 +73,9 @@ public class Connection { this.state = state; switch (state) { case HANDSHAKING: - // connection established, logging in + // connection established, starting threads and logging in + network.startPacketThread(); + startHandlingThread(); ConnectionState next = (onlyPing ? ConnectionState.STATUS : ConnectionState.LOGIN); network.sendPacket(new PacketHandshake(getHost(), getPort(), next, (onlyPing) ? -1 : getVersion().getVersion())); // after sending it, switch to next state @@ -128,4 +120,17 @@ public class Connection { public void sendPacket(ServerboundPacket p) { network.sendPacket(p); } + + private void startHandlingThread() { + Thread handleThread = new Thread(() -> { + while (getConnectionState() != ConnectionState.DISCONNECTED) { + while (handlingQueue.size() > 0) { + handlingQueue.get(0).handle(getHandler()); + handlingQueue.remove(0); + } + Util.sleep(1); + } + }); + handleThread.start(); + } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/Network.java b/src/main/java/de/bixilon/minosoft/protocol/network/Network.java index 5f4fae028..a648f125f 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/Network.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/Network.java @@ -108,14 +108,16 @@ public class Network { } }); socketThread.start(); + } + public void startPacketThread() { // compressed data, makes packets to binary data // read data // safety first, but will not occur // sleep 1 ms Thread packetThread = new Thread(() -> { // compressed data, makes packets to binary data - while (connection.getConnectionState() != ConnectionState.DISCONNECTING) { + while (connection.getConnectionState() != ConnectionState.DISCONNECTED) { while (queue.size() > 0) { ServerboundPacket p = queue.get(0); @@ -170,10 +172,8 @@ public class Network { Util.sleep(1); // sleep 1 ms } - connection.setConnectionState(ConnectionState.DISCONNECTED); }); packetThread.start(); - } public void sendPacket(ServerboundPacket p) {