美文网首页
netty源码debug 解析

netty源码debug 解析

作者: 释_远 | 来源:发表于2017-09-24 19:51 被阅读0次

1.  EventLoopGroup bossGroup =newNioEventLoopGroup() 创建线程组

       看一下 EventExecutorGroup 接口 依赖关系

       它的主要的方法是next 方法

看一下 newNioEventLoopGroup 依赖关系

2 创建newNioEventLoopGroup 它的时候方法

2.1 重点看一下 SelectProvider.provider ,同时看一下类SelectProvider 这个类的作用

可参考 SelectProvider类解析

2.2

看一下 这个类  KQueueSelectorImpl的解析参考的构造方法 KQueueSelectorImpl(SelectorProvider var1)

IOUtil.makePipe(false);是一个static native方法,所以我们没办法查看源码。但是我们可以知道该函数返回了一个非堵塞的管道(pipe),底层是通过Linux的pipe系统调用实现的;创建了一个管道pipe并返回了一个64为的long型整数,该数的高32位存放了该管道读端的文件描述符,低32位存放了该pipe的写端的文件描述符。

3.完成Selector和Channel 绑定的在 Channel的 initAndRegister 

因为在group 中完成reactor线程模型的同事 注入了Selector 选择器 group中的对象EventLoopGroup是包含Selector的 这就和我们的NIO模型

4 .看一下注册事件 

5 下面看一下 AbstractCHannel的register方法

6.看到register0(promise) 很高兴终于看到正真的注册的方法了

但是还需要看doRegister();

7.看看doRegister 里面的for循环做了什么

重点说一下这个方法

将NioServerSocketChannel注册到NioEventLoop的Selector上,this是对象NioServerSocketChannel 作为注册的附件 attachment  这样终于看到了 selector ,channel ,和attachment,NioEventLoop持有Selector对象在构造 reactor 线程模型的时候构造的,channel 是NioServerSocketChannel 是在初始化的时候构造的

8.接着上面的图看看pipeline.fireChannelRegistered()做了什么?

上图中的initChannel((C) ctx,chanel)完成了我们实际的 pipeline的注入 并且移除我们默认的defaultChannPipeline 这个是在我们创建channel的时候默认的,用了这么久现在可以remove,感叹设计的优秀呀。

到此完成了 selector  在reactor 模型中创建,channel 在 bind中创建 实例化,在上面看到了 Channel 和Selector的绑定,现在有看到了 pipeline的实例化。

9,pipeline的实例化 把所有的handel按照顺序放入其中。

10.构造处理chain 链表结构

到此完成实例化。

10.现在我要找到run 方法

11.此处的execute其实是父类的方法执行,

12.我们看到了startExecution

13.接下来看看 executor.execute

14 调用的是SingleThreadEventExecutor.this.run 其实是调用了NioEventLoop的run

15 看到了selectNow 和select 等 接着又是runallTasks

16.看看runallTasks

17.再把 pollTak() 方法看一下

相关文章

网友评论

      本文标题:netty源码debug 解析

      本文链接:https://www.haomeiwen.com/subject/xhlkextx.html