Netty核心技术及源码剖析-管道、处理器、上下文创建源码剖析

ChannelHandlerContext作用及设计

1、ChannelHandlerContext UML图


ChannelHandlerContext继承了出站方法调用接口和入站方法调用接口
1、ChannelOutBoundInvoker和ChannelInboundInvoker部分源码




2、ChannelHandlerContext部分源码


  • ChannelHandlerContext不仅仅是继承了他们两个的方法,同时也定义了一些自己的方法
  • 这些方法能够获取Context上下文环境中对应的比如channel,executor,handler,pipeline,内存分配器,关联的handler是否被删除。

2、ChannelPipeline|ChannelHandler|ChannelHandlerContext创建过程

分为3个步骤来看创建的过程

  • 任何一个ChannelSocket创建的同时都会创建一个pipeline。
  • 当用户或系统内部调用pipeline的add***方法添加handler时,都会创建一个包装这handler的Context。
  • 这些Context在pipeline中组成了双向链表。
    2.1Socket创建的时候创建pipeline
    在SocketChannel的抽象父类AbstractChannel的构造方法中
    protected AbstractChannel(Channel parent) {
        this.parent = parent;//断点测试
        id = newId();
        unsafe = newUnsafe();
        pipeline = newChannelPipeline();
    }

debug一下,可以看到代码会执行到这里然后继续追踪到

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

1、将channel复制给channel字段,用于pipeline操作channel。
2、创建一个future和promise,用于异步回调使用。
3、创建一个inbound的tailContext,创建一个即是inbound类型又是outbound类型的haadContext。
4、最后,将两个Context互相连接,形成双向链表。
5、tailContext和HeadContext非常的重要,所有pipeline中的事件都会流经他们。
2.2在addxxx添加处理器的时候创建Contextxxx
看下DefaultChannelPipeline的addLast方法如何创建的Context,代码如下

    @Override
    public final ChannelPipeline addLast(EventExecutorGroup executor, ChannelHandler... handlers) {
        if (handlers == null) {//断点
            throw new NullPointerException("handlers");
        }

        for (ChannelHandler h: handlers) {
            if (h == null) {
                break;
            }
            addLast(executor, null, h);
        }

        return this;
    }
    @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;
    }

说明:
1、pipeline添加handler,参数是线程池,name是null,handler是我们或者系统传入的handler。Netty为了防止多个线程导致安全问题,同步了这段代码,步骤如下:
2、检查这个handler实例是否是共享的,如果不是,并且已经被别的pipeline使用了,则抛出异常。
3、调用newContext(group,filterName(name,handler),handler)方法,创建一个Context。从这里可以看出来了,每次添加一个handler都会创建一个关联Context。
4、调用addLast0方法,将Context追加到链表中。
5、如果这个通道还没有注册到selector上,就将这个Context添加到这个pipeline的待办任务中,当注册好了以后,就会调用callHandlerAdded0方法(默认是什么都不做,用户可以实现这个方法)。
6、到这里,针对三对象创建过程,了解的差不多了,和最初说的一样,每当创建ChannelSocket的时候都会创建一个绑定的pipeline,一对一的关系,创建pipeline的时候也会创建tail节点和head节点,形成最初的链表。tail是入站inbound类型的handler,head既是inbound也是outbound类型的handler。在调用pipeline的addLast方法的时候,会根据给定的handler创建一个Context,然后,将这个Context插入到链表的尾端(tail前面),到此就OK了。

你可能感兴趣的:(Netty核心技术及源码剖析-管道、处理器、上下文创建源码剖析)