Implemented packet pooling
This commit is contained in:
@@ -113,7 +113,6 @@ public class Control extends Module{
|
|||||||
"name", mobile || gwt ? "player" : UCore.getProperty("user.name"),
|
"name", mobile || gwt ? "player" : UCore.getProperty("user.name"),
|
||||||
"servers", "",
|
"servers", "",
|
||||||
"color", Color.rgba8888(playerColors[8]),
|
"color", Color.rgba8888(playerColors[8]),
|
||||||
"lastVersion", "3.2",
|
|
||||||
"lastBuild", 0
|
"lastBuild", 0
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.badlogic.gdx.net.HttpRequestBuilder;
|
|||||||
import com.badlogic.gdx.utils.Array;
|
import com.badlogic.gdx.utils.Array;
|
||||||
import com.badlogic.gdx.utils.IntMap;
|
import com.badlogic.gdx.utils.IntMap;
|
||||||
import com.badlogic.gdx.utils.ObjectMap;
|
import com.badlogic.gdx.utils.ObjectMap;
|
||||||
|
import com.badlogic.gdx.utils.Pools;
|
||||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||||
import io.anuke.mindustry.io.Platform;
|
import io.anuke.mindustry.io.Platform;
|
||||||
import io.anuke.mindustry.net.Packet.ImportantPacket;
|
import io.anuke.mindustry.net.Packet.ImportantPacket;
|
||||||
@@ -55,6 +56,7 @@ public class Net{
|
|||||||
for(int i = 0; i < packetQueue.size; i ++){
|
for(int i = 0; i < packetQueue.size; i ++){
|
||||||
Log.info("Processing {0} packet post-load.", ClassReflection.getSimpleName(packetQueue.get(i).getClass()));
|
Log.info("Processing {0} packet post-load.", ClassReflection.getSimpleName(packetQueue.get(i).getClass()));
|
||||||
handleClientReceived(packetQueue.get(i));
|
handleClientReceived(packetQueue.get(i));
|
||||||
|
Pools.free(packetQueue.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//clear inbound packet queue
|
//clear inbound packet queue
|
||||||
@@ -182,9 +184,12 @@ public class Net{
|
|||||||
if(clientLoaded || object instanceof ImportantPacket){
|
if(clientLoaded || object instanceof ImportantPacket){
|
||||||
if(clientListeners.get(object.getClass()) != null) clientListeners.get(object.getClass()).accept(object);
|
if(clientListeners.get(object.getClass()) != null) clientListeners.get(object.getClass()).accept(object);
|
||||||
if(listeners.get(object.getClass()) != null) listeners.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)){
|
}else if(!(object instanceof UnimportantPacket)){
|
||||||
packetQueue.add(object);
|
packetQueue.add(object);
|
||||||
Log.info("Queuing packet {0}.", ClassReflection.getSimpleName(object.getClass()));
|
Log.info("Queuing packet {0}.", ClassReflection.getSimpleName(object.getClass()));
|
||||||
|
}else{
|
||||||
|
Pools.free(object);
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
Log.err("Unhandled packet type: '{0}'!", ClassReflection.getSimpleName(object.getClass()));
|
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 || listeners.get(object.getClass()) != null){
|
||||||
if(serverListeners.get(object.getClass()) != null) serverListeners.get(object.getClass()).accept(connection, object);
|
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);
|
if(listeners.get(object.getClass()) != null) listeners.get(object.getClass()).accept(object);
|
||||||
|
Pools.free(object);
|
||||||
}else{
|
}else{
|
||||||
Log.err("Unhandled packet type: '{0}'!", ClassReflection.getSimpleName(object.getClass()));
|
Log.err("Unhandled packet type: '{0}'!", ClassReflection.getSimpleName(object.getClass()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
package io.anuke.mindustry.net;
|
package io.anuke.mindustry.net;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.utils.Pool.Poolable;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public interface Packet {
|
public interface Packet extends Poolable{
|
||||||
void read(ByteBuffer buffer);
|
void read(ByteBuffer buffer);
|
||||||
void write(ByteBuffer buffer);
|
void write(ByteBuffer buffer);
|
||||||
|
|
||||||
|
default void reset() {}
|
||||||
|
|
||||||
interface ImportantPacket{}
|
interface ImportantPacket{}
|
||||||
interface UnimportantPacket{}
|
interface UnimportantPacket{}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -328,6 +328,8 @@ public class Packets {
|
|||||||
byte[] n = new byte[nlength];
|
byte[] n = new byte[nlength];
|
||||||
buffer.get(n);
|
buffer.get(n);
|
||||||
name = new String(n);
|
name = new String(n);
|
||||||
|
}else{
|
||||||
|
name = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
text = IOUtils.readString(buffer);
|
text = IOUtils.readString(buffer);
|
||||||
@@ -531,8 +533,7 @@ public class Packets {
|
|||||||
@Override
|
@Override
|
||||||
public void write(ByteBuffer buffer) {
|
public void write(ByteBuffer buffer) {
|
||||||
buffer.putInt(info.playerid);
|
buffer.putInt(info.playerid);
|
||||||
buffer.putShort((short)info.ip.getBytes().length);
|
IOUtils.writeString(buffer, info.ip);
|
||||||
buffer.put(info.ip.getBytes());
|
|
||||||
buffer.put(info.modclient ? (byte)1 : 0);
|
buffer.put(info.modclient ? (byte)1 : 0);
|
||||||
buffer.put(info.android ? (byte)1 : 0);
|
buffer.put(info.android ? (byte)1 : 0);
|
||||||
|
|
||||||
@@ -548,11 +549,9 @@ public class Packets {
|
|||||||
@Override
|
@Override
|
||||||
public void read(ByteBuffer buffer) {
|
public void read(ByteBuffer buffer) {
|
||||||
int id = buffer.getInt();
|
int id = buffer.getInt();
|
||||||
short iplen = buffer.getShort();
|
String ip = IOUtils.readString(buffer);
|
||||||
byte[] ipb = new byte[iplen];
|
|
||||||
buffer.get(ipb);
|
|
||||||
|
|
||||||
info = new TraceInfo(new String(ipb));
|
info = new TraceInfo(ip);
|
||||||
|
|
||||||
info.playerid = id;
|
info.playerid = id;
|
||||||
info.modclient = buffer.get() == 1;
|
info.modclient = buffer.get() == 1;
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package io.anuke.kryonet;
|
package io.anuke.kryonet;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.utils.Pools;
|
||||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||||
import com.badlogic.gdx.utils.reflect.ReflectionException;
|
|
||||||
import com.esotericsoftware.kryonet.FrameworkMessage;
|
import com.esotericsoftware.kryonet.FrameworkMessage;
|
||||||
import com.esotericsoftware.kryonet.serialization.Serialization;
|
import com.esotericsoftware.kryonet.serialization.Serialization;
|
||||||
import io.anuke.mindustry.net.Packet;
|
import io.anuke.mindustry.net.Packet;
|
||||||
@@ -24,23 +24,20 @@ public class ByteSerializer implements Serialization {
|
|||||||
throw new RuntimeException("Unregistered class: " + ClassReflection.getSimpleName(o.getClass()));
|
throw new RuntimeException("Unregistered class: " + ClassReflection.getSimpleName(o.getClass()));
|
||||||
byteBuffer.put(id);
|
byteBuffer.put(id);
|
||||||
((Packet) o).write(byteBuffer);
|
((Packet) o).write(byteBuffer);
|
||||||
|
Pools.free(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object read(ByteBuffer byteBuffer) {
|
public Object read(ByteBuffer byteBuffer) {
|
||||||
try {
|
byte id = byteBuffer.get();
|
||||||
byte id = byteBuffer.get();
|
if(id == -2){
|
||||||
if(id == -2){
|
return FrameworkSerializer.read(byteBuffer);
|
||||||
return FrameworkSerializer.read(byteBuffer);
|
}else{
|
||||||
}else {
|
Class<?> type = Registrator.getByID(id);
|
||||||
Class<?> type = Registrator.getByID(id);
|
Packet packet = (Packet)Pools.obtain(type);
|
||||||
Packet packet = (Packet) ClassReflection.newInstance(type);
|
packet.read(byteBuffer);
|
||||||
packet.read(byteBuffer);
|
return packet;
|
||||||
return packet;
|
|
||||||
}
|
|
||||||
}catch (ReflectionException e){
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user