NOTE
2.67 2.服务端初始化
调用 bind - doBind - initAndRegister - channelFactory.newChannel() - init(channel) 我们重点看这个 ServerBootstrap - init
这是历史学习笔记,可能存在过时或不完整的理解。
调用
bind
- doBind
- initAndRegister
- channelFactory.newChannel()
- init(channel) 我们重点看这个
ServerBootstrap
- init
void init(Channel channel) throws Exception {
//使用刚刚创建的config类配置options参数
final Map<ChannelOption<?>, Object> options = options0();
synchronized (options) {
setChannelOptions(channel, options, logger);
}
//使用刚刚创建的config类配置attrs参数
final Map<AttributeKey<?>, Object> attrs = attrs0();
synchronized (attrs) {
for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
@SuppressWarnings("unchecked")
AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
channel.attr(key).set(e.getValue());
}
}
//。。。。
p.addLast(new ChannelInitializer<Channel>() {
@Override
public void initChannel(final Channel ch) throws Exception {
final ChannelPipeline pipeline = ch.pipeline();
//拿到用户通过handler()定义的handler类,加入pipeline
ChannelHandler handler = config.handler();
if (handler != null) {
pipeline.addLast(handler);
}
ch.eventLoop().execute(new Runnable() {
@Override
public void run() {
//将ServerBootstrapAcceptor加入pipeline
//这个ServerBootstrapAcceptor有我们自定义的EventLoopGroup、childHandler、options、attrs等
pipeline.addLast(new ServerBootstrapAcceptor(
ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
}
});
}