NOTE

2. Server Initialization

Call path and ServerBootstrap.init

JavaCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

Call Path

bind

  • doBind
  • initAndRegister
    • channelFactory.newChannel()
    • init(channel) — this is the part to focus on.

ServerBootstrap

  • init
void init(Channel channel) throws Exception {
    // Use the config class just created to configure options.
    final Map<ChannelOption<?>, Object> options = options0();
    synchronized (options) {
        setChannelOptions(channel, options, logger);
    }
    // Use the config class just created to configure 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();
            // Obtain the handler class defined by the user through handler(),
            // then add it to the pipeline.
            ChannelHandler handler = config.handler();
            if (handler != null) {
                pipeline.addLast(handler);
            }

            ch.eventLoop().execute(new Runnable() {
                @Override
                public void run() {
                    // Add ServerBootstrapAcceptor to the pipeline.
                    // This ServerBootstrapAcceptor contains our custom
                    // EventLoopGroup, childHandler, options, attrs, and so on.
                    pipeline.addLast(new ServerBootstrapAcceptor(
                            ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
                }
            });
        }
    });
}

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub