fix concurrency issue in packethandler

This commit is contained in:
sirjonasxx 2020-05-22 18:32:36 +02:00
parent 72b612f796
commit cfe6b23c33

View File

@ -23,7 +23,8 @@ public abstract class PacketHandler {
volatile boolean isDataStream = false; volatile boolean isDataStream = false;
private volatile int currentIndex = 0; private volatile int currentIndex = 0;
private final Object lock = new Object(); private final Object manipulationLock = new Object();
private final Object sendLock = new Object();
private RC4 decryptcipher = null; private RC4 decryptcipher = null;
private RC4 encryptcipher = null; private RC4 encryptcipher = null;
@ -58,7 +59,7 @@ public abstract class PacketHandler {
payloadBuffer.push(buffer); payloadBuffer.push(buffer);
} }
else if (!HConnection.DECRYPTPACKETS) { else if (!HConnection.DECRYPTPACKETS) {
synchronized (lock) { synchronized (sendLock) {
out.write(buffer); out.write(buffer);
} }
} }
@ -125,7 +126,7 @@ public abstract class PacketHandler {
} }
public void sendToStream(byte[] buffer) { public void sendToStream(byte[] buffer) {
synchronized (lock) { synchronized (sendLock) {
try { try {
out.write( out.write(
(!isEncryptedStream) (!isEncryptedStream)
@ -139,7 +140,7 @@ public abstract class PacketHandler {
} }
public void flush() throws IOException { public void flush() throws IOException {
synchronized (lock) { synchronized (manipulationLock) {
HPacket[] hpackets = payloadBuffer.receive(); HPacket[] hpackets = payloadBuffer.receive();
for (HPacket hpacket : hpackets){ for (HPacket hpacket : hpackets){
@ -150,11 +151,13 @@ public abstract class PacketHandler {
} }
if (!hMessage.isBlocked()) { if (!hMessage.isBlocked()) {
out.write( synchronized (sendLock) {
(!isencrypted) out.write(
? hMessage.getPacket().toBytes() (!isencrypted)
: encryptcipher.rc4(hMessage.getPacket().toBytes()) ? hMessage.getPacket().toBytes()
); : encryptcipher.rc4(hMessage.getPacket().toBytes())
);
}
} }
currentIndex++; currentIndex++;
} }