一、Channel
Channel意为通道,即某个实体传输的载体,在socket编程中可以理解为网络套接字的一个传输通道,可以在通道上进行读写操作,它是Java NIO和netty编程的重要组件之一。
从类结构可以看出,channel支持不同的编程模型具有不同的子接口(ServerChannel可以理解为ServerSocket,SocketChannel可以理解为Socket等等),并且提供了一个默认的抽象实现AbstractChannel。
二、Channel实例化
在配置启动的时候,会传入channel参数,具体的参数值,根据客户自身需求自行传入(NIO、OIO还是Epoll)。在前一节中的initAndRegister方法中第一件事就是实例化channel。channel的实例化是通过ChannelFactory工厂通过反射调用实例化,本节分析取NioServerSocketChannel来分析整个实例化过程,看看Channel实例化过程需要做些什么工作。
- NioServerSocketChannel的构造方法
SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
ChannelMetadata METADATA = new ChannelMetadata(false, 16);
ServerSocketChannelConfig config;
public NioServerSocketChannel() {
this(newSocket(DEFAULT_SELECTOR_PROVIDER));
}
构造方法中的newSocket(DEFAULT_SELECTOR_PROVIDER)是通过provider返回JDK底层的channel对象ServerSocketChannel。
public NioServerSocketChannel(ServerSocketChannel channel) {
super(null, channel, SelectionKey.OP_ACCEPT);//接着调用父类的构造方法
config = new NioServerSocketChannelConfig(this, javaChannel().socket());
//初始化配置类
}
NioServerSocketChannel的构造方法主要是实例化config对象,然后调用父类构造方法。
- AbstractNioMessageChannel
protected AbstractNioMessageChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
super(parent, ch, readInterestOp);
}
- AbstractNioChannel
protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
super(parent);
this.ch = ch;
this.readInterestOp = readInterestOp;//默认是accept
ch.configureBlocking(false);//因为NIO是非阻塞的所以必须设置为false
}
- AbstractChannel
protected AbstractChannel(Channel parent) {
this.parent = parent;
id = newId();//生成一个ChannelId,这个在低版本中是没有的
unsafe = newUnsafe();//实例化unsafe对象,方法的实现是子类中,具体返回new NioMessageUnsafe()对象
pipeline = newChannelPipeline();//实例化pipeline对象,前文分析中的pipeline对象就是这里生成的。返回的是new DefaultChannelPipeline(this)实例
}
前文中多处用到了pipeline这个属性,后边很多地方也会用到,那么这个pipeline具体是什么,有什么作用呢?源码来看pipeline的接口是ChannelPipeline,接口中的方法就是对链表的添加删除等操作。从实现类来看,类中维护着ChannelHandlerContext的一个双向链表,(ChannelHandlerContext顾名思义就是处理ChannelHandler的一个上下文,这里不做具体展开),添加的每个handler都会包装成一个DefaultChannelHandlerContext实现类,DefaultChannelPipeline中维护着表头和表尾。表头HeadContext继承自AbstractChannelHandlerContext实现了ChannelOutboundHandler, ChannelInboundHandler接口,表尾同样继承自AbstractChannelHandlerContext实现了ChannelInboundHandler(其中Inbound称为入站handler,Outbound称为出站)这里我们只需要知道pipeline中维护着一个ChannelHandlerContext的双向链表即可,至于有什么作用为什么这样,我们后续再一点点分析(还有一点就是每个channel中都会维护着这样一个pipeline)。
到这里NioServerSocketChannel的实例化过程就分析完成,总结来说就是初始化底层的channel对象,以及unsafe和pipeline属性,这些属性在后续的调用中都会经常遇到。
以上,有任何不对的地方,请指正,敬请谅解。