netty NioSocketChannel解析

论坛 期权论坛     
选择匿名的用户   2021-5-30 02:30   180   0
netty 抽象BootStrap定义:[url]http://donald-draper.iteye.com/blog/2392492[/url]
<br>netty ServerBootStrap解析:[url]http://donald-draper.iteye.com/blog/2392572[/url]
<br>netty Bootstrap解析:[url]http://donald-draper.iteye.com/blog/2392593[/url]
<br>netty 通道接口定义:[url]http://donald-draper.iteye.com/blog/2392740[/url]
<br>netty 抽象通道初始化:[url]http://donald-draper.iteye.com/blog/2392801[/url]
<br>netty 抽象Unsafe定义:[url]http://donald-draper.iteye.com/blog/2393053[/url]
<br>netty 通道Outbound缓冲区:[url]http://donald-draper.iteye.com/blog/2393098[/url]
<br>netty 抽象通道后续:[url]http://donald-draper.iteye.com/blog/2393166[/url]
<br>netty 抽象nio通道:[url]http://donald-draper.iteye.com/blog/2393269[/url]
<br>netty 抽象nio字节通道:[url]http://donald-draper.iteye.com/blog/2393323[/url]
<br>netty 抽象nio消息通道:[url]http://donald-draper.iteye.com/blog/2393364[/url]
<br>netty NioServerSocketChannel解析:[url]http://donald-draper.iteye.com/blog/2393443[/url]
<br>netty 通道配置接口定义:[url]http://donald-draper.iteye.com/blog/2393484[/url]
<br>netty 默认通道配置初始化:[url]http://donald-draper.iteye.com/blog/2393504[/url]
<br>netty 默认通道配置后续:[url]http://donald-draper.iteye.com/blog/2393510[/url]
<br>netty 字节buf定义:[url]http://donald-draper.iteye.com/blog/2393813[/url]
<br>netty 资源泄漏探测器:[url]http://donald-draper.iteye.com/blog/2393940[/url]
<br>netty 抽象字节buf解析:[url]http://donald-draper.iteye.com/blog/2394078[/url]
<br>netty 抽象字节buf引用计数器:[url]http://donald-draper.iteye.com/blog/2394109[/url]
<br>netty 复合buf概念:[url]http://donald-draper.iteye.com/blog/2394408[/url]
<br>netty 抽象字节buf分配器:[url]http://donald-draper.iteye.com/blog/2394419[/url]
<br>netty Unpooled字节buf分配器:[url]http://donald-draper.iteye.com/blog/2394619[/url]
<br>netty Pooled字节buf分配器:[url]http://donald-draper.iteye.com/blog/2394814[/url]
<br>[size&#61;medium][b]引言:[/b][/size]
<br>上一篇文章我们看了Pooled字节buf分配器,先来回顾一下:
<br> Pooled字节buf分配器,内部有一个堆buf和direct buf分配Region区(PoolArena),每个Region的内存块(PoolChunk)size为chunkSize,每个内存块内存页(PoolSubpage)大小,默认为8k。Pooled 堆buf是基于字节数组,而direct buf是基于nio 字节buf。Pooled字节分配器分配heap和direct buf时,首先获取线程本地buf缓存PoolThreadCache,从buf获取对应的heap或direct分配区,分配区创建buf(PooledByteBuf),然后将buf放到内存块中管理,根据buf的容量,将放到相应tiny,small,normal Memory Region Cache(MemoryRegionCache)中。每个Pooled buf通过内存的Recycler,重用buf。Pool字节buf内部有一个回收器Recycler,管理字节buf,而回收器内部是将对象放在一个线程本地栈中管理。
<br>从看了ServerSocket通道之后,我们把字节buf(heap,direct),及字节buf分配器(Unpooled,Pooled),
<br>今天我们回到Socket通道,由于socket通道我们讲了好久,先来把Nio socket通道的父类抽象字节通道回顾一下:
<br> 写通道Outbound缓冲区,即遍历刷新链上的写请求,如果写请求消息为字节buf,则调用doWriteBytes完成实际数据发送操作,待子类扩展,如果写请求消息为文件Region,调用doWriteFileRegion完成实际数据发送操作,待子类扩展,数据发送,则更新通道的数据发送进度,并从刷新链上移除写请求;如果所有写请求发送完毕,则重新添加写操作事件到选择key兴趣事件集,否则继续刷新通道Outbound缓冲区中的写请求。
<br> nio字节Unsafe读操作,从通道接收缓冲区读取数据,通知通道处理读取数据,触发Channel管道线的fireChannelRead事件,待数据读取完毕,触发Channel管道线的fireChannelReadComplete事件,如果在读数据的过程中,通道关闭,则触发通道输入关闭事件(fireUserEventTriggered),如果在读数据的过程中,发生异常,则读取缓存区中没有读完的数据,并通道通道处理剩余数据。
<br>
<br>现在来看socket通道:
<br>
<pre class="blockcode"><code class="language-java">/**<br> * {&#64;link io.netty.channel.socket.SocketChannel} which uses NIO selector based implementation.<br> */<br>public class NioSocketChannel extends AbstractNioByteChannel implements io.netty.channel.socket.SocketChannel {<!-- --><br>    private static final InternalLogger logger &#61; InternalLoggerFactory.getInstance(NioSocketChannel.class);<br>    //选择器提供者<br>    private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER &#61; SelectorProvider.provider();<br>    private final SocketChannelConfig config;//socket通道配置<br>    /**<br>     * Create a new instance<br>     */<br>    public NioSocketChannel() {<!-- --><br>        th
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP