mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-02-11 03:08:38 +07:00
Added packet pooling synchronization
This commit is contained in:
parent
c8069dfee7
commit
085d309528
@ -26,6 +26,8 @@ import java.io.IOException;
|
||||
import static io.anuke.mindustry.Vars.*;
|
||||
|
||||
public class Net{
|
||||
public static final Object packetPoolLock = new Object();
|
||||
|
||||
private static boolean server;
|
||||
private static boolean active;
|
||||
private static boolean clientLoaded;
|
||||
@ -183,12 +185,16 @@ public class Net{
|
||||
if(clientLoaded || object instanceof ImportantPacket){
|
||||
if(clientListeners.get(object.getClass()) != null) clientListeners.get(object.getClass()).accept(object);
|
||||
if(listeners.get(object.getClass()) != null) listeners.get(object.getClass()).accept(object);
|
||||
Pools.free(object);
|
||||
synchronized (packetPoolLock) {
|
||||
Pools.free(object);
|
||||
}
|
||||
}else if(!(object instanceof UnimportantPacket)){
|
||||
packetQueue.add(object);
|
||||
Log.info("Queuing packet {0}.", ClassReflection.getSimpleName(object.getClass()));
|
||||
}else{
|
||||
Pools.free(object);
|
||||
synchronized (packetPoolLock) {
|
||||
Pools.free(object);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
Log.err("Unhandled packet type: '{0}'!", ClassReflection.getSimpleName(object.getClass()));
|
||||
@ -201,7 +207,9 @@ public class Net{
|
||||
if(serverListeners.get(object.getClass()) != null || listeners.get(object.getClass()) != null){
|
||||
if(serverListeners.get(object.getClass()) != null) serverListeners.get(object.getClass()).accept(connection, object);
|
||||
if(listeners.get(object.getClass()) != null) listeners.get(object.getClass()).accept(object);
|
||||
Pools.free(object);
|
||||
synchronized (packetPoolLock) {
|
||||
Pools.free(object);
|
||||
}
|
||||
}else{
|
||||
Log.err("Unhandled packet type: '{0}'!", ClassReflection.getSimpleName(object.getClass()));
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user