Netty线程模型源码
2022-04-10 19:56:41 0 举报
Netty线程模型源码
作者其他创作
大纲/内容
从head开始调用ServerChannel的pipeline里的所有InboundHandler
直接调用socketChannel的pipeline里的所有handler的channelRead方法
eventLoop.execute(new Runnable(){@Overridepublic void run(){register0(promise);}})
SingleThreadEventExecutor.this.run()
ServerBootstrapAcceptor
pipeline.invokeHandlerAddedIfNeeded()
初始化channel,并将channel感兴趣得事件设置为OP_ACCEPT
NettyServerHanler
注册channel到selector
newChannelPipeline()
TailContext
OP_READ | OP_ACCEPT
task
ServerBootstrap.init(channel)
当channel注册时会调用
socketChannel pipeline
wokerGroup
doRegister()
初始化NioServerSocketChannel
初始化SocketChannel的pipeline
ServerBootstrapAcceptor.channelRead()
HeadContext
for循环处理selectedKeys里所有的key
register0(promise)
SocketChannel注册逻辑跟ServerSocketChannel注册逻辑一样,注册完会调用SocketChannel里的ChannelInitializer把里面我们自己写的Handler全部放入到pipeline
调用pipeline里每个handler的channelRegistered方法
NioServerSocketChannel()
select(wakeUp.getAndSet(false))
bing(9000)
ChannelInitializer
super(parent)
pipeline.fireChannelRead(byteBuf)
selector.select(timeoutMills)
不同线程组的线程的selector监听处理
task.run()
pipeline
当有客户端往服务端发送数据,SocketChannel则发生OP_READ事件
channelFactory.newChannel()
将SocketChannel包装为NioSocketChannel
channel注册时调用,调用完会删除该handler
ServerBootstrap
NioEventLoop.run()
serverSocketChannel pipeline
运行TaskQueue异步队列里的任务
p.addLast(new ChannelInitializer<Channel>(){......})
监听端口
channel.pipeline()
将channel感兴趣的事件设置为OP_READ
next().register(channel)
childGroup.register(child)
initChannel(ctx)
当客户端连接,则会发生OP_ACCEPT事件
把task线程放入TaskQueue异步执行
startThread()
初始化ServerChannel的pipeline
从bossGroup里哪一个线程佬处理channel的注册,将其注册到线程自己的selector上
initAndRegister()
配置channel非阻塞
child.pipeline().addLast(childHandler)
runAlltasks()
addTask(task)
获取SocketChannel
绑定网络监听端口
调用pipeline里每个handler的hadnlerAdded方法
将连接过来的socketChannel注册到workerGroup里一个线程的selector上
processSelectedKeys()
pipeline.fireChannelRegistered()
doReadMessages(readBuf)
client
unsafe.read()(NioMessageUnsafe)
config().group().register(channel)
把ServerChannel绑定到网络端口
死循环执行监听IO事件
当timeoutMills超时或有事件发生会break处理
pipeline.fireChannelRead(readBuf.get(i))
ch.configureBlocking(false)
OP_READ
bossGroup
taskQueue.offer(task)
SocketUtils.accept(javaChannel())
for循环处理selectedKeys里的所有key
unsafe.read()(NioByteUnsafe)
readBud里放的是OP_ACCEPT事件连接过来的所有SocketChannel
processSelectedKeysOptimized()
childHandler就是netty服务端初始代码我们自己写的ChannelInitializer
死循环执行
0 条评论
下一页