netty 1.2 ServerBootstrap::bind

bind方法会执行到doBind,关键在于initAndRegister->initdoBind0

private ChannelFuture doBind(final SocketAddress localAddress) {
        final ChannelFuture regFuture = initAndRegister();
        final Channel channel = regFuture.channel();
        if (regFuture.cause() != null) {
            return regFuture;
        }

        if (regFuture.isDone()) {
            // At this point we know that the registration was complete and successful.
            ChannelPromise promise = channel.newPromise();
            doBind0(regFuture, channel, localAddress, promise);
            return promise;
        } else {
            // 和上面的流程差不多,加了点异步处理
            ...
        }
    }

init

initAndRegister主要是调用init,这里添加了个ChannelInitializer,后者的回调行为是添加一个ServerBootstrapAcceptor,这定义了ServerChannel的行为

@Override
    void init(Channel channel) throws Exception {
        ...

        p.addLast(new ChannelInitializer<Channel>() {
            @Override
            public void initChannel(final Channel ch) throws Exception {
                final ChannelPipeline pipeline = ch.pipeline();
                ChannelHandler handler = config.handler();
                if (handler != null) {
                    pipeline.addLast(handler);
                }

                ch.eventLoop().execute(new Runnable() {
                    @Override
                    public void run() {
                        pipeline.addLast(new ServerBootstrapAcceptor(
                                ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
                    }
                });
            }
        });
    }

疑问:为什么不直接执行pipeline.addLast,而要间接通过ChannelInitializer去添加呢?

然后看下ServerBootstrapAcceptor::channelRead,其行为是监听到读来的Channel时:

  1. 帮助注册用户设置的childHandler(以便触发回调注册)
  2. 并将其注册到childGroup上:
@Override
        @SuppressWarnings("unchecked")
        public void channelRead(ChannelHandlerContext ctx, Object msg) {
            final Channel child = (Channel) msg;

            child.pipeline().addLast(childHandler);  // 触发设置的回调

            setChannelOptions(child, childOptions, logger);

            for (Entry<AttributeKey<?>, Object> e: childAttrs) {
                child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
            }

            try {
            	// 把Channel注册到childGroup
                childGroup.register(child).addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (!future.isSuccess()) {
                            forceClose(child, future.cause());
                        }
                    }
                });
            } catch (Throwable t) {
                forceClose(child, t);
            }
        }

疑问:ServerBootstrapAcceptor何时会被触发channelRead?

doBind0

private static void doBind0(
        final ChannelFuture regFuture, final Channel channel,
        final SocketAddress localAddress, final ChannelPromise promise) {

    // This method is invoked before channelRegistered() is triggered.  Give user handlers a chance to set up
    // the pipeline in its channelRegistered() implementation.
    channel.eventLoop().execute(new Runnable() {
        @Override
        public void run() {
            if (regFuture.isSuccess()) {
                channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
            } else {
                promise.setFailure(regFuture.cause());
            }
        }
    });
}

// ChannelFutureListener.java
ChannelFutureListener CLOSE_ON_FAILURE = new ChannelFutureListener() {
    @Override
    public void operationComplete(ChannelFuture future) {
        if (!future.isSuccess()) {
            future.channel().close();
        }
    }
};

顾名思义,doBind0做真正的端口绑定。channel.bind绑定到了端口,还加了个失败时关闭ServerChannel的监听器。

总结

ServerBootstrap::bind

  1. 通过为ServerChannel绑定ServerBootstrapAcceptor,定义了只要收到Channel就进行设置,并注册到childGroup。
  2. 然后通过doBind0监听端口,并绑定了监听器CLOSE_ON_FAILURE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值