-----------------一个NioEventLoopGroup 的初始化的时候,会初始化一个 NioEventLoop数组,每个NioEventLoop在初始化的时候,会open一个selector放到自己的属性中,再开启一个线程exector,然后调用Run方法,实际上调用的是NioEventLoop的run方法,在这个run
方法里面,执行的是一个for循环,不停的用那个selector来select注册到上面去的channel,然后根据channel的状态,处理key。
private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
if (!k.isValid()) {
final EventLoop eventLoop;
try {
eventLoop = ch.eventLoop();
} catch (Throwable ignored) {
return;
}
if (eventLoop != this || eventLoop == null) {
return;
}
unsafe.close(unsafe.voidPromise());
return;
}
try {
int readyOps = k.readyOps();
if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
int ops = k.interestOps();
ops &= ~SelectionKey.OP_CONNECT;
k.interestOps(ops);
unsafe.finishConnect();
}
if ((readyOps & SelectionKey.OP_WRITE) != 0) {
ch.unsafe().forceFlush();
}
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
--------------------如果channel可读,那么调用unsafe的read方法,下面看read方法的逻辑
unsafe.read();
}
} catch (CancelledKeyException ignored) {
unsafe.close(unsafe.voidPromise());
}
}

回顾一下netty的bootstrap的启动:

一、以serverBootStrap为例,

@Override
public final void read() {
final ChannelConfig config = config();
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);
------byteBuf是对nio中的buffer的封装?
allocHandle.lastBytesRead(doReadBytes(byteBuf));
if (allocHandle.lastBytesRead() <= 0) {
byteBuf.release();
byteBuf = null;
close = allocHandle.lastBytesRead() < 0;
if (close) {
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 {
if (!readPending && !config.isAutoRead()) {
removeReadOp();
}
}
}
}
@Override
protected int doReadBytes(final ByteBuf byteBuf) throws Exception {
final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
allocHandle.attemptedBytesRead(byteBuf.writableBytes());
--------------再往下看
return byteBuf.writeBytes(javaChannel(), allocHandle.attemptedBytesRead());
}
 @Override
public int writeBytes(ScatteringByteChannel in, int length) throws IOException {
ensureWritable(length);
----------再往下看
int writtenBytes = setBytes(writerIndex, in, length);
if (writtenBytes > 0) {
writerIndex += writtenBytes;
}
return writtenBytes;
}
@Override
public int setBytes(int index, ScatteringByteChannel in, int length) throws IOException {
ensureAccessible();
ByteBuffer tmpBuf = internalNioBuffer();
tmpBuf.clear().position(index).limit(index + length);
try {
-----------------------随便找了一个bytebuf的继承类,发现调用的还是nio的channel的read到一个nio的buffer的方法,而这个tmpNiobuf是netty的bytebuf里面的一个属性,是nio的bytebuffer。
return in.read(tmpNioBuf);
} catch (ClosedChannelException ignored) {
return -1;
}
}

JavaChannel把channel中的数据read到bytebuf中之后,调用pipeline.fireChannelRead,让bytebuf从headContext到tailContext流动,head是一个outbound为true的handler,也就是说,是一个为

写这个动作把最后一关的。相反的,tail是为读这个动作把关,但是正常都会在这之间加一个handler处理数据和逻辑,而不会到tail这一步。同理,写也是会在这中间加一个,而不会到head这一步。

netty ------------ 如果selector检测到一个channel可以读了的更多相关文章

  1. Netty之心跳检测技术(四)

    Netty之心跳检测技术(四) 一.简介 "心跳"听起来感觉很牛X的样子,其实只是一种检测端到端连接状态的技术.举个简单的"栗子",现有A.B两端已经互相连接, ...

  2. 【Netty】利用Netty实现心跳检测和重连机制

    一.前言 心跳机制是定时发送一个自定义的结构体(心跳包),让对方知道自己还活着,以确保连接的有效性的机制.   我们用到的很多框架都用到了心跳检测,比如服务注册到 Eureka Server 之后会维 ...

  3. Netty快速入门(09)channel组件介绍

    书接上回,继续介绍组件. ChannelHandler组件介绍 ChannelHandler组件包含了业务处理核心逻辑,是由用户自定义的内容,开发人员百分之九十的代码都是ChannelHandler. ...

  4. netty深入学习之中的一个: 入门篇

    netty深入学习之中的一个: 入门篇 本文代码下载: http://download.csdn.net/detail/cheungmine/8497549 1)Netty是什么 Netty是Java ...

  5. Netty入门(二):Channel

    前言 Netty系列索引: 1.Netty入门(一):ByteBuf 2.Netty入门(二):Channel 在Netty框架中,Channel是其中之一的核心概念,是Netty网络通信的主体,由它 ...

  6. netty系列之:netty中各不同种类的channel详解

    目录 简介 ServerChannel和它的类型 Epoll和Kqueue AbstractServerChannel ServerSocketChannel ServerDomainSocketCh ...

  7. pytorch中,不同的kernel对不同的feature map进行卷积之后输出某一个channel对应的多个feature map如何得到一个channel的feature map

    实际上在卷积操作的时候,比如说,我某一层输出的feature map的size为4713*13 channel的数目为7,设经过某卷积层之后,网络输出的feature map的channel的数目为1 ...

  8. 通过 Netty、ZooKeeper 手撸一个 RPC 服务

    说明 项目链接 微服务框架都包括什么? 如何实现 RPC 远程调用? 开源 RPC 框架 限定语言 跨语言 RPC 框架 本地 Docker 搭建 ZooKeeper 下载镜像 启动容器 查看容器日志 ...

  9. 使用Netty和动态代理实现一个简单的RPC

    RPC(remote procedure call)远程过程调用 RPC是为了在分布式应用中,两台主机的Java进程进行通信,当A主机调用B主机的方法时,过程简洁,就像是调用自己进程里的方法一样.RP ...

随机推荐

  1. HashMap的两种排序方式

    Map<String, Integer> map = new HashMap<String, Integer>();map.put("d", 2);map. ...

  2. java把类似a=1&b=2&c=3的String类型数据转成map集合

    public static Map<String, Object> transStringToMap(String mapString, String separator, String ...

  3. introsort(内省排序)

    本文转载于:https://blog.csdn.net/sky453589103/article/details/51116264 快速排序是一种很快的算法,它平均的时间复杂度WieO(nlgn), ...

  4. Oracle11g温习-第十九章:审计(audit)

    2013年4月27日 星期六 10:52 1.审计的功能:监控用户在database 的 action (操作) 2.审计分类 1) session :在同一个session,相同的语句只产生一个审计 ...

  5. json字符串转Map、json数组

    json数组转map public static void main(String[] args){ String strArr = "[{\"0\":\"zh ...

  6. 深入理解php内核

    目录 第一部分 基本原理 第一章 准备工作和背景知识 第一节 环境搭建 第二节 源码布局及阅读方法 第三节 常用代码 第四节 小结 第二章 用户代码的执行 第一节 PHP生命周期 第二节 从SAPI开 ...

  7. Python标准数据类型的二次加工

    基于类继承的原理实现: class Li(list): #继承标准数据类型 list def app(self,p_object): #派生出新的 append功能 if not isinstance ...

  8. 使用机器学习检测TLS 恶意加密流——业界调研***有开源的数据集,包括恶意证书的,以及恶意tls pcap报文***

    2018 年的文章, Using deep neural networks to hunt malicious TLS certificates from:https://techxplore.com ...

  9. java 常用命令

    #查看堆使用情况jmap -heap [pid]#查看占用内存高的对象jmap -histo:live [pid] | head -n 100#查看占用内存高的对象,dump成文件,线下分析jmap ...

  10. oracle的case when的用法和decode函数的用法

    oracle中,我们要使用case when时,要怎样使用 如下测试数据,我要把ClassId 的1变成一班,2变成二班,3变成三班,那sql要怎样写呢? 1.case when的用法 -- orac ...