程序员

netty源码分析(14)- channel代码架构总结

2019-02-25  本文已影响10人  Jorgezhong

上一节学习客户端channel的创建过程。至此,客户端和服务端的创建过程我们都学习了一般。
本节总结一下channel的分类。经过简化后得如下UML

channel分类
    private final Channel parent;
    private final ChannelId id;
    private final Unsafe unsafe;
    private final DefaultChannelPipeline pipeline;
    private final VoidChannelPromise unsafeVoidPromise = new VoidChannelPromise(this, false);
    private final CloseFuture closeFuture = new CloseFuture(this);

    private volatile SocketAddress localAddress;
    private volatile SocketAddress remoteAddress;
    private volatile EventLoop eventLoop;
    private volatile boolean registered;
    private boolean closeInitiated;

    /** Cache for the string representation of this channel */
    private boolean strValActive;
    private String strVal;
    private final SelectableChannel ch;
    protected final int readInterestOp;
    volatile SelectionKey selectionKey;
    boolean readPending;
  1. 回忆分别创建客户端和服务端的过程,它们都想父类注册了读事件,但是其含义并非以言个,对于客户端来书,读指的是IO读写。而对于服务端来说读指的是检测新连接接入。
    protected AbstractNioByteChannel(Channel parent, SelectableChannel ch) {
        //传入感兴趣的读事件:客户端channel的读事件
        super(parent, ch, SelectionKey.OP_READ);
    }
    public NioServerSocketChannel(ServerSocketChannel channel) {
        //传入感兴趣的读事件:服务端channel的读事件(accept)
        super(null, channel, SelectionKey.OP_ACCEPT);
        config = new NioServerSocketChannelConfig(this, javaChannel().socket());
    }
  1. 对于实际上不同的读实现。则交给了分别的内部类NioMessageUnsafeNioByteUnsafe来实现。因此服务端和客户但分别持有的unsalf则是这两个类的实例,实现了具体的读写协议。

  2. 服务端NioServerSocketChannel和客户端NioSocketChannel都持有config,分别是:NioServerSocketChannelConfigNioSocketChannelConfig,主要存储每种channel底层的一个配置。对应着channel的架构,config的架构也是有一些公共的配置以及配置的获取和设置被抽象出来,如下图UML所示。

config
上一篇 下一篇

猜你喜欢

热点阅读