Netty源码—四、事件处理
前面经过channel初始化、注册,所需要的数据结构(epoll_event)基本上准备好了,serverSocket也处于监听状态,可以接收来自客户端的请求了。NioServerSocketChannel注册在了NioEventLoop#selector,在注册过程中启动了NioEventLoop,run方法会循环执行,每次循环都会执行select和执行所有的task。如果select有事件,则会处理收到的事件。
private void processSelectedKeys() {
if (selectedKeys != null) {
// 是否使用优化过的selectionKey
processSelectedKeysOptimized();
} else {
processSelectedKeysPlain(selector.selectedKeys());
}
}
前面在NioEventLoop初始化的时候说过关于selectionKey优化的问题,这里不再赘述。两种方式主要是遍历selectionKey的方式不同,具体处理事件的调用是一样的。这里以processSelectedKeysOptimized为例。
accept
private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
// channel是NioServerSocketChannel
// unsafe是NioMessageUnsafe
final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
// 省略中间代码...
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
// 调用NioMessageUnsafe.read
unsafe.read();
}
} catch (CancelledKeyException ignored) {
unsafe.close(unsafe.voidPromise());
}
public void read() {
// 省略中间代码...
// 由于是ServerSocket,只负责accept,如果有IO事件说明就是有新的客户端连接,所以这里就是创建NioSocketChannel
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;
// 注册刚刚创建的NioSocketChannel
pipeline.fireChannelRead(readBuf.get(i));
}
readBuf.clear();
allocHandle.readComplete();
pipeline.fireChannelReadComplete();
// 省略中间代码...
}
}
protected int doReadMessages(List<Object> buf) throws Exception {
// 调用java.nio.channels.ServerSocketChannel#accept来创建SocketChannel
SocketChannel ch = SocketUtils.accept(javaChannel());
try {
if (ch != null) {
// 创建NioSocketChannel
buf.add(new NioSocketChannel(this, ch));
return 1;
}
} catch (Throwable t) {
// 省略中间代码...
}
return 0;
}
上面创建了NioSocketChannel之后,接下来注册所有客户端连接的NioSocketChannel,调用的是DefaultChannelPipeline#fireChannelRead方法,接下来是执行pipeline中的handler,在初始化的时候添加了LoggingHandler (如果启动的时候配置了的话),那么目前pipeline中的handler有
- io.netty.channel.DefaultChannelPipeline$HeadContext:pipeline创建的时候默认的第一个handler
- io.netty.handler.logging.LoggingHandler:启动的时候用户配置的handler
- io.netty.bootstrap.ServerBootstrap$ServerBootstrapAcceptor
- io.netty.channel.DefaultChannelPipeline$TailContext:pipeline创建的时候默认的最后一个handler
下面看下ServerBootstrap$ServerBootstrapAcceptor是什么时候添加到handler的
// io.netty.bootstrap.ServerBootstrap#init
// 这个方法是NioServerSocketChannel初始化的时候调用的
void init(Channel channel) throws Exception {
// 省略中间代码...
p.addLast(new ChannelInitializer<Channel>() {
@Override
public void initChannel(final Channel ch) throws Exception {
final ChannelPipeline pipeline = ch.pipeline();
ChannelHandler handler = config.handler();
if (handler != null) {
pipeline.addLast(handler);
}
ch.eventLoop().execute(new Runnable() {
@Override
public void run() {
// 在pipeline中添加ServerBootstrapAcceptor
pipeline.addLast(new ServerBootstrapAcceptor(
ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
}
});
}
之所以说ServerBootstrapAcceptor,是因为NioSocketChannel的register过程是这个handler的channelRead方法开始的
public void channelRead(ChannelHandlerContext ctx, Object msg) {
final Channel child = (Channel) msg;
child.pipeline().addLast(childHandler);
setChannelOptions(child, childOptions, logger);
// 配置NioSocketChannel
for (Entry<AttributeKey<?>, Object> e: childAttrs) {
child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
}
try {
// 这里childGroup就是一开始我们配置的workerGroup
// 所以调用的是io.netty.channel.MultithreadEventLoopGroup#register(io.netty.channel.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);
}
}
接下来的注册过程和NioServerSocketChannel的注册过程是类似的,创建socket,创建SelectionKeyImpl等。只不过NioSocketChannel不监听accept事件。
read
上面在接收到来自客户端的连接请求后,将NioSocketChannel注册到selector上,这个selector也是在NioEventLoop里面的,后面和这个客户端的通信都会通过这个channel进行,如果客户端发送来数据,也是selector收到读事件通知,然后调用processSelectedKey来处理read事件。
private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
// channel是NioSocketChannel
// unsafe是NioSocketChannelUnsafe
final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
// 省略中间代码...
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
// 调用NioByteUnsafe.read
unsafe.read();
}
} catch (CancelledKeyException ignored) {
unsafe.close(unsafe.voidPromise());
}
public final void read() {
final ChannelConfig config = config();
if (shouldBreakReadReady(config)) {
clearReadPending();
return;
}
final ChannelPipeline pipeline = pipeline();
// PooledByteBufAllocator,默认的内存申请管理器
final ByteBufAllocator allocator = config.getAllocator();
// AdaptiveRecvByteBufAllocator$HandleImpl
final RecvByteBufAllocator.Handle allocHandle = recvBufAllocHandle();
allocHandle.reset(config);
ByteBuf byteBuf = null;
boolean close = false;
try {
do {
// 申请内存
byteBuf = allocHandle.allocate(allocator);
// 读取数据
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中的handler
pipeline.fireChannelRead(byteBuf);
byteBuf = null;
} while (allocHandle.continueReading());
// 省略中间代码
}
}
一般来说NioSocketChannel中的handler包括
- io.netty.channel.DefaultChannelPipeline$HeadContext
- org.lep.test.netty.protocol.custom.codec.NettyMessageDecoder:自定义的解码器
- org.lep.test.netty.protocol.custom.codec.NettyMessageEncoder:自定义的编码器
- org.lep.test.netty.protocol.custom.server.LoginAuthRespHandler:自定义的handler
- org.lep.test.netty.protocol.custom.server.HeartBeatRespHandler:自定义的handler
- io.netty.channel.DefaultChannelPipeline$TailContext
netty提供了一些基本的编解码功能,自己可以根据实际需要扩展使用,然后自定义自己的逻辑处理handler。
上面还涉及到内存的分配部分留在下一节介绍。
总结
read事件处理过程:
- 接收到read事件
- 分配内存,初始化buffer
- 调用channel.read将数据读取到buffer中
- 执行pipeline中的handler,包括了编解码的handler,自定义的handler来处理数据
Netty源码—四、事件处理的更多相关文章
- Netty 源码 ChannelHandler(四)编解码技术
Netty 源码 ChannelHandler(四)编解码技术 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) 一.拆包与粘 ...
- Netty 源码解析(四): Netty 的 ChannelPipeline
今天是猿灯塔“365篇原创计划”第四篇. 接下来的时间灯塔君持续更新Netty系列一共九篇 Netty 源码解析(一): 开始 Netty 源码解析(二): Netty 的 Channel Netty ...
- Netty源码分析第4章(pipeline)---->第1节: pipeline的创建
Netty源码分析第四章: pipeline 概述: pipeline, 顾名思义, 就是管道的意思, 在netty中, 事件在pipeline中传输, 用户可以中断事件, 添加自己的事件处理逻辑, ...
- EventLoop(netty源码死磕4)
精进篇:netty源码 死磕4-EventLoop的鬼斧神工 目录 1. EventLoop的鬼斧神工 2. 初识 EventLoop 3. Reactor模式回顾 3.1. Reactor模式的组 ...
- Pipeline inbound(netty源码7)
netty源码死磕7 Pipeline 入站流程详解 1. Pipeline的入站流程 在讲解入站处理流程前,先脑补和铺垫一下两个知识点: (1)如何向Pipeline添加一个Handler节点 ( ...
- Netty源码分析之NioEventLoop(三)—NioEventLoop的执行
前面两篇文章Netty源码分析之NioEventLoop(一)—NioEventLoop的创建与Netty源码分析之NioEventLoop(二)—NioEventLoop的启动中我们对NioEven ...
- 【Netty源码解析】NioEventLoop
上一篇博客[Netty源码学习]EventLoopGroup中我们介绍了EventLoopGroup,实际说来EventLoopGroup是EventLoop的一个集合,EventLoop是一个单线程 ...
- netty源码解析目录
第一章 java nio三大组件与使用姿势 二.netty使用姿势 三.netty服务端启动源码 四.netty客户端启动源码 五.NioEventLoop与netty线程模型 六.ChannelPi ...
- netty源码解解析(4.0)-10 ChannelPipleline的默认实现--事件传递及处理
事件触发.传递.处理是DefaultChannelPipleline实现的另一个核心能力.在前面在章节中粗略地讲过了事件的处理流程,本章将会详细地分析其中的所有关键细节.这些关键点包括: 事件触发接口 ...
随机推荐
- Elasticsearch 编程API入门系列---说在前面的话
前提,是 Eclipse下Maven新建项目.自动打依赖jar包(包含普通项目和Web项目) setting.xml配置文件 如何在Maven官网下载历史版本 HBase 开发环境搭建(Eclipse ...
- Java接口和抽象类以及接口的意义,instanceof的利用
接口interface: 1. 在接口中没有变量,成员无论如何定义,都是公共常量,public static final即使不显式声明也如此. 2. 所有接口方法均隐含public abstract即 ...
- 115个Java面试题和答案——终极列表(下)【转】
第一篇讨论了面向对象编程和它的特点,关于Java和它的功能的常见问题,Java的集合类,垃圾收集器,本章主要讨论异常处理,Java小应用程序,Swing,JDBC,远程方法调用(RMI),Servle ...
- 单片机开发——03工欲善其事必先利其器(AD软件安装破解)
在单片机开发中,有了Keil的程序编程,Protues的模拟仿真,那么问题来了,怎么去进行电路图设计以及硬件调试呢?此刻就必须引入本文的Altium Designer (下文简称AD)安装与破解.在硬 ...
- 《T-SQL查询》读书笔记Part 3.索引的基本知识
索引优化是查询优化中最重要的一部分,索引是一种用于排序和搜索的结构,在查找数据时索引可以减少对I/O的需要:当计划中的某些元素需要或是可以利用经过排序的数据时,也会减少对排序的需要.某些方面的优化可以 ...
- iframe跨域动态设置主窗口宽高
Q:在A项目的a页面嵌入一个iframe,src是B项目的b页面,怎样让a页面的高度跟b页面的高度一样? A:解决跨域方案:增加一个A项目的c页面. 操作步骤: 一,a页面的iframe设置: 获取到 ...
- 如何在Visual Studio和CodeBlocks中反编译C++代码
在Visual Studio中 第一步:打断点 第二步:Debug->Star Debugging 或直接按"F5" 第三步:Debug->Windows->Di ...
- Jacob工具类使用文件互转服务 word转html html转excel word转pdf excel转pdf ppt转pdf
前提条件 必须安装MS office 1.jdk使用jdk1.8 2.jacob.dll放在..\jdk1.8\jre\bin目录下 3.eclipse的jre版本要和jdk一致,window-&g ...
- C语言gets雨scanf函数的用法
/*1.不同点: scanf不能接受空格.制表符Tab.回车等: 而gets能够接受空格.制表符Tab和回车等: 2.相同点: 字符串接受结束后自动加'\0'. 使用scanf("%s&q ...
- python使用itchat库实现微信机器人
itchat是一个开源的微信个人号接口,可以使用该库进行微信网页版中的所有操作,比如:所有好友.添加好友.拉好友群聊.微信机器人等等.详细用户请看文档介绍,在这里. 本文主要使用该库完成一个能够处理微 ...