only start handling and packet thread when connected to server

This commit is contained in:
bixilon 2020-06-03 14:47:07 +02:00
parent 38843dedde
commit dfefd5c50f
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 19 additions and 14 deletions

View File

@ -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();
}
}

View File

@ -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) {