remove packet thread (only 1 network thread now)

This commit is contained in:
Bixilon 2020-07-16 01:31:37 +02:00
parent e5450616e0
commit 838a6231c1
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 98 additions and 142 deletions

View File

@ -95,7 +95,6 @@ public class Connection {
switch (state) {
case HANDSHAKING:
// connection established, starting threads and logging in
network.startPacketThread();
startHandlingThread();
ConnectionState next = ((reason == ConnectionReason.CONNECT) ? ConnectionState.LOGIN : ConnectionState.STATUS);
network.sendPacket(new PacketHandshake(getHost(), getPort(), next, (next == ConnectionState.STATUS) ? -1 : getVersion().getVersionNumber()));

View File

@ -38,15 +38,12 @@ import java.util.List;
public class Network {
final Connection connection;
final List<ServerboundPacket> queue;
final List<byte[]> binQueue;
final List<byte[]> binQueueIn;
Thread socketThread;
int compressionThreshold = -1;
Socket socket;
OutputStream outputStream;
InputStream cipherInputStream;
InputStream inputStream;
Thread packetThread;
boolean encryptionEnabled = false;
SecretKey secretKey;
boolean connected;
@ -54,8 +51,6 @@ public class Network {
public Network(Connection c) {
this.connection = c;
this.queue = new ArrayList<>();
this.binQueue = new ArrayList<>();
this.binQueueIn = new ArrayList<>();
}
public void connect() {
@ -86,65 +81,6 @@ public class Network {
break;
}
while (binQueue.size() > 0) {
// something to send it, send it
byte[] b = binQueue.get(0);
// send, flush and remove
outputStream.write(b);
outputStream.flush();
binQueue.remove(0);
// check if should enable encryption
if (!encryptionEnabled && secretKey != null) {
enableEncryption(secretKey);
}
}
// everything sent for now, waiting for data
if (inputStream.available() > 0) { // available seems not to work in CipherInputStream
int numRead = 0;
int length = 0;
byte read;
do {
read = cipherInputStream.readNBytes(1)[0];
int value = (read & 0b01111111);
length |= (value << (7 * numRead));
numRead++;
if (numRead > 5) {
throw new RuntimeException("VarInt is too big");
}
} while ((read & 0b10000000) != 0);
byte[] raw = cipherInputStream.readNBytes(length);
binQueueIn.add(raw);
packetThread.interrupt();
}
Util.sleep(1);
}
socket.close();
connected = false;
connection.setConnectionState(ConnectionState.DISCONNECTED);
} catch (IOException e) {
// Could not connect
connection.setConnectionState(ConnectionState.DISCONNECTED);
e.printStackTrace();
}
});
socketThread.setName("Socket-Thread");
socketThread.start();
}
public void startPacketThread() {
// compressed data, makes packets to binary data
// read data
// safety first, but will not occur
packetThread = new Thread(() -> {
// compressed data, makes packets to binary data
while (connection.getConnectionState() != ConnectionState.DISCONNECTING) {
while (queue.size() > 0) {
ServerboundPacket p = queue.get(0);
@ -177,18 +113,36 @@ public class Network {
data = bufferWithLengthPrefix.getOutBytes();
}
binQueue.add(data);
outputStream.write(data);
outputStream.flush();
if (p instanceof PacketEncryptionResponse) {
// enable encryption
secretKey = ((PacketEncryptionResponse) p).getSecretKey();
enableEncryption(secretKey);
}
}
while (binQueueIn.size() > 0) {
// read data
byte[] data = binQueueIn.get(0);
binQueueIn.remove(0);
// everything sent for now, waiting for data
if (inputStream.available() > 0) { // available seems not to work in CipherInputStream
int numRead = 0;
int length = 0;
byte read;
do {
read = cipherInputStream.readNBytes(1)[0];
int value = (read & 0b01111111);
length |= (value << (7 * numRead));
numRead++;
if (numRead > 5) {
throw new RuntimeException("VarInt is too big");
}
} while ((read & 0b10000000) != 0);
byte[] data = cipherInputStream.readNBytes(length);
if (compressionThreshold != -1) {
// compression is enabled
// check if there is a need to decompress it and if so, do it!
@ -246,22 +200,26 @@ public class Network {
e.printStackTrace();
}
}
try {
// sleep, wait for an interrupt from other thread
//noinspection BusyWait
Thread.sleep(100);
} catch (InterruptedException ignored) {
}
Util.sleep(1);
}
});
packetThread.setName("Packet-Thread");
packetThread.start();
socket.close();
connected = false;
connection.setConnectionState(ConnectionState.DISCONNECTED);
} catch (IOException e) {
// Could not connect
connection.setConnectionState(ConnectionState.DISCONNECTED);
e.printStackTrace();
}
});
socketThread.setName("Socket-Thread");
socketThread.start();
}
public void sendPacket(ServerboundPacket p) {
queue.add(p);
packetThread.interrupt();
socketThread.interrupt();
}
public void enableEncryption(SecretKey secretKey) {
@ -278,7 +236,7 @@ public class Network {
}
public void disconnect() {
packetThread.interrupt();
socketThread.interrupt();
}
}

View File

@ -35,8 +35,7 @@ public class Util {
public static void sleep(int ms) {
try {
Thread.sleep(ms);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InterruptedException ignored) {
}
}