netty
2023-01-12 14:53:54 0 举报
1
作者其他创作
大纲/内容
EventLoopGroup group = new NioEventLoopGroup();
tail = new TailContext(this);
接受连接,获取socketChannel相当于nioSocketChannel sc = ssc.accept();
selector.select(timeoutMillis);
注册ServerSocketChannel到selector
AbstractNioMessageChannelread()
child.pipeline().addLast(childHandler);
将childHandler入队到socketchannel的pipeline
AbstractNioByteChannelread();
SingleThreadEventExecutorexecute(Runnable task)
pipeline.invokeHandlerAddedIfNeeded();
head = new HeadContext(this);
SocketChannel ch = SocketUtils.accept(javaChannel());
AbstractChannel register0(promise);
使用线程池执行runnable
初始化pipeline
OP_READ | OP_ACCEPT
ChannelFuture regFuture = config().group().register(channel);
ServerBootstrap.channel(NioServerSocketChannel.class)
执行ServerBootstrapAcceptor.ChannelRead
NioEventLoop注册一个selector
绑定监听端口
遍历,处理所有key
childGroup.register(child)
当channel注册时,用调用ChannelInitializer的initChannel
用于释放资源
startThread();
执行pipeline每个handler的ChannelRegistered
ServerBootstrapinit(channel);
客户端
初始化端口
ChannelInitializer
socketChannel注册到work group中
this.group = group;this.childGroup = childGroup;
取出任务执行
handler().handlerAdded(this);
initChannel(final Channel ch)
注册group到ServerBootstrap
task入队
从bossGroup取一个EventLoop处理channel注册
异步注册
new NioServerSocketChannel()
taskQueue.offer(task)
1、for循环监听select事件2、处理selectedKeys所有key
1、AbstractChannel初始化Pipeline(如下)2、this.readInterestOp=SelectionKey.OP_ACCEPT3、ch.configureBlocking(false);
runAllTasks();
select(wakenUp.getAndSet(false));
NioEventLoopunsafe.read();
2
childHandler赋值
processSelectedKeysOptimized();
pipeline.fireChannelRead(readBuf.get(i));
设置ChannelOptions属性值
socketChannel的注册逻辑和serversocketchannel一样
pipeline.addLast(ServerBootstrapAcceptor);
DefaultChannelPipelineDefaultChannelPipeline(Channel channel)
channel = channelFactory.newChannel();
int localRead = doReadMessages(readBuf);
反射调用ServerSocketChannel类型的无参构造方法
for (;;) {select(wakenUp.getAndSet(false));processSelectedKeys();runAllTasks();}
添加一个ChannelHandlerContext到链表中
双向链表ChannelHandlerContext
NioServerSocketChanneldoReadMessages(List<Object> buf)
ServerBootstrapAcceptor
声明serverSocketChannel类型,封装到channelFactory中
bServerBootstrap.bind()
p.addLast( ChannelInitializer)
pipeline.fireChannelRead(byteBuf);
taskQueue.poll();
readBuf是OP_ACCEPT事件连接过来的所有socketChannel
ServerBootstrap.childHandler(ChannelHandler childHandler)
initChannel(ctx)pipeline.remove(this);移除自己
/*服务端启动必备*/ ServerBootstrap b = new ServerBootstrap();
3、注册channel
1、初始化channel
2、添加pipeline()
调用pipeline每个handler的handlerAdded
SingleThreadEventExecutor.this.run();
addTask(task);
executor.execute()线程池是直接new一个线程执行task
NioEventLoop.run()
socketChannel封装成NioSocketChannel
OP_READ
pipeline.fireChannelRegistered();
ServerBootstrap.localAddress(new InetSocketAddress(port))
3
OP_ACCEPT
new NioServerSocketChannel 相当于nio的ServerSocketChannel.open()
next().register(channel)
1
ChannelPipeline p = channel.pipeline();
1、初始化serverSocketChannel
eventLoop.execute(new Runnable() { @Override public void run() { register0(promise); } });
死循环执行
task.run()
AbstractBootstrapdoBind(final SocketAddress localAddress)
死循环,有事件或超时等,跳出循环
监听事件
doRegister();
AbstractChannelnewChannelPipeline()
final ChannelFuture regFuture = initAndRegister();
processSelectedKeys();
socketChannel注册read事件,设置成非阻塞
NioEventLoopfinal SelectorTuple selectorTuple = openSelector(); selector = selectorTuple.selector;(nio的Selector.open())
0 条评论
回复 删除
下一页