netty学习之ServerChannel

首先我们先来看serverchannel的类继承层次,然后再一个一个的研究。




   现在我们来挨个的介绍各个类。

    AbstractChannel应该是整个Channel子类的父类,这个里面有一个static的变量allChannels,这个里面放着所有的channel,现在还不能体会放这个得好处。里面感觉没有什么特殊的,allocateId这个方法应该是给channel分配一个id,System.identityHashCode这个方法就是根据对象的内存地址来生成hashCode的,putIfAbsent这个方法是CurrentHashMap的方法,就是如果没有就put,如果存在就返回,这个名字起的挺好的。

         我们再来看它的局部变量:

 

private final Integer id;
    private final Channel parent;
    private final ChannelFactory factory;
    private final ChannelPipeline pipeline;
    private final ChannelFuture succeededFuture = new SucceededChannelFuture(this);
    private final ChannelCloseFuture closeFuture = new ChannelCloseFuture();
    private volatile int interestOps = OP_READ;

    /** Cache for the string representation of this channel */
    private boolean strValConnected;
    private String strVal;

 这个里面没有什么特殊的,不过从局部变量来看,可以看到一个channel的整个职责,一个id,一个parent,和一个创建它的ChannelFactory,和这个Channel相对应的ChannelPipeline,和相应的success future和closefuther,后面的两个变量是这个channel的名称,相对来说这个类还是比较好理解的,以后还会对整个类层次的意图进行讲解的。

   AbstractServerChannel应该是整个ServerChannel的抽象父类了,也感觉没什么,就不多讲解了。

        LocalServerChannel是也是一个接口,是用来进行local 传输的,这个比较简单。

          NioServerSocketChannel和OioServerSocketChannel这两个类应该是我们平时用的最多的类,这两个类封装了在nio和bio下面创建ServerSocket的不同,总之是比较简单的。