Netty源码分析(三)ChannelPipeline

ChannelPipeline可以认为是Netty所有操作的聚合。Netty默认给了唯一的一个实现DefaultChannelPipeline。
DefaultChannelPipeline包含了一组双向的ChannelHandler,这组ChannelHandler以责任链的模式提供一组逻辑操作。


Netty源码分析(三)ChannelPipeline_第1张图片
image.png

DefaultChannelPipeline在构造时生成head,tail2个handler用于整个队列首尾。

final AbstractChannelHandlerContext head;    
final AbstractChannelHandlerContext tail;

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;
}

//创建Handler对应的Context
private AbstractChannelHandlerContext newContext(EventExecutorGroup group, String name, ChannelHandler handler) {
        return new DefaultChannelHandlerContext(this, childExecutor(group), name, handler);
}
当添加handler时:
从头部插入
@Override
    public final ChannelPipeline addFirst(EventExecutorGroup group, String name, ChannelHandler handler) {
        final AbstractChannelHandlerContext newCtx;
        synchronized (this) {
            checkMultiplicity(handler);
            name = filterName(name, handler);

            newCtx = newContext(group, name, handler);

            addFirst0(newCtx);

            // If the registered is false it means that the channel was not registered on an eventloop yet.
            // In this case we add the context to the pipeline and add a task that will call
            // ChannelHandler.handlerAdded(...) once the channel is registered.
            if (!registered) {
                newCtx.setAddPending();
                callHandlerCallbackLater(newCtx, true);
                return this;
            }

            EventExecutor executor = newCtx.executor();
            if (!executor.inEventLoop()) {
                newCtx.setAddPending();
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        callHandlerAdded0(newCtx);
                    }
                });
                return this;
            }
        }
        callHandlerAdded0(newCtx);
        return this;
    }

private void addFirst0(AbstractChannelHandlerContext newCtx) {
        AbstractChannelHandlerContext nextCtx = head.next;
        newCtx.prev = head;
        newCtx.next = nextCtx;
        head.next = newCtx;
        nextCtx.prev = newCtx;
    }
Netty源码分析(三)ChannelPipeline_第2张图片
image.png
从尾部插入
@Override
    public final ChannelPipeline addLast(EventExecutorGroup group, String name, ChannelHandler handler) {
        final AbstractChannelHandlerContext newCtx;
        synchronized (this) {
            checkMultiplicity(handler);

            newCtx = newContext(group, filterName(name, handler), handler);

            addLast0(newCtx);

            // If the registered is false it means that the channel was not registered on an eventloop yet.
            // In this case we add the context to the pipeline and add a task that will call
            // ChannelHandler.handlerAdded(...) once the channel is registered.
            if (!registered) {
                newCtx.setAddPending();
                callHandlerCallbackLater(newCtx, true);
                return this;
            }

            EventExecutor executor = newCtx.executor();
            if (!executor.inEventLoop()) {
                newCtx.setAddPending();
                executor.execute(new Runnable() {
                    @Override
                    public void run() {
                        callHandlerAdded0(newCtx);
                    }
                });
                return this;
            }
        }
        callHandlerAdded0(newCtx);
        return this;
    }

private void addLast0(AbstractChannelHandlerContext newCtx) {
        AbstractChannelHandlerContext prev = tail.prev;
        newCtx.prev = prev;
        newCtx.next = tail;
        prev.next = newCtx;
        tail.prev = newCtx;
    }
Netty源码分析(三)ChannelPipeline_第3张图片
image.png

当发生事件时
ChannelOutboundHandler相关事件由tail开始

@Override
    public final ChannelFuture connect(SocketAddress remoteAddress) {
        return tail.connect(remoteAddress);
    }

@Override
    public final ChannelPipeline read() {
        tail.read();
        return this;
    }

@Override
    public final ChannelFuture write(Object msg) {
        return tail.write(msg);
    }

ChannelInboundHandler相关事件由head开始

@Override
    public final ChannelPipeline fireChannelRegistered() {
        AbstractChannelHandlerContext.invokeChannelRegistered(head);
        return this;
    }

    @Override
    public final ChannelPipeline fireChannelUnregistered() {
        AbstractChannelHandlerContext.invokeChannelUnregistered(head);
        return this;
    }

以fireChannelRegistered为例,我们看下具体流程

static void invokeChannelRegistered(final AbstractChannelHandlerContext next) {
        EventExecutor executor = next.executor();
        if (executor.inEventLoop()) {//当Context的EventLoop是当前线程时,直接执行
            next.invokeChannelRegistered();
        } else {//否则在当前EventLoop线程中执行
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    next.invokeChannelRegistered();
                }
            });
        }
    }

    private void invokeChannelRegistered() {
        if (invokeHandler()) {//如果handler状态为可被调用
            try {
                ((ChannelInboundHandler) handler()).channelRegistered(this);
            } catch (Throwable t) {
                notifyHandlerException(t);
            }
        } else {
            fireChannelRegistered();
        }
    }

//判断当前handler状态
private boolean invokeHandler() {
        // Store in local variable to reduce volatile reads.
        int handlerState = this.handlerState;
        return handlerState == ADD_COMPLETE || (!ordered && handlerState == ADD_PENDING);
    }

//当前Handler处理完后,必须调用该函数触发下一个Handler行为,否则将直接结束
@Override
    public ChannelHandlerContext fireChannelRegistered() {
        invokeChannelRegistered(findContextInbound());
        return this;
    }

//查找下一个可用的ChannelInboundHandler
private AbstractChannelHandlerContext findContextInbound() {
        AbstractChannelHandlerContext ctx = this;
        do {
            ctx = ctx.next;
        } while (!ctx.inbound);
        return ctx;
    }

我们看到当ChannelRegistered被触发后,从headContext开始,通过每个ChannelHandler调用fireChannelRegistered()向后一直到tailContext,如果中途某一个Handler没有调用fireChannelRegistered()则直接中断。
所有ChannelOutBoundHandler/ChannelInboundHandler中的接口都可以通过这个流程执行,那么第一次触发fireChannelRegistered()等函数的入口在哪呢,就是在Channel中另一个重要接口Unsafe,下一节我们来分析下Unsafe代码。

你可能感兴趣的:(Netty源码分析(三)ChannelPipeline)