Netty源碼分析-Channel分類

先來看下Channel相關(guān)類圖:


Netty中channel類圖

為了便于理解棚亩,上面的類圖對層次關(guān)系做了一定的簡化陪蜻。
Channel接口定義了Netty中網(wǎng)絡(luò)IO最頂層的框架慎陵。AbstractChannel是Channel接口的骨架實現(xiàn),這個類中定義了channel的幾個重要成員脑溢,id(ChannelId)僵朗,unsafe(Unsafe),pipeline(DefaultChannelPipeline)屑彻,eventLoop(EventLoop)验庙。服務(wù)端channel(NioServerSocketChannel)和客戶端channel(NioSocketChannel)都會逐層的調(diào)用父類構(gòu)造函數(shù),從而創(chuàng)建創(chuàng)建或綁定上述幾個成員變量社牲。AbstractNioChannel主要作用是負責(zé)Nio相關(guān)的部分粪薛,使用selector的方式監(jiān)聽讀寫事件。AbstractNioChannel有成員變量SelectionKey搏恤,成員變量SelectableChannel(保存底層jdk的channel)违寿,成員變量readInterestOp(OP_READ或OP_ACCEPT事件)湃交。
AbstractNioChannel的構(gòu)造函數(shù):

    protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
        super(parent);
        this.ch = ch; // 保存底層jdk的channel
        this.readInterestOp = readInterestOp; // 保存感興趣的事件
        try {
            ch.configureBlocking(false); // 設(shè)置jdk的底層channel為非阻塞模式
        } catch (IOException e) {
            try {
                ch.close();
            } catch (IOException e2) {
                if (logger.isWarnEnabled()) {
                    logger.warn(
                            "Failed to close a partially initialized socket.", e2);
                }
            }

            throw new ChannelException("Failed to enter non-blocking mode.", e);
        }
    }

NioSocketChannel和NioServerSocketChannel注冊事件區(qū)別

接下來就是兩大陣營服務(wù)端channel(AbstractNioMessageChannel,NioServerSocketChannel)和客戶端channel(AbstractNioByteChannel藤巢,NioServerChannel)搞莺。它們都繼承了AbstractNioChannel,說明它們都是通過selector輪詢IO事件的掂咒,它們之間最大的區(qū)別是它們向selector注冊的IO事件不同才沧。

    public NioServerSocketChannel(ServerSocketChannel channel) {
        super(null, channel, SelectionKey.OP_ACCEPT); // 服務(wù)端channel注冊O(shè)P_ACCEPT事件
        config = new NioServerSocketChannelConfig(this, javaChannel().socket());
    }
    public NioSocketChannel(Channel parent, SocketChannel socket) {
        super(parent, socket); // 調(diào)用 AbstractNioByteChannel 的構(gòu)造函數(shù)
        config = new NioSocketChannelConfig(this, socket.socket());
    }
    protected AbstractNioByteChannel(Channel parent, SelectableChannel ch) {
        super(parent, ch, SelectionKey.OP_READ); // 客戶端channel注冊O(shè)P_READ事件,關(guān)心數(shù)據(jù)的讀寫
    }

NioSocketChannel和NioServerSocketChannel抽象讀事件的區(qū)別

服務(wù)端channel和客戶端channel的另一個區(qū)別是底層的Unsafe不同绍刮。Unsafe負責(zé)具體實現(xiàn)是客戶端channel還是客戶端channel的協(xié)議温圆。服務(wù)端channel對應(yīng)的是NioMessageUnsafe,客戶端channel對應(yīng)的是NioByteUnsafe(NioSocketChannelUnsafe繼承自它)录淡。
從源碼中來分析客戶端unsafe的創(chuàng)建:

    protected AbstractNioUnsafe newUnsafe() {
        return new NioSocketChannelUnsafe();  // 客戶端channel直接創(chuàng)建unsafe
    }

  // NioSocketChannelUnsafe 只有一個準(zhǔn)備關(guān)閉的方法捌木,大部分功能還是來自于 NioByteUnsafe
    private final class NioSocketChannelUnsafe extends NioByteUnsafe {
        @Override
        protected Executor prepareToClose() {
            try {
                if (javaChannel().isOpen() && config().getSoLinger() > 0) {
                    // We need to cancel this key of the channel so we may not end up in a eventloop spin
                    // because we try to read or write until the actual close happens which may be later due
                    // SO_LINGER handling.
                    // See https://github.com/netty/netty/issues/4449
                    doDeregister();
                    return GlobalEventExecutor.INSTANCE;
                }
            } catch (Throwable ignore) {
                // Ignore the error as the underlying channel may be closed in the meantime and so
                // getSoLinger() may produce an exception. In this case we just return null.
                // See https://github.com/netty/netty/issues/4449
            }
            return null;
        }
    }

服務(wù)端channel的unsafe油坝,在AbstractNioMessageChannel中可以看到:

    protected AbstractNioUnsafe newUnsafe() {
        return new NioMessageUnsafe(); // 服務(wù)端channel創(chuàng)建unsafe
    }

服務(wù)端channel和客戶端channel的第三個不同:讀取的內(nèi)容不同嫉戚。服務(wù)端channel的讀是讀取一條新的連接;客戶端channel的讀是讀取IO數(shù)據(jù)澈圈。
我們來看服務(wù)端channel讀事件相關(guān)的源碼彬檀,NioMessageUnsafe的read方法:

    private final class NioMessageUnsafe extends AbstractNioUnsafe {

        private final List<Object> readBuf = new ArrayList<Object>();

        @Override
        public void read() {
            assert eventLoop().inEventLoop();
            final ChannelConfig config = config();
            final ChannelPipeline pipeline = pipeline();
            final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
            allocHandle.reset(config);

            boolean closed = false;
            Throwable exception = null;
            try {
                try {
                    do {
                        // 最核心的功能doReadMessage,也就是讀取一條連接
                        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;
                    pipeline.fireChannelRead(readBuf.get(i));
                }
                readBuf.clear();
                allocHandle.readComplete();
                pipeline.fireChannelReadComplete();

                if (exception != null) {
                    closed = closeOnReadError(exception);

                    pipeline.fireExceptionCaught(exception);
                }

                if (closed) {
                    inputShutdown = true;
                    if (isOpen()) {
                        close(voidPromise());
                    }
                }
            } finally {
                // Check if there is a readPending which was not processed yet.
                // This could be for two reasons:
                // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
                // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
                //
                // See https://github.com/netty/netty/issues/2254
                if (!readPending && !config.isAutoRead()) {
                    removeReadOp();
                }
            }
        }
    }
    protected int doReadMessages(List<Object> buf) throws Exception {
        // 核心代碼
        SocketChannel ch = SocketUtils.accept(javaChannel());

        try {
            if (ch != null) {
                buf.add(new NioSocketChannel(this, ch));
                return 1;
            }
        } catch (Throwable t) {
            logger.warn("Failed to create a new channel from an accepted socket.", t);

            try {
                ch.close();
            } catch (Throwable t2) {
                logger.warn("Failed to close a socket.", t2);
            }
        }

        return 0;
    }

  public static SocketChannel accept(final ServerSocketChannel serverSocketChannel) throws IOException {
        try {
            return AccessController.doPrivileged(new PrivilegedExceptionAction<SocketChannel>() {
                @Override
                public SocketChannel run() throws IOException {
                    // 服務(wù)端channel瞬女,accept客戶端連接
                    return serverSocketChannel.accept();
                }
            });
        } catch (PrivilegedActionException e) {
            throw (IOException) e.getCause();
        }
    }

來看客戶端channel的讀事件窍帝,NioByteUnsafe的read方法:

        @Override
        public final void read() {
            final ChannelConfig config = config();
            if (shouldBreakReadReady(config)) {
                clearReadPending();
                return;
            }
            final ChannelPipeline pipeline = pipeline();
            final ByteBufAllocator allocator = config.getAllocator();
            final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
            allocHandle.reset(config);

            ByteBuf byteBuf = null;
            boolean close = false;
            try {
                do {
                    byteBuf = allocHandle.allocate(allocator);
                    // 從這里看出,客戶端channel是讀字節(jié)
                    allocHandle.lastBytesRead(doReadBytes(byteBuf));
                    if (allocHandle.lastBytesRead() <= 0) {
                        // nothing was read. release the buffer.
                        byteBuf.release();
                        byteBuf = null;
                        close = allocHandle.lastBytesRead() < 0;
                        if (close) {
                            // There is nothing left to read as we received an EOF.
                            readPending = false;
                        }
                        break;
                    }

                    allocHandle.incMessagesRead(1);
                    readPending = false;
                    pipeline.fireChannelRead(byteBuf);
                    byteBuf = null;
                } while (allocHandle.continueReading());

                allocHandle.readComplete();
                pipeline.fireChannelReadComplete();

                if (close) {
                    closeOnRead(pipeline);
                }
            } catch (Throwable t) {
                handleReadException(pipeline, byteBuf, t, close, allocHandle);
            } finally {
                // Check if there is a readPending which was not processed yet.
                // This could be for two reasons:
                // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
                // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
                //
                // See https://github.com/netty/netty/issues/2254
                if (!readPending && !config.isAutoRead()) {
                    removeReadOp();
                }
            }
        }
    }

NioSocketChannel和NioServerSocketChannel綁定channelConfig的區(qū)別

最后一點诽偷,服務(wù)端channel和客戶端channel綁定的channeConfig不同坤学。


netty channelConfig.PNG
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市报慕,隨后出現(xiàn)的幾起案子深浮,更是在濱河造成了極大的恐慌,老刑警劉巖眠冈,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件飞苇,死亡現(xiàn)場離奇詭異,居然都是意外死亡蜗顽,警方通過查閱死者的電腦和手機布卡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來雇盖,“玉大人忿等,你說我怎么就攤上這事〈尥冢” “怎么了这弧?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵娃闲,是天一觀的道長。 經(jīng)常有香客問我匾浪,道長皇帮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任蛋辈,我火速辦了婚禮属拾,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘冷溶。我一直安慰自己渐白,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布逞频。 她就那樣靜靜地躺著纯衍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪苗胀。 梳的紋絲不亂的頭發(fā)上襟诸,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天,我揣著相機與錄音基协,去河邊找鬼歌亲。 笑死,一個胖子當(dāng)著我的面吹牛澜驮,可吹牛的內(nèi)容都是我干的陷揪。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼杂穷,長吁一口氣:“原來是場噩夢啊……” “哼悍缠!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起耐量,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤飞蚓,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后拴鸵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體玷坠,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年劲藐,在試婚紗的時候發(fā)現(xiàn)自己被綠了八堡。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡聘芜,死狀恐怖兄渺,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情汰现,我是刑警寧澤挂谍,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布叔壤,位于F島的核電站,受9級特大地震影響口叙,放射性物質(zhì)發(fā)生泄漏炼绘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一妄田、第九天 我趴在偏房一處隱蔽的房頂上張望俺亮。 院中可真熱鬧,春花似錦疟呐、人聲如沸脚曾。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽本讥。三九已至,卻和暖如春鲁冯,著一層夾襖步出監(jiān)牢的瞬間拷沸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工晓褪, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留堵漱,地道東北人综慎。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓涣仿,卻偏偏與公主長得像,于是被迫代替她去往敵國和親示惊。 傳聞我的和親對象是個殘疾皇子好港,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容