Skip to content

Commit

Permalink
optmize code
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm committed Jan 1, 2024
1 parent 95ac2e0 commit 7b36433
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
if (in == null) {
return;
}
if (in.readableBytes() < CONSTANT_MAGIC_FLAG.length + VERSION.length + 4 + 4) {
final int prefixLength = CONSTANT_MAGIC_FLAG.length + VERSION.length;
if (in.readableBytes() < prefixLength + 4 + 4) {
// Not enough data to read the package length and header length
return;
}
Expand All @@ -112,12 +113,12 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
validateFlagAndVersion(flagBytes, versionBytes, ctx);
final int packageLength = in.readInt();
final int headerLength = in.readInt();
if (in.readableBytes() < packageLength - 13) {
if (in.readableBytes() < packageLength - prefixLength) {
// Not enough data yet, reset the reader index and wait for more data
in.resetReaderIndex();
return;
}
final int bodyLength = packageLength - CONSTANT_MAGIC_FLAG.length - VERSION.length - headerLength;
final int bodyLength = packageLength - prefixLength - headerLength;
Header header = parseHeader(in, headerLength);
Object body = parseBody(in, header, bodyLength);

Expand Down

0 comments on commit 7b36433

Please sign in to comment.