From 7a94ebcf2a2aee1692a9e2b8f10946a514963787 Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 7 May 2018 14:23:52 -0400 Subject: [PATCH] Implemented packet pooling --- core/src/io/anuke/mindustry/core/Control.java | 1 - core/src/io/anuke/mindustry/net/Net.java | 6 +++++ core/src/io/anuke/mindustry/net/Packet.java | 6 ++++- core/src/io/anuke/mindustry/net/Packets.java | 11 ++++----- .../src/io/anuke/kryonet/ByteSerializer.java | 23 ++++++++----------- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/core/src/io/anuke/mindustry/core/Control.java b/core/src/io/anuke/mindustry/core/Control.java index fdf693e0a5..ceb98e2042 100644 --- a/core/src/io/anuke/mindustry/core/Control.java +++ b/core/src/io/anuke/mindustry/core/Control.java @@ -113,7 +113,6 @@ public class Control extends Module{ "name", mobile || gwt ? "player" : UCore.getProperty("user.name"), "servers", "", "color", Color.rgba8888(playerColors[8]), - "lastVersion", "3.2", "lastBuild", 0 ); diff --git a/core/src/io/anuke/mindustry/net/Net.java b/core/src/io/anuke/mindustry/net/Net.java index e41b960694..d0889d85c1 100644 --- a/core/src/io/anuke/mindustry/net/Net.java +++ b/core/src/io/anuke/mindustry/net/Net.java @@ -8,6 +8,7 @@ import com.badlogic.gdx.net.HttpRequestBuilder; import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.IntMap; import com.badlogic.gdx.utils.ObjectMap; +import com.badlogic.gdx.utils.Pools; import com.badlogic.gdx.utils.reflect.ClassReflection; import io.anuke.mindustry.io.Platform; import io.anuke.mindustry.net.Packet.ImportantPacket; @@ -55,6 +56,7 @@ public class Net{ for(int i = 0; i < packetQueue.size; i ++){ Log.info("Processing {0} packet post-load.", ClassReflection.getSimpleName(packetQueue.get(i).getClass())); handleClientReceived(packetQueue.get(i)); + Pools.free(packetQueue.get(i)); } } //clear inbound packet queue @@ -182,9 +184,12 @@ 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); }else if(!(object instanceof UnimportantPacket)){ packetQueue.add(object); Log.info("Queuing packet {0}.", ClassReflection.getSimpleName(object.getClass())); + }else{ + Pools.free(object); } }else{ Log.err("Unhandled packet type: '{0}'!", ClassReflection.getSimpleName(object.getClass())); @@ -198,6 +203,7 @@ 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); }else{ Log.err("Unhandled packet type: '{0}'!", ClassReflection.getSimpleName(object.getClass())); } diff --git a/core/src/io/anuke/mindustry/net/Packet.java b/core/src/io/anuke/mindustry/net/Packet.java index 3c0084ba1e..4bbef9db0b 100644 --- a/core/src/io/anuke/mindustry/net/Packet.java +++ b/core/src/io/anuke/mindustry/net/Packet.java @@ -1,11 +1,15 @@ package io.anuke.mindustry.net; +import com.badlogic.gdx.utils.Pool.Poolable; + import java.nio.ByteBuffer; -public interface Packet { +public interface Packet extends Poolable{ void read(ByteBuffer buffer); void write(ByteBuffer buffer); + default void reset() {} + interface ImportantPacket{} interface UnimportantPacket{} } diff --git a/core/src/io/anuke/mindustry/net/Packets.java b/core/src/io/anuke/mindustry/net/Packets.java index ff371d221e..a0cce621e1 100644 --- a/core/src/io/anuke/mindustry/net/Packets.java +++ b/core/src/io/anuke/mindustry/net/Packets.java @@ -328,6 +328,8 @@ public class Packets { byte[] n = new byte[nlength]; buffer.get(n); name = new String(n); + }else{ + name = null; } text = IOUtils.readString(buffer); @@ -531,8 +533,7 @@ public class Packets { @Override public void write(ByteBuffer buffer) { buffer.putInt(info.playerid); - buffer.putShort((short)info.ip.getBytes().length); - buffer.put(info.ip.getBytes()); + IOUtils.writeString(buffer, info.ip); buffer.put(info.modclient ? (byte)1 : 0); buffer.put(info.android ? (byte)1 : 0); @@ -548,11 +549,9 @@ public class Packets { @Override public void read(ByteBuffer buffer) { int id = buffer.getInt(); - short iplen = buffer.getShort(); - byte[] ipb = new byte[iplen]; - buffer.get(ipb); + String ip = IOUtils.readString(buffer); - info = new TraceInfo(new String(ipb)); + info = new TraceInfo(ip); info.playerid = id; info.modclient = buffer.get() == 1; diff --git a/kryonet/src/io/anuke/kryonet/ByteSerializer.java b/kryonet/src/io/anuke/kryonet/ByteSerializer.java index 257ffce93c..7a032af514 100644 --- a/kryonet/src/io/anuke/kryonet/ByteSerializer.java +++ b/kryonet/src/io/anuke/kryonet/ByteSerializer.java @@ -1,7 +1,7 @@ package io.anuke.kryonet; +import com.badlogic.gdx.utils.Pools; import com.badlogic.gdx.utils.reflect.ClassReflection; -import com.badlogic.gdx.utils.reflect.ReflectionException; import com.esotericsoftware.kryonet.FrameworkMessage; import com.esotericsoftware.kryonet.serialization.Serialization; import io.anuke.mindustry.net.Packet; @@ -24,23 +24,20 @@ public class ByteSerializer implements Serialization { throw new RuntimeException("Unregistered class: " + ClassReflection.getSimpleName(o.getClass())); byteBuffer.put(id); ((Packet) o).write(byteBuffer); + Pools.free(o); } } @Override public Object read(ByteBuffer byteBuffer) { - try { - byte id = byteBuffer.get(); - if(id == -2){ - return FrameworkSerializer.read(byteBuffer); - }else { - Class type = Registrator.getByID(id); - Packet packet = (Packet) ClassReflection.newInstance(type); - packet.read(byteBuffer); - return packet; - } - }catch (ReflectionException e){ - throw new RuntimeException(e); + byte id = byteBuffer.get(); + if(id == -2){ + return FrameworkSerializer.read(byteBuffer); + }else{ + Class type = Registrator.getByID(id); + Packet packet = (Packet)Pools.obtain(type); + packet.read(byteBuffer); + return packet; } }