mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-01-19 16:57:20 +07:00
Committing local changes from deprecated netty udp protocol
This commit is contained in:
parent
fbc4019b23
commit
b19d61b8f2
@ -54,6 +54,8 @@ public class Client extends ApplicationAdapter {
|
||||
.addLast(new ChannelInboundHandlerAdapter() {
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
client.init(ctx);
|
||||
client.init(ctx);
|
||||
client.init(ctx);
|
||||
ctx.pipeline().remove(this);
|
||||
}
|
||||
|
@ -1,23 +1,259 @@
|
||||
package com.riiablo.server.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
|
||||
import com.riiablo.net.packet.netty.Netty;
|
||||
import com.badlogic.gdx.utils.Bits;
|
||||
|
||||
public class Packet {
|
||||
public int id;
|
||||
public long time;
|
||||
public ByteBuffer buffer;
|
||||
public Netty data;
|
||||
static final int SINGLE = 0;
|
||||
static final int FRAGMENTED = 1;
|
||||
static final int SLICED = 2;
|
||||
static final int SLICEDACK = 3;
|
||||
static final int MAX_VALUE = SLICEDACK;
|
||||
|
||||
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(buffer);
|
||||
return packet;
|
||||
static final int PROTOCOL_OFFSET = 0;
|
||||
static final int PROTOCOL_SIZE = 1; // ubyte
|
||||
|
||||
static final int TYPE_OFFSET = PROTOCOL_OFFSET + PROTOCOL_SIZE;
|
||||
static final int TYPE_SIZE = 1; // ubyte
|
||||
|
||||
static final int SEQ_OFFSET = TYPE_OFFSET + TYPE_SIZE;
|
||||
static final int SEQ_SIZE = 2; // ushort
|
||||
|
||||
public static int getProtocol(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(PROTOCOL_OFFSET);
|
||||
}
|
||||
|
||||
public static int getType(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(TYPE_OFFSET);
|
||||
}
|
||||
|
||||
public static int getSEQ(ByteBuf bb) {
|
||||
return bb.getUnsignedShort(SEQ_OFFSET);
|
||||
}
|
||||
|
||||
static void setProtocol(ByteBuf bb, int value) {
|
||||
bb.setByte(PROTOCOL_OFFSET, value);
|
||||
}
|
||||
|
||||
static void setType(ByteBuf bb, int value) {
|
||||
assert 0 <= value && value <= MAX_VALUE;
|
||||
bb.setByte(TYPE_OFFSET, value);
|
||||
}
|
||||
|
||||
static void setSEQ(ByteBuf bb, int value) {
|
||||
bb.setShort(SEQ_OFFSET, value);
|
||||
}
|
||||
|
||||
static String toString(ByteBuf bb) {
|
||||
return String.format("PROTO:%d TYPE:%d SEQ:%d",
|
||||
getProtocol(bb), getType(bb), getSEQ(bb));
|
||||
}
|
||||
|
||||
static class Single extends Packet {
|
||||
static final int ACK_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
static final int ACK_SIZE = 2; // ushort
|
||||
|
||||
static final int ACK_BITS_OFFSET = ACK_OFFSET + ACK_SIZE;
|
||||
static final int ACK_BITS_SIZE = 4; // int
|
||||
|
||||
static final int CONTENT_SIZE_OFFSET = ACK_BITS_OFFSET + ACK_BITS_SIZE;
|
||||
static final int CONTENT_SIZE_SIZE = 2; // ushort
|
||||
|
||||
static final int CONTENT_OFFSET = CONTENT_SIZE_OFFSET + CONTENT_SIZE_SIZE;
|
||||
|
||||
public static int getACK(ByteBuf bb) {
|
||||
return bb.getUnsignedShort(ACK_OFFSET);
|
||||
}
|
||||
|
||||
public static int getACK_BITS(ByteBuf bb) {
|
||||
return bb.getInt(ACK_BITS_OFFSET);
|
||||
}
|
||||
|
||||
public static int getContentSize(ByteBuf bb) {
|
||||
return bb.getUnsignedShort(CONTENT_SIZE_OFFSET);
|
||||
}
|
||||
|
||||
public static ByteBuf getHeader(ByteBuf bb) {
|
||||
return bb.slice(0, CONTENT_OFFSET);
|
||||
}
|
||||
|
||||
public static ByteBuf getContent(ByteBuf bb) {
|
||||
return bb.slice(CONTENT_OFFSET, getContentSize(bb));
|
||||
}
|
||||
|
||||
static void setACK(ByteBuf bb, int value) {
|
||||
bb.setShort(ACK_OFFSET, value);
|
||||
}
|
||||
|
||||
static void setACK_BITS(ByteBuf bb, int value) {
|
||||
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) {
|
||||
setContentSize(bb, src.remaining());
|
||||
src.mark();
|
||||
bb.setBytes(CONTENT_OFFSET, src);
|
||||
src.reset();
|
||||
}
|
||||
|
||||
static void createHeader(ByteBuf bb, int protocol, int seq, int ack, int ack_bits) {
|
||||
bb.writerIndex(CONTENT_OFFSET);
|
||||
setProtocol(bb, protocol);
|
||||
setSEQ(bb, seq);
|
||||
setACK(bb, ack);
|
||||
setACK_BITS(bb, ack_bits);
|
||||
}
|
||||
|
||||
static String toString(ByteBuf bb) {
|
||||
return String.format("%s ACK:%d ACK_BITS:%08x CSIZE:%d",
|
||||
Packet.toString(bb), getACK(bb), getACK_BITS(bb), getContentSize(bb));
|
||||
}
|
||||
}
|
||||
|
||||
static class Fragmented extends Packet {
|
||||
static final int FRAGID_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
static final int FRAGID_SIZE = 1; // ubyte
|
||||
|
||||
static final int NUMFRAG_OFFSET = FRAGID_OFFSET + FRAGID_SIZE;
|
||||
static final int NUMFRAG_SIZE = 1; // ubyte
|
||||
|
||||
static final int FRAGSIZE_OFFSET = NUMFRAG_OFFSET + NUMFRAG_SIZE;
|
||||
static final int FRAGSIZE_SIZE = 2; // ushort
|
||||
|
||||
static final int CONTENT_OFFSET = FRAGSIZE_OFFSET + FRAGSIZE_SIZE;
|
||||
|
||||
public static int getFragmentId(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(FRAGID_OFFSET);
|
||||
}
|
||||
|
||||
public static int getNumFragments(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(NUMFRAG_OFFSET);
|
||||
}
|
||||
|
||||
public static int getFragmentSize(ByteBuf bb) {
|
||||
return bb.getUnsignedShort(FRAGSIZE_OFFSET);
|
||||
}
|
||||
|
||||
public static ByteBuf getHeader(ByteBuf bb) {
|
||||
return bb.slice(0, CONTENT_OFFSET);
|
||||
}
|
||||
|
||||
public static ByteBuf getFragment(ByteBuf bb) {
|
||||
return bb.slice(CONTENT_OFFSET, getFragmentSize(bb));
|
||||
}
|
||||
|
||||
static void setFragmentId(ByteBuf bb, int value) {
|
||||
bb.setByte(FRAGID_OFFSET, value);
|
||||
}
|
||||
|
||||
static void setNumFragments(ByteBuf bb, int value) {
|
||||
bb.setByte(NUMFRAG_OFFSET, value);
|
||||
}
|
||||
|
||||
static void setFragmentSize(ByteBuf bb, int value) {
|
||||
bb.setShort(FRAGSIZE_OFFSET, value);
|
||||
}
|
||||
|
||||
static void createHeader(ByteBuf bb, int protocol, int seq, int fragId, int numFrags) {
|
||||
bb.writerIndex(CONTENT_OFFSET);
|
||||
setProtocol(bb, protocol);
|
||||
setSEQ(bb, seq);
|
||||
setFragmentId(bb, fragId);
|
||||
setNumFragments(bb, numFrags);
|
||||
}
|
||||
|
||||
static String toString(ByteBuf bb) {
|
||||
return String.format("%s FRAGID:%d NUMFRAGS:%d FSIZE:%d",
|
||||
Packet.toString(bb), getFragmentId(bb), getNumFragments(bb), getFragmentSize(bb));
|
||||
}
|
||||
}
|
||||
|
||||
static class Sliced extends Packet {
|
||||
static final int CHUNKID_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
static final int CHUNKID_SIZE = 1; // ubyte
|
||||
|
||||
static final int SLICEID_OFFSET = CHUNKID_OFFSET + CHUNKID_SIZE;
|
||||
static final int SLICEID_SIZE = 1; // ubyte
|
||||
|
||||
static final int NUMSLICE_OFFSET = SLICEID_OFFSET + SLICEID_SIZE;
|
||||
static final int NUMSLICE_SIZE = 1; // ubyte
|
||||
|
||||
static final int SLICESIZE_OFFSET = NUMSLICE_OFFSET + NUMSLICE_SIZE;
|
||||
static final int SLICESIZE_SIZE = 2; // ushort
|
||||
|
||||
static final int CONTENT_OFFSET = SLICESIZE_OFFSET + SLICESIZE_SIZE;
|
||||
|
||||
static final int MAX_SLICE_SIZE = 1 << 10;
|
||||
|
||||
public static int getChunkId(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(CHUNKID_OFFSET);
|
||||
}
|
||||
|
||||
public static int getSliceId(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(SLICEID_OFFSET);
|
||||
}
|
||||
|
||||
public static int getNumSlices(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(NUMSLICE_OFFSET);
|
||||
}
|
||||
|
||||
public static int getSliceSize(ByteBuf bb) {
|
||||
return bb.getUnsignedShort(SLICESIZE_OFFSET);
|
||||
}
|
||||
|
||||
public static ByteBuf getHeader(ByteBuf bb) {
|
||||
return bb.slice(0, CONTENT_OFFSET);
|
||||
}
|
||||
|
||||
public static ByteBuf getSlice(ByteBuf bb) {
|
||||
return bb.slice(CONTENT_OFFSET, getSliceSize(bb));
|
||||
}
|
||||
|
||||
static String toString(ByteBuf bb) {
|
||||
return String.format("%s CHUNKID:%d SLICEID:%d NUMSLICES:%d SSIZE:%d",
|
||||
Packet.toString(bb), getChunkId(bb), getSliceId(bb), getNumSlices(bb), getSliceSize(bb));
|
||||
}
|
||||
}
|
||||
|
||||
static class SlicedAck extends Packet {
|
||||
static final int CHUNKID_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
static final int CHUNKID_SIZE = 1; // ubyte
|
||||
|
||||
static final int NUMSLICE_OFFSET = CHUNKID_OFFSET + CHUNKID_SIZE;
|
||||
static final int NUMSLICE_SIZE = 1; // ubyte
|
||||
|
||||
static final int ACK_BITS_OFFSET = NUMSLICE_OFFSET + NUMSLICE_SIZE;
|
||||
static final int ACK_BITS_SIZE = 32; // array
|
||||
|
||||
static final int CONTENT_OFFSET = ACK_BITS_OFFSET + ACK_BITS_SIZE;
|
||||
|
||||
public static int getChunkId(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(CHUNKID_OFFSET);
|
||||
}
|
||||
|
||||
public static int getNumSlices(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(NUMSLICE_OFFSET);
|
||||
}
|
||||
|
||||
public static Bits getACK_BITS(ByteBuf bb) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
public static ByteBuf getHeader(ByteBuf bb) {
|
||||
return bb;
|
||||
}
|
||||
|
||||
static String toString(ByteBuf bb) {
|
||||
return String.format("%s CHUNKID:%d NUMSLICES:%d ACK_BITS:NULL",
|
||||
Packet.toString(bb), getChunkId(bb), getNumSlices(bb));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
23
server/netty/src/com/riiablo/server/netty/PacketTuple.java
Normal file
23
server/netty/src/com/riiablo/server/netty/PacketTuple.java
Normal file
@ -0,0 +1,23 @@
|
||||
package com.riiablo.server.netty;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import com.badlogic.gdx.utils.TimeUtils;
|
||||
|
||||
import com.riiablo.net.packet.netty.Netty;
|
||||
|
||||
public class PacketTuple {
|
||||
public int id;
|
||||
public long time;
|
||||
public ByteBuffer buffer;
|
||||
public Netty data;
|
||||
|
||||
public static PacketTuple obtain(int id, ByteBuffer buffer) {
|
||||
PacketTuple packet = new PacketTuple();
|
||||
packet.id = id;
|
||||
packet.time = TimeUtils.millis();
|
||||
packet.buffer = buffer;
|
||||
packet.data = Netty.getRootAsNetty(buffer);
|
||||
return packet;
|
||||
}
|
||||
}
|
@ -51,10 +51,10 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
|
||||
try {
|
||||
boolean valid = processHeader(ctx, in);
|
||||
if (!valid) return;
|
||||
ByteBuf content = ReliableUtil.getContent(in);
|
||||
ByteBuf content = Packet.Single.getContent(in);
|
||||
if (DEBUG_INBOUND) Gdx.app.log(TAG, " " + ByteBufUtil.hexDump(content));
|
||||
ByteBuffer buffer = content.nioBuffer();
|
||||
Packet packet = Packet.obtain(0, buffer);
|
||||
PacketTuple packet = PacketTuple.obtain(0, buffer);
|
||||
processPacket(ctx, packet.data);
|
||||
} finally {
|
||||
// in.release(); // Automatically released by channelRead() right now
|
||||
@ -62,37 +62,55 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
|
||||
}
|
||||
|
||||
protected boolean processHeader(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
|
||||
if (DEBUG_INBOUND) Gdx.app.log(TAG, " " + ByteBufUtil.hexDump(ReliableUtil.getHeader(in)));
|
||||
int remoteProtocol = ReliableUtil.getProtocol(in);
|
||||
int remoteProtocol = Packet.getProtocol(in);
|
||||
if (remoteProtocol != PROTOCOL) {
|
||||
Gdx.app.log(TAG, String.format(" rejected incoming PROTO:%d", remoteProtocol));
|
||||
Gdx.app.log(TAG, " rejected incoming PROTO:" + remoteProtocol);
|
||||
return false;
|
||||
}
|
||||
|
||||
int remoteSeq = ReliableUtil.getSEQ(in);
|
||||
int remoteAck = ReliableUtil.getACK(in);
|
||||
int remoteAckBits = ReliableUtil.getACK_BITS(in);
|
||||
int csize = ReliableUtil.getContentSize(in);
|
||||
|
||||
Gdx.app.log(TAG, " accepted incoming " + ReliableUtil.toString(in));
|
||||
if (ack < 0) {
|
||||
ack = remoteSeq;
|
||||
Gdx.app.log(TAG, " init ack=" + ack);
|
||||
} else if (sequenceGreater(remoteSeq, ack)) {
|
||||
int shift = difference(remoteSeq, ack);
|
||||
Gdx.app.log(TAG, " remoteSeq=" + remoteSeq + "; ack=" + ack + "; shift=" + shift);
|
||||
ack_bits <<= shift;
|
||||
ack_bits |= (1 << (shift - 1));
|
||||
ack = remoteSeq;
|
||||
} else {
|
||||
int diff = difference(ack, remoteSeq);
|
||||
Gdx.app.log(TAG, " diff=" + diff);
|
||||
if (diff <= Integer.SIZE) {
|
||||
ack_bits |= (1 << (diff - 1));
|
||||
int type = Packet.getType(in);
|
||||
if (DEBUG_INBOUND) {
|
||||
ByteBuf header = null;
|
||||
switch (type) {
|
||||
case Packet.SINGLE: header = Packet.Single.getHeader(in); break;
|
||||
case Packet.FRAGMENTED: header = Packet.Fragmented.getHeader(in); break;
|
||||
case Packet.SLICED: header = Packet.Sliced.getHeader(in); break;
|
||||
case Packet.SLICEDACK: header = Packet.SlicedAck.getHeader(in); break;
|
||||
default:
|
||||
Gdx.app.log(TAG, " rejected incoming TYPE:" + type);
|
||||
return false;
|
||||
}
|
||||
Gdx.app.log(TAG, " " + ByteBufUtil.hexDump(header));
|
||||
}
|
||||
|
||||
Gdx.app.log(TAG, " " + String.format("ACK:%d ACK_BITS:%08x", ack, ack_bits));
|
||||
switch (type) {
|
||||
case Packet.SINGLE: {
|
||||
int remoteSeq = Packet.getSEQ(in);
|
||||
|
||||
Gdx.app.log(TAG, " accepted incoming " + Packet.Single.toString(in));
|
||||
if (ack < 0) {
|
||||
ack = remoteSeq;
|
||||
Gdx.app.log(TAG, " init ack=" + ack);
|
||||
} else if (sequenceGreater(remoteSeq, ack)) {
|
||||
int shift = difference(remoteSeq, ack);
|
||||
Gdx.app.log(TAG, " remoteSeq=" + remoteSeq + "; ack=" + ack + "; shift=" + shift);
|
||||
ack_bits <<= shift;
|
||||
ack_bits |= (1 << (shift - 1));
|
||||
ack = remoteSeq;
|
||||
} else {
|
||||
int diff = difference(ack, remoteSeq);
|
||||
Gdx.app.log(TAG, " diff=" + diff);
|
||||
if (diff <= Integer.SIZE) {
|
||||
ack_bits |= (1 << (diff - 1));
|
||||
}
|
||||
}
|
||||
|
||||
Gdx.app.log(TAG, " " + String.format("ACK:%d ACK_BITS:%08x", ack, ack_bits));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new AssertionError();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -123,12 +141,12 @@ public class ReliableChannelHandler implements ChannelHandler, ChannelInboundHan
|
||||
Gdx.app.log(TAG, "channelWrite0 Packet to " + receiver.getHostName() + ":" + receiver.getPort());
|
||||
|
||||
ByteBuf header = ctx.alloc().buffer(); // TODO: worth sizing this correctly?
|
||||
ReliableUtil.createHeader(header, PROTOCOL, nextSequence(), ack, ack_bits);
|
||||
if (DEBUG_SEQ) Gdx.app.log(TAG, " " + ReliableUtil.toString(header));
|
||||
Packet.Single.createHeader(header, PROTOCOL, nextSequence(), ack, ack_bits);
|
||||
if (DEBUG_SEQ) Gdx.app.log(TAG, " " + Packet.Single.toString(header));
|
||||
if (DEBUG_OUTBOUND) Gdx.app.log(TAG, " " + ByteBufUtil.hexDump(header));
|
||||
|
||||
ByteBuf content = (ByteBuf) msg;
|
||||
ReliableUtil.setContentSize(header, content.readableBytes());
|
||||
Packet.Single.setContentSize(header, content.readableBytes());
|
||||
|
||||
CompositeByteBuf composite = ctx.alloc().compositeBuffer(2)
|
||||
.addComponent(true, header)
|
||||
|
@ -1,28 +1,76 @@
|
||||
package com.riiablo.server.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import java.nio.ByteBuffer;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
public class ReliableUtil {
|
||||
private ReliableUtil() {}
|
||||
/*
|
||||
static final int PACKET_SINGLE = 0;
|
||||
static final int PACKET_FRAGMENT = 1;
|
||||
static final int PACKET_SLICE = 2;
|
||||
static final int PACKET_SLICEACK = 3;
|
||||
|
||||
private static final int PROTOCOL_OFFSET = 0;
|
||||
private static final int PROTOCOL_SIZE = 1; // ubyte
|
||||
private static final int PROTOCOL_OFFSET = 0;
|
||||
private static final int PROTOCOL_SIZE = 1; // ubyte
|
||||
|
||||
private static final int SEQ_OFFSET = PROTOCOL_OFFSET + PROTOCOL_SIZE;
|
||||
private static final int SEQ_SIZE = 2; // ushort
|
||||
private static final int TYPE_OFFSET = PROTOCOL_OFFSET + PROTOCOL_SIZE;
|
||||
private static final int TYPE_SIZE = 1; // ubyte
|
||||
|
||||
private static final int ACK_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
private static final int ACK_SIZE = 2; // ushort
|
||||
private static final int SEQ_OFFSET = TYPE_OFFSET + TYPE_SIZE;
|
||||
private static final int SEQ_SIZE = 2; // ushort
|
||||
|
||||
private static final int ACK_BITS_OFFSET = ACK_OFFSET + ACK_SIZE;
|
||||
private static final int ACK_BITS_SIZE = 4; // int
|
||||
static class TYPE0 {
|
||||
private static final int ACK_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
private static final int ACK_SIZE = 2; // ushort
|
||||
|
||||
private static final int CONTENT_SIZE_OFFSET = ACK_BITS_OFFSET + ACK_BITS_SIZE;
|
||||
private static final int CONTENT_SIZE_SIZE = 2; // ushort
|
||||
private static final int ACK_BITS_OFFSET = ACK_OFFSET + ACK_SIZE;
|
||||
private static final int ACK_BITS_SIZE = 4; // int
|
||||
|
||||
static final int CONTENT_OFFSET = CONTENT_SIZE_OFFSET + CONTENT_SIZE_SIZE;
|
||||
private static final int CONTENT_SIZE_OFFSET = ACK_BITS_OFFSET + ACK_BITS_SIZE;
|
||||
private static final int CONTENT_SIZE_SIZE = 2; // ushort
|
||||
|
||||
static final int CONTENT_OFFSET = CONTENT_SIZE_OFFSET + CONTENT_SIZE_SIZE;
|
||||
}
|
||||
|
||||
static class TYPE1 {
|
||||
private static final int FRAGID_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
private static final int FRAGID_SIZE = 1; // ubyte
|
||||
|
||||
private static final int NUMFRAG_OFFSET = FRAGID_OFFSET + FRAGID_SIZE;
|
||||
private static final int NUMFRAG_SIZE = 1; // ubyte
|
||||
|
||||
private static final int FRAG_SIZE_OFFSET = NUMFRAG_OFFSET + NUMFRAG_SIZE;
|
||||
private static final int FRAG_SIZE_SIZE = 2; // ushort
|
||||
|
||||
static final int CONTENT_OFFSET = FRAG_SIZE_OFFSET + FRAG_SIZE_SIZE;
|
||||
}
|
||||
|
||||
static class TYPE2 {
|
||||
private static final int CHUNKID_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
private static final int CHUNKID_SIZE = 1; // ubyte
|
||||
|
||||
private static final int SLICEID_OFFSET = CHUNKID_OFFSET + CHUNKID_SIZE;
|
||||
private static final int SLICEID_SIZE = 1; // ubyte
|
||||
|
||||
private static final int NUMSLICE_OFFSET = SLICEID_OFFSET + SLICEID_SIZE;
|
||||
private static final int NUMSLICE_SIZE = 1; // ubyte
|
||||
|
||||
private static final int SLICE_SIZE_OFFSET = NUMSLICE_OFFSET + NUMSLICE_SIZE;
|
||||
private static final int SLICE_SIZE_SIZE = 2; // ushort
|
||||
|
||||
static final int CONTENT_OFFSET = SLICE_SIZE_OFFSET + SLICE_SIZE_SIZE;
|
||||
}
|
||||
|
||||
static class TYPE3 {
|
||||
private static final int CHUNKID_OFFSET = SEQ_OFFSET + SEQ_SIZE;
|
||||
private static final int CHUNKID_SIZE = 1; // ubyte
|
||||
|
||||
private static final int NUMSLICE_OFFSET = CHUNKID_OFFSET + CHUNKID_SIZE;
|
||||
private static final int NUMSLICE_SIZE = 1; // ubyte
|
||||
|
||||
private static final int ACK_BITS_OFFSET = NUMSLICE_OFFSET + NUMSLICE_SIZE;
|
||||
private static final int ACK_BITS_SIZE = 32; // array
|
||||
|
||||
static final int CONTENT_OFFSET = ACK_BITS_OFFSET + ACK_BITS_SIZE;
|
||||
}
|
||||
|
||||
public static int getProtocol(ByteBuf bb) {
|
||||
return bb.getUnsignedByte(PROTOCOL_OFFSET);
|
||||
@ -92,4 +140,5 @@ public class ReliableUtil {
|
||||
return String.format("PROTO:%d SEQ:%d ACK:%d ACK_BITS:%08x CSIZE:%d",
|
||||
getProtocol(bb), getSEQ(bb), getACK(bb), getACK_BITS(bb), getContentSize(bb));
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@ -41,12 +41,12 @@ public class ReliableUtilTest {
|
||||
final int ACK = 0x0F0F;
|
||||
final int ACK_BITS = 0xFF0000FF;
|
||||
|
||||
ReliableUtil.setProtocol(bb, PROTOCOL);
|
||||
ReliableUtil.setSEQ(bb, SEQ);
|
||||
ReliableUtil.setACK(bb, ACK);
|
||||
ReliableUtil.setACK_BITS(bb, ACK_BITS);
|
||||
Packet.Single.setProtocol(bb, PROTOCOL);
|
||||
Packet.Single.setSEQ(bb, SEQ);
|
||||
Packet.Single.setACK(bb, ACK);
|
||||
Packet.Single.setACK_BITS(bb, ACK_BITS);
|
||||
|
||||
bb.writerIndex(ReliableUtil.CONTENT_OFFSET); // hack to force writer position passed header
|
||||
bb.writerIndex(Packet.Single.CONTENT_OFFSET); // hack to force writer position passed header
|
||||
System.out.println(ByteBufUtil.hexDump(bb)); // note: hexDump requires writerIndex
|
||||
|
||||
FlatBufferBuilder builder = new FlatBufferBuilder();
|
||||
@ -54,17 +54,17 @@ public class ReliableUtilTest {
|
||||
int dataOffset = Connection.endConnection(builder);
|
||||
int offset = Netty.createNetty(builder, NettyData.Connection, dataOffset);
|
||||
Netty.finishNettyBuffer(builder, offset);
|
||||
ReliableUtil.setContent(bb, builder.dataBuffer());
|
||||
Packet.Single.setContent(bb, builder.dataBuffer());
|
||||
|
||||
bb.writerIndex(ReliableUtil.CONTENT_OFFSET + ReliableUtil.getContentSize(bb)); // hack to force writer position passed content
|
||||
bb.writerIndex(Packet.Single.CONTENT_OFFSET + Packet.Single.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)));
|
||||
System.out.printf("%-8s %-5s %02x%n", "PROTOCOL", PROTOCOL == Packet.Single.getProtocol(bb), Packet.Single.getProtocol(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "SEQ", SEQ == Packet.Single.getSEQ(bb), Packet.Single.getSEQ(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "ACK", ACK == Packet.Single.getACK(bb), Packet.Single.getACK(bb));
|
||||
System.out.printf("%-8s %-5s %08x%n", "ACK_BITS", ACK_BITS == Packet.Single.getACK_BITS(bb), Packet.Single.getACK_BITS(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "CSIZE", builder.dataBuffer().remaining() == Packet.Single.getContentSize(bb), Packet.Single.getContentSize(bb));
|
||||
System.out.printf("%-8s %-5s %s%n", "CONTENT", StringUtils.equals(ByteBufUtil.hexDump(builder.sizedByteArray()), ByteBufUtil.hexDump(Packet.Single.getContent(bb))), ByteBufUtil.hexDump(Packet.Single.getContent(bb)));
|
||||
}
|
||||
|
||||
static void testComposite(CompositeByteBuf bb) {
|
||||
@ -74,7 +74,7 @@ public class ReliableUtilTest {
|
||||
final int ACK_BITS = 0xFF0000FF;
|
||||
|
||||
ByteBuf bbHeader = bb.alloc().buffer();
|
||||
ReliableUtil.createHeader(bbHeader, PROTOCOL, SEQ, ACK, ACK_BITS);
|
||||
Packet.Single.createHeader(bbHeader, PROTOCOL, SEQ, ACK, ACK_BITS);
|
||||
System.out.println("HEADER: " + ByteBufUtil.hexDump(bbHeader)); // note: hexDump requires writerIndex
|
||||
|
||||
FlatBufferBuilder builder = new FlatBufferBuilder();
|
||||
@ -93,17 +93,17 @@ public class ReliableUtilTest {
|
||||
|
||||
bb.addComponents(true, bbHeader, bbContent);
|
||||
|
||||
ReliableUtil.setContentSize(bb, bbContent.readableBytes());
|
||||
Packet.Single.setContentSize(bb, bbContent.readableBytes());
|
||||
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)));
|
||||
System.out.printf("%-8s %-5s %02x%n", "PROTOCOL", PROTOCOL == Packet.Single.getProtocol(bb), Packet.Single.getProtocol(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "SEQ", SEQ == Packet.Single.getSEQ(bb), Packet.Single.getSEQ(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "ACK", ACK == Packet.Single.getACK(bb), Packet.Single.getACK(bb));
|
||||
System.out.printf("%-8s %-5s %08x%n", "ACK_BITS", ACK_BITS == Packet.Single.getACK_BITS(bb), Packet.Single.getACK_BITS(bb));
|
||||
System.out.printf("%-8s %-5s %04x%n", "CSIZE", builder.dataBuffer().remaining() == Packet.Single.getContentSize(bb), Packet.Single.getContentSize(bb));
|
||||
System.out.printf("%-8s %-5s %s%n", "CONTENT", StringUtils.equals(ByteBufUtil.hexDump(builder.sizedByteArray()), ByteBufUtil.hexDump(Packet.Single.getContent(bb))), ByteBufUtil.hexDump(Packet.Single.getContent(bb)));
|
||||
|
||||
ByteBuf content = ReliableUtil.getContent(bb);
|
||||
ByteBuf content = Packet.Single.getContent(bb);
|
||||
ByteBuffer nioContent = content.nioBuffer();
|
||||
System.out.println(ByteBufUtil.hexDump(BufferUtils.readRemaining(nioContent)));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user