Added packet pooling synchronization

This commit is contained in:
Anuken
2018-06-27 14:55:37 -04:00
parent c8069dfee7
commit 085d309528
2 changed files with 21 additions and 7 deletions

View File

@@ -9,6 +9,8 @@ import io.anuke.mindustry.net.Registrator;
import java.nio.ByteBuffer;
import static io.anuke.mindustry.net.Net.packetPoolLock;
public class ByteSerializer implements Serialization {
@Override
@@ -24,7 +26,9 @@ public class ByteSerializer implements Serialization {
throw new RuntimeException("Unregistered class: " + ClassReflection.getSimpleName(o.getClass()));
byteBuffer.put(id);
((Packet) o).write(byteBuffer);
Pools.free(o);
synchronized (packetPoolLock) {
Pools.free(o);
}
}
}
@@ -35,9 +39,11 @@ public class ByteSerializer implements Serialization {
return FrameworkSerializer.read(byteBuffer);
}else{
Class<?> type = Registrator.getByID(id);
Packet packet = (Packet)Pools.obtain(type);
packet.read(byteBuffer);
return packet;
synchronized (packetPoolLock) {
Packet packet = (Packet) Pools.obtain(type);
packet.read(byteBuffer);
return packet;
}
}
}