Netty's easy to understand --43.ServerBootstrap.bind () method in-depth analysis

ChannelFuture analysis method we finished, we continue to analyze bind serverBootstrap of top-down 

Create a new channel object, and then bind it:

Call the private method of doBind 

We see initAndRegister () returns an object channelFuture

Into initAndRegister ()

To create a channel objects by channelFactory

Into channelFactory 

 NewChannel specific implementation method is to ReflectiveChannelFactory (who do not know if you can enter here all through the debug method returns an object and then view it is)

It is clear that the return of our incoming NioServerSocketChannel.class

 Once you've created a channel, we call the init () method to initialize the state:

Enter init () method:

View achieve ServerBootstrap its inherited class

Come found, there is a first line option0 () method

We enter option0 () view, is a return of options Example:

 Examples of options, it is actually a structure LinkedHashMap

When it AbstractBootstrap constructor has been initialized:

setChannelOptions way 

Into setChannelOptions () method View

Is the constant value inside the loop options, assign it to a ChannelOptions in. What is the specific assignment to debug later I'll pass on the way to the line analysis

There is also a method attrs0 () of

It is also a structure LinkedHashMap

Valued property in the constructor

 

Then property assignment in the current channel:

After completion of the assignment attribute, call Pipeline Channel () method, the role of: connecting a plurality of components together.

 

By Pipeline () method returns a target channelPipeline

查看ChannelPipeline之后发现,它里面的成员方法是我们之前用过的,addFirst()、addLast()

 

将childGroup和childHandler赋值,里面的childHandler是我们自定义的那个处理,childGroup使我们定义的workGroup。

 

对选项属性的一些设定

 

我们自定义处理器的时候继承的也是ChannelInitializer,这里说明了服务器在初始化的时候就已经增加了一个处理器。

 

我们进入到ChannelInitializer源码中查看initChannel方法:

这个方法将会在channel管道被注册的时候被调用。在这个方法返回实例之后将会从当前channel中的ChannelPipeline中被移除。

 它也是当前channel中获取到它的channelPipeline

获取一个config的handler

 

我们进入到config.handler()中:返回一个channelHandler对象

 

我们进入到bootsrap.handler()中:

 

我们会发现,它就是我们在之前说到的那个可有可无的handler()

 

当我们没有调用的时候就会返回null 

如果不为空就把它添加到管道中:

这个handler是处理你bossGroup的

 继续往下分析:

eventLoop本身是一个事件循环组,也就是一个线程池

 

 然后将其放入到runnable中进行执行:

 

pipeline是当前channel的通道

 

服务端启动接收器

 

 进入到ServerBootstrapAcceptor

我们应该和你熟悉ChannelInboundHandlerAdapter:处理channel进入后的处理器适配器

 总结:init方法实现了options的属性设定,然后获取当前channel的管道pipeline,配置管道的处理器handler的属性

Guess you like

Origin blog.csdn.net/qq_37909508/article/details/91314105