Netty分析之--pipeline

netty是如何判断ChannelHandler类型的

对于ChannelHandler的添加应该遵循什么样的顺序

用户手动触发事件传播,不同的触发方式有什么样的区别

1.pipeline的初始化
2.添加删除ChannelHandler
3.事件和异常的传播

1.pipeline的初始化

1.pipeline在创建Channel的时候被创建
2.pipeline节点数据结构:ChannelHandlerContext
3.Pipeline中的两大哨兵;head和tail
//1.pipiline在创建Channel的时候被创建
protected AbstractChannel(Channel parent){
    this.parent = parent;
    id = newId();
    unsafe = newUnsafe();
    pipeline = newChannelPipeline();
}
//2.pipeline节点数据结构:ChannelHandleContext
class DefaultChannelPipeline implements ChannelPipeline {
    final Channel channel; // pipeline所属的channel
    //head和tail都是handler上下文
    final DefaultChannelHandlerContext head;
    final DefaultChannelHandlerContext tail;
    ...
    public 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;
    }  
}

//ChannelHandlerContext数据结构
public interface ChannelHandlerContext extends AttributeMap, ChannelInboundInvoker, ChannelOutboundInvoker {
    Channel channel();

    EventExecutor executor();

    String name();

    ChannelHandler handler();

    boolean isRemoved();

    ChannelHandlerContext fireChannelRegistered();

    ChannelHandlerContext fireChannelUnregistered();

    ChannelHandlerContext fireChannelActive();

    ChannelHandlerContext fireChannelInactive();

    ChannelHandlerContext fireExceptionCaught(Throwable var1);

    ChannelHandlerContext fireUserEventTriggered(Object var1);

    ChannelHandlerContext fireChannelRead(Object var1);

    ChannelHandlerContext fireChannelReadComplete();

    ChannelHandlerContext fireChannelWritabilityChanged();

    ChannelHandlerContext read();

    ChannelHandlerContext flush();

    ChannelPipeline pipeline();

    ByteBufAllocator alloc();

    /** @deprecated */
    @Deprecated
     Attribute attr(AttributeKey var1);

    /** @deprecated */
    @Deprecated
     boolean hasAttr(AttributeKey var1);
}
//3.pipeline中的两个哨兵
HeadContext(DefaultChannelPipeline pipeline) {
    super(pipeline, null, HEAD_NAME, false, true);
    unsafe = pipeline.channel().unsafe();
}

TailContext(DefaultChannelPipeline pipeline) {
    super(pipeline, null, HEAD_NAME, true, false);
    unsafe = pipeline.channel().unsafe();
}

它调用了*父类 AbstractChannelHandlerContext 的构造器, 并传入参数 inbound = false, outbound = true.TailContext 的构造器与 HeadContext 的相反, 它调用了父AbstractChannelHandlerContext 的构造器, 并传入参数 inbound = true, outbound = false.即 header 是一个 outboundHandler, 而 tail 是一个inboundHandler, 关于这一点, 大家要特别注意, 因为在后面的分析中, 我们会反复用inbound 和 outbound 这两个属性.

2.添加删除ChannelHandler

//添加
1.判断是否重复添加
2.创建节点并添加至链表
3.回调添加完成事件

//删除
1.找到节点
2.链表的删除
3.回调删除Handler事件

3.事件和异常的传播

3.1inBound事件的传播

1.何为inBound事件以及ChannelInBoundHandler
2.ChannelRead事件的传播
3.SimpleInBoundHandler处理器

3.2outBound事件的传播

1.outBound事件和ChannelOutBoundHandler
2.write事件的传播

你可能感兴趣的:(Netty分析之--pipeline)