public class LengthFieldBasedFrameDecoder extends FrameDecoder
A decoder that splits the received
ChannelBuffer
s dynamically by the value of the length field in the message. It is这个解码器分割动态的buffer更加消息的长度
particularly useful when you decode a binary message which has an integer header field that represents the length of the
有一个代表消息体的长度的头部当你解码一个2进制消息是很有用的
message body or the whole message.
LengthFieldBasedFrameDecoder
has many configuration parameters so that it can decode any message with a
有很多参数所以有一个固定长度的域能解码然后消息
length field, which is often seen in proprietary client-server protocols. Here are some example that will give you the basic idea on which option does what.
下面是一些例子
2 bytes length field at offset 0, do not strip header
2个字节长度 ,不会去掉头部
The value of the length field in this example is 12 (0x0C) which represents the length of "HELLO, WORLD". By default, the值是12代表“HELLO, WORLD”的长度
decoder assumes that the length field represents the number of the bytes that follows the length field. Therefore, it can be decoded with the simplistic parameter combination.
lengthFieldOffset = 0 lengthFieldLength = 2 lengthAdjustment = 0 initialBytesToStrip = 0 (= do not strip header) // 属性 BEFORE DECODE (14 bytes) AFTER DECODE (14 bytes) +--------+----------------+ +--------+----------------+ | Length | Actual Content |----->| Length | Actual Content | | 0x000C | "HELLO, WORLD" | | 0x000C | "HELLO, WORLD" | +--------+----------------+ +--------+----------------+
2 bytes length field at offset 0, strip header
去掉头部
Because we can get the length of the content by callingChannelBuffer.readableBytes()
, you might want to strip the length field by specifying initialBytesToStrip. In this example, we specified 2, that is same with the length of the length field, to strip the first two bytes.
lengthFieldOffset = 0 lengthFieldLength = 2 lengthAdjustment = 0 initialBytesToStrip = 2 (= the length of the Length field)// 去掉头部字节 BEFORE DECODE (14 bytes) AFTER DECODE (12 bytes) +--------+----------------+ +----------------+ | Length | Actual Content |----->| Actual Content | | 0x000C | "HELLO, WORLD" | | "HELLO, WORLD" | +--------+----------------+ +----------------+
2 bytes length field at offset 0, do not strip header, the length field represents the
length of the whole message
2个字节代表整个消息的长度
In most cases, the length field represents the length of the message body only, as shown in the previous examples. However, in some protocols, the length field represents the length of the whole message, including the message header. In such a case, we specify a non-zero lengthAdjustment. Because the length value in this example message is always greater than the body length by 2, we specify -2 as lengthAdjustment for compensation.lengthFieldOffset = 0 lengthFieldLength = 2 lengthAdjustment = -2 (= the length of the Length field) initialBytesToStrip = 0 BEFORE DECODE (14 bytes) AFTER DECODE (14 bytes) +--------+----------------+ +--------+----------------+ | Length | Actual Content |----->| Length | Actual Content | | 0x000E | "HELLO, WORLD" | | 0x000E | "HELLO, WORLD" | +--------+----------------+ +--------+----------------+
3 bytes length field at the end of 5 bytes header, do not strip header
The following message is a simple variation of the first example. An extra header value is prepended to the message. lengthAdjustment is zero again because the decoder always takes the length of the prepended data into account during frame length calculation.lengthFieldOffset = 2 (= the length of Header 1) lengthFieldLength = 3 lengthAdjustment = 0 initialBytesToStrip = 0 BEFORE DECODE (17 bytes) AFTER DECODE (17 bytes) +----------+----------+----------------+ +----------+----------+----------------+ | Header 1 | Length | Actual Content |----->| Header 1 | Length | Actual Content | | 0xCAFE | 0x00000C | "HELLO, WORLD" | | 0xCAFE | 0x00000C | "HELLO, WORLD" | +----------+----------+----------------+ +----------+----------+----------------+
3 bytes length field at the beginning of 5 bytes header, do not strip header
This is an advanced example that shows the case where there is an extra header between the length field and the message body. You have to specify a positive lengthAdjustment so that the decoder counts the extra header into the frame length calculation.lengthFieldOffset = 0 lengthFieldLength = 3 lengthAdjustment = 2 (= the length of Header 1) initialBytesToStrip = 0 BEFORE DECODE (17 bytes) AFTER DECODE (17 bytes) +----------+----------+----------------+ +----------+----------+----------------+ | Length | Header 1 | Actual Content |----->| Length | Header 1 | Actual Content | | 0x00000C | 0xCAFE | "HELLO, WORLD" | | 0x00000C | 0xCAFE | "HELLO, WORLD" | +----------+----------+----------------+ +----------+----------+----------------+
2 bytes length field at offset 1 in the middle of 4 bytes header, strip the first header field and the length field
This is a combination of all the examples above. There are the prepended header before the length field and the extra header after the length field. The prepended header affects thelengthFieldOffset and the extra header affects the lengthAdjustment. We also specified a non-zero initialBytesToStrip to strip the length field and the prepended header from the frame. If you don't want to strip the prepended header, you could specify 0 for initialBytesToSkip.lengthFieldOffset = 1 (= the length of HDR1) lengthFieldLength = 2 lengthAdjustment = 1 (= the length of HDR2) initialBytesToStrip = 3 (= the length of HDR1 + LEN) BEFORE DECODE (16 bytes) AFTER DECODE (13 bytes) +------+--------+------+----------------+ +------+----------------+ | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content | | 0xCA | 0x000C | 0xFE | "HELLO, WORLD" | | 0xFE | "HELLO, WORLD" | +------+--------+------+----------------+ +------+----------------+
2 bytes length field at offset 1 in the middle of 4 bytes header, strip the first header field and the length field, the length field represents the length of the whole message
Let's give another twist to the previous example. The only difference from the previous example is that the length field represents the length of the whole message instead of the message body, just like the third example. We have to count the length of HDR1 and Length into lengthAdjustment. Please note that we don't need to take the length of HDR2 into account because the length field already includes the whole header length.lengthFieldOffset = 1 lengthFieldLength = 2 lengthAdjustment = -3 (= the length of HDR1 + LEN, negative) initialBytesToStrip = 3 BEFORE DECODE (16 bytes) AFTER DECODE (13 bytes) +------+--------+------+----------------+ +------+----------------+ | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content | | 0xCA | 0x0010 | 0xFE | "HELLO, WORLD" | | 0xFE | "HELLO, WORLD" | +------+--------+------+----------------+ +------+----------------+
LengthFieldPrepender
相关推荐
netty中,大多数的协议在协议头中都会携带长度字段,用于...LengthFieldBasedFrameDecoder通过指定长度来标识整包消息,这样就可以自动的处理黏包和半包消息,只要传入正确的参数,就可以轻松解决“读半包”的问题。
LengthFieldBasedFrameDecoder decoder = new LengthFieldBasedFrameDecoder( maxFrameLength, // 最大数据包长度 lengthFieldOffset, // 长度字段在消息中的起始位置 lengthFieldLength, // 长度字段的长度 ...
Netty 中的 LengthFieldBasedFrameDecoder 是一种常用的处理大数据分包传输问题的解决类。该类提供了多种参数来调整帧的解码方式,从而满足不同的应用场景。 1. maxFrameLength:解码的帧的最大长度。该参数用于...
3. **Netty中的编解码处理**:Netty 提供了多种ChannelHandler(处理器)来解决这个问题,其中`LengthFieldBasedFrameDecoder`和`LengthFieldPrepender`是处理粘包和拆包的常用工具。 - `...
4. **LengthFieldBasedFrameDecoder**:最灵活的一种,它需要消息头包含数据包的长度信息,然后按照长度来拆分消息。 使用这些解码器,开发者可以将它们添加到Netty的ChannelPipeline(责任链)中,例如: ```java...
2. **使用FrameDecoder**:Netty的ChannelInboundHandlerAdapter中有多个预定义的解码器,例如LengthFieldBasedFrameDecoder,它可以基于前导长度字段来拆分消息。用户需要指定长度字段的位置、长度、偏移量等参数,...
协议的编码、解码工作由`MyMessageEncoder`,`MyMessageDecoder`两个类完成,在tcp传输过程中的拆、粘包问题使用`LengthFieldBasedFrameDecoder`类解决。 #### 心跳机制 - 服务端采用 `IdleStateHandler`,在一段...
LengthFieldPrepender在数据前面添加一个表示数据长度的字段,而LengthFieldBasedFrameDecoder则根据这个长度字段来拆分和解析接收到的数据,确保正确地处理不同长度的数据包。 总的来说,Netty利用Java NIO的非...
添加io.netty的maven库, 在com.zhao的包下,可以自行修改 有较多的注释,可以学习使用
- Netty 提供了多种序列化方式,包括ByteBuf、LengthFieldBasedFrameDecoder、ProtobufVarint32FrameDecoder等。在本例中,POJO(Plain Old Java Object)对象需要转换成二进制格式在网络上传输,这通常需要用到...
通常,我们可以使用`LengthFieldBasedFrameDecoder`来根据数据包的长度字段来分割数据。 ```java import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec...
2. **高效的数据编码与解码**:Netty提供了丰富的编解码器,如LineBasedFrameDecoder用于处理按行分隔的数据,LengthFieldBasedFrameDecoder用于处理带有长度字段的协议数据,这些预定义的编解码器简化了网络协议的...
7. **Netty的编解码器**:如LengthFieldBasedFrameDecoder用于处理带有长度字段的协议,LineBasedFrameDecoder用于按行处理数据。理解自定义编解码器的设计原则。 8. **WebSocket和HTTP/2支持**:Netty提供了对...
2. **高度可定制性**: Netty提供了多种编解码器,如LengthFieldBasedFrameDecoder用于处理固定长度或可变长度的数据帧,ByteToMessageDecoder和MessageToByteEncoder则可以自定义数据解析和序列化方式。 3. **零...
4. **强大的编码和解码器**:Netty 提供了丰富的编解码器,如LengthFieldBasedFrameDecoder用于处理带有长度字段的协议,LineBasedFrameDecoder用于按行解析数据等,这些组件使得处理各种网络协议变得非常便捷。...
Netty提供了一系列的编码解码器,如StringDecoder、StringEncoder、LengthFieldBasedFrameDecoder等,方便进行数据的转换,使得不同协议的数据能在同一套框架下处理。 6. **Pipeline** ChannelPipeline是事件处理...
- Netty 提供了一系列的编解码器,如 LengthFieldBasedFrameDecoder、LineBasedFrameDecoder 等,用于将原始字节流转换为有意义的协议消息。 6. **WebSocket 支持** - Netty 提供了 WebSocket 协议的支持,可以...
4. **编码器与解码器**:Netty 提供了一系列预定义的编解码器,如 LengthFieldBasedFrameDecoder 可以自动处理带有长度字段的协议,LineBasedFrameDecoder 用于按行处理数据。开发者也可以自定义编解码器以适应特定...
4. **Netty的编码解码器**:Netty提供了丰富的编码解码器,如LengthFieldBasedFrameDecoder用于处理带有长度字段的消息,StringDecoder和StringEncoder用于字符串的转换,这些都极大地简化了协议解析的过程。...