Channel Register

image.png

io.netty.channel.AbstractChannel.AbstractUnsafe#register

開始注冊(cè)

   if (eventLoop.inEventLoop()) {
                register0(promise);
            } else {
                try {
                    eventLoop.execute(new Runnable() {
                        @Override
                        public void run() {
                            register0(promise);
                        }
                    });
                } catch (Throwable t) {
                }
            }

進(jìn)入 io.netty.util.concurrent.SingleThreadEventExecutor#execute,SingleThreadEventExecutor重寫線程池的execute方法

  1. 添加register0到任務(wù)隊(duì)列
  2. 會(huì)調(diào)用線程start

inEventLoop()方法用來(lái)判斷當(dāng)前是否在EventLoop線程執(zhí)行,注冊(cè)的時(shí)候是在main線程執(zhí)行的,所以為false。

eventLoop.execute

    @Override
    public void execute(Runnable task) {
        if (task == null) {
            throw new NullPointerException("task");
        }

        boolean inEventLoop = inEventLoop();
        if (inEventLoop) {
            addTask(task);
        } else {
            startThread();
            addTask(task);
            if (isShutdown() && removeTask(task)) {
                reject();
            }
        }

        if (!addTaskWakesUp && wakesUpForTask(task)) {
            wakeup(inEventLoop);
        }
    }

register0

斷點(diǎn) io.netty.channel.AbstractChannel.AbstractUnsafe#register0
SingleThreadEventExecutor的Thread運(yùn)行的是NioEventLoop的run。

image.png
  doRegister();
pipeline.invokeHandlerAddedIfNeeded();
 pipeline.fireChannelRegistered();

doRegister 會(huì)綁定channel到selector

invokeHandlerAddedIfNeeded

斷點(diǎn) ServerBootstrap 181
斷點(diǎn) SimpleServerHandler#handlerAdded
斷點(diǎn) ServerBootstrap 193

 p.addLast(new ChannelInitializer<Channel>() {
            @Override
            public void initChannel(Channel ch) throws Exception {
                final ChannelPipeline pipeline = ch.pipeline();
                ChannelHandler handler = handler();
                if (handler != null) {
                    pipeline.addLast(handler);//斷點(diǎn)
                }

                ch.eventLoop().execute(new Runnable() {
                    @Override
                    public void run() {
                        pipeline.addLast(new ServerBootstrapAcceptor(
                                currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
                    }
                });
            }
        });

調(diào)用pipeline.addLast(handler);之前,pipeline中只有head ServerBootstrap$1#0 tail
調(diào)用之后,pipeline中有 head ServerBootstrap$1#0 SimpleServerHandler tail
函數(shù)返回后,進(jìn)入
io.netty.channel.ChannelInitializer#initChannel(io.netty.channel.ChannelHandlerContext)

if (initMap.putIfAbsent(ctx, Boolean.TRUE) == null) { // Guard against re-entrance.
            try {
                initChannel((C) ctx.channel());
            } catch (Throwable cause) {
                // Explicitly call exceptionCaught(...) as we removed the handler before calling initChannel(...).
                // We do so to prevent multiple calls to initChannel(...).
                exceptionCaught(ctx, cause);
            } finally {
                remove(ctx);
            }
            return true;
        }

然后會(huì)把ServerBootstrap$1#0刪除。

另外,對(duì)于pipeline添加ServerBootstrapAcceptor,會(huì)以任務(wù)方式提交到隊(duì)列。

image.png

fireChannelRegistered

SimpleServerHandler#channelRegistered

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容