Netty解码器内存消耗
我使用Netty IO(最终版本为4.0.29)对来自TCP服务器的字节进行解码。在管道的第一个位置,我使用LengthFieldBasedFrameDecoder在帧中分割字节(该处理程序工作正常)。在管道的第二个位置,我实现了自己的处理程序,该处理程序通过Netty的teToMessageDecoder进行扩展 在这个自定义处理程序中,我要执行几个操作:Netty解码器内存消耗,netty,heap-memory,Netty,Heap Memory,我使用Netty IO(最终版本为4.0.29)对来自TCP服务器的字节进行解码。在管道的第一个位置,我使用LengthFieldBasedFrameDecoder在帧中分割字节(该处理程序工作正常)。在管道的第二个位置,我实现了自己的处理程序,该处理程序通过Netty的teToMessageDecoder进行扩展 在这个自定义处理程序中,我要执行几个操作: 将字节转换为十六进制表示形式以便在中录制 文本文件 验证帧的校验和 基于字节#14和#15聚合帧,这两个字节分别表示段号和段总计数(因此,
私人ByteBuf骨料;
私有短ID;
@凌驾
受保护的无效解码(ChannelHandlerContext ctx、ByteBuf in、,
列出)抛出异常
{
字符串hextump=bytebuutil.hextump(in);
RecordingService.record(hextump);
in=in.order(字节顺序、小字节);
字节segId=in.getByte(14);
字节segCnt=in.getByte(15);
int readableBytes=in.readableBytes();
整数和=0;
for(int index=0;index
谢谢。我认为最好在
ServerHandler
类中,在方法channelRead
内部,执行ServerHandler
类中的1点和2点,在方法channelRead
private ByteBuf aggregate;
private short lastSegmentId;
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in,
List<Object> out) throws Exception
{
String hexDump = ByteBufUtil.hexDump(in);
RecordingService.record(hexDump);
in = in.order(ByteOrder.LITTLE_ENDIAN);
byte segId = in.getByte(14);
byte segCnt = in.getByte(15);
int readableBytes = in.readableBytes();
int sum = 0;
for (int index = 0; index < readableBytes-2; index++)
{
short s = in.readUnsignedByte();
sum += s;
}
int checksum = in.readUnsignedShort();
if (sum == checksum)
{
//
// Checksum correct
//
if (segId == (lastSegmentId + 1))
{
lastSegmentId = segId;
byte[] byteArray = new byte[readableBytes-18];
in.getBytes(16, byteArray, 0, readableBytes-18);
aggregate.writeBytes(byteArray);
if (lastSegmentId == segCnt)
{
aggregate.readerIndex(0);
out.add(aggregate.copy());
lastSegmentId = 0;
aggregate.clear();
}
else
{
lastSegmentId = 0;
aggregate.clear();
}
}
else
{
//
// Checksum not correct
//
}
}
}