Files
jsowell-charger-web/jsowell-netty/src/main/java/com/jsowell/netty/decoder/MyLengthFieldBasedFrameDecoder.java
2024-11-26 16:30:30 +08:00

31 lines
1.3 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.jsowell.netty.decoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import lombok.extern.slf4j.Slf4j;
/**
* 自定义LengthFieldBasedFrameDecoder
*/
@Slf4j
public class MyLengthFieldBasedFrameDecoder extends LengthFieldBasedFrameDecoder {
/*
构造方法
maxFrameLength指定解码器所能处理的数据包的最大长度超过该长度则抛出 TooLongFrameException 异常;
lengthFieldOffset指定长度字段的起始位置
lengthFieldLength指定长度字段的长度目前支持1(byte)、2(short)、3(3个byte)、4(int)、8(Long)
lengthAdjustment指定长度字段所表示的消息长度值与实际长度值之间的差值可以用于调整解码器的计算和提高灵活性。
initialBytesToStrip指定解码器在将数据包分离出来后跳过的字节数因为这些字节通常不属于消息体内容而是协议头或其他控制信息。
lengthAdjustment可正可负需要满足以下的计算关系
*/
public MyLengthFieldBasedFrameDecoder() {
super(208, 1, 1, 0, 2);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
return super.decode(ctx, in);
}
}