netty源码基础知识

1:
NioEventLoop:NIO 事件的循环  selector 的打开和事件
Selector类的select()方法会无限阻塞等待,直到有信道准备好了IO操做,或另外一个线程唤醒了它(调用了该选择器的wakeup())。
selectNow()方法执行就绪检查过程,但不阻塞。若是当前没有通道就绪,它将当即返回0

2:
NioServerSocketChannel->AbstractNioMessageChannel
NioSocketChannel->AbstractNioByteChannel: AbstractUnsafe:bind->NioServerSocketChannel:dobind

监听分发事件:NioEventLoop:processSelectedKey-->>NioMessageUnsafe:read
-->>NioServerSocketChannel:doReadMessages(批量《list》接受客户端的请求,创建一个新NioSocketChannel)
--->>DefaultChannelPipeline:fireChannelRead-->
-->> AbstractChannelHandlerContext:channelRead--->>ServerBootstrap:ServerBootstrapAcceptor:channelRead
--->>NioEventLoop:register
3:循环监听
SingleThreadEventExecutor() 新建时候会调用run(),而且赋值给本身的thread;run内部会循环selector
具体这个run何时启动呢?
AbstractBootstrap:initAndRegister:  (ChannelFuture regFuture = group().register(channel);)
-->AbstractChannel:register( eventLoop.execute(new Runnable() )

4:LineBasedFrameDecoder
netty 中 LineBasedFrameDecoder 会调用slice(),切割bytebuf,可是底层只有一个bytebuf,其中一个修改,都会影响整个bytebuf

6.初始化 NioEventLoop 打开 selector

7.ChannelOutboundBuffer 存储写的信息,flush才是真正的发送
  在写如数据先判断:isWritable(),是否能够写,实际会调用ChannelOutboundBuffer的方法判断

8.ctx.writeAndFlush(message) 会将messge的buf进行装换,假如不是isDirect,会装换成Direct,而且是否老的buf

9. 写入数据的时候假如是netty本身的线程,则直接发送。用户的线程则走任务的形势发送。防止多个线程引发的锁的问题
10.在netty内部运行一个任务,netty会判断是不是本身内部已经启动的线程(  if (!inEventLoop()) ),是就运行,不然就放入一个队列中(taskQueue)
,
相关文章
相关标签/搜索