首页 > 代码库 > ChannelHandler原理

ChannelHandler原理


1.创建ChannelPipeline的方法是实现接口 ChannelPipelineFactory 中的那个 getPipeline() 方法,然后根据自己的业务需要加入相应的 ChannelHandler。
 
2. 


接口 ChannelHandlerContext 提供的功能是统一管理这些ChannelHandler,使其能够和这个ChannelPipeline及其他Handler进行交互,一个handler可以往上流或者下流发送一个ChannelEvent,动态修改其所属的Channelpipeline。
充当链表节点的 DefaultChannelHandlerContext 里面的重要成员有: 链表指针next , prev, 以及是否能够处理downStream,upStream事件的布尔变量。

private final class DefaultChannelHandlerContext implements ChannelHandlerContext {
    volatile DefaultChannelHandlerContext next;
    volatile DefaultChannelHandlerContext prev;
    private final String name ;
    private final ChannelHandler handler;
    private final boolean canHandleUpstream ;
    private final boolean canHandleDownstream ;
    private volatile Object attachment ;

    DefaultChannelHandlerContext(
            DefaultChannelHandlerContext prev, DefaultChannelHandlerContext next,
            String name, ChannelHandler handler) {

        if (name == null) {
            throw new NullPointerException( "name");
        }
        if (handler == null) {
            throw new NullPointerException( "handler");
        }
        canHandleUpstream = handler instanceof ChannelUpstreamHandler;
        canHandleDownstream = handler instanceof ChannelDownstreamHandler;


        if (!canHandleUpstream && ! canHandleDownstream) {
            throw new IllegalArgumentException(
                    "handler must be either " +
                    ChannelUpstreamHandler. class.getName() + " or " +
                    ChannelDownstreamHandler. class.getName() + ‘.‘);
        }

        this .prev = prev;
        this .next = next;
        this .name = name;
        this .handler = handler;
    }

    public Channel getChannel() {
        return getPipeline().getChannel();
    }

    public ChannelPipeline getPipeline() {
        return DefaultChannelPipeline. this ;
    }

    public boolean canHandleDownstream() {
        return canHandleDownstream ;
    }

    public boolean canHandleUpstream() {
        return canHandleUpstream ;
    }

    public ChannelHandler getHandler() {
        return handler ;
    }

    public String getName() {
        return name ;
    }

    public Object getAttachment() {
        return attachment ;
    }

    public void setAttachment(Object attachment) {
        this .attachment = attachment;
    }

    public void sendDownstream(ChannelEvent e) {
        DefaultChannelHandlerContext prev = getActualDownstreamContext( this. prev);
        if (prev == null) {
            try {
                getSink().eventSunk( DefaultChannelPipeline. this, e);
            } catch (Throwable t) {
                notifyHandlerException(e, t);
            }
        } else {
            DefaultChannelPipeline. this.sendDownstream(prev, e);
        }
    }

    public void sendUpstream(ChannelEvent e) {
        DefaultChannelHandlerContext next = getActualUpstreamContext( this. next);
        if (next != null) {
            DefaultChannelPipeline. this.sendUpstream(next, e);
        }
    }
}