Netty源碼分析第3章(客戶端接入流程)---->第4節: NioSocketChannel註冊到selector

 

Netty源碼分析第三章: 客戶端接入流程html

 

第四節: NioSocketChannel註冊到selectorjava

 

咱們回到最初的NioMessageUnsafe的read()方法:promise

public void read() { //必須是NioEventLoop方法調用的, 不能經過外部線程調用
    assert eventLoop().inEventLoop(); //服務端channel的config
    final ChannelConfig config = config(); //服務端channel的pipeline
    final ChannelPipeline pipeline = pipeline(); //處理服務端接入的速率
    final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle(); //設置配置
 allocHandle.reset(config); boolean closed = false; Throwable exception = null; try { try { do { //建立jdk底層的channel //readBuf用於臨時承載讀到連接
                int localRead = doReadMessages(readBuf); if (localRead == 0) { break; } if (localRead < 0) { closed = true; break; } //分配器將讀到的連接進行計數
 allocHandle.incMessagesRead(localRead); //鏈接數是否超過最大值
            } while (allocHandle.continueReading()); } catch (Throwable t) { exception = t; } int size = readBuf.size(); //遍歷每一條客戶端鏈接
        for (int i = 0; i < size; i ++) { readPending = false; //傳遞事件, 將建立NioSokectChannel進行傳遞 //最終會調用ServerBootstrap的內部類ServerBootstrapAcceptor的channelRead()方法
 pipeline.fireChannelRead(readBuf.get(i)); } readBuf.clear(); allocHandle.readComplete(); pipeline.fireChannelReadComplete(); //代碼省略
    } finally { //代碼省略
 } }

在while循環結束以後, 將會經過一個for循環遍歷readBuf集合, 並將建立的NioSocketChannel傳入fireChannelRead()中, 傳播channel的讀取事件ide

有關pipeline的知識, 咱們下一章會詳細剖析, 並會根據剖析後的內容回顧以前的有關pipeline的操做, 這裏咱們只需知道, 經過fireChannelRead()咱們最終調用了ServerBootstrap的內部類ServerBootstrapAcceptor 中的channelRead()方法oop

跟到channelRead()方法中:源碼分析

public void channelRead(ChannelHandlerContext ctx, Object msg) { final Channel child = (Channel) msg; //代碼省略
    try { //work線程註冊channel
        childGroup.register(child).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { forceClose(child, future.cause()); } } }); } catch (Throwable t) { forceClose(child, t); } }

其中參數的msg就是最初傳入fireChannelRead()方法的NioSocketChannel學習

因此這裏能夠經過 final Channel child = (Channel) msg 這種方式拿到NioSocketChannelthis

其中childGroup是咱們最初初始化的work線程, 這裏的register()方法跟boss線程同樣, 經過next()方法獲選擇一個線程進行註冊, 這裏再也不贅述spa

咱們緊跟調用鏈, 跟到SingleThreadEventLoop的register()方法:線程

public ChannelFuture register(final ChannelPromise promise) { ObjectUtil.checkNotNull(promise, "promise"); promise.channel().unsafe().register(this, promise); return promise; }

這裏的unsafe(), 根據咱們以前的剖析, 是NioByteUnsafe, 這裏的register最終會調用AbstractUnsafe的register()方法, 並NioSocketChannel

不知道同窗們是否記得, 當初NioServerSocketChannel註冊的時候也走的這個方法

咱們跟到register()這個方法中:

public final void register(EventLoop eventLoop, final ChannelPromise promise) { //省略驗證代碼 //全部的複製操做, 都交給eventLoop處理
    AbstractChannel.this.eventLoop = eventLoop; if (eventLoop.inEventLoop()) { //作實際主註冊
 register0(promise); } else { try { eventLoop.execute(new Runnable() { @Override public void run() { register0(promise); } }); } catch (Throwable t) { //代碼省略
 } } }

咱們學習過NioEventLoop相關知識以後, 應該對這部分代碼不太陌生, 首先判斷是否是當前NioEventLoop線程, 若是是, 則直接進行註冊操做, 若是不是, 則封裝成task在當前NioEventLoop中執行

走到這裏不難明白, 這裏並非當前NioEventLoop線程, 這是boss線程執行的, 因此這裏會走到else, 若是是第一次的鏈接操做, work線程的NioEventLoop並無啓動, 因此這裏也會啓動NioEventLoop, 並開始輪詢操做

 

跟到register0(promise)中看其是如何作實際操做的:

private void register0(ChannelPromise promise) { try { //省略代碼 //作實際的註冊
 doRegister(); neverRegistered = false; registered = true; //觸發事件
 pipeline.invokeHandlerAddedIfNeeded(); safeSetSuccess(promise); //觸發註冊成功事件
 pipeline.fireChannelRegistered(); if (isActive()) { if (firstRegistration) { //傳播active事件(4)
 pipeline.fireChannelActive(); } else if (config().isAutoRead()) { beginRead(); } } } catch (Throwable t) { //省略代碼
 } }

這段代碼咱們一樣並不陌生, 由於NioServerSokectChannel中也走這一部分, 咱們繼續關注doRegister()方法:

protected void doRegister() throws Exception { boolean selected = false; for (;;) { try { //jdk底層的註冊方法 //第一個參數爲selector, 第二個參數表示不關心任何事件
            selectionKey = javaChannel().register(eventLoop().selector, 0, this); return; } catch (CancelledKeyException e) { //省略代碼
 } } }

這部分也是咱們以前剖析過的jdk底層的註冊, 只是不一樣的是, 這裏的javaChannel()是SocketChanel而不是ServerSocketChannel

一樣, 這裏也是表示不關心任何事件, 只是在當前NioEventLoop綁定的selector上註冊

至此, NioSocketChannel完成註冊

 

上一節: NioSocketChannel的建立

下一節: 監聽讀事件

相關文章
相關標籤/搜索