Netty源码分析:1.3初始化NioServerSocketChannel

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GG_and_DD/article/details/80416941

第一章节是主要是服务器启动的代码分析。章节目录有:
|———1.1初始化NioEventLoopGroup
|———1.2初始化NioEventLoop
|———1.3初始化NioServerSocketChannel
|———1.4服务器启动流程
为什么先从初始化开始了解服务器启动?
因为在我看服务器启动的相关源码的时候,有很多地方都是初始化的时候已经建立好的。所以我就从初始化的源码开始看起。这是我第一次看源码的笔记,仍有很多理解错误的地方和不解的地方。欢迎讨论。

本篇目录:

  • 类的继承关系图
  • 时序图
  • 代码分析
  • 疑问

类的继承关系图

image.png

时序图

image.png

NioServerSocketChannel的构造器

super(null, channel, SelectionKey.OP_ACCEPT);
第一个参数是Channel类
第二个参数是SelectableChannel
第三个参数是int:SelectionKey.OP_ACCEPT 让我想起了NIO中给一个channel绑定一个selector管理器。

public NioServerSocketChannel(ServerSocketChannel channel) {
        super(null, channel, SelectionKey.OP_ACCEPT);
        config = new NioServerSocketChannelConfig(this, javaChannel().socket());
    }

初始化开始

1. DefaultAttributeMap

首先是从DefaultAttributeMap开始初始化
该类实现了AttributeMap接口,主要是更加降低损耗。

2. AbstractChannel

  • parent 属性为null: 从NioServerSocketChannel的构造器可以知道第一个参数Channel 是null。
  • unsafe:新建一个NioMessageUnsafe
  • pipeline:新建一个Pipeline。也就是说一个channel一个Pipeline.
    netty对底层socket的操作都是通过unsafe来做的。unsafe主要由两种不同的实现NioMessageUnsafe和NioByteUnsafe.NioMessageUnsafe是服务端的。
protected AbstractChannel(Channel parent) {
        this.parent = parent;
        id = newId();
        unsafe = newUnsafe();
        pipeline = newChannelPipeline();
    }

3. AbstractNioChannel

  • 1 初始化AbstractChannel
  • 2 ch的类型是SelectableChannel。就是相当于存储该selector管理器
  • 3 存储Int ,也就是初始化SelectionKey.OP_ACCEPT 感兴趣事件。
  • 4 设置非阻塞模式。相当于nio里设置channel非阻塞。
 protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
        super(parent); //1
        this.ch = ch;  //2 
        this.readInterestOp = readInterestOp; //3
        try {
            ch.configureBlocking(false); //4
        } catch (IOException e) {
            try {
                ch.close();
            } catch (IOException e2) {                
            }
        }
    }

4. AbstractNioMessageChannel

protected AbstractNioMessageChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
        super(parent, ch, readInterestOp);
    }

5. NioServerSocketChannel

  • 1 对Channel进行配置。与ChannelOption类类似。
public NioServerSocketChannel(ServerSocketChannel channel) {
        super(null, channel, SelectionKey.OP_ACCEPT);
        config = new NioServerSocketChannelConfig(this, javaChannel().socket()); //1
    }

这样一个NioServerSocketChannel的对象就已经建立完成了。

总结

整个NioServerSocketChannel的初始化,我觉得就是建立一个channel对象,在该对象里面附带一些属性,这些属性有:
- unsafe:NioMessageUnsafe
- pipeline管道。
- SelectableChannel并将该Channel设置为非阻塞
- 附带一个感兴趣事件:OP_ACCEPT
- 设置默认的Channel配置。

疑问

unsafe 是什么?

nterface Unsafe {
    SocketAddress localAddress();
    SocketAddress remoteAddress();
    void register(EventLoop eventLoop, ChannelPromise promise);
    void bind(SocketAddress localAddress, ChannelPromise promise);
    void connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise);
    void disconnect(ChannelPromise promise);
    void close(ChannelPromise promise);
    void closeForcibly();
    void deregister(ChannelPromise promise);
    void beginRead();
    void write(Object msg, ChannelPromise promise);
    void flush();
    ChannelPromise voidPromise();
    ChannelOutboundBuffer outboundBuffer();
}

可以看出,这些方法都是java底层的Socket的操作。
所以回到底层操作,都是需要用到这些方法。

pipeline 的初始化到底做了什么?

public DefaultChannelPipeline(AbstractChannel channel) {
    if (channel == null) {
        throw new NullPointerException("channel");
    }
    this.channel = channel;

    tail = new TailContext(this);
    head = new HeadContext(this);

    head.next = tail;
    tail.prev = head;
}
  • 首先是Pipeline与channel绑定在一次。也就是Pipeline里有和channel的参数,用来存储该实例。
  • headtail是双向链表的头和尾。因为Pipeline是一个双向链表。此时头和尾都是指向自己。

猜你喜欢

转载自blog.csdn.net/GG_and_DD/article/details/80416941