Moved over Streamable inner classes, added test map
This commit is contained in:
@@ -13,9 +13,9 @@ import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
import io.anuke.mindustry.core.Platform;
|
||||
import io.anuke.mindustry.net.Packet.ImportantPacket;
|
||||
import io.anuke.mindustry.net.Packet.UnimportantPacket;
|
||||
import io.anuke.mindustry.net.Streamable.StreamBegin;
|
||||
import io.anuke.mindustry.net.Packets.StreamBegin;
|
||||
import io.anuke.mindustry.net.Streamable.StreamBuilder;
|
||||
import io.anuke.mindustry.net.Streamable.StreamChunk;
|
||||
import io.anuke.mindustry.net.Packets.StreamChunk;
|
||||
import io.anuke.ucore.core.Timers;
|
||||
import io.anuke.ucore.function.BiConsumer;
|
||||
import io.anuke.ucore.function.Consumer;
|
||||
|
||||
@@ -156,4 +156,46 @@ public class Packets {
|
||||
public enum AdminAction{
|
||||
kick, ban, trace
|
||||
}
|
||||
|
||||
/**Marks the beginning of a stream.*/
|
||||
public static class StreamBegin implements Packet{
|
||||
private static int lastid;
|
||||
|
||||
public int id = lastid ++;
|
||||
public int total;
|
||||
public Class<? extends Streamable> type;
|
||||
|
||||
@Override
|
||||
public void write(ByteBuffer buffer) {
|
||||
buffer.putInt(id);
|
||||
buffer.putInt(total);
|
||||
buffer.put(Registrator.getID(type));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(ByteBuffer buffer) {
|
||||
id = buffer.getInt();
|
||||
total = buffer.getInt();
|
||||
type = (Class<? extends Streamable>)Registrator.getByID(buffer.get());
|
||||
}
|
||||
}
|
||||
|
||||
public static class StreamChunk implements Packet{
|
||||
public int id;
|
||||
public byte[] data;
|
||||
|
||||
@Override
|
||||
public void write(ByteBuffer buffer) {
|
||||
buffer.putInt(id);
|
||||
buffer.putShort((short)data.length);
|
||||
buffer.put(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(ByteBuffer buffer) {
|
||||
id = buffer.getInt();
|
||||
data = new byte[buffer.getShort()];
|
||||
buffer.get(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ package io.anuke.mindustry.net;
|
||||
import com.badlogic.gdx.utils.ObjectIntMap;
|
||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
import io.anuke.mindustry.net.Packets.*;
|
||||
import io.anuke.mindustry.net.Streamable.StreamBegin;
|
||||
import io.anuke.mindustry.net.Streamable.StreamChunk;
|
||||
import io.anuke.mindustry.net.Packets.StreamBegin;
|
||||
import io.anuke.mindustry.net.Packets.StreamChunk;
|
||||
|
||||
public class Registrator {
|
||||
private static Class<?>[] classes = {
|
||||
|
||||
@@ -3,57 +3,15 @@ package io.anuke.mindustry.net;
|
||||
import com.badlogic.gdx.utils.reflect.ClassReflection;
|
||||
import com.badlogic.gdx.utils.reflect.ReflectionException;
|
||||
import io.anuke.mindustry.net.Packet.ImportantPacket;
|
||||
import io.anuke.mindustry.net.Packets.StreamBegin;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
public class Streamable implements ImportantPacket{
|
||||
public transient ByteArrayInputStream stream;
|
||||
|
||||
/**Marks the beginning of a stream.*/
|
||||
public static class StreamBegin implements Packet{
|
||||
private static int lastid;
|
||||
|
||||
public int id = lastid ++;
|
||||
public int total;
|
||||
public Class<? extends Streamable> type;
|
||||
|
||||
@Override
|
||||
public void write(ByteBuffer buffer) {
|
||||
buffer.putInt(id);
|
||||
buffer.putInt(total);
|
||||
buffer.put(Registrator.getID(type));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(ByteBuffer buffer) {
|
||||
id = buffer.getInt();
|
||||
total = buffer.getInt();
|
||||
type = (Class<? extends Streamable>)Registrator.getByID(buffer.get());
|
||||
}
|
||||
}
|
||||
|
||||
public static class StreamChunk implements Packet{
|
||||
public int id;
|
||||
public byte[] data;
|
||||
|
||||
@Override
|
||||
public void write(ByteBuffer buffer) {
|
||||
buffer.putInt(id);
|
||||
buffer.putShort((short)data.length);
|
||||
buffer.put(data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(ByteBuffer buffer) {
|
||||
id = buffer.getInt();
|
||||
data = new byte[buffer.getShort()];
|
||||
buffer.get(data);
|
||||
}
|
||||
}
|
||||
|
||||
public static class StreamBuilder{
|
||||
public final int id;
|
||||
public final Class<? extends Streamable> type;
|
||||
|
||||
Reference in New Issue
Block a user