Created test fbs packets

This commit is contained in:
Collin Smith 2020-06-14 20:59:27 -07:00
parent cbd55ee164
commit f5454be03d
10 changed files with 205 additions and 14 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,19 @@
namespace com.riiablo.net.packet.netty;
table Connection {
// request
// response
}
table Disconnect {
// request
// response
}
table Ping {
// request
// response
}

View File

@ -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<DatagramPacket> {
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

View File

@ -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.");
}

View File

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