mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-22 05:38:46 +07:00
Created Header table in Netty fbs and sample packet
This commit is contained in:
41
core/gen/com/riiablo/net/packet/netty/Header.java
Normal file
41
core/gen/com/riiablo/net/packet/netty/Header.java
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
// 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 Header extends Table {
|
||||||
|
public static Header getRootAsHeader(ByteBuffer _bb) { return getRootAsHeader(_bb, new Header()); }
|
||||||
|
public static Header getRootAsHeader(ByteBuffer _bb, Header 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 Header __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
|
||||||
|
|
||||||
|
public int sequence() { int o = __offset(4); return o != 0 ? bb.getShort(o + bb_pos) & 0xFFFF : 0; }
|
||||||
|
public int ack() { int o = __offset(6); return o != 0 ? bb.getShort(o + bb_pos) & 0xFFFF : 0; }
|
||||||
|
public int ackBits() { int o = __offset(8); return o != 0 ? bb.getInt(o + bb_pos) : 0; }
|
||||||
|
|
||||||
|
public static int createHeader(FlatBufferBuilder builder,
|
||||||
|
int sequence,
|
||||||
|
int ack,
|
||||||
|
int ack_bits) {
|
||||||
|
builder.startObject(3);
|
||||||
|
Header.addAckBits(builder, ack_bits);
|
||||||
|
Header.addAck(builder, ack);
|
||||||
|
Header.addSequence(builder, sequence);
|
||||||
|
return Header.endHeader(builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void startHeader(FlatBufferBuilder builder) { builder.startObject(3); }
|
||||||
|
public static void addSequence(FlatBufferBuilder builder, int sequence) { builder.addShort(0, (short)sequence, (short)0); }
|
||||||
|
public static void addAck(FlatBufferBuilder builder, int ack) { builder.addShort(1, (short)ack, (short)0); }
|
||||||
|
public static void addAckBits(FlatBufferBuilder builder, int ackBits) { builder.addInt(2, ackBits, 0); }
|
||||||
|
public static int endHeader(FlatBufferBuilder builder) {
|
||||||
|
int o = builder.endObject();
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -14,21 +14,26 @@ public final class Netty extends Table {
|
|||||||
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 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 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 Header header() { return header(new Header()); }
|
||||||
public Table data(Table obj) { int o = __offset(6); return o != 0 ? __union(obj, o) : null; }
|
public Header header(Header obj) { int o = __offset(4); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
|
||||||
|
public byte dataType() { int o = __offset(6); return o != 0 ? bb.get(o + bb_pos) : 0; }
|
||||||
|
public Table data(Table obj) { int o = __offset(8); return o != 0 ? __union(obj, o) : null; }
|
||||||
|
|
||||||
public static int createNetty(FlatBufferBuilder builder,
|
public static int createNetty(FlatBufferBuilder builder,
|
||||||
|
int headerOffset,
|
||||||
byte data_type,
|
byte data_type,
|
||||||
int dataOffset) {
|
int dataOffset) {
|
||||||
builder.startObject(2);
|
builder.startObject(3);
|
||||||
Netty.addData(builder, dataOffset);
|
Netty.addData(builder, dataOffset);
|
||||||
|
Netty.addHeader(builder, headerOffset);
|
||||||
Netty.addDataType(builder, data_type);
|
Netty.addDataType(builder, data_type);
|
||||||
return Netty.endNetty(builder);
|
return Netty.endNetty(builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void startNetty(FlatBufferBuilder builder) { builder.startObject(2); }
|
public static void startNetty(FlatBufferBuilder builder) { builder.startObject(3); }
|
||||||
public static void addDataType(FlatBufferBuilder builder, byte dataType) { builder.addByte(0, dataType, 0); }
|
public static void addHeader(FlatBufferBuilder builder, int headerOffset) { builder.addOffset(0, headerOffset, 0); }
|
||||||
public static void addData(FlatBufferBuilder builder, int dataOffset) { builder.addOffset(1, dataOffset, 0); }
|
public static void addDataType(FlatBufferBuilder builder, byte dataType) { builder.addByte(1, dataType, 0); }
|
||||||
|
public static void addData(FlatBufferBuilder builder, int dataOffset) { builder.addOffset(2, dataOffset, 0); }
|
||||||
public static int endNetty(FlatBufferBuilder builder) {
|
public static int endNetty(FlatBufferBuilder builder) {
|
||||||
int o = builder.endObject();
|
int o = builder.endObject();
|
||||||
return o;
|
return o;
|
||||||
|
@ -2,6 +2,12 @@ include "Packets.fbs";
|
|||||||
|
|
||||||
namespace com.riiablo.net.packet.netty;
|
namespace com.riiablo.net.packet.netty;
|
||||||
|
|
||||||
|
table Header {
|
||||||
|
sequence:uint16;
|
||||||
|
ack:uint16;
|
||||||
|
ack_bits:int32;
|
||||||
|
}
|
||||||
|
|
||||||
union NettyData {
|
union NettyData {
|
||||||
Connection,
|
Connection,
|
||||||
Disconnect,
|
Disconnect,
|
||||||
@ -9,6 +15,7 @@ union NettyData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
table Netty {
|
table Netty {
|
||||||
|
header:Header;
|
||||||
data:NettyData;
|
data:NettyData;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.riiablo.server.netty;
|
package com.riiablo.server.netty;
|
||||||
|
|
||||||
|
import com.google.flatbuffers.ByteBufferUtil;
|
||||||
import com.google.flatbuffers.FlatBufferBuilder;
|
import com.google.flatbuffers.FlatBufferBuilder;
|
||||||
import io.netty.bootstrap.Bootstrap;
|
import io.netty.bootstrap.Bootstrap;
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
@ -14,6 +15,7 @@ import io.netty.channel.socket.DatagramChannel;
|
|||||||
import io.netty.channel.socket.DatagramPacket;
|
import io.netty.channel.socket.DatagramPacket;
|
||||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
import com.badlogic.gdx.Application;
|
import com.badlogic.gdx.Application;
|
||||||
import com.badlogic.gdx.ApplicationAdapter;
|
import com.badlogic.gdx.ApplicationAdapter;
|
||||||
@ -23,6 +25,7 @@ import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration;
|
|||||||
|
|
||||||
import com.riiablo.codec.Animation;
|
import com.riiablo.codec.Animation;
|
||||||
import com.riiablo.net.packet.netty.Connection;
|
import com.riiablo.net.packet.netty.Connection;
|
||||||
|
import com.riiablo.net.packet.netty.Header;
|
||||||
import com.riiablo.net.packet.netty.Netty;
|
import com.riiablo.net.packet.netty.Netty;
|
||||||
import com.riiablo.net.packet.netty.NettyData;
|
import com.riiablo.net.packet.netty.NettyData;
|
||||||
|
|
||||||
@ -71,16 +74,27 @@ public class Client extends ApplicationAdapter {
|
|||||||
InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
|
InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
|
||||||
Gdx.app.log(TAG, "Connecting to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
|
Gdx.app.log(TAG, "Connecting to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
|
||||||
|
|
||||||
FlatBufferBuilder builder = new FlatBufferBuilder(0);
|
FlatBufferBuilder builder = new FlatBufferBuilder();
|
||||||
|
int headerOffset = Header.createHeader(builder, 0, 0, 0);
|
||||||
Connection.startConnection(builder);
|
Connection.startConnection(builder);
|
||||||
int dataOffset = Connection.endConnection(builder);
|
int dataOffset = Connection.endConnection(builder);
|
||||||
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
|
int offset = Netty.createNetty(builder, headerOffset, NettyData.Connection, dataOffset);
|
||||||
Netty.finishSizePrefixedNettyBuffer(builder, offset);
|
Netty.finishSizePrefixedNettyBuffer(builder, offset);
|
||||||
|
|
||||||
|
// sanity(builder.dataBuffer());
|
||||||
|
|
||||||
ByteBuf byteBuf = Unpooled.wrappedBuffer(builder.dataBuffer());
|
ByteBuf byteBuf = Unpooled.wrappedBuffer(builder.dataBuffer());
|
||||||
ctx.writeAndFlush(byteBuf);
|
ctx.writeAndFlush(byteBuf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sanity(ByteBuffer buffer) {
|
||||||
|
ByteBuffer tmp = ByteBufferUtil.removeSizePrefix(buffer);
|
||||||
|
Netty netty = Netty.getRootAsNetty(tmp);
|
||||||
|
Gdx.app.log(TAG, " " + NettyData.name(netty.dataType()));
|
||||||
|
Header header = netty.header(new Header());
|
||||||
|
Gdx.app.log(TAG, " " + String.format("SEQ:%d ACK:%d ACK_BITS:%08x", header.sequence(), header.ack(), header.ackBits()));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {
|
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) {
|
||||||
// ctx.writeAndFlush(msg);
|
// ctx.writeAndFlush(msg);
|
||||||
|
@ -12,6 +12,7 @@ import io.netty.channel.nio.NioEventLoopGroup;
|
|||||||
import io.netty.channel.socket.DatagramChannel;
|
import io.netty.channel.socket.DatagramChannel;
|
||||||
import io.netty.channel.socket.DatagramPacket;
|
import io.netty.channel.socket.DatagramPacket;
|
||||||
import io.netty.channel.socket.nio.NioDatagramChannel;
|
import io.netty.channel.socket.nio.NioDatagramChannel;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
import org.apache.commons.cli.CommandLine;
|
import org.apache.commons.cli.CommandLine;
|
||||||
import org.apache.commons.cli.CommandLineParser;
|
import org.apache.commons.cli.CommandLineParser;
|
||||||
import org.apache.commons.cli.DefaultParser;
|
import org.apache.commons.cli.DefaultParser;
|
||||||
@ -25,6 +26,7 @@ import com.badlogic.gdx.backends.headless.HeadlessApplicationConfiguration;
|
|||||||
|
|
||||||
import com.riiablo.Riiablo;
|
import com.riiablo.Riiablo;
|
||||||
import com.riiablo.codec.Animation;
|
import com.riiablo.codec.Animation;
|
||||||
|
import com.riiablo.net.packet.netty.Header;
|
||||||
import com.riiablo.net.packet.netty.NettyData;
|
import com.riiablo.net.packet.netty.NettyData;
|
||||||
|
|
||||||
public class Main extends ApplicationAdapter {
|
public class Main extends ApplicationAdapter {
|
||||||
@ -100,8 +102,11 @@ public class Main extends ApplicationAdapter {
|
|||||||
Gdx.app.log(TAG, "Packet from " + msg.sender().getHostName() + ":" + msg.sender().getPort());
|
Gdx.app.log(TAG, "Packet from " + msg.sender().getHostName() + ":" + msg.sender().getPort());
|
||||||
ByteBuf in = msg.content();
|
ByteBuf in = msg.content();
|
||||||
try {
|
try {
|
||||||
Packet packet = Packet.obtain(0, in.nioBuffer());
|
ByteBuffer buffer = in.nioBuffer();
|
||||||
|
Packet packet = Packet.obtain(0, buffer);
|
||||||
Gdx.app.log(TAG, " " + NettyData.name(packet.data.dataType()));
|
Gdx.app.log(TAG, " " + NettyData.name(packet.data.dataType()));
|
||||||
|
Header header = packet.data.header(new Header());
|
||||||
|
Gdx.app.log(TAG, " " + String.format("SEQ:%d ACK:%d ACK_BITS:%08x", header.sequence(), header.ack(), header.ackBits()));
|
||||||
} finally {
|
} finally {
|
||||||
in.release(); // TODO: release after packet is processed by server
|
in.release(); // TODO: release after packet is processed by server
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user