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

How to initialize Channel in Netty component

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is to share with you about how to initialize Channel in Netty components, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

I. instantiation of DefaultChannelPipeline

The DefaultChannelPipeline is created with the creation of the Channel, that is, as long as the Channel is created, the corresponding ChannelPipeline is created at the same time. The following code is called when Channel is instantiated, at the end of the last article.

1.ChannelHandlerContext class diagram structure

ChannelHandlerContext is intuitively named as ChannelHandler context, and the corresponding ChannelHandler is passed in each construction of DefaultChannelHandlerContext.

ChannelHandlerContext class diagram structure

2.ChannelPipeline class diagram structure

As can be seen from the following class diagram structure, ChannelPipeline provides many methods to manipulate linked lists, such as addFirst/addLast/addBefore/addLast/remove/replace, etc., and the input parameter is ChannelHandler. All kinds of fire operations of ChannelPipeline are handled by HandlerContext.

3. Linked table relation diagram

First, take a look at the linked list structure of the operation from the following code. The screenshot is as follows.

Sample code

Memory structure

Draw a diagram to illustrate the relationship among ChannelPipeline, ChannelHandlerContext and ChannelHandler.

Relationship diagram

I. Channel initialization

Switch to the Channel initialization process, and when the client boot class Bootstrap calls b.connect () or the server boot class ServerBootstrap calls bind (), the initAndRegister () of the abstract boot class AbstractBootstrap is called. The red part below is the channel initialization entry.

1. Client initialization

@ 1 client initialization code to add ChannelHandler to the ChannelPipeline linked list through DefaultChannelHandlerContext

@ 2 setChannelOptions take NioChannelOption as an example, and the client ends up calling SocketChannelImpl#setOption (); you can set the following properties

StandardSocketOptions.SO_RCVBUF / / accept cache size

StandardSocketOptions.SO_SNDBUF / / send cache size

StandardSocketOptions.SO_LINGER / / set the time for delayed shutdown

StandardSocketOptions.IP_TOS / / set packet priority

StandardSocketOptions.IP_MULTICAST_TTL / / sets the TTL value of multicast group data

...

two。 Server initialization

@ 1 setChannelOptions also takes NioChannelOption as an example. The server will call ServerSocketChannelImpl#setOption (). For more information, please see client initialization @ 1.

@ 2 ChannelInitializer realizes that ChannelHandler is added to the linked list of ChannelPipeline, and the logic in it is combed when analyzing EventLoopGroup in another article.

The above is how to initialize Channel in the Netty component. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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

Internet Technology

Wechat

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

12
Report