Netty源码流程图
2024-03-05 20:11:03 0 举报
Netty源码流程图展示了Netty框架的核心结构,包括不同模块之间的交互和关系。Netty是一个用于Java的高性能网络应用框架,主要用于开发异步、事件驱动的网络应用程序。在流程图中,我们可以看到Netty的主要组件,如Channel、EventLoop、ChannelPipeline和Handler。此外,流程图还展示了Netty处理的不同类型的数据,如字节流和消息对象。整个框架的设计和实现是基于事件驱动的,这种设计方式使得Netty在处理高并发、大数据量的网络应用时具有极高的性能。
作者其他创作
大纲/内容
ServerBootstrap.init(channel)
注册channel到selector
processSelectedKeys()
pipeline
TailContext
HeadContext
1.newTaskQueue(taskQueueFactory)2.this.provider=selectorProvider3.openSelector()4.this.selector = selectorTuple.selector
bind()
调用pipeline里面的ChannelRegister方法
next().register(channel)
doStartThread()
this.childHandler=childHandler
select(curDeadlineNanos)
设置accept事件
当客户端发送数据,SocketChannel则会发生OP_READ事件
SingleThreadEventExecutor.execute()
processSelectedKey()
config().group().register(channel)
SingleThreadEventExecutor.this.run()
ChannelInitializer
this.childGroup=childGroup
channelFactory.newChannel()
pipeline.invokeHandlerAddedIfNeeded()
调用pipeline里面的handler
super(nThreads == 0 ? font color=\"#b71c1c\
child.pipeline().addLast(childHandler)
ServerBootstrap
taskQueue.take()
taskQueue.offer(task)
AbstractChannel初始化处理链
MultithreadEventLoopGroup.register()
pipeline = newChannelPipeline()
initAndRegister()
ReflectiveChannelFactory.newChannel()
super()
channel调用注册时会删除
AbstractChannel.register()
pipeline.fireChannelRead(readBuf.get(i))
EventLoopGroup workerGroup = new NioEventLoopGroup();
OP_READ
AbstractBootstrap.group()
childGroup.register(child)
doReadMessages(readBuf)
调用pipeline里面的handleradd
this.constructor = clazz.getConstructor()
添加ChannelInitializer
new ReflectiveChannelFactory()
eventLoop.execute(new Runnable() { @Override public void run() { register0(promise); } });
SocketChannel的注册逻辑与ServerSocketChannel注册逻辑一样,注册完会把我们自己写的handler注册到pipeline里去
NioByteUnsafe.read()
ch.configureBlocking(false)
设置为非阻塞
runAllTasks()
register0(promise)
client
channel.pipeline()
group()
NioEventLoopGroup()
safeExecute(task)
ServerBootstrapAcceptor.channelRead()
Accept事件
option()
processSelectedKeysOptimized()
ServerSocketChannel pipeline
constructor.newInstance()
doRegister()
addTask(task)
NioServerSocketChannel()
this.group = group
自定义的Handler
unsafe.read()
for循环处理所有selectKey
初始化构造函数
MultithreadEventLoopGroup
childHandler()
NioMessageUnsafe.read()
ServerBootstrapAcceptor
将SocketChannel保包装成NioSocketChannel
NioServerSocketChannel.doReadMessages(readBuf)
initChannel()
AbstractNioChannel.doRegister()
MultithreadEventExecutorGroup
将连接过来的socketChannel注册到EventLoop的selector上
children = new EventExecutor[nThreads]
EventLoopGroup bossGroup = new NioEventLoopGroup();
调用所有pipeline里的ChannelRead方法
childHandler就是服务端初始化时候的ChannelInitializer
NioEventLoop.run()
static { font color=\"#b71c1c\
SocketUtils.accept(javaChannel())
channel()
for(;;)死循环
pipeline.fireChannelRegistered()
(readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0
SingleThreadEventLoop.register()
pollTask()
p.addLast()
startThread()
当客户端有连接,ServerSocketChannel则会发生OP_ACCEPT事件
super.group(parentGroup)
selector.select(timeoutMillis)
端口
当channel注册时会调用
NioEventLoopGroup.newChild()
0 条评论
下一页