Netty进阶:客户端连接源码分析

版权声明:作者:TheLudlows 载请注明出处: https://blog.csdn.net/TheLudlows/article/details/82708030

1. BootStrap启动代码

客户端方面的代码开始

EventLoopGroup group = new NioEventLoopGroup();
try {
    Bootstrap b = new Bootstrap();
    b.group(group)
     .channel(NioSocketChannel.class)
     .option(ChannelOption.TCP_NODELAY, true)
     .handler(new ChannelInitializer<SocketChannel>() {
         @Override
         public void initChannel(SocketChannel ch) throws Exception {
             ChannelPipeline p = ch.pipeline();
             p.addLast(new EchoClientHandler());
         }
     });
    ChannelFuture f = b.connect(HOST, PORT).sync();
    f.channel().closeFuture().sync();
} finally {
    group.shutdownGracefully();
}

客户端初始化时所需的所有内容:

  1. EventLoopGroup: 不论是服务器端还是客户端, 都必须指定 EventLoopGroup. 在这个例子中, 指定了 NioEventLoopGroup, 表示一个 NIO 的EventLoopGroup
  2. ChannelType: 指定 Channel 的类型. 因为是客户端, 因此使用了 NioSocketChannel
  3. Handler: 设置数据的处理器

2.NioSocketChannel 的初始化以及实例化过程

看过NioServerSocketChannel源码分析的读者了解NioServerSocketChannel是对Java NIO ServerSocketChannel的一层封装,同理NioSocketChannel也是对Java SocketChannel的封装。
NioSocketChannel 的类层次结构如下:

netty

除了 TCP 协议以外, Netty 还支持很多其他的连接协议, 并且每种协议还有 NIO(异步 IO) 和 OIO(Old-IO, 即传统的阻塞 IO) 版本的区别. 不同协议不同的阻塞类型的连接都有不同的 Channel 类型与之对应下面是一些常用的 Channel 类型:

NioSocketChannel, 代表异步的客户端 TCP Socket 连接.

NioServerSocketChannel, 异步的服务器端 TCP Socket 连接.

NioDatagramChannel, 异步的 UDP 连接

NioSctpChannel, 异步的客户端 Sctp 连接.

NioSctpServerChannel, 异步的 Sctp 服务器端连接.

OioSocketChannel, 同步的客户端 TCP Socket 连接.

OioServerSocketChannel, 同步的服务器端 TCP Socket 连接.

OioDatagramChannel, 同步的 UDP 连接

OioSctpChannel, 同步的 Sctp 服务器端连接.

OioSctpServerChannel, 同步的客户端 TCP Socket 连接.

下面分析启动代码,channel()方法非常熟悉了,和服务端的类似就是创建NioSocketChannel的工厂类,为了方便后续的新建channel。

NioSocketChannel真正实例化的过程是在Bootstrap.connect -> Bootstrap.doResolveAndConnect -> AbstractBootstrap.initAndRegister

下面是initAndRegister方法:

final ChannelFuture initAndRegister() {
    Channel channel = null;
    channel = channelFactory.newChannel();
    init(channel);
    ChannelFuture regFuture = config().group().register(channel);

}

在 newChannel 中, 通过类对象的 newInstance 来获取一个新 Channel 实例, 因而会调用NioSocketChannel 的默认构造器。这里牵扯到了一系列的父类构造函数调用链,值至AbstractChannel初始化pipleLine和unsafe。此过程和NioServerSocketChannel中相似,在次不再赘述。

3. 客户端连接

客户端通过调用 Bootstrap 的 connect 方法进行连接.在 connect 中, 会进行一些参数初始化, 最终调用的是 doConnect 方法, 其实现如下:

private static void doConnect(
           final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise connectPromise) {

       final Channel channel = connectPromise.channel();
       channel.eventLoop().execute(new Runnable() {
           @Override
           public void run() {
               if (localAddress == null) {
                   channel.connect(remoteAddress, connectPromise);
               } else {
                   channel.connect(remoteAddress, localAddress, connectPromise);
               }
               connectPromise.addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
           }
       });
   }

eventloop 线程中调用 Channel 的 connect 方法, 而这个 Channel 的具体类型是什么呢? 我们在 Channel 初始化这一小节中已经分析过了, 这里 channel 的类型就是 NioSocketChannel.进行跟踪到 channel.connect 中, 我们发现它调用的是 DefaultChannelPipeline#connect:

public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
        return pipeline.connect(remoteAddress, localAddress, promise);
}

pipeline 的 connect 代码如下:

public final ChannelFuture connect(
            SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
        return tail.connect(remoteAddress, localAddress, promise);
}

tail 字段是一个 TailContext 的实例, 而 TailContext 又是 AbstractChannelHandlerContext 的子类, 并且没有实现 connect 方法, 因此这里调用的其实是 AbstractChannelHandlerContext.connect, 我们看一下这个方法的实现:

public ChannelFuture connect(
            final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {

        final AbstractChannelHandlerContext next = findContextOutbound();
        EventExecutor executor = next.executor();
        if (executor.inEventLoop()) {
            next.invokeConnect(remoteAddress, localAddress, promise);
        } else {
            safeExecute(executor, new Runnable() {
                @Override
                public void run() {
                    next.invokeConnect(remoteAddress, localAddress, promise);
                }
            }, promise, null);
        }
        return promise;
    }

上面的代码中有一个关键的地方, 即 final AbstractChannelHandlerContext next = findContextOutbound(), 这里调用 findContextOutbound 方法, 从 DefaultChannelPipeline 内的双向链表的 tail 开始, 不断向前寻找第一个 outbound 为 true 的 AbstractChannelHandlerContext:

private AbstractChannelHandlerContext findContextOutbound() {
        AbstractChannelHandlerContext ctx = this;
        do {
            ctx = ctx.prev;
        } while (!ctx.outbound);
        return ctx;
    }

head 是 HeadContext 的实例, 它实现了 ChannelOutboundHandler 接口, 并且它的 outbound 字段为 true. 因此在 findContextOutbound 中, 找到的 AbstractChannelHandlerContext 对象其实就是 head,然后调用AbstractChannelHandlerContext的 invokeConnect 方法, 其代码如下:

private void invokeConnect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise) {
        if (invokeHandler()) {
           remoteAddress, localAddress, promise);
        } else {
            connect(remoteAddress, localAddress, promise);
        }
    }

invokeConnect 方法执行head的connect方法:

@Override
public void connect(
        ChannelHandlerContext ctx,
        SocketAddress remoteAddress, SocketAddress localAddress,
        ChannelPromise promise) throws Exception {
    unsafe.connect(remoteAddress, localAddress, promise);
}

这个 connect 方法很简单, 仅仅调用了 unsafe 的 connect 方法,这个unsafe就是在构造HeadContext节点时从NioSocketChannel中获取的:

 HeadContext(DefaultChannelPipeline pipeline) {
            super(pipeline, null, HEAD_NAME, false, true);
            unsafe = pipeline.channel().unsafe();
            setAddComplete();
        }

饶了一大圈,回到了NioSocketChannel的unsafe,unsafe是Channel一个内部接口,他的connect方法实现在AbstractNioChannel.AbstractNioUnsafe中:

 public final void connect(
                final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) {
                boolean wasActive = isActive();
                if (doConnect(remoteAddress, localAddress)) {
                    fulfillConnectPromise(promise, wasActive);
                } else {
                   ...
            }
        }

doConnect 方法是在 NioSocketChannel 中实现的:

protected boolean doConnect(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception {
    boolean connected = SocketUtils.connect(javaChannel(), remoteAddress);
}

//SocketUtils
public static boolean connect(final SocketChannel socketChannel, final SocketAddress remoteAddress)
            throws IOException {
          return socketChannel.connect(remoteAddress);
    }

首先是获取 Java NIO SocketChannel, 从 NioSocketChannel.javaChannel 返回的 SocketChannel 对象; 然后是调用 SocketUtils.connect 方法完成 Java NIO 层面上的 Socket 的连接.

猜你喜欢

转载自blog.csdn.net/TheLudlows/article/details/82708030