Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

1 检测新连接

Netty源码阅读入门实战(五)-新连接检测
Netty源码阅读入门实战(五)-新连接检测
打断点

telnet 此机地址,引起连接建立


Netty源码阅读入门实战(五)-新连接检测
执行到此,进入查看

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测
以上即对应 read 方法

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

继续返回原先位置

2 NioSocketChannel的创建

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

禁止小包组成大包再发送方法


Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

看看这个静态变量
Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

由于 netty 常部署在服务端,所以默认启动 TCP无延时
Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

3 Channel的分类

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

先看看最顶层的框架 Channel


Netty源码阅读入门实战(五)-新连接检测

网络 Socket 读写绑定的一种抽象
Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测
作为骨架,保存了很多基础且重要的信息

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

使用选择器监听 IO 事件的一个抽象类
保存了 JDK 底层的一个 Channel


Netty源码阅读入门实战(五)-新连接检测
开启非阻塞模式

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测
回到了抽象类,即保存了一个ACCEPT 事件

看过服务 Channel 后,再看看客户端 Channel
Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

客户端与服务端的 Channel 在 unsafe 类上也不同
客户端 Chanel


Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

服务端 Chanel
Netty源码阅读入门实战(五)-新连接检测

用于某种 Chanel 的读写情况
Netty源码阅读入门实战(五)-新连接检测
主要负责读事件

Netty源码阅读入门实战(五)-新连接检测
读字节

不同端配置项也不同
Netty源码阅读入门实战(五)-新连接检测

4 新连接NioEventLoop的分配和selector注册

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

即为此处调用


Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测
回调之后,最终都会删除自身

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

回去看看 register 方法


Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

5 NioSocketChannel读事件的注册

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

可以开始接收数据了


Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

回顾一下创建
Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

6 新连接接入总结

Netty源码阅读入门实战(五)-新连接检测

Netty源码阅读入门实战(五)-新连接检测

boss线程的第一个过程轮训处Accept事件,然后boss线程第二个过程通过jdk底层的channel的accept方法创建该连接。


Netty源码阅读入门实战(五)-新连接检测

新连接NioEventLoop的分配和selector注册可以回答。boss线程调用chooser的next方法,拿到一个NioEventLoop,然后将这条连接注册到NioEventLoop的selector上面。

相关文章: