Rolled Client debugging outbound packet data logging into ReliableChannelHandler

This commit is contained in:
Collin Smith
2020-06-17 22:57:40 -07:00
parent fb7377980b
commit 2b6d06f330
2 changed files with 31 additions and 13 deletions

View File

@ -1,6 +1,5 @@
package com.riiablo.server.netty;
import com.google.flatbuffers.ByteBufferUtil;
import com.google.flatbuffers.FlatBufferBuilder;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
@ -15,7 +14,6 @@ import io.netty.channel.socket.DatagramChannel;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
@ -89,8 +87,6 @@ public class Client extends ApplicationAdapter {
int offset = Netty.createNetty(builder, headerOffset, NettyData.Connection, dataOffset);
Netty.finishSizePrefixedNettyBuffer(builder, offset);
sanity(builder.dataBuffer());
ByteBuf byteBuf = Unpooled.wrappedBuffer(builder.dataBuffer());
ctx.writeAndFlush(byteBuf);
}
@ -100,14 +96,6 @@ public class Client extends ApplicationAdapter {
ctx.fireChannelRead(msg);
}
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
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
Gdx.app.error(TAG, cause.getMessage(), cause);

View File

@ -1,6 +1,8 @@
package com.riiablo.server.netty;
import com.google.flatbuffers.ByteBufferUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
@ -21,7 +23,16 @@ import com.riiablo.net.packet.netty.Netty;
public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHandler, ChannelOutboundHandler {
private static final String TAG = "ReliableChannelHandler";
private static final boolean DEBUG = true;
private static final boolean DEBUG_OUTBOUND = DEBUG && true;
private final TypeParameterMatcher matcher;
private final Header header = new Header();
int protocol = 0;
int seq = -1; // 0xFFFFFFFF
int ack = -1; // 0xFFFFFFFF
int ack_bits = 0;
ReliableChannelHandler() {
matcher = TypeParameterMatcher.get(DatagramPacket.class);
@ -38,7 +49,7 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
try {
ByteBuffer buffer = in.nioBuffer();
Packet packet = Packet.obtain(0, buffer);
processHeader(ctx, packet.data.header());
processHeader(ctx, packet.data.header(header));
processPacket(ctx, packet.data);
} finally {
// in.release(); // Automatically released by channelRead() right now
@ -53,6 +64,24 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
}
protected void channelWrite0(ChannelHandlerContext ctx, Object msg) throws Exception {
InetSocketAddress receiver = (InetSocketAddress) ctx.channel().remoteAddress();
Gdx.app.log(TAG, "channelRead0 Packet to " + receiver.getHostName() + ":" + receiver.getPort());
ByteBuf out = (ByteBuf) msg;
try {
Gdx.app.debug(TAG, "out.nioBufferCount()=" + out.nioBufferCount());
ByteBuffer nioBuffer = out.nioBufferCount() > 0
? out.nioBuffer()
: ByteBuffer.wrap(ByteBufUtil.getBytes(out));
Gdx.app.debug(TAG, "nioBuffer=" + nioBuffer);
nioBuffer = ByteBufferUtil.removeSizePrefix(nioBuffer);
Netty netty = Netty.getRootAsNetty(nioBuffer);
netty.header(header);
Gdx.app.log(TAG, " " + String.format("SEQ:%d ACK:%d ACK_BITS:%08x", header.sequence(), header.ack(), header.ackBits()));
} finally {
}
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
Gdx.app.debug(TAG, "channelRegistered");
@ -151,6 +180,7 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
Gdx.app.debug(TAG, "write");
if (DEBUG_OUTBOUND) channelWrite0(ctx, msg);
ctx.write(msg, promise);
}