Netty 源碼解析(二):Netty 的 Channel

原創申明:本文由公衆號【猿燈塔】原創,轉載請說明出處標註

今天是猿燈塔「365篇原創計劃」第二篇。java

接下來的時間燈塔君持續更新Netty系列一共九篇面試

Netty源碼解析(一):開始 微信

當前:Netty 源碼解析(二): Netty 的 Channel socket

Netty 源碼解析(三): Netty 的 Future 和 Promiseide

Netty 源碼解析(四): Netty 的 ChannelPipelineoop

Netty 源碼解析(五): Netty 的線程池分析this

Netty 源碼解析(六): Channel 的 register 操做spa

Netty 源碼解析(七): NioEventLoop 工做流程線程

Netty 源碼解析(八): 回到 Channel 的 register 操做3d

Netty 源碼解析(九): connect 過程和 bind 過程分析

今天呢!燈塔君跟你們講: 

Netty 的 Channel

這節咱們來看看 NioSocketChannel 是怎麼和 JDK 底層的 SocketChannel 聯繫在一塊兒的,它們是一對一的關係。NioServerSocketChannel 和 ServerSocketChannel 同理,也是一對一的關係。

在 Bootstrap(客戶端) 和 ServerBootstrap(服務端) 的啓動過程當中都會調用 channel(…) 方法:

下面,咱們來看 channel(…) 方法的源碼:

1// AbstractBootstrap
2public B channel(Class<? extends C> channelClass) {
3    if (channelClass == null) {
4        throw new NullPointerException("channelClass");
5    }
6    return channelFactory(new ReflectiveChannelFactory<C>(channelClass));
7}

咱們能夠看到,這個方法只是設置了 channelFactory 爲 ReflectiveChannelFactory 的一個實例,而後咱們看下這裏的 ReflectiveChannelFactory 究竟是什麼:

newChannel() 方法是 ChannelFactory 接口中的惟一方法,工廠模式你們都很熟悉。咱們能夠看到,ReflectiveChannelFactory#newChannel() 方法中使用了反射調用 Channel 的無參構造方法來建立 Channel,咱們只要知道,ChannelFactory 的 newChannel() 方法何時會被調用就能夠了。

  • 對於NioSocketChannel,因爲它充當客戶端的功能,它的建立時機在 connect(…) 的時候;
  • 對於NioServerSocketChannel來講,它充當服務端功能,它的建立時機在綁定端口bind(…)的時候。

接下來,咱們來簡單追蹤下充當客戶端的Bootstrap中NioSocketChannel的建立過程,看看NioSocketChannel是怎麼和JDK 中的SocketChannel關聯在一塊兒的:

1// Bootstrap
2public ChannelFuture connect(String inetHost, int inetPort) {
3    return connect(InetSocketAddress.createUnresolved(inetHost, inetPort));
4}

而後再往裏看到這個方法:

1public ChannelFuture connect(SocketAddress remoteAddress) {
2    if (remoteAddress == null) {
3        throw new NullPointerException("remoteAddress");
4    // validate 只是校驗一下各個參數是否是正確設置了
5    validate();
6    return doResolveAndConnect(remoteAddress, config.localAddress());
7}

繼續:

1// 再往裏就到這裏了
2private ChannelFuture doResolveAndConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) {
3    // 咱們要說的部分在這裏
4    final ChannelFuture regFuture = initAndRegister();
5    final Channel channel = regFuture.channel();
6    ......
7}

而後,咱們看initAndRegister()方法:

1final ChannelFuture initAndRegister() {
2    Channel channel = null;
3    try {
4        // 前面咱們說過,這裏會進行 Channel 的實例化
5        channel = channelFactory.newChannel();
6        init(channel);
7    } catch (Throwable t) {
8        ...
9    }
10    ...
11    return regFuture;
12}

咱們找到了channel = channelFactory.newChannel()這行代碼,根據前面說的,這裏會調用相應Channel的無參構造方法。

而後咱們就能夠去看NioSocketChannel的構造方法了:

1public NioSocketChannel() {
2    // SelectorProvider 實例用於建立 JDK 的 SocketChannel 實例
3    this(DEFAULT_SELECTOR_PROVIDER);
4}
5
6public NioSocketChannel(SelectorProvider provider) {
7    // 看這裏,newSocket(provider) 方法會建立 JDK 的 SocketChannel
8    this(newSocket(provider));
9}

咱們能夠看到,在調用 newSocket(provider) 的時候,會建立JDK NIO的一個 SocketChannel實例:

1private static SocketChannel newSocket(SelectorProvider provider) {
2    try {
3        // 建立 SocketChannel 實例
4        return provider.openSocketChannel();
5    } catch (IOException e) {
6        throw new ChannelException("Failed to open a socket.", e);
7    }
8}

NioServerSocketChannel同理,也很是簡單,從ServerBootstrap#bind(...)方法一路點進去就清楚了。

因此咱們知道了,NioSocketChannel 在實例化過程當中,會先實例化JDK底層的 SocketChannel,NioServerSocketChannel 也同樣,會先實例化 ServerSocketChannel 實例:

說到這裏,咱們順便再繼續往裏看一下NioSocketChannel的構造方法:

1public NioSocketChannel(SelectorProvider provider) {
2    this(newSocket(provider));
3}

剛纔咱們看到這裏newSocket(provider)建立了底層的SocketChannel 實例咱們繼續往下看構造方法:

1public NioSocketChannel(Channel parent, SocketChannel socket) {
2    super(parent, socket);
3    config = new NioSocketChannelConfig(this, socket.socket());
4}

上面有兩行代碼,第二行代碼很簡單,實例化了內部的 NioSocketChannelConfig 實例,它用於保存channel的配置信息,這裏沒有咱們如今須要關心的內容,直接跳過。

第一行調用父類構造器,除了設置屬性外,還設置了SocketChannel的非阻塞模式:

1protected AbstractNioByteChannel(Channel parent, SelectableChannel ch) {
2    // 毫無疑問,客戶端關心的是 OP_READ 事件,等待讀取服務端返回數據
3    super(parent, ch, SelectionKey.OP_READ);
4}
5
6// 而後是到這裏
7protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
8    super(parent);
9    this.ch = ch;
10    // 咱們看到這裏只是保存了 SelectionKey.OP_READ 這個信息,在後面的時候會用到
11    this.readInterestOp = readInterestOp;
12    try {
13        // ******設置 channel 的非阻塞模式******
14        ch.configureBlocking(false);
15    } catch (IOException e) {
16        ......
17    }
18}

NioServerSocketChannel的構造方法相似,也設置了非阻塞,而後設置服務端關心的SelectionKey.OP_ACCEPT事件:

1public NioServerSocketChannel(ServerSocketChannel channel) {
2    // 對於服務端來講,關心的是 SelectionKey.OP_ACCEPT 事件,等待客戶端鏈接
3    super(null, channel, SelectionKey.OP_ACCEPT);
4    config = new NioServerSocketChannelConfig(this, javaChannel().socket());
5}

這節關於Channel的內容咱們先介紹這麼多,主要就是實例化了JDK層的SocketChannel或ServerSocketChannel,而後設置了非阻塞模式,咱們後面再繼續深刻下去。

365天干貨不斷,能夠微信搜索「 猿燈塔」第一時間閱讀,回覆【資料】【面試】【簡歷】有我準備的一線大廠面試資料和簡歷模板
相關文章
相關標籤/搜索