In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to use the frame decoder in netty". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the frame decoder in netty" can help you solve the problem.
Brief introduction
The data in netty is transmitted through ByteBuf, and a ByteBuf may contain multiple meaningful data, which can be called frame, that is, a ByteBuf can contain multiple Frame.
For the recipient of the message, after receiving the ByteBuf and parsing the useful data from the ByteBuf, it is necessary to split and parse the frame in the ByteBuf.
Generally speaking, there are some specific delimiters between different frame. We can distinguish the frame by these delimiters, thus realizing the parsing of the data.
Netty provides us with some suitable frame decoders, through the use of these frame decoders can effectively simplify our work. The following figure shows several common frame decoders in netty:
Next, let's introduce the use of the above frame decoders in detail.
LineBasedFrameDecoder
LineBasedFrameDecoder distinguishes frame by line from its name. Depending on the operating system, line breaks can have two newline characters, which are "\ n" and "\ r\ n".
The basic principle of LineBasedFrameDecoder is to read the corresponding characters from ByteBuf to compare the characters with "\ n" and "\ r\ n". These frameDecoder also have certain requirements for character coding, generally speaking, UTF-8 coding is required. Because in such a code, "\ n" and "\ r" appear as a byte and will not be used in other combined codes, it is very safe to use "\ n" and "\ r" to judge.
There are several important attributes in LineBasedFrameDecoder, one of which is the property of maxLength, which is used to detect the length of the received message, and if the length limit is exceeded, a TooLongFrameException exception is thrown.
There is also a stripDelimiter attribute that determines whether the delimiter needs to be filtered out.
The other is failFast, and if the value is true, TooLongFrameException is thrown as long as the length of the frame exceeds the maxFrameLength, regardless of whether the frame is read or not. If the value is false, then TooLongFrameException will not be thrown until the entire frame has been fully read.
The core logic of LineBasedFrameDecoder is to first find the position of the line delimiter, and then read the corresponding frame information according to this position. Here, take a look at the findEndOfLine method of finding the row delimiter:
Private int findEndOfLine (final ByteBuf buffer) {int totalLength = buffer.readableBytes (); int I = buffer.forEachByte (buffer.readerIndex () + offset, totalLength-offset, ByteProcessor.FIND_LF); if (I > = 0) {offset = 0; if (I > 0 & & buffer.getByte (i1) = ='\ r') }} else {offset = totalLength;} return I;}
A forEachByte of ByteBuf is used here to traverse the ByteBuf. The character we are looking for is: ByteProcessor.FIND_LF.
Finally, the object decoded by LineBasedFrameDecoder is still a ByteBuf.
DelimiterBasedFrameDecoder
The LineBasedFrameDecoder mentioned above is only valid for line delimiters, and LineBasedFrameDecoder cannot be used if our frame is divided by other delimiters, so netty provides a more generic DelimiterBasedFrameDecoder, which can customize the delimiter:
Public class DelimiterBasedFrameDecoder extends ByteToMessageDecoder {public DelimiterBasedFrameDecoder (int maxFrameLength, ByteBuf delimiter) {this (maxFrameLength, true, delimiter);}
The delimiter passed in is a ByteBuf, so the delimiter may have more than one character.
To solve this problem, an array of ByteBuf is defined in DelimiterBasedFrameDecoder:
Private final ByteBuf [] delimiters; delimiters= delimiter.readableBytes ()
This delimiters is obtained by calling delimiter's readableBytes.
The logic of DelimiterBasedFrameDecoder is similar to that of LineBasedFrameDecoder, intercepting data in bufer by comparing characters in bufer, but DelimiterBasedFrameDecoder can accept multiple delimiters, so its usefulness will be based on a wide range of applications.
FixedLengthFrameDecoder
In addition to frame splitting by comparing characters in ByteBuf, there are other common methods of frame splitting, such as distinguishing by specific length, and netty provides one such decoder called FixedLengthFrameDecoder.
Public class FixedLengthFrameDecoder extends ByteToMessageDecoder
FixedLengthFrameDecoder is also inherited from ByteToMessageDecoder, and its definition is simple. You can pass in the length of a frame:
Public FixedLengthFrameDecoder (int frameLength) {checkPositive (frameLength, "frameLength"); this.frameLength = frameLength;}
Then call the readRetainedSlice method of ByteBuf to read the fixed-length data:
In.readRetainedSlice (frameLength)
Finally, the read data is returned.
LengthFieldBasedFrameDecoder
There are also some frame that contain a specific length field that indicates how much data is readable in the ByteBuf. Such a frame is called LengthFieldBasedFrame.
A corresponding processing decoder is also provided in netty:
Public class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder
The logic of reading is simple: read the length first, and then read the data based on the length. To implement this logic, LengthFieldBasedFrameDecoder provides four fields, lengthFieldOffset,lengthFieldLength,lengthAdjustment and initialBytesToStrip.
LengthFieldOffset specifies the starting position of the length field, lengthFieldLength defines the length of the length field, lengthAdjustment adjusts the lengthFieldLength, and initialBytesToStrip indicates whether the length field needs to be removed.
It sounds like it's hard to understand. Let's give a few examples, first of all, the simplest:
BEFORE DECODE (14 bytes) AFTER DECODE (14 bytes) +-+ | Length | Actual Content |-> | Length | Actual Content | | 0x000C | "HELLO, WORLD" | | 0x000C | "HELLO" WORLD "| +-+
The message to be encoded has a length field, followed by the real data, and 0x000C is a hexadecimal that represents the data of 12, which is the length of the string in "HELLO, WORLD".
The values of the four properties are:
LengthFieldOffset = 0 lengthFieldLength = 2 lengthAdjustment = 0 initialBytesToStrip = 0
It means that the length field starts at 0, and the length field has two bytes, and the length does not need to be adjusted, nor does it need to be adjusted.
Let's take a look at a more complex example, in which the four attribute values are as follows:
LengthFieldOffset = 1 lengthFieldLength = 2 lengthAdjustment = 1 initialBytesToStrip = 3
The corresponding encoded data is as follows:
BEFORE DECODE (16 bytes) AFTER DECODE (13 bytes) +-+ | HDR1 | Length | HDR2 | Actual Content |-> | HDR2 | Actual Content | | 0xCA | 0x000C | 0xFE | "HELLO" WORLD "| | 0xFE |" HELLO, WORLD "| +-+
In the above example, the length field starts from the first byte (the 0th byte is HDR1), the length field occupies 2 bytes, and the length is adjusted by another byte, and the starting position of the final data is 1 "2" 1 "4, and then the first 3 bytes of data are intercepted to get the final result.
This is the end of the content about "how to use the frame Decoder in netty". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.