From f5454be03dda7b30bb987b4266f814974e8ff587 Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Sun, 14 Jun 2020 20:59:27 -0700 Subject: [PATCH] Created test fbs packets --- .../riiablo/net/packet/netty/Connection.java | 24 ++++++++++++ .../riiablo/net/packet/netty/Disconnect.java | 24 ++++++++++++ .../com/riiablo/net/packet/netty/Netty.java | 39 +++++++++++++++++++ .../riiablo/net/packet/netty/NettyData.java | 16 ++++++++ .../com/riiablo/net/packet/netty/Ping.java | 24 ++++++++++++ core/src/com/riiablo/net/netty/Netty.fbs | 15 +++++++ core/src/com/riiablo/net/netty/Packets.fbs | 19 +++++++++ .../src/com/riiablo/server/netty/Client.java | 25 +++++++----- .../src/com/riiablo/server/netty/Main.java | 9 ++--- .../src/com/riiablo/server/netty/Packet.java | 24 ++++++++++++ 10 files changed, 205 insertions(+), 14 deletions(-) create mode 100644 core/gen/com/riiablo/net/packet/netty/Connection.java create mode 100644 core/gen/com/riiablo/net/packet/netty/Disconnect.java create mode 100644 core/gen/com/riiablo/net/packet/netty/Netty.java create mode 100644 core/gen/com/riiablo/net/packet/netty/NettyData.java create mode 100644 core/gen/com/riiablo/net/packet/netty/Ping.java create mode 100644 core/src/com/riiablo/net/netty/Netty.fbs create mode 100644 core/src/com/riiablo/net/netty/Packets.fbs create mode 100644 server/netty/src/com/riiablo/server/netty/Packet.java diff --git a/core/gen/com/riiablo/net/packet/netty/Connection.java b/core/gen/com/riiablo/net/packet/netty/Connection.java new file mode 100644 index 00000000..61daf7f6 --- /dev/null +++ b/core/gen/com/riiablo/net/packet/netty/Connection.java @@ -0,0 +1,24 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package com.riiablo.net.packet.netty; + +import java.nio.*; +import java.lang.*; +import java.util.*; +import com.google.flatbuffers.*; + +@SuppressWarnings("unused") +public final class Connection extends Table { + public static Connection getRootAsConnection(ByteBuffer _bb) { return getRootAsConnection(_bb, new Connection()); } + public static Connection getRootAsConnection(ByteBuffer _bb, Connection obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } + public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; vtable_start = bb_pos - bb.getInt(bb_pos); vtable_size = bb.getShort(vtable_start); } + public Connection __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } + + + public static void startConnection(FlatBufferBuilder builder) { builder.startObject(0); } + public static int endConnection(FlatBufferBuilder builder) { + int o = builder.endObject(); + return o; + } +} + diff --git a/core/gen/com/riiablo/net/packet/netty/Disconnect.java b/core/gen/com/riiablo/net/packet/netty/Disconnect.java new file mode 100644 index 00000000..f7dadb4d --- /dev/null +++ b/core/gen/com/riiablo/net/packet/netty/Disconnect.java @@ -0,0 +1,24 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package com.riiablo.net.packet.netty; + +import java.nio.*; +import java.lang.*; +import java.util.*; +import com.google.flatbuffers.*; + +@SuppressWarnings("unused") +public final class Disconnect extends Table { + public static Disconnect getRootAsDisconnect(ByteBuffer _bb) { return getRootAsDisconnect(_bb, new Disconnect()); } + public static Disconnect getRootAsDisconnect(ByteBuffer _bb, Disconnect obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } + public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; vtable_start = bb_pos - bb.getInt(bb_pos); vtable_size = bb.getShort(vtable_start); } + public Disconnect __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } + + + public static void startDisconnect(FlatBufferBuilder builder) { builder.startObject(0); } + public static int endDisconnect(FlatBufferBuilder builder) { + int o = builder.endObject(); + return o; + } +} + diff --git a/core/gen/com/riiablo/net/packet/netty/Netty.java b/core/gen/com/riiablo/net/packet/netty/Netty.java new file mode 100644 index 00000000..4d67307f --- /dev/null +++ b/core/gen/com/riiablo/net/packet/netty/Netty.java @@ -0,0 +1,39 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package com.riiablo.net.packet.netty; + +import java.nio.*; +import java.lang.*; +import java.util.*; +import com.google.flatbuffers.*; + +@SuppressWarnings("unused") +public final class Netty extends Table { + public static Netty getRootAsNetty(ByteBuffer _bb) { return getRootAsNetty(_bb, new Netty()); } + public static Netty getRootAsNetty(ByteBuffer _bb, Netty obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } + public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; vtable_start = bb_pos - bb.getInt(bb_pos); vtable_size = bb.getShort(vtable_start); } + public Netty __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } + + public byte dataType() { int o = __offset(4); return o != 0 ? bb.get(o + bb_pos) : 0; } + public Table data(Table obj) { int o = __offset(6); return o != 0 ? __union(obj, o) : null; } + + public static int createNetty(FlatBufferBuilder builder, + byte data_type, + int dataOffset) { + builder.startObject(2); + Netty.addData(builder, dataOffset); + Netty.addDataType(builder, data_type); + return Netty.endNetty(builder); + } + + public static void startNetty(FlatBufferBuilder builder) { builder.startObject(2); } + public static void addDataType(FlatBufferBuilder builder, byte dataType) { builder.addByte(0, dataType, 0); } + public static void addData(FlatBufferBuilder builder, int dataOffset) { builder.addOffset(1, dataOffset, 0); } + public static int endNetty(FlatBufferBuilder builder) { + int o = builder.endObject(); + return o; + } + public static void finishNettyBuffer(FlatBufferBuilder builder, int offset) { builder.finish(offset); } + public static void finishSizePrefixedNettyBuffer(FlatBufferBuilder builder, int offset) { builder.finishSizePrefixed(offset); } +} + diff --git a/core/gen/com/riiablo/net/packet/netty/NettyData.java b/core/gen/com/riiablo/net/packet/netty/NettyData.java new file mode 100644 index 00000000..4ac41943 --- /dev/null +++ b/core/gen/com/riiablo/net/packet/netty/NettyData.java @@ -0,0 +1,16 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package com.riiablo.net.packet.netty; + +public final class NettyData { + private NettyData() { } + public static final byte NONE = 0; + public static final byte Connection = 1; + public static final byte Disconnect = 2; + public static final byte Ping = 3; + + public static final String[] names = { "NONE", "Connection", "Disconnect", "Ping", }; + + public static String name(int e) { return names[e]; } +} + diff --git a/core/gen/com/riiablo/net/packet/netty/Ping.java b/core/gen/com/riiablo/net/packet/netty/Ping.java new file mode 100644 index 00000000..808bd28c --- /dev/null +++ b/core/gen/com/riiablo/net/packet/netty/Ping.java @@ -0,0 +1,24 @@ +// automatically generated by the FlatBuffers compiler, do not modify + +package com.riiablo.net.packet.netty; + +import java.nio.*; +import java.lang.*; +import java.util.*; +import com.google.flatbuffers.*; + +@SuppressWarnings("unused") +public final class Ping extends Table { + public static Ping getRootAsPing(ByteBuffer _bb) { return getRootAsPing(_bb, new Ping()); } + public static Ping getRootAsPing(ByteBuffer _bb, Ping obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); } + public void __init(int _i, ByteBuffer _bb) { bb_pos = _i; bb = _bb; vtable_start = bb_pos - bb.getInt(bb_pos); vtable_size = bb.getShort(vtable_start); } + public Ping __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; } + + + public static void startPing(FlatBufferBuilder builder) { builder.startObject(0); } + public static int endPing(FlatBufferBuilder builder) { + int o = builder.endObject(); + return o; + } +} + diff --git a/core/src/com/riiablo/net/netty/Netty.fbs b/core/src/com/riiablo/net/netty/Netty.fbs new file mode 100644 index 00000000..b2793465 --- /dev/null +++ b/core/src/com/riiablo/net/netty/Netty.fbs @@ -0,0 +1,15 @@ +include "Packets.fbs"; + +namespace com.riiablo.net.packet.netty; + +union NettyData { + Connection, + Disconnect, + Ping, +} + +table Netty { + data:NettyData; +} + +root_type Netty; \ No newline at end of file diff --git a/core/src/com/riiablo/net/netty/Packets.fbs b/core/src/com/riiablo/net/netty/Packets.fbs new file mode 100644 index 00000000..4f49b72a --- /dev/null +++ b/core/src/com/riiablo/net/netty/Packets.fbs @@ -0,0 +1,19 @@ +namespace com.riiablo.net.packet.netty; + +table Connection { + // request + + // response +} + +table Disconnect { + // request + + // response +} + +table Ping { + // request + + // response +} \ No newline at end of file diff --git a/server/netty/src/com/riiablo/server/netty/Client.java b/server/netty/src/com/riiablo/server/netty/Client.java index 1e0e17eb..7977e7f7 100644 --- a/server/netty/src/com/riiablo/server/netty/Client.java +++ b/server/netty/src/com/riiablo/server/netty/Client.java @@ -1,5 +1,6 @@ package com.riiablo.server.netty; +import com.google.flatbuffers.FlatBufferBuilder; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; @@ -21,6 +22,9 @@ import com.badlogic.gdx.backends.headless.HeadlessApplication; import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration; import com.riiablo.codec.Animation; +import com.riiablo.net.packet.netty.Connection; +import com.riiablo.net.packet.netty.Netty; +import com.riiablo.net.packet.netty.NettyData; public class Client extends ApplicationAdapter { private static final String TAG = "Client"; @@ -58,31 +62,34 @@ public class Client extends ApplicationAdapter { } public static class EchoClientHandler extends SimpleChannelInboundHandler { - private final ByteBuf buf; - EchoClientHandler() { super(false); - buf = Unpooled.buffer(256); - for (int i = 0; i < buf.capacity(); i++) { - buf.writeByte((byte) i); - } } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); Gdx.app.log(TAG, "Connecting to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort()); - ctx.writeAndFlush(buf); + + FlatBufferBuilder builder = new FlatBufferBuilder(0); + Connection.startConnection(builder); + int dataOffset = Connection.endConnection(builder); + int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset); + Netty.finishSizePrefixedNettyBuffer(builder, offset); + + ByteBuf byteBuf = Unpooled.wrappedBuffer(builder.dataBuffer()); + ctx.writeAndFlush(byteBuf); } @Override protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) { - ctx.writeAndFlush(msg); +// ctx.writeAndFlush(msg); + msg.release(); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - System.out.println("Read complete."); +// System.out.println("Read complete."); } @Override diff --git a/server/netty/src/com/riiablo/server/netty/Main.java b/server/netty/src/com/riiablo/server/netty/Main.java index 228eb20c..0cfea3b3 100644 --- a/server/netty/src/com/riiablo/server/netty/Main.java +++ b/server/netty/src/com/riiablo/server/netty/Main.java @@ -12,12 +12,10 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.DatagramChannel; import io.netty.channel.socket.DatagramPacket; import io.netty.channel.socket.nio.NioDatagramChannel; -import io.netty.util.CharsetUtil; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Options; -import org.apache.commons.text.StringEscapeUtils; import com.badlogic.gdx.Application; import com.badlogic.gdx.ApplicationAdapter; @@ -27,6 +25,7 @@ import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration; import com.riiablo.Riiablo; import com.riiablo.codec.Animation; +import com.riiablo.net.packet.netty.NettyData; public class Main extends ApplicationAdapter { private static final String TAG = "Server"; @@ -101,15 +100,15 @@ public class Main extends ApplicationAdapter { Gdx.app.log(TAG, "Packet from " + msg.sender().getHostName() + ":" + msg.sender().getPort()); ByteBuf in = msg.content(); try { - System.out.print(StringEscapeUtils.escapeJava(in.toString(CharsetUtil.US_ASCII))); + Packet packet = Packet.obtain(0, in.nioBuffer()); + Gdx.app.log(TAG, " " + NettyData.name(packet.data.dataType())); } finally { - in.release(); + in.release(); // TODO: release after packet is processed by server } } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { - System.out.println(); System.out.println("Read complete."); } diff --git a/server/netty/src/com/riiablo/server/netty/Packet.java b/server/netty/src/com/riiablo/server/netty/Packet.java new file mode 100644 index 00000000..2732b57c --- /dev/null +++ b/server/netty/src/com/riiablo/server/netty/Packet.java @@ -0,0 +1,24 @@ +package com.riiablo.server.netty; + +import com.google.flatbuffers.ByteBufferUtil; +import java.nio.ByteBuffer; + +import com.badlogic.gdx.utils.TimeUtils; + +import com.riiablo.net.packet.netty.Netty; + +public class Packet { + public int id; + public long time; + public ByteBuffer buffer; + public Netty data; + + public static Packet obtain(int id, ByteBuffer buffer) { + Packet packet = new Packet(); + packet.id = id; + packet.time = TimeUtils.millis(); + packet.buffer = buffer; + packet.data = Netty.getRootAsNetty(ByteBufferUtil.removeSizePrefix(buffer)); + return packet; + } +}