zoukankan      html  css  js  c++  java
  • Netty源码分析第3章(客户端接入流程)---->第5节: 监听读事件

     

    Netty源码分析第三章: 客户端接入流程

     

    第五节: 监听读事件

     

    我们回到AbstractUnsafe的register0()方法:

    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) {
            //省略代码
        }
    }

    doRegister()做完实际的注册之后, 会走到if (isActive())这个判断, 因为这个时候链路已经完成, 所以这里是true, 默认判断条件if (firstRegistration)也为true, 所以这里会走到pipeline.fireChannelActive()这一步

    有关pipeline我们会在下一章进行详细分析, 这里我们只需要知道, 最后会流转到AbstractUnsafe的beginRead()方法

    跟到beginRead()方法:

    public final void beginRead() {
        assertEventLoop();
        if (!isActive()) {
            return;
        }
        try {
            doBeginRead();
        } catch (final Exception e) {
            //代码省略
        }
    }

    这块代码同样我们也不陌生, 因为我们分析NioServerSocketChannel也分析过了这一步

    我们继续跟到doBeginRead():

    protected void doBeginRead() throws Exception {
        //拿到selectionKey
        final SelectionKey selectionKey = this.selectionKey;
        if (!selectionKey.isValid()) {
            return;
        }
        readPending = true;
        //获得感兴趣的事件
        final int interestOps = selectionKey.interestOps();
        //判断是不是对任何事件都不监听
        if ((interestOps & readInterestOp) == 0) {
            //此条件成立
            //将之前的accept事件注册, readInterest代表可以读取一个新连接的意思
            selectionKey.interestOps(interestOps | readInterestOp);
        }
    }

    这段代码相信大家会比较熟悉, 因为我们服务端channel注册完之后也走到了这里

    因为我们在创建NioSocketChannel的时候初始化的是read事件, selectionKey是channel在注册时候返回的key, 所以selectionKey.interestOps(interestOps | readInterestOp)这一步, 会将当前channel的读事件注册到selector中去

    注册完成之后, NioEventLoop就可以轮询当前channel的读事件了

    以上就是NioSocketChannel注册监听事件的流程

     

     

    第三章总结

        本章学习了有关客户端接入, NioSocketChannel的创建, 注册等相关操作, 并且涉及到了上一小节剖析的eventLoop的相关逻辑, 同学们可以将相关的流程通过debug的方式走一遍以加深印象

     

    上一节: NioSocketChannel注册到selector

    下一节: pipeline的创建

     

  • 相关阅读:
    一个别人的心得(转发的)
    常见的游戏设计技术
    查看更新
    xml,json和各种序列化工具的对比
    python游戏环境搭建
    快速制作游戏
    子网和掩码
    nat
    pycharm使用技巧
    IP的面向无连接状态
  • 原文地址:https://www.cnblogs.com/xiangnan6122/p/10204030.html
Copyright © 2011-2022 走看看