Netty(六):NioServerSocketChannel源码解析

论坛 期权论坛     
选择匿名的用户   2021-5-30 02:30   232   0
<div class="blogpost-body" id="cnblogs_post_body">
<p>我们在<a href="https://www.cnblogs.com/insaneXs/p/9900006.html">Netty学习系列五</a>的最后提出了一些问题还没得到回答,今天来通过学习NioServerSocketChannel的源码来帮我们找到之前问题的答案。</p>
<p>先看一下NioServerSocketChannel的继承结构。</p>
<p> <img alt="" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-54cfc094ec28c2c1cf3f0d2d51378530.png"></p>
<p>AttributeMap接口及DefaultAttributeMap主要是提供了体检属性和获取属性的能力,便于我们为Channel绑定额外的属性。</p>
<p> </p>
<p>AbstractChannel实现了Channel接口,实现了Channel通用的行为和方法,我们在<a href="https://www.cnblogs.com/insaneXs/p/9854440.html">Netty学习系列四</a>中已经介绍过了。</p>
<p> </p>
<p>AbstractNioChannel抽象类关联了Channel接口与JDK的NIOChannel,也就是让底层的通信交给Nio来实现。</p>
<p>简单介绍下源码:</p>
<div class="cnblogs_code">
  <img alt="" class="code_img_closed" id="code_img_closed_005dca42-1eed-47d2-8040-d0fb0ed90615" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-8f900a89c6347c561fdf2122f13be562.gif">
  <img alt="" class="code_img_opened" id="code_img_opened_005dca42-1eed-47d2-8040-d0fb0ed90615" src="https://beijingoptbbs.oss-cn-beijing.aliyuncs.com/cs/5606289-961ddebeb323a10fe0623af514929fc1.gif">
  <div class="cnblogs_code_hide" id="cnblogs_code_open_005dca42-1eed-47d2-8040-d0fb0ed90615">
   <pre class="blockcode"><span style="color:#008080;">  1</span> <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">abstract</span> <span style="color:#0000ff;">class</span> AbstractNioChannel <span style="color:#0000ff;">extends</span><span style="color:#000000;"> AbstractChannel {
</span><span style="color:#008080;">  2</span>
<span style="color:#008080;">  3</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">final</span> InternalLogger logger &#61;
<span style="color:#008080;">  4</span>             InternalLoggerFactory.getInstance(AbstractNioChannel.<span style="color:#0000ff;">class</span><span style="color:#000000;">);
</span><span style="color:#008080;">  5</span>
<span style="color:#008080;">  6</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">final</span> ClosedChannelException DO_CLOSE_CLOSED_CHANNEL_EXCEPTION &#61;<span style="color:#000000;"> ThrowableUtil.unknownStackTrace(
</span><span style="color:#008080;">  7</span>             <span style="color:#0000ff;">new</span> ClosedChannelException(), AbstractNioChannel.<span style="color:#0000ff;">class</span>, &#34;doClose()&#34;<span style="color:#000000;">);
</span><span style="color:#008080;">  8</span>
<span style="color:#008080;">  9</span>     <span style="color:#008000;">//</span><span style="color:#008000;">和Java NIO的Channel绑定</span>
<span style="color:#008080;"> 10</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">final</span><span style="color:#000000;"> SelectableChannel ch;
</span><span style="color:#008080;"> 11</span>     <span style="color:#008000;">//</span><span style="color:#008000;">为SelectableChannel注册的时间</span>
<span style="color:#008080;"> 12</span>     <span style="color:#0000ff;">protected</span> <span style="color:#0000ff;">final</span> <span style="color:#0000ff;">int</span><span style="color:#000000;"> readInterestOp;
</span><span style="color:#008080;"> 13</span>     <span style="color:#0000ff;">volatile</span><span style="color:#000000;"> SelectionKey selectionKey;
</span><span style="color:#008080;"> 14</span>
<span style="color:#008080;"> 15</span>     <span style="color:#0000ff;">boolean</span><span style="color:#000000;"> readPending;
</span><span style="color:#008080;"> 16</span>     
<span style="color:#008080;"> 17</span>     <span style="color:#0000ff;">private</span> <span style="color:#0000ff;">final</span> Runnable clearReadPendingRunnable &#61; <span style="color:#0000ff;">new</span><span style="color:#000000;"> Runnable() {
</span><span style="color:#008080;"> 18</span> <span style="color:#000000;">        &#64;Override
</span><span style="color:#008080;"> 19</span>         <span style="color:#0000ff;">public</span> <span style="color:#0000ff;">void</span><span style="color:#000000;"> run() {
</span><span style="color:#008080;"> 20</span> <span style="color:#000000;">            clearReadPending0();
</span><span style="color:#008080;"> 21</span> <span style="color:#000000;">        }
</span><span style="color:#008080;"> 22</span> <span style="color:#000000;">    };
</span><span style="color:#008080;"> 23</span>
<span style="color:#008080;"> 24</span>     <span style="color:#008000;">/**</span>
<span style="color:#008080;"> 25</span> <span style="color:#008000;">     * The future of the current connection attempt.  If not null, subsequent
</span><span style="color:#008080;"> 26</span> <span style="color:#008000;">     * connection attempts will fail.
</span><span style="color:#008080;"> 27</span>      <span style="color:#008000;">*/</span>
<span style="color:#008080;"> 28</span>     <span style="color:#0000ff;">private</span><span style="color:#000000;"> ChannelPromise connectPromise;
</span><span sty
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

下载期权论坛手机APP