netty服务端启动源码&客户端连接
2022-05-26 00:18:13 0 举报
netty服务端启动源码&客户端连接
作者其他创作
大纲/内容
final int interestOps = selectionKey.interestOps();if ((interestOps & readInterestOp) == 0) { selectionKey.interestOps(interestOps | readInterestOp); }
相当于NIO中的ServerSocketChannel.open();
在pipeline中添加了一个ChannelInitializer
从任务队列中取出来
selector.select
启动阶段没有key触发,不会执行
通过反射在Selector和NioEventLoop之间共享SelectionKey
pipeline.invokeHandlerAddedIfNeeded()
serverSocketChannel.accept()
AbstractNioChannel#doBeginRead
最终
TailContext
ServerBootstrap启动&客户端连接源码分析
MultithreadEventExecutorGroup
设置为非阻塞ch.configureBlocking(false)
开始构建socket连接的pipeline
添加到任务队列addTask
selector.select(timeoutMillis)
BY 静默 email 1193094618@qq.com
childGroup
Context(ChannelInitializer)
配置了SelectorProvider.provider()windows系统下selector实现类WindowsSelectorProvider
自定义的handler
doBind0
AbstractChannel.AbstractUnsafe#bind
headContext
类型ChannelInitializer
连接已经建立返回true
NioServerSocketChannel待会也会走这个路径
AbstractNioChannel
NioServerSocketChannel
记录ServerSocketChannelthis.ch
实例化
invokeLater(Runnable(pipeline.fireChannelActive()))
unwrappedSelector = provider.openSelector()
DefaultChannelPipeline#callHandlerCallbackLater
最终调到这里
bind
在这里解决空轮询
构建连接链路pipeline
初始化大小
channelFactory.newChannel()
ServerBootstrapAcceptor
客户端连接的时候也会走register这段逻辑childGroup(workGroup)注册NioSocketChannel
最后生成
注册config().group().register(channel)
ServerBootstrap
初始化NioServerSocketChannel
selectedKeys = selectedKeySet
initChannel((C) ctx.channel())
将key记录起来this.readInterestOp
NioServerSocketChannel#doReadMessages
NioEventLoop[]
pipeline.addLast
从parentGroup(bossGroup)中选择一个NioEventLoop
DefaultChannelPipeline.HeadContext#channelActive
AbstractChannel.AbstractUnsafeunsafe.beginRead()
使用childGroup(workGroup)处理socketchannel的读写事件
openSelector()
共享
反射实例化
children = new EventExecutor[nThreads]
ServerBootstrapAcceptor#channelRead
类型NioEventLoopGroup
pipeline.fireChannelActive()
SelectorProvider.provider().openServerSocketChannel()
NioEventLoopGroup#newChild
持有这个provider
获取到客户端SocketChannel
initChannel
最终调到
在绑定端口后才修改为感兴趣的事件(OP_ACCEPT)
START
SocketChannel ch = SocketUtils.accept(javaChannel())
childHandler
AbstractNioChannel#doRegister
parentGroup
isActive
task.run()
task.execute()
pollTask()
processSelectedKeysOptimized
指明了OP_ACCEPT的key
DefaultChannelPipeline#addLast0
register0
safeExecute(task)
yes
直接更新key
在initChannel方法中添加了一个new ServerBootstrapAcceptor
在循环监听达到一定次数后退出
客户端连接|发数据
windows平台下返回Selector是WindowsSelectorImpl
childGroup.register(child)
配置
new NioEventLoop
此时还没有绑定端口返回false
unsafe.read();
pipeline.fireChannelRead
持有selector
NioMessageUnsafe#read
使用的是parentGroup(bossGroup)注册NioServerSocketChannel
NioEventLoopGroup
boolean registered控制不是业务处理的就添加task链表处理
移除这个ChannelInitializer
NioEventLoop#run
Field selectedKeysField = selectorImplClass.getDeclaredField(\"selectedKeys\")
更新了selectedKeys
processSelectedKeys();
NioSocketChannelreadInterestOp=OP_ACCEPT
tail.read();
将ServerSocketChannel注册到NioEventLoop引用的selector上感兴趣的事件0
被拿出来
NioSocketChannel
当调用select时
select
开启线程startThread
newSocket
NioEventLoopGroup父类构造方法
pipeline.remove(this);
看到了OP_READ
WindowsSelectorProvider
NioServerSocketChannel#doBind
NioEventLoop#processSelectedKeysOptimized
到这里和NioServerSocketChannel初始化差不多,都是通过父类AbstractNioChannel
(HeadContext)next.invokeRead()
这里找到最前面的OutBound进行读最终找到HeadContext
PendingHandlerCallback
说明
initAndRegister
new DefaultChannelPipeline(this)
register
NioServerSocketChannelreadInterestOp=OP_ACCEPT
创建了默认的pipeline
执行任务队列中的所有任务runAllTasks
WindowsSelectorImpl.this.selectedKeys.add
channel.read();
手动调用
添加添加任务(回调的函数)用于处理添加的这个ChannelInitializer
客户端发起连接此时触发OP_ACCEPT
创建ServerSocketChannel
AbstractChannel#register
SingleThreadEventExecutor#execute
AbstractChannel#register0
SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet()
这个pipeline是NioServerSocketChannel的
DefaultChannel源码分析
pipeline.read()
init(channel)
收藏
0 条评论
下一页