Netty's description of several Decoder

Disclaimer: This article is a blogger original article, welcome to reprint. https://blog.csdn.net/guo_xl/article/details/85933678

FixedLengthFrameDecoder

We have inherited from FixedLengthFrameDecoder

  • MarshallingDecoder
  • ObjectDecoder
    these two objects are to be used to convert

Such as the packet is received:

 * +---+----+------+----+
 * | A | BC | DEFG | HI |
 * +---+----+------+----+

If the definition of new FixedLengthFrameDecoder (3)
will resolve to

 * +-----+-----+-----+
 * | ABC | DEF | GHI |
 * +-----+-----+-----+

LengthFieldBasedFrameDecoder

Message according to separate in advance of the message length field

Message Format

  • Header: a few bytes to identify the length of the message body to Length
  • Message Body

= Total length of message header length + message length (Length) + lengthAdjustmen

For this type of message parsing, you may be used to provide the tools to do LengthFieldBasedFrameDecoder netty, in particular by

Analytical combination of several different cases to complete fields of a message
byte length of the message body lengthFieldOffset identifier in a message set in the byte position of the start
byte length of the message body identifier lengthFieldLength set a total number of bytes
lengthAdjustment message Total Length = total length of the message header message body + total length (the length) + lengthAdjustment
initialBytesToStrip which a few bytes to skip length

Case 1:
+--------+----------------+      +--------+----------------+
| Length | Actual Content |----->| Length | Actual Content |
| 0x000C | "HELLO, WORLD" |      | 0x000C | "HELLO, WORLD" |
+--------+----------------+      +--------+----------------+

lengthFieldOffset 0 byte identifies the length of the message in a message set in the byte position of the start
byte of the message identifies the length lengthFieldLength 2 set of a total number of bytes
lengthAdjustment 0
initialBytesToStrip 0

Case 2: Case 1 contains message header Prefer
+--------+----------------+      +----------------+
| Length | Actual Content |----->| Actual Content |
| 0x000C | "HELLO, WORLD" |      | "HELLO, WORLD" |
+--------+----------------+      +----------------+

0 lengthFieldOffset
lengthFieldLength 2
lengthAdjustment 0
initialBytesToStrip skipping two fields 2

Case 3:

= Total length of the total length of the message header message body + total length (the Length)
message overall length = 2 + 17 = 19
had only the long message 17, 19 has now become, certainly not so by setting lengthAdjustment = -2, the final
message overall length = 2 + 17 ± 2 (lengthAdjustment) = 17

+--------+----------------+      +--------+----------------+
| Length | Actual Content |----->| Length | Actual Content |
| 0x000E | "HELLO, WORLD" |      | 0x000E | "HELLO, WORLD" |
+--------+----------------+      +--------+----------------+

0 lengthFieldOffset
lengthFieldLength 2
lengthAdjustment length adjustment -2 -2
initialBytesToStrip 0

Case 4:

Total length is the total length of the message header message body + total length (length is the total length of the message body), but not calculated into the Header 1, Header 1 length to be added, so lengthAdjustment = 2

 +----------+----------+----------------+      +----------+----------+----------------+
 |  Length  | Header 1 | Actual Content |----->|  Length  | Header 1 | Actual Content |
 | 0x00000C |  0xCAFE  | "HELLO, WORLD" |      | 0x00000C |  0xCAFE  | "HELLO, WORLD" |
 +----------+----------+----------------+      +----------+----------+----------------+

lengthFieldOffset = 0
lengthFieldLength = 3
lengthAdjustment = 2
initialBytesToStrip = 0

Case 5: case 4 want to skip the message header
  +------+--------+------+----------------+      +------+----------------+
  | HDR1 | Length | HDR2 | Actual Content |----->| HDR2 | Actual Content |
  | 0xCA | 0x000C | 0xFE | "HELLO, WORLD" |      | 0xFE | "HELLO, WORLD" |
  +------+--------+------+----------------+      +------+----------------+

lengthFieldOffset = 0
lengthFieldLength = 3
lengthAdjustment = 2
initialBytesToStrip = 3

Guess you like

Origin blog.csdn.net/guo_xl/article/details/85933678