Implemented packet pooling

This commit is contained in:
Anuken
2018-05-07 14:23:52 -04:00
parent 84a126253a
commit 7a94ebcf2a
5 changed files with 26 additions and 21 deletions

View File

@@ -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;
}
}