Moved ByteToMessageDecoder to it's own declared class

This commit is contained in:
Collin Smith 2020-06-27 16:39:48 -07:00
parent e05e9bebc6
commit 70cf9d016a

View File

@ -75,19 +75,7 @@ public class Main extends ApplicationAdapter implements PacketProcessor {
Main.this.endpoint = endpoint;
ch.pipeline()
.addFirst(connectionLimiter)
.addLast(new ByteToMessageDecoder() {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) return;
in.markReaderIndex();
final int length = in.readIntLE();
if (in.readableBytes() < length) {
in.resetReaderIndex();
return;
}
out.add(in.readRetainedSlice(length));
}
})
.addLast(new SizePrefixedDecoder())
.addLast(new EndpointedChannelHandler<>(ByteBuf.class, endpoint))
;
}
@ -253,4 +241,18 @@ public class Main extends ApplicationAdapter implements PacketProcessor {
return this;
}
}
private static class SizePrefixedDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() < 4) return;
in.markReaderIndex();
final int length = in.readIntLE();
if (in.readableBytes() < length) {
in.resetReaderIndex();
return;
}
out.add(in.readRetainedSlice(length));
}
}
}