Initial test
This commit is contained in:
@@ -251,7 +251,7 @@ public class Packets{
|
|||||||
public void read(ByteBuffer buffer){
|
public void read(ByteBuffer buffer){
|
||||||
id = buffer.getInt();
|
id = buffer.getInt();
|
||||||
total = buffer.getInt();
|
total = buffer.getInt();
|
||||||
type = (Class<? extends Streamable>) Registrator.getByID(buffer.get());
|
type = (Class<? extends Streamable>) Registrator.getByID(buffer.get()).type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,30 @@
|
|||||||
package io.anuke.mindustry.net;
|
package io.anuke.mindustry.net;
|
||||||
|
|
||||||
import com.badlogic.gdx.utils.ObjectIntMap;
|
import com.badlogic.gdx.utils.ObjectIntMap;
|
||||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
|
||||||
import io.anuke.mindustry.net.Packets.*;
|
import io.anuke.mindustry.net.Packets.*;
|
||||||
|
import io.anuke.ucore.function.Supplier;
|
||||||
|
import io.anuke.ucore.util.Pooling;
|
||||||
|
|
||||||
public class Registrator{
|
public class Registrator{
|
||||||
private static Class<?>[] classes = {
|
private static ClassEntry[] classes = {
|
||||||
StreamBegin.class,
|
new ClassEntry(StreamBegin.class, StreamBegin::new),
|
||||||
StreamChunk.class,
|
new ClassEntry(StreamChunk.class, StreamChunk::new),
|
||||||
WorldStream.class,
|
new ClassEntry(WorldStream.class, WorldStream::new),
|
||||||
ConnectPacket.class,
|
new ClassEntry(ConnectPacket.class, ConnectPacket::new),
|
||||||
ClientSnapshotPacket.class,
|
new ClassEntry(ClientSnapshotPacket.class, ClientSnapshotPacket::new),
|
||||||
InvokePacket.class
|
new ClassEntry(InvokePacket.class, InvokePacket::new)
|
||||||
};
|
};
|
||||||
private static ObjectIntMap<Class<?>> ids = new ObjectIntMap<>();
|
private static ObjectIntMap<Class> ids = new ObjectIntMap<>();
|
||||||
|
|
||||||
static{
|
static{
|
||||||
if(classes.length > 127) throw new RuntimeException("Can't have more than 127 registered classes!");
|
if(classes.length > 127) throw new RuntimeException("Can't have more than 127 registered classes!");
|
||||||
for(int i = 0; i < classes.length; i++){
|
for(int i = 0; i < classes.length; i++){
|
||||||
if(!ClassReflection.isAssignableFrom(Packet.class, classes[i]) &&
|
Pooling.registerType((Class<Packet>) classes[i].type, (Supplier<Packet>) classes[i].constructor);
|
||||||
!ClassReflection.isAssignableFrom(Streamable.class, classes[i]))
|
ids.put(classes[i].type, i);
|
||||||
throw new RuntimeException("Not a packet: " + classes[i]);
|
|
||||||
ids.put(classes[i], i);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?> getByID(byte id){
|
public static ClassEntry getByID(byte id){
|
||||||
return classes[id];
|
return classes[id];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,7 +32,17 @@ public class Registrator{
|
|||||||
return (byte) ids.get(type, -1);
|
return (byte) ids.get(type, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class<?>[] getClasses(){
|
public static ClassEntry[] getClasses(){
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class ClassEntry{
|
||||||
|
public final Class<?> type;
|
||||||
|
public final Supplier<?> constructor;
|
||||||
|
|
||||||
|
public <T extends Packet> ClassEntry(Class<T> type, Supplier<T> constructor){
|
||||||
|
this.type = type;
|
||||||
|
this.constructor = constructor;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,9 +35,8 @@ public class ByteSerializer implements Serialization {
|
|||||||
if(id == -2){
|
if(id == -2){
|
||||||
return FrameworkSerializer.read(byteBuffer);
|
return FrameworkSerializer.read(byteBuffer);
|
||||||
}else{
|
}else{
|
||||||
Class<?> type = Registrator.getByID(id);
|
|
||||||
synchronized (packetPoolLock) {
|
synchronized (packetPoolLock) {
|
||||||
Packet packet = (Packet) Pooling.obtain(type);
|
Packet packet = (Packet) Pooling.obtain(Registrator.getByID(id).type);
|
||||||
packet.read(byteBuffer);
|
packet.read(byteBuffer);
|
||||||
return packet;
|
return packet;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user