Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of MessageToByteEncoder Logic of Netty distributed Abstract Encoder

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you the Netty distributed abstract encoder MessageToByteEncoder logic example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

MessageToByteEncoder

Like the decoder, there is an abstract class called MessageToByteEncoder in the encoder, in which the skeleton method of the encoder is defined, and the specific coding logic is given to the subclass to implement.

The decoder is also a handler, which intercepts the written data. In learning pipeline, we know that the write event is passed when the data is written, and the write method of handler is called during the transfer process, so the encoder can rewrite the write method, encode the data into a binary byte stream, and then continue to pass the write event.

First, look at MessageToByteEncoder's class declaration public abstract class MessageToByteEncoder extends ChannelOutboundHandlerAdapter {/ / omit the class body}

Here, it inherits ChannelOutboundHandlerAdapter, which means it is an outBoundhandler. We know that the write event is an outBound event, and the outBound event can only be transmitted through outBoundHandler.

The write method of handler is called during the propagation of write events.

We follow the write method of MessageToByteEncoder:

Public void write (ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {ByteBuf buf = null; try {if (acceptOutboundMessage (msg)) {@ SuppressWarnings ("unchecked") I cast = (I) msg; buf = allocateBuffer (ctx, cast, preferDirect); try {encode (ctx, cast, buf) } finally {ReferenceCountUtil.release (cast);} if (buf.isReadable ()) {ctx.write (buf, promise);} else {buf.release (); ctx.write (Unpooled.EMPTY_BUFFER, promise);} buf = null } else {ctx.write (msg, promise);}} catch (EncoderException e) {throw e;} catch (Throwable e) {throw new EncoderException (e);} finally {if (buf! = null) {buf.release ();}

First of all, judge whether the current object can be processed by if (acceptOutboundMessage (msg)).

If it can be processed, enter the logic in the if block, if it cannot be processed, then enter the else block and continue to pass the write event through ctx.write (msg, promise)

Let's look at the if block.

I cast = (I) msg here is forced type conversion, converted to I type, I type is a generic, the specific type is defined by the user

Buf = allocateBuffer (ctx, cast, preferDirect) buffer allocation is made here

Follow the protected ByteBuf allocateBuffer (ChannelHandlerContext ctx, @ SuppressWarnings ("unused") I msg, boolean preferDirect) throws Exception {if (preferDirect) {return ctx.alloc () .ioBuffer ();} else {return ctx.alloc () .heapBuffer ();}} in the allocateBuffer method

Here, memory is allocated directly through ctx's memory allocator, and heap memory or out-of-heap memory is allocated by judging preferDirect. By default, out-of-heap memory is allocated.

We have done some analysis on memory allocation before.

Go back to the write method:

After the memory allocation, the encode (ctx, cast, buf) method is called to encode, and this class is implemented by subclasses.

The subclass can complete the coding by inheriting the class, overriding the encode method, encoding the parameter object cast into bytes and writing it to the incoming ByteBuf.

After the encoding is completed, the cast object is released through ReferenceCountUtil.release (cast)

If (buf.isReadable ()) here determines whether the buf has readable bytes, and if so, continues to pass the write event

If there are no readable bytes, release the buf, continue propagating the write event, and pass an empty ByteBuf

Finally, set buf to empty

The above is all the content of the article "sample Analysis of MessageToByteEncoder Logic of Netty distributed Abstract Encoder". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report