fix network packet stuff, better network performance

This commit is contained in:
bixilon 2020-06-04 21:18:09 +02:00
parent f5fa4c9ead
commit b9e1d9ba5e
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 19 additions and 26 deletions

View File

@ -50,6 +50,7 @@ public class Network {
socket.setKeepAlive(true); socket.setKeepAlive(true);
DataOutputStream dOut = new DataOutputStream(socket.getOutputStream()); DataOutputStream dOut = new DataOutputStream(socket.getOutputStream());
DataInputStream dIn = new DataInputStream(socket.getInputStream()); DataInputStream dIn = new DataInputStream(socket.getInputStream());
socket.getInputStream();
while (connection.getConnectionState() != ConnectionState.DISCONNECTING) { while (connection.getConnectionState() != ConnectionState.DISCONNECTING) {
@ -73,31 +74,25 @@ public class Network {
continue; continue;
} }
// everything sent for now, waiting for data // everything sent for now, waiting for data
List<Byte> raw = new ArrayList<>();
byte[] buffer = new byte[1]; if (dIn.available() > 0) {
while (true) { int numRead = 0;
if (raw.size() > ProtocolDefinition.PROTOCOL_PACKET_MAX_SIZE) { int length = 0;
raw = null; byte read;
break; do {
} read = dIn.readByte();
if (dIn.available() == 0) { int value = (read & 0b01111111);
// packet end length |= (value << (7 * numRead));
break;
} numRead++;
dIn.readFully(buffer, 0, 1); if (numRead > 5) {
raw.add(buffer[0]); throw new RuntimeException("VarInt is too big");
}
} while ((read & 0b10000000) != 0);
byte[] raw = dIn.readNBytes(length);
binQueueIn.add(raw);
} }
if (raw == null || raw.size() == 0) {
// data was tto long, ...
continue;
}
// convert to array
byte[] in = new byte[raw.size()];
for (int i = 0; i < raw.size(); i++) {
in[i] = raw.get(i);
}
// add to queue
binQueueIn.add(in);
Util.sleep(1); Util.sleep(1);
} }

View File

@ -2,12 +2,10 @@ package de.bixilon.minosoft.protocol.protocol;
public class InPacketBuffer extends InByteBuffer { public class InPacketBuffer extends InByteBuffer {
private final int command; private final int command;
private final int length; // not interested in yet
public InPacketBuffer(byte[] bytes) { public InPacketBuffer(byte[] bytes) {
super(bytes); super(bytes);
// ToDo: compression // ToDo: compression
length = readVarInt();
command = readVarInt(); command = readVarInt();
} }