美文网首页
ChannelPipeline、DefaultChannelPi

ChannelPipeline、DefaultChannelPi

作者: 追梦小蜗牛 | 来源:发表于2020-06-18 07:07 被阅读0次
    xxx.png

    介绍:

    类似于一个容器,包括了很多ChannelHandler:用于处理Channel的inbound events and outbound operations;它定义了ChannelHandler之间如何相互协助,提供给用户很大的灵活性。
    DefaultChannelPipeline构造方法:从下面的代码可以看出来,pipeline里面的数据结构是链表。

    protected DefaultChannelPipeline(Channel channel) {
            this.channel = ObjectUtil.checkNotNull(channel, "channel");
            succeededFuture = new SucceededChannelFuture(channel, null);
            voidPromise =  new VoidChannelPromise(channel, true);
    
            tail = new TailContext(this);
            head = new HeadContext(this);
    
            head.next = tail;
            tail.prev = head;
        }
    

    并且还是个双链表;这肯定是利用了双链表查询性能高、插入、删除更快、更灵活的特点,其实关于链表这个点上面,有一点特性还是有必要再详细的说一下的。
    链表:其实大家都知道,最容易和链表一起做对比的就是数组了,数组是连续的一整块内存空间,查询效率相当高;而给链表分配的内存空间不是连续的,是分散的,对空间的利用率也是相当高。

    一般说,数组的查询时间复杂度是O(1),插入和删除是O(n),这句话没问题;但是说链表的插入和删除时间复杂度是O(1),是需要分情况来说的,双链表是没问题的,但是单链表是要分插入或者删除的位置的,比如说有个Node A:

    • (单链表)在A节点的后面插入或者删除A后面的节点,时间复杂度是O(1)
    • (单链表)在A节点的前面插入或者删除A节点或者A前面的节点,时间复杂度是O(n),这是因为每个节点里面存放的指针只有他后面节点的;所以在这种情况下,会出现一个查询的时间消耗,最坏情况下也就是O(n)。
    链表.png

    一幅形象生动图:


    ChannelPipeline结构图.png

    事件流动:

     * <pre>
     *                                                 I/O Request
     *                                            via {@link Channel} or
     *                                        {@link ChannelHandlerContext}
     *                                                      |
     *  +---------------------------------------------------+---------------+
     *  |                           ChannelPipeline         |               |
     *  |                                                  \|/              |
     *  |    +---------------------+            +-----------+----------+    |
     *  |    | Inbound Handler  N  |            | Outbound Handler  1  |    |
     *  |    +----------+----------+            +-----------+----------+    |
     *  |              /|\                                  |               |
     *  |               |                                  \|/              |
     *  |    +----------+----------+            +-----------+----------+    |
     *  |    | Inbound Handler N-1 |            | Outbound Handler  2  |    |
     *  |    +----------+----------+            +-----------+----------+    |
     *  |              /|\                                  .               |
     *  |               .                                   .               |
     *  | ChannelHandlerContext.fireIN_EVT() ChannelHandlerContext.OUT_EVT()|
     *  |        [ method call]                       [method call]         |
     *  |               .                                   .               |
     *  |               .                                  \|/              |
     *  |    +----------+----------+            +-----------+----------+    |
     *  |    | Inbound Handler  2  |            | Outbound Handler M-1 |    |
     *  |    +----------+----------+            +-----------+----------+    |
     *  |              /|\                                  |               |
     *  |               |                                  \|/              |
     *  |    +----------+----------+            +-----------+----------+    |
     *  |    | Inbound Handler  1  |            | Outbound Handler  M  |    |
     *  |    +----------+----------+            +-----------+----------+    |
     *  |              /|\                                  |               |
     *  +---------------+-----------------------------------+---------------+
     *                  |                                  \|/
     *  +---------------+-----------------------------------+---------------+
     *  |               |                                   |               |
     *  |       [ Socket.read() ]                    [ Socket.write() ]     |
     *  |                                                                   |
     *  |  Netty Internal I/O Threads (Transport Implementation)            |
     *  +-------------------------------------------------------------------+
     * </pre>
    

    关键方法:

    • Inbound event propagation methods:
    *     <ul>
     *     <li>{@link ChannelHandlerContext#fireChannelRegistered()}</li>
     *     <li>{@link ChannelHandlerContext#fireChannelActive()}</li>
     *     <li>{@link ChannelHandlerContext#fireChannelRead(Object)}</li>
     *     <li>{@link ChannelHandlerContext#fireChannelReadComplete()}</li>
     *     <li>{@link ChannelHandlerContext#fireExceptionCaught(Throwable)}</li>
     *     <li>{@link ChannelHandlerContext#fireUserEventTriggered(Object)}</li>
     *     <li>{@link ChannelHandlerContext#fireChannelWritabilityChanged()}</li>
     *     <li>{@link ChannelHandlerContext#fireChannelInactive()}</li>
     *     <li>{@link ChannelHandlerContext#fireChannelUnregistered()}</li>
     *     </ul>
    
    • Outbound event propagation methods:
    *     <ul>
     *     <li>{@link ChannelHandlerContext#bind(SocketAddress, ChannelPromise)}</li>
     *     <li>{@link ChannelHandlerContext#connect(SocketAddress, SocketAddress, ChannelPromise)}</li>
     *     <li>{@link ChannelHandlerContext#write(Object, ChannelPromise)}</li>
     *     <li>{@link ChannelHandlerContext#flush()}</li>
     *     <li>{@link ChannelHandlerContext#read()}</li>
     *     <li>{@link ChannelHandlerContext#disconnect(ChannelPromise)}</li>
     *     <li>{@link ChannelHandlerContext#close(ChannelPromise)}</li>
     *     <li>{@link ChannelHandlerContext#deregister(ChannelPromise)}</li>
     *     </ul>
    

    总结:

    细心、用心...

    相关文章

      网友评论

          本文标题:ChannelPipeline、DefaultChannelPi

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