Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

channelFuture我们分析完了,我们继续往下分析serverBootstrap的bind的方法 

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

创建一个新的channel对象,然后绑定它:

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

调用私有的doBind方法 

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

我们看到initAndRegister()返回的是channelFuture对象

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

进入到initAndRegister()

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

通过channelFactory来创建channel对象

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

进入到channelFactory 

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

 newChannel方法的具体实现是在ReflectiveChannelFactory(如果不清楚你可以通过debug的方法进入都这里然后查看它的返回对象是谁)

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

很显然返回的是我们传入的NioServerSocketChannel.class

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

 创建好了channel之后,我们调用init()方法来初始化这个状态:

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

进入init()方法:

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

查看它的继承类中的实现ServerBootstrap

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

进来发现,第一行出现了一个option0()方法

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

我们进入option0()查看,返回的是一个options实例:

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

 options实例,它实质是一个LinkedHashMap结构

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

它在AbstractBootstrap构造方法的时候就已经初始化了:

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

setChannelOptions方法 

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

进入到setChannelOptions()方法中查看

就是不断循环options里面的值,将其赋值到ChannelOptions中。具体赋值是什么到后面我会通过debug的方式就行分析

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

这里还有一个attrs0()的方法

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

它也是一个LinkedHashMap结构

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

在构造方法中赋值属性

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

然后在当前channel中进行属性赋值:

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

属性赋值完之后,channel调用pipeline()方法,作用:将多个组件连接到一起。

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

通过pipeline()方法,返回一个channelPipeline对象

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

对选项属性的一些设定

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

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

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

获取一个config的handler

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

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

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

这个handler是处理你bossGroup的

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

 继续往下分析:

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

pipeline是当前channel的通道

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

服务端启动接收器

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析 

 进入到ServerBootstrapAcceptor

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

Netty的深入浅出--43.ServerBootstrap.bind()方法深入分析

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