mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-09 07:18:03 +07:00
Created setContentSize method to support composite buffers
Created setContentSize method to support composite buffers Removed UDP header fields from Netty fbs table
This commit is contained in:
@ -23,6 +23,7 @@ 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 {
|
||||
@ -76,12 +77,12 @@ public class Client extends ApplicationAdapter {
|
||||
|
||||
void init(ChannelHandlerContext ctx) {
|
||||
InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress();
|
||||
Gdx.app.log(TAG, "Connecting to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
|
||||
Gdx.app.log(TAG, "Sending Connection packet to " + remoteAddress.getHostString() + ":" + remoteAddress.getPort());
|
||||
|
||||
FlatBufferBuilder builder = new FlatBufferBuilder();
|
||||
Connection.startConnection(builder);
|
||||
int dataOffset = Connection.endConnection(builder);
|
||||
createNetty(builder, NettyData.Connection, dataOffset);
|
||||
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
|
||||
|
||||
ByteBuf byteBuf = Unpooled.wrappedBuffer(builder.dataBuffer());
|
||||
ctx.writeAndFlush(byteBuf);
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.riiablo.server.netty;
|
||||
|
||||
import com.google.flatbuffers.ByteBufferUtil;
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
@ -48,18 +47,27 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
|
||||
Gdx.app.log(TAG, "channelRead0 Packet from " + sender.getHostName() + ":" + sender.getPort());
|
||||
ByteBuf in = msg.content();
|
||||
try {
|
||||
boolean valid = processHeader(ctx, in);
|
||||
ByteBuffer buffer = in.nioBuffer();
|
||||
Packet packet = Packet.obtain(0, buffer);
|
||||
processHeader(ctx, packet.data);
|
||||
processPacket(ctx, packet.data);
|
||||
} finally {
|
||||
// in.release(); // Automatically released by channelRead() right now
|
||||
}
|
||||
}
|
||||
|
||||
protected void processHeader(ChannelHandlerContext ctx, Netty netty) throws Exception {
|
||||
Gdx.app.log(TAG, " incoming " + String.format("PROTO:%d SEQ:%d ACK:%d ACK_BITS:%08x", netty.protocol(), netty.sequence(), netty.ack(), netty.ackBits()));
|
||||
int remoteSeq = netty.sequence();
|
||||
protected boolean processHeader(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
|
||||
int remoteProtocol = ReliableUtil.getProtocol(in);
|
||||
if (remoteProtocol != PROTOCOL) {
|
||||
Gdx.app.log(TAG, String.format(" rejected incoming PROTO:%d", remoteProtocol));
|
||||
return false;
|
||||
}
|
||||
|
||||
int remoteSeq = ReliableUtil.getSEQ(in);
|
||||
int remoteAck = ReliableUtil.getACK(in);
|
||||
int remoteAckBits = ReliableUtil.getACK_BITS(in);
|
||||
|
||||
Gdx.app.log(TAG, " accepted incoming " + String.format("PROTO:%d SEQ:%d ACK:%d ACK_BITS:%08x", remoteProtocol, remoteSeq, remoteAck, remoteAckBits));
|
||||
if (ack < 0) {
|
||||
ack = remoteSeq;
|
||||
Gdx.app.log(TAG, " init ack=" + ack);
|
||||
@ -78,6 +86,7 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
|
||||
}
|
||||
|
||||
Gdx.app.log(TAG, " " + String.format("ACK:%d ACK_BITS:%08x", ack, ack_bits));
|
||||
return true;
|
||||
}
|
||||
|
||||
protected static boolean sequenceGreater(int a, int b) {
|
||||
@ -98,9 +107,8 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
|
||||
|
||||
}
|
||||
|
||||
protected void createNetty(FlatBufferBuilder builder, byte data_type, int dataOffset) {
|
||||
int offset = Netty.createNetty(builder, PROTOCOL, seq = (seq + 1) & 0xFFFF, ack, ack_bits, data_type, dataOffset);
|
||||
Netty.finishSizePrefixedNettyBuffer(builder, offset);
|
||||
protected int nextSequence() {
|
||||
return seq = (seq + 1) & 0xFFFF;
|
||||
}
|
||||
|
||||
protected void channelWrite0(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
@ -114,8 +122,7 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
|
||||
: ByteBuffer.wrap(ByteBufUtil.getBytes(out));
|
||||
Gdx.app.debug(TAG, "nioBuffer=" + nioBuffer);
|
||||
nioBuffer = ByteBufferUtil.removeSizePrefix(nioBuffer);
|
||||
Netty netty = Netty.getRootAsNetty(nioBuffer);
|
||||
Gdx.app.log(TAG, " " + String.format("PROTO:%d SEQ:%d ACK:%d ACK_BITS:%08x", netty.protocol(), netty.sequence(), netty.ack(), netty.ackBits()));
|
||||
Gdx.app.log(TAG, " " + String.format("PROTO:%d SEQ:%d ACK:%d ACK_BITS:%08x", 0, 0, 0, 0));
|
||||
} finally {
|
||||
}
|
||||
}
|
||||
|
@ -64,10 +64,14 @@ public class ReliableUtil {
|
||||
bb.setInt(ACK_BITS_OFFSET, value);
|
||||
}
|
||||
|
||||
static void setContentSize(ByteBuf bb, int value) {
|
||||
Validate.isTrue(value <= 0xFFFF, "cannot encode content size as ushort, src.remaining()=" + value);
|
||||
bb.setShort(CONTENT_SIZE_OFFSET, value);
|
||||
}
|
||||
|
||||
static void setContent(ByteBuf bb, ByteBuffer src) {
|
||||
Validate.isTrue(src.remaining() <= 0xFFFF, "cannot encode content size as ushort, src.remaining()=" + src.remaining());
|
||||
setContentSize(bb, src.remaining());
|
||||
src.mark();
|
||||
bb.setShort(CONTENT_SIZE_OFFSET, src.remaining());
|
||||
bb.setBytes(CONTENT_OFFSET, src);
|
||||
src.reset();
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package com.riiablo.server.netty;
|
||||
import com.google.flatbuffers.FlatBufferBuilder;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufUtil;
|
||||
import io.netty.buffer.CompositeByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -20,6 +21,16 @@ public class ReliableUtilTest {
|
||||
} finally {
|
||||
ReferenceCountUtil.release(bb);
|
||||
}
|
||||
|
||||
System.out.println("----");
|
||||
|
||||
CompositeByteBuf composite = null;
|
||||
try {
|
||||
composite = Unpooled.compositeBuffer(2);
|
||||
testComposite(composite);
|
||||
} finally {
|
||||
ReferenceCountUtil.release(composite);
|
||||
}
|
||||
}
|
||||
|
||||
static void test(ByteBuf bb) {
|
||||
@ -39,7 +50,7 @@ public class ReliableUtilTest {
|
||||
FlatBufferBuilder builder = new FlatBufferBuilder();
|
||||
Connection.startConnection(builder);
|
||||
int dataOffset = Connection.endConnection(builder);
|
||||
int offset = Netty.createNetty(builder, PROTOCOL, SEQ, ACK, ACK_BITS, NettyData.Connection, dataOffset);
|
||||
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
|
||||
Netty.finishNettyBuffer(builder, offset);
|
||||
ReliableUtil.setContent(bb, builder.dataBuffer());
|
||||
|
||||
@ -53,4 +64,45 @@ public class ReliableUtilTest {
|
||||
System.out.printf("%-8s %-5s %04x%n", "CSIZE", builder.dataBuffer().remaining() == ReliableUtil.getContentSize(bb), ReliableUtil.getContentSize(bb));
|
||||
System.out.printf("%-8s %-5s %s%n", "CONTENT", StringUtils.equals(ByteBufUtil.hexDump(builder.sizedByteArray()), ByteBufUtil.hexDump(ReliableUtil.getContent(bb))), ByteBufUtil.hexDump(ReliableUtil.getContent(bb)));
|
||||
}
|
||||
|
||||
static void testComposite(CompositeByteBuf bb) {
|
||||
final int PROTOCOL = 0b10010001;
|
||||
final int SEQ = 0xF0F0;
|
||||
final int ACK = 0x0F0F;
|
||||
final int ACK_BITS = 0xFF0000FF;
|
||||
|
||||
ByteBuf bbHeader = bb.alloc().buffer();
|
||||
|
||||
ReliableUtil.setProtocol(bbHeader, PROTOCOL);
|
||||
ReliableUtil.setSEQ(bbHeader, SEQ);
|
||||
ReliableUtil.setACK(bbHeader, ACK);
|
||||
ReliableUtil.setACK_BITS(bbHeader, ACK_BITS);
|
||||
|
||||
bbHeader.writerIndex(ReliableUtil.CONTENT_OFFSET); // hack to force writer position passed header
|
||||
System.out.println("HEADER: " + ByteBufUtil.hexDump(bbHeader)); // note: hexDump requires writerIndex
|
||||
|
||||
FlatBufferBuilder builder = new FlatBufferBuilder();
|
||||
Connection.startConnection(builder);
|
||||
int dataOffset = Connection.endConnection(builder);
|
||||
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
|
||||
Netty.finishNettyBuffer(builder, offset);
|
||||
|
||||
ByteBuf bbContent = bb.alloc().buffer();
|
||||
bbContent.writeBytes(builder.dataBuffer());
|
||||
|
||||
System.out.println("CONTENT: " + ByteBufUtil.hexDump(bbContent)); // note: hexDump requires writerIndex
|
||||
|
||||
bb.addComponents(bbHeader, bbContent);
|
||||
|
||||
ReliableUtil.setContentSize(bb, bbContent.readableBytes());
|
||||
bb.writerIndex(ReliableUtil.CONTENT_OFFSET + ReliableUtil.getContentSize(bb)); // hack to force writer position passed content
|
||||
System.out.println(ByteBufUtil.hexDump(bb)); // note: hexDump requires writerIndex
|
||||
|
||||
System.out.printf("%-8s %-5s %02x%n", "PROTOCOL", PROTOCOL == ReliableUtil.getProtocol(bb), ReliableUtil.getProtocol(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "SEQ", SEQ == ReliableUtil.getSEQ(bb), ReliableUtil.getSEQ(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "ACK", ACK == ReliableUtil.getACK(bb), ReliableUtil.getACK(bb));
|
||||
System.out.printf("%-8s %-5s %08x%n", "ACK_BITS", ACK_BITS == ReliableUtil.getACK_BITS(bb), ReliableUtil.getACK_BITS(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "CSIZE", builder.dataBuffer().remaining() == ReliableUtil.getContentSize(bb), ReliableUtil.getContentSize(bb));
|
||||
System.out.printf("%-8s %-5s %s%n", "CONTENT", StringUtils.equals(ByteBufUtil.hexDump(builder.sizedByteArray()), ByteBufUtil.hexDump(ReliableUtil.getContent(bb))), ByteBufUtil.hexDump(ReliableUtil.getContent(bb)));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user