From 990910393be8162a0371a3509db10f528748ca3b Mon Sep 17 00:00:00 2001 From: Collin Smith Date: Mon, 22 Jun 2020 12:03:40 -0700 Subject: [PATCH] Implemented headerSize and fragmentSize fields --- .../src/com/riiablo/net/reliable/Packet.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/server/netty/src/com/riiablo/net/reliable/Packet.java b/server/netty/src/com/riiablo/net/reliable/Packet.java index 5e7e2f90..bb8e0042 100644 --- a/server/netty/src/com/riiablo/net/reliable/Packet.java +++ b/server/netty/src/com/riiablo/net/reliable/Packet.java @@ -189,6 +189,9 @@ public abstract class Packet { public int fragmentId; public int numFragments; + int headerSize; + int fragmentSize; + @Override public String toString() { return new ToStringBuilder(this) @@ -208,6 +211,8 @@ public abstract class Packet { return -1; } + int startIndex = bb.readerIndex(); + if (bb.readableBytes() < FRAGMENT_HEADER_SIZE) { logError("buffer too small for fragment header"); return -1; @@ -244,16 +249,24 @@ public abstract class Packet { } ack = packetHeader.ack; ackBits = packetHeader.ackBits; + fragmentSize = bb.readableBytes(); } else { ack = 0; ackBits = 0; + fragmentSize = bb.readableBytes(); } - // TODO: validate fragmentBytes <= fragmentSize - // TODO: validate size of fragmentId == fragmentSize + if (fragmentSize > config.fragmentSize) { + logError("fragment bytes %d > fragment size %d", fragmentSize, config.fragmentSize); + return -1; + } - // TODO: implement support - int headerSize = 0; + if (fragmentId != numFragments - 1 && fragmentSize != config.fragmentSize) { + logError("fragment %d is %d bytes, which is not the expected fragment size %d bytes", fragmentId, fragmentSize, config.fragmentSize); + return -1; + } + + headerSize = bb.readerIndex() - startIndex + 1; // include prefixByte return headerSize; } }