Netty's channelhandler Some understanding and channelpipeline

From the current point of view, channelpipeline together and channelhandler is understandable. For me, channelpipeline is similar to a container, the container is stored channelhandler.

First, it is concerned that under channelhandler. It consists of two sub-interfaces: channelinboundhandler and channeloutboundhandler, respectively, the processing of inbound and outbound transactions.

The channelinboundhandler general have channelinboundhandleradapter and simplerchannelinboundhandler, then this is what difference does it make?

Netty general use to send and receive data inherit SimpleChannelInboundHandler and ChannelInboundHandlerAdapter two abstract classes, then the two in the end what difference does it make?

In fact, with these two abstract classes is luxurious, the client's business Handler inherited SimpleChannelInboundHandler, and inherit the server side is ChannelInboundHandlerAdapter.

The main difference is SimpleChannelInboundHandler after receiving the data will automatically release the resource data out Bytebuffer occupied (automatic call Bytebuffer.release ()). And why the server can not use it, because we want the server to send data back to the client's request, and the server may not have finished before channelRead method returns data, it can not automatically release. (Reference https://www.cnblogs.com/Anders888/p/5769016.html )

From here understanding is that the server-side use ChannelInboundHandlerAdapter.

 

The question then summarized as follows:

1, in this channelpipeline in, is how to implement channelhandler of?

2, for the server side, it is to deal with inbound or outbound processing?

 

20200326

About channelpipeline inside channelhandler, there is a new understanding, are summarized as follows:

1, channelpipeline can register multiple channelhandler ( The handler must inherit netty of ChannelInboundHandler and ChannelOutboundHandler it );

2, transfer between ChannelInboundHandler, by calling ctx.fireChannelRead (msg); call ctx.write (msg) is passed to ChannelOutboundHandler;

3, ChannelOutboundHandler at the time of registration needs to be placed before the last ChannelInboundHandler, otherwise it will not be delivered to ChannelOutboundHandler.

4, outBound and Inbound who should perform for the client and server, the client initiated the request again accept data, then the first outbound inbound, server and vice versa.

 

4, on addlast and addfirst

Published 125 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/jiezhang656/article/details/105031957