手撕netty源码(四)- ServerBootstrap是如何监听事件的

文章目录

  • 前言
  • 一、OP_ACCEPT事件注册
    • 1.1 bind 完成之后监听OP_ACCEPT
    • 1.2 register0注册完成之后监听OP_ACCEPT
  • 二、事件处理
    • 在这里插入图片描述
  • 三、总结


前言

文档中的图片如果不清晰可以直接在线看processOn
processOn文档跳转
接上一篇:手撕netty源码(三)- ServerBootstrap绑定端口

本篇从源码讲解 ServerBootstrap 是如何监听各种事件的。


一、OP_ACCEPT事件注册

注意:这里ops传的是0,代表现在还没有准备开始监听事件,那是从什么时候开始监听感兴趣的事件呢?

//io/netty/channel/nio/AbstractNioChannel.java
protected void doRegister() throws Exception {
    boolean selected = false;
    for (;;) {
        try {
            selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
            return;
        } catch (CancelledKeyException e) {
            if (!selected) {
                // Force the Selector to select now as the "canceled" SelectionKey may still be
                // cached and not removed because no Select.select(..) operation was called yet.
                eventLoop().selectNow();
                selected = true;
            } else {
                // We forced a select operation on the selector before but the SelectionKey is still cached
                // for whatever reason. JDK bug ?
                throw e;
            }
        }
    }
}

最初我们遇到accept是在创建NioServerSocketChannel实例的时候,只是将SelectionKey.OP_ACCEPT赋值给属性‘this.readInterestOp’

// io/netty/channel/socket/nio/NioServerSocketChannel.java
public NioServerSocketChannel(ServerSocketChannel channel) {
    super(null, channel, SelectionKey.OP_ACCEPT);
    config = new NioServerSocketChannelConfig(this, javaChannel().socket());
}

// io/netty/channel/nio/AbstractNioChannel.java
protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
    super(parent);
    this.ch = ch;
    this.readInterestOp = readInterestOp;
    try {
        ch.configureBlocking(false);
    } catch (IOException e) {
        try {
            ch.close();
        } catch (IOException e2) {
            logger.warn(
                        "Failed to close a partially initialized socket.", e2);
        }

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

1.1 bind 完成之后监听OP_ACCEPT

用debug方式发现,最终开始监听OP_ACCEPT是在io/netty/channel/nio/AbstractNioChannel.java#doBeginRead(),从线程栈信息可以看出来,是在doBind完成之后,异步执行pipeline.fireChannelActive();来监听OP_ACCEPT事件的
在这里插入图片描述
在这里插入图片描述

// io/netty/channel/AbstractChannel.java#AbstractUnsafe
public final void bind(final SocketAddress localAddress, final ChannelPromise promise) {
    assertEventLoop();

    if (!promise.setUncancellable() || !ensureOpen(promise)) {
        return;
    }

    // See: https://github.com/netty/netty/issues/576
    if (Boolean.TRUE.equals(config().getOption(ChannelOption.SO_BROADCAST)) &&
        localAddress instanceof InetSocketAddress &&
        !((InetSocketAddress) localAddress).getAddress().isAnyLocalAddress() &&
        !PlatformDependent.isWindows() && !PlatformDependent.maybeSuperUser()) {
        // Warn a user about the fact that a non-root user can't receive a
        // broadcast packet on *nix if the socket is bound on non-wildcard address.
        logger.warn(
                "A non-root user can't receive a broadcast packet if the socket " +
                "is not bound to a wildcard address; binding to a non-wildcard " +
                "address (" + localAddress + ") anyway as requested.");
    }

    boolean wasActive = isActive();
    try {
        doBind(localAddress);
    } catch (Throwable t) {
        safeSetFailure(promise, t);
        closeIfClosed();
        return;
    }

    if (!wasActive && isActive()) {
        invokeLater(new Runnable() {
            @Override
            public void run() {
                pipeline.fireChannelActive();
            }
        });
    }

    safeSetSuccess(promise);
}

1.2 register0注册完成之后监听OP_ACCEPT

在register0()方法中,注册完成之后,也有pipeline.fireChannelActive();的逻辑,同样也可以调用到io/netty/channel/nio/AbstractNioChannel.java#doBeginRead()
这是因为register0是异步执行的,可能pipeline.fireChannelRegistered();执行之后,bind方法已经执行完成,那么这里就要fireChannelActive,这也是为什么doBind执行之前要先获取wasActive 的原因

// io/netty/channel/AbstractChannel.java
private void register0(ChannelPromise promise) {
    try {
        // check if the channel is still open as it could be closed in the mean time when the register
        // call was outside of the eventLoop
        if (!promise.setUncancellable() || !ensureOpen(promise)) {
            return;
        }
        boolean firstRegistration = neverRegistered;
        doRegister();
        neverRegistered = false;
        registered = true;

        // Ensure we call handlerAdded(...) before we actually notify the promise. This is needed as the
        // user may already fire events through the pipeline in the ChannelFutureListener.
        pipeline.invokeHandlerAddedIfNeeded();

        safeSetSuccess(promise);
        pipeline.fireChannelRegistered();
        // Only fire a channelActive if the channel has never been registered. This prevents firing
        // multiple channel actives if the channel is deregistered and re-registered.
        if (isActive()) {
            if (firstRegistration) {
                pipeline.fireChannelActive();
            } else if (config().isAutoRead()) {
                // This channel was registered before and autoRead() is set. This means we need to begin read
                // again so that we process inbound data.
                //
                // See https://github.com/netty/netty/issues/4805
                beginRead();
            }
        }
    } catch (Throwable t) {
        // Close the channel directly to avoid FD leak.
        closeForcibly();
        closeFuture.setClosed();
        safeSetFailure(promise, t);
    }
}

二、事件处理

源码看到现在,已经完成了OP_ACCEPT事件的监听和端口的绑定,基本上bossGroup已经完成,接下来看看各种读、写、连接事件是如何处理的。

当bossGroup中EventLoop开始执行任务时,会死循环执行processSelectedKeys();和runAllTasks();
processSelectedKeys()负责处理各种读写连接事件;
runAllTasks()负责执行提交给EventLoop的异步任务;

protected void run() {
    int selectCnt = 0;
    for (;;) {
        try {
            int strategy;
            try {
                strategy = selectStrategy.calculateStrategy(selectNowSupplier, hasTasks());
                switch (strategy) {
                case SelectStrategy.CONTINUE:
                    continue;

                case SelectStrategy.BUSY_WAIT:
                    // fall-through to SELECT since the busy-wait is not supported with NIO

                case SelectStrategy.SELECT:
                    long curDeadlineNanos = nextScheduledTaskDeadlineNanos();
                    if (curDeadlineNanos == -1L) {
                        curDeadlineNanos = NONE; // nothing on the calendar
                    }
                    nextWakeupNanos.set(curDeadlineNanos);
                    try {
                        if (!hasTasks()) {
                            strategy = select(curDeadlineNanos);
                        }
                    } finally {
                        // This update is just to help block unnecessary selector wakeups
                        // so use of lazySet is ok (no race condition)
                        nextWakeupNanos.lazySet(AWAKE);
                    }
                    // fall through
                default:
                }
            } catch (IOException e) {
                // If we receive an IOException here its because the Selector is messed up. Let's rebuild
                // the selector and retry. https://github.com/netty/netty/issues/8566
                rebuildSelector0();
                selectCnt = 0;
                handleLoopException(e);
                continue;
            }

            selectCnt++;
            cancelledKeys = 0;
            needsToSelectAgain = false;
            final int ioRatio = this.ioRatio;
            boolean ranTasks;
            if (ioRatio == 100) {
                try {
                    if (strategy > 0) {
                        processSelectedKeys();
                    }
                } finally {
                    // Ensure we always run tasks.
                    ranTasks = runAllTasks();
                }
            } else if (strategy > 0) {
                final long ioStartTime = System.nanoTime();
                try {
                    processSelectedKeys();
                } finally {
                    // Ensure we always run tasks.
                    final long ioTime = System.nanoTime() - ioStartTime;
                    ranTasks = runAllTasks(ioTime * (100 - ioRatio) / ioRatio);
                }
            } else {
                ranTasks = runAllTasks(0); // This will run the minimum number of tasks
            }

            if (ranTasks || strategy > 0) {
                if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS && logger.isDebugEnabled()) {
                    logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.",
                            selectCnt - 1, selector);
                }
                selectCnt = 0;
            } else if (unexpectedSelectorWakeup(selectCnt)) { // Unexpected wakeup (unusual case)
                selectCnt = 0;
            }
        } catch (CancelledKeyException e) {
            // Harmless exception - log anyway
            if (logger.isDebugEnabled()) {
                logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?",
                        selector, e);
            }
        } catch (Error e) {
            throw e;
        } catch (Throwable t) {
            handleLoopException(t);
        } finally {
            // Always handle shutdown even if the loop processing threw an exception.
            try {
                if (isShuttingDown()) {
                    closeAll();
                    if (confirmShutdown()) {
                        return;
                    }
                }
            } catch (Error e) {
                throw e;
            } catch (Throwable t) {
                handleLoopException(t);
            }
        }
    }
}

下面看一下在EventLoop中是如何处理事件的:

在这里插入图片描述

三、总结

netty的服务端源码讲解已经接近尾声,但是每一块都比较散,没有将整体串起来,下一篇我会把整个netty服务端的线程模型和逻辑架构串起来,对整体有个清晰的认识。

祝大家天天向上~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/594009.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

基于Springboot的校园疫情防控系统(有报告)。Javaee项目,springboot项目。

演示视频: 基于Springboot的校园疫情防控系统(有报告)。Javaee项目,springboot项目。 项目介绍: 采用M(model)V(view)C(controller)三层体系结构…

力扣每日一题112:路径总和

题目 简单 给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。 叶子节点 是…

FilterListener详解

文章目录 MVC模式和三层架构MVC模式三层架构MVC和三层架构 JavaWeb的三大组件Filter概述快速入门过滤器API介绍过滤器开发步骤配置过滤器俩种方式修改idea的过滤器模板 使用细节生命周期拦截路径过滤器链 案例统一解决全站乱码问题登录权限校验验 ServletContextServletContext…

多器官和多模态图像的通用异常检测模型-不受特定模型约束

文章目录 A Model-Agnostic Framework for Universal Anomaly Detection of Multi-organ and Multi-modal Images摘要方法实验结果 A Model-Agnostic Framework for Universal Anomaly Detection of Multi-organ and Multi-modal Images 摘要 背景与挑战:深度学习在…

Android Binder机制

一.简介 Binder是什么? Android系统中,涉及到多进程间的通信底层都是依赖于Binder IPC机制。 例如当进程A中的Activity要向进程B中的Service通信,这便需要依赖于Binder IPC。不仅于 此,整个Android系统架构中,大量采…

520表白代码

一、以下代码用html及css编写 代码用记事本打开可直接使用 二、效果如下 代码如下&#xff0c;以下代码复制记事本里面&#xff0c;文件名称后缀改成.html格式&#xff0c;即可运行 名称可在记事本里进行更改 文件名称更改后&#xff0c;文件会变成如下图所示的样式 <ht…

Initialize failed: invalid dom.

项目场景&#xff1a; 在vue中使用Echarts出现的错误 问题描述 提示&#xff1a;这里描述项目中遇到的问题&#xff1a; 例如&#xff1a;在vue中使用Echarts出现的错误 ERROR Initialize failed: invalid dom.at Module.init (webpack-internal:///./node_modules/echarts…

一对一WebRTC视频通话系列(四)——offer、answer、candidate信令实现

本篇博客主要讲解offer、answer、candidate信令实现&#xff0c;涵盖了媒体协商和网络协商相关实现。 本系列博客主要记录一对一WebRTC视频通话实现过程中的一些重点&#xff0c;代码全部进行了注释&#xff0c;便于理解WebRTC整体实现。 一对一WebRTC视频通话系列往期博客 一…

Cocos2d,一个能实现梦想的 Python 库

大家好&#xff01;我是爱摸鱼的小鸿&#xff0c;关注我&#xff0c;收看每期的编程干货。 一个简单的库&#xff0c;也许能够开启我们的智慧之门&#xff0c; 一个普通的方法&#xff0c;也许能在危急时刻挽救我们于水深火热&#xff0c; 一个新颖的思维方式&#xff0c;也许能…

神经网络之防止过拟合

今天我们来看一下神经网络中防止模型过拟合的方法 在机器学习和深度学习中&#xff0c;过拟合是指模型在训练数据上表现得非常好&#xff0c;但在新的、未见过的数据上表现不佳的现象。这是因为模型过于复杂&#xff0c;以至于它学习了训练数据中的噪声和细节&#xff0c;而不…

保研面试408复习 2——操作系统、计网

文章目录 1、操作系统一、进程、线程的概念以及区别&#xff1f;二、进程间的通信方式&#xff1f; 2、计算机网络一、香农准则二、协议的三要素1. 语法2. 语义3. 时序 标记文字记忆&#xff0c;加粗文字注意&#xff0c;普通文字理解。 1、操作系统 一、进程、线程的概念以及…

揭秘大模型应用如何成为当红顶流?

Kimi广告神话背后的关键词战略 如果你生活在中国&#xff0c;你可能不认识ChatGPT&#xff0c;但你一定知道Kimi。无论是学生党还是打工人&#xff0c;都无法避开Kimi的广告。 刘同学在B站上搜教学视频时&#xff0c;弹出了一则软广&#xff0c;上面写着&#xff1a;“作业有…

python学习笔记B-16:序列结构之字典--字典的遍历与访问

下面是字典的访问和遍历方法&#xff1a; d {10:"hello",20:"python",30:"world"} print(d[10],"--",d[20],"--",d[30]) print(d.get(10)) print("以上两种访问方式的区别是&#xff0c;d[key]若键是空值&#xff0c…

代码随想录算法训练营Day12 | 239.滑动窗口最大值、347.前K个高频元素

239.滑动窗口最大值 题目&#xff1a;给你一个整数数组 nums&#xff0c;有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧。你只可以看到在滑动窗口内的 k 个数字。滑动窗口每次只向右移动一位。 返回 滑动窗口中的最大值 。 示例 1&#xff1a; 输入&#xff1…

创造价值与回报:创业者的思维格局与商业智慧

在纷繁复杂的商业世界中&#xff0c;有一种信念始终贯穿于无数创业者的心中——那就是创造价值。张磊的这句“只要不断地创造价值&#xff0c;迟早会有回报”道出了创业者的核心思维格局和商业智慧。本文将从创业者的角度&#xff0c;探讨创造价值的重要性&#xff0c;以及如何…

动态炫酷的新年烟花网页代码

烟花效果的实现可以采用前端技术&#xff0c;如HTML、CSS和JavaScript。通过结合动画、粒子效果等技术手段&#xff0c;可以创建出独特而炫目的烟花效果。同时&#xff0c;考虑到性能和兼容性&#xff0c;需要确保效果在各种设备上都能够良好运行。 效果显示http://www.bokequ.…

【分布式系统的金线】——Base理论深度解析与实战指南

关注微信公众号 “程序员小胖” 每日技术干货&#xff0c;第一时间送达&#xff01; 引言 在当今这个数据密集、服务分布的数字时代&#xff0c;设计高效且可靠的分布式系统成为了技术领域的核心挑战之一。提及分布式系统设计的理论基石&#xff0c;CAP理论——即一致性(Cons…

[HNOI2003]激光炸弹

原题链接&#xff1a;登录—专业IT笔试面试备考平台_牛客网 目录 1. 题目描述 2. 思路分析 3. 代码实现 1. 题目描述 2. 思路分析 二维前缀和板题。 注意从&#xff08;1,1&#xff09;开始存即可&#xff0c;所以每次输入x,y之后&#xff0c;要x,y。 因为m的范围最大为…

uniapp+vue基于移动端的药品进销存系统r275i

最后我们通过需求分析、测试调整&#xff0c;与药品进销存管理系统管理系统的实际需求相结合&#xff0c;设计实现了药品进销存管理系统管理系统。 系统功能需求包含业务需求、功能需求用户需求&#xff0c;系统功能需求分析是在了解用户习惯、开发人员技术和实力等各个因素的前…

美易官方:2024美联储降息,该如何布局

2024美联储降息&#xff0c;该如何布局 #热点引擎计划# 随着2024年美联储降息预期的逐渐升温&#xff0c;全球投资者开始重新考虑其资产配置策略。中金公司认为&#xff0c;面对这一重要的经济事件&#xff0c;投资者需要密切关注市场动态&#xff0c;灵活调整投资策略&#xf…