mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-13 01:16:46 -04:00
remove packet thread (only 1 network thread now)
This commit is contained in:
parent
e5450616e0
commit
838a6231c1
@ -95,7 +95,6 @@ public class Connection {
|
|||||||
switch (state) {
|
switch (state) {
|
||||||
case HANDSHAKING:
|
case HANDSHAKING:
|
||||||
// connection established, starting threads and logging in
|
// connection established, starting threads and logging in
|
||||||
network.startPacketThread();
|
|
||||||
startHandlingThread();
|
startHandlingThread();
|
||||||
ConnectionState next = ((reason == ConnectionReason.CONNECT) ? ConnectionState.LOGIN : ConnectionState.STATUS);
|
ConnectionState next = ((reason == ConnectionReason.CONNECT) ? ConnectionState.LOGIN : ConnectionState.STATUS);
|
||||||
network.sendPacket(new PacketHandshake(getHost(), getPort(), next, (next == ConnectionState.STATUS) ? -1 : getVersion().getVersionNumber()));
|
network.sendPacket(new PacketHandshake(getHost(), getPort(), next, (next == ConnectionState.STATUS) ? -1 : getVersion().getVersionNumber()));
|
||||||
|
@ -38,15 +38,12 @@ import java.util.List;
|
|||||||
public class Network {
|
public class Network {
|
||||||
final Connection connection;
|
final Connection connection;
|
||||||
final List<ServerboundPacket> queue;
|
final List<ServerboundPacket> queue;
|
||||||
final List<byte[]> binQueue;
|
|
||||||
final List<byte[]> binQueueIn;
|
|
||||||
Thread socketThread;
|
Thread socketThread;
|
||||||
int compressionThreshold = -1;
|
int compressionThreshold = -1;
|
||||||
Socket socket;
|
Socket socket;
|
||||||
OutputStream outputStream;
|
OutputStream outputStream;
|
||||||
InputStream cipherInputStream;
|
InputStream cipherInputStream;
|
||||||
InputStream inputStream;
|
InputStream inputStream;
|
||||||
Thread packetThread;
|
|
||||||
boolean encryptionEnabled = false;
|
boolean encryptionEnabled = false;
|
||||||
SecretKey secretKey;
|
SecretKey secretKey;
|
||||||
boolean connected;
|
boolean connected;
|
||||||
@ -54,8 +51,6 @@ public class Network {
|
|||||||
public Network(Connection c) {
|
public Network(Connection c) {
|
||||||
this.connection = c;
|
this.connection = c;
|
||||||
this.queue = new ArrayList<>();
|
this.queue = new ArrayList<>();
|
||||||
this.binQueue = new ArrayList<>();
|
|
||||||
this.binQueueIn = new ArrayList<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void connect() {
|
public void connect() {
|
||||||
@ -86,65 +81,6 @@ public class Network {
|
|||||||
break;
|
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) {
|
while (queue.size() > 0) {
|
||||||
ServerboundPacket p = queue.get(0);
|
ServerboundPacket p = queue.get(0);
|
||||||
@ -177,18 +113,36 @@ public class Network {
|
|||||||
data = bufferWithLengthPrefix.getOutBytes();
|
data = bufferWithLengthPrefix.getOutBytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
outputStream.write(data);
|
||||||
binQueue.add(data);
|
outputStream.flush();
|
||||||
if (p instanceof PacketEncryptionResponse) {
|
if (p instanceof PacketEncryptionResponse) {
|
||||||
// enable encryption
|
// enable encryption
|
||||||
secretKey = ((PacketEncryptionResponse) p).getSecretKey();
|
secretKey = ((PacketEncryptionResponse) p).getSecretKey();
|
||||||
|
enableEncryption(secretKey);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (binQueueIn.size() > 0) {
|
|
||||||
|
|
||||||
// read data
|
|
||||||
byte[] data = binQueueIn.get(0);
|
// everything sent for now, waiting for data
|
||||||
binQueueIn.remove(0);
|
|
||||||
|
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) {
|
if (compressionThreshold != -1) {
|
||||||
// compression is enabled
|
// compression is enabled
|
||||||
// check if there is a need to decompress it and if so, do it!
|
// check if there is a need to decompress it and if so, do it!
|
||||||
@ -246,22 +200,26 @@ public class Network {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
Util.sleep(1);
|
||||||
// sleep, wait for an interrupt from other thread
|
|
||||||
//noinspection BusyWait
|
|
||||||
Thread.sleep(100);
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
socket.close();
|
||||||
packetThread.setName("Packet-Thread");
|
connected = false;
|
||||||
packetThread.start();
|
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) {
|
public void sendPacket(ServerboundPacket p) {
|
||||||
queue.add(p);
|
queue.add(p);
|
||||||
packetThread.interrupt();
|
socketThread.interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void enableEncryption(SecretKey secretKey) {
|
public void enableEncryption(SecretKey secretKey) {
|
||||||
@ -278,7 +236,7 @@ public class Network {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() {
|
public void disconnect() {
|
||||||
packetThread.interrupt();
|
socketThread.interrupt();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -35,8 +35,7 @@ public class Util {
|
|||||||
public static void sleep(int ms) {
|
public static void sleep(int ms) {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(ms);
|
Thread.sleep(ms);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException ignored) {
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user