netty源码分析 之四 transport(ChannelPipeline)

前面看了channel定义,以及接口定义,可知道实现类。

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


在看下接口方法


ChannelPipeline 主要是 inbound和outbound方法


inbound方法一般是内部定义的模板方法,继承实现之后就期待调用即可

outbound方法一般是对方提供api接口,比如bind connect read write等



DefaultChannelPipeline

然后我们看下channelPipeline的实现类,如何构造pipe的

    final AbstractChannel channel;

    final AbstractChannelHandlerContext head;
    final AbstractChannelHandlerContext tail;

    private final Map<String, AbstractChannelHandlerContext> name2ctx =
            new HashMap<String, AbstractChannelHandlerContext>(4);

    /**
     * @see #findInvoker(EventExecutorGroup)
     */
    private Map<EventExecutorGroup, ChannelHandlerInvoker> childInvokers;

    DefaultChannelPipeline(AbstractChannel channel) {
        if (channel == null) {
            throw new NullPointerException("channel");
        }
        this.channel = channel;

        tail = new TailContext(this);
        head = new HeadContext(this);

        head.next = tail;
        tail.prev = head;
    }

关键的两个类tailContext和headcontext,这样的话就扯出我们下一个接口   ChannelHandler

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值