netty Bootstrap connect方法

connect方法调用如下方法,值得分析

private ChannelFuture doResolveAndConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
            final ChannelFuture regFuture = initAndRegister();
            final Channel channel = regFuture.channel();
    
            if (regFuture.isDone()) {
                if (!regFuture.isSuccess()) {
                    return regFuture;
                }
                return doResolveAndConnect0(channel, remoteAddress, localAddress, channel.newPromise());
            } else {
                // Registration future is almost always fulfilled already, but just in case it's not.
                final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
                regFuture.addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        // Direclty obtain the cause and do a null check so we only need one volatile read in case of a
                        // failure.
                        Throwable cause = future.cause();
                        if (cause != null) {
                            // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                            // IllegalStateException once we try to access the EventLoop of the Channel.
                            promise.setFailure(cause);
                        } else {
                            // Registration was successful, so set the correct executor to use.
                            // See https://github.com/netty/netty/issues/2586
                            promise.registered();
                            doResolveAndConnect0(channel, remoteAddress, localAddress, promise);
                        }
                    }
                });
                return promise;
            }
        }

initAndRegister:
channel实例化:ReflectiveChannelFactory反射调用
初始化:ChannelInitalizer加入pipeline,ChannelConfig 填充(来源于BootStrap),属性填充(来源于BootStrap)
注册:生成DefaultChannelPromise,选择EventLoopGroup内的EventLoop(此时Channel的EventLoop就定下来了),提交register任务(channel注册到刚选出的EventLoop的selector上,不关注任何事件)到对应EventLoop,返回DefaultChannelPromise(ChannelFuture)

1 注册过程先选定特定的EventLoop,线程返回,由EventLoop完成实际注册并通知,若已完成且失败,返回用户,若已完成且成功,返回connect的future(新建)给用户,此时connect可能还没完成
2 若注册未完成,返回PendingRegistrationPromise给用户,注册future的Listener通知PendingRegistrationPromise给到用户,避免用户也给注册future加listener,导致还未connect便先执行用户的listener

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值