From cfe6b23c33605d41ea422ddcf32b3c5e4512da3c Mon Sep 17 00:00:00 2001 From: sirjonasxx <36828922+sirjonasxx@users.noreply.github.com> Date: Fri, 22 May 2020 18:32:36 +0200 Subject: [PATCH] fix concurrency issue in packethandler --- .../protocol/packethandler/PacketHandler.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java b/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java index c72b25e..49b9379 100644 --- a/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java +++ b/G-Earth/src/main/java/gearth/protocol/packethandler/PacketHandler.java @@ -23,7 +23,8 @@ public abstract class PacketHandler { volatile boolean isDataStream = false; 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 encryptcipher = null; @@ -58,7 +59,7 @@ public abstract class PacketHandler { payloadBuffer.push(buffer); } else if (!HConnection.DECRYPTPACKETS) { - synchronized (lock) { + synchronized (sendLock) { out.write(buffer); } } @@ -125,7 +126,7 @@ public abstract class PacketHandler { } public void sendToStream(byte[] buffer) { - synchronized (lock) { + synchronized (sendLock) { try { out.write( (!isEncryptedStream) @@ -139,7 +140,7 @@ public abstract class PacketHandler { } public void flush() throws IOException { - synchronized (lock) { + synchronized (manipulationLock) { HPacket[] hpackets = payloadBuffer.receive(); for (HPacket hpacket : hpackets){ @@ -150,11 +151,13 @@ public abstract class PacketHandler { } if (!hMessage.isBlocked()) { - out.write( - (!isencrypted) - ? hMessage.getPacket().toBytes() - : encryptcipher.rc4(hMessage.getPacket().toBytes()) - ); + synchronized (sendLock) { + out.write( + (!isencrypted) + ? hMessage.getPacket().toBytes() + : encryptcipher.rc4(hMessage.getPacket().toBytes()) + ); + } } currentIndex++; }