netty
2023-02-14 20:38:02 6 举报
AI智能生成
netty
作者其他创作
大纲/内容
创建NioEventLoopGroup
没设置线程数为CPU的2倍吗,传了参数为自己设置的
children = new EventExecutor[nThreads];
创建一个数组
创建一个数组
遍历数组,为每一个EventExecutor是设置LinkedBlockQueue
final SelectorTuple selectorTuple = openSelector();
this.selector = selectorTuple.selector;
为每一个EventExecutor执行Selector.open
this.selector = selectorTuple.selector;
为每一个EventExecutor执行Selector.open
设置bootstrap属性
调用服务端方法,传入两个线程池
bootstrap.group(boosGroup, workerGroup)
bootstrap.group(boosGroup, workerGroup)
将bootstrap的group属性设置为boosGroup
将bootstrap的childGroup属性设置为workerGroup
.channel(NioServerSocketChannel.class)
将ServerSocketChannel构造方法设置属性
.option(ChannelOption.SO_BACKLOG, 1024)
将这两个值放入map
options.put(option, value);
options.put(option, value);
.childHandler
将childHandler属性赋值为一个ChannelInitialize
bootstrap.bind(9000).sync();
绑定端口
绑定端口
initAndRegister();
init
反射出ServerSocketCHannel
super(null, channel, SelectionKey.OP_ACCEPT);
将Accept事件保存
将Accept事件保存
pipeline = newChannelPipeline();
初始化一个pipeline,有一个head和一个tail
初始化一个pipeline,有一个head和一个tail
ch.configureBlocking(false);
将ServerSocketChannel设置为非阻塞
将ServerSocketChannel设置为非阻塞
init(channel);
往piepline里面放ChannelInitializer
往piepline里面放ChannelInitializer
往piepline里面添加ServerBootstrapAcceptor
ChannelFuture regFuture = config().group().register(channel);
将注册任务加入taskQueue
将注册任务加入taskQueue
eventLoop.execute(new Runnable() {
@Override
public void run() {
register0(promise);
}
});
@Override
public void run() {
register0(promise);
}
});
addTask(task);
将注册任务添加到taskQueue
将注册任务添加到taskQueue
startThread();
doStartThread();
SingleThreadEventExecutor.this.run();
strategy = select(curDeadlineNanos);
在里面进行selector.select()
在里面进行selector.select()
if (strategy > 0) {
processSelectedKeys();
}
processSelectedKey(SelectionKey k, AbstractNioChannel ch)
processSelectedKeys();
}
processSelectedKey(SelectionKey k, AbstractNioChannel ch)
ranTasks = runAllTasks();
执行taskQueue里面的任务
register0
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
serverSocketChannel注册selector
serverSocketChannel注册selector
pipeline.invokeHandlerAddedIfNeeded();
会回调ChannelInitializer
pipeline.fireChannelRegistered();
执行Handler
doBind0(regFuture, channel, localAddress, promise);
绑定端口
绑定端口
channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
(SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0
int localRead = doReadMessages(readBuf);读信息
1.SocketChannel ch = SocketUtils.accept(javaChannel());
执行accept,获取socketChannel
执行accept,获取socketChannel
2.如果获取的socketChannel是null,就创建一个新的SocketChannel
3.SelectionKey.OP_READ
SocketChannel将SelectKey设置为读
SocketChannel设置于非阻塞
SocketChannel将SelectKey设置为读
SocketChannel设置于非阻塞
pipeline.fireChannelRead(readBuf.get(i));
处理channel
处理channel
会调用ServerBootstrapAcceptor的channelRead()
child.pipeline().addLast(childHandler);
初始化时候创建添加的new ChannelInitializer
初始化时候创建添加的new ChannelInitializer
childGroup.register(child)
processSelectedKey(SelectionKey k, AbstractNioChannel ch)
走的AbstractNioByteChannel的read()
走的AbstractNioByteChannel的read()
0 条评论
下一页