Netty源码分析
2021-03-30 17:37:37 34 举报
Netty源码分析
作者其他创作
大纲/内容
TailContext
运行TaskQueue异步队列里的任务
readBuf集合就是所有的socket连接for循环遍历所有的socket连接
NioServerSocketChannelbuf.add(new NioSocketChannel(ch))
读取数据
pipeline
NioEventLooprunAllTasks()
HeadContext
ch.configureBlocking(false)
AbstractNioChanneldoRegister()
AbstractChannelregister0(promise)
调用pipeline里的所有handler的channelRead方法
初始化NioSocketChannel的pipeline
AbstractBootstrap>ServerBootStrapconfig().group().register(channel)
ChannelFactory>ReflectiveChannelFactorychannelFactory.newChannel()
AbstractBootstrap>ServerBootStrapinitAndRegister()
建立客户端与服务端的连接
ServerBootstrap.ServerBootstrapAcceptorchildGroup.register(child)
ChannelInitializer
当从客户端读取数据OP_READ
SingleThreadEventExecutortaskQueue.offer(task)
设置NioSocketChannel非阻塞
NioEventLoopGroup
初始化NioServerSocketChannel的pipeline
AbstractChannelnewChannelPipeline()
bossGroup
循环处理selectKeys锁有的Key
当timeout超时或者有时间发生时break
ServerSocketChannel>NioServerSocketChannelNioServerSocketChannel(serverSocketChannel)构造方法
NioEventLoopprocessSelectedKeys()
NioServerSocketChanneldoReadMessages(readBuf)
OP_READ
AbstractNioChannelsuper(parent)
next()得到SingleThreadEventLoop对象
死循环执行
NioServerSocketChannelNioMessageUnsafe.read()
newChannel流程
AbstractNioByteChanneldoReadBytes(byteBuf)
accept得到SocketChannel,然后封装成NioSocketChannel添加到buf中
AbstractNioChannelpipeline.invokeHandlerAddedIfNeeded()
当有客户端连接OP_ACCEPT
Selectorselector.select(timeoutMillis)
设置NioServerSocketChannel非阻塞
NioSocketChannelNioByteUnsafe.read()
NioEventLoopunsafe.read()
往pipeline双线链表中添加ChannelInitializer
p.addLast(new ChannelInitializer<Channel>() {initChannel( ch)})
init完成后
NioEventLoopprocessSelectedKeysOptimized()
init流程
调用pipeline每个handler的handerAdd方法
SingleThreadEventExecutorexecute(task)
workerGroup
把ServerSocketChannel绑定到网络端口
ServerBootstrap
AbstractNioChannelpipeline.fireChannelRegistered()
绑定网络监听端口
利用反射,创建NioServerSocketChannel对象
channel.pipeline()
结束
把建立的NioSocketChannel注册到工作组中的Selector上
ChannelPipelinefireChannelRead(byteBuf)
调用pipeline每个handler的channelRegistered方法
SingleThreadEventExecutorSingleThreadEventExecutor.this.run()
AbstractChanneleventLoop.execute(new Runnable() { public void run() { AbstractUnsafe.this.register0(promise); }})
OP_ACCEPT
ChannelInitializerinitChannel()
ChannelPipelinefireChannelRead(readBuf.get(i))
ServerBootstrap.ServerBootstrapAcceptorchild.pipeline().addLast(childHandler)
Client
AbstractBootstrap>ServerBootStrapinit(channel)
AbstractChanneldoRegister()
AbstractBootstrap>ServerBootStrapbind(9000)
SingleThreadEventExecutordoStartThread()
NioServerSocketChannelSocketUtils.accept(this.javaChannel())
NioServerSocketChannel创建完成后
NioEventLooprun()
初始化注册流程
OP_ACCEPT|OP_READ
SingleThreadEventExecutorstartThread()
NioEventLoopselect(this.wakenUp.getAndSet(false))
初始化注册完成后
遍历所有的任务task.run()
NioEventLoop继承SingleThreadEventLoopSingleThreadEventLoop继承SingleThreadEventExecutorSingleThreadEventExecutor的execute方法
0 条评论
下一页