Channel全名是 io.netty.channel.Channel
是netty通信的载体,是netty网络操作的抽象接口,包含了JDK提供的Channel的功能,还额外聚合了一组功能。
Chnanel包含的东西相当庞杂,这里只做一个简介,当一回源码的搬运工。
Channel 源码上的说明:(英语战五渣,全靠翻译工具)
io.netty.channel.Channel
A nexus to a network socket or a component which is capable of I/O operations such as read, write, connect, and bind.
【连接到网络socket或组件的连接,它能够进行I/O操作,如读、写、连接和绑定。】
A channel provides a user: 【一个channel 能提供:】
- the current state of the channel (e.g. is it open? is it connected?), 【channel当前的状态,例如 是否开启,是否连接】
- the configuration parameters of the channel (e.g. receive buffer size), 【channel的配置数据,例如 接收的buffer长度】
- the I/O operations that the channel supports (e.g. read, write, connect, and bind), 【channel支持的I/O操作,例如,读、写、连接、绑定】
- the ChannelPipeline which handles all I/O events and requests associated with the channel.【获取关联的ChannelPipeline,使用上面注册的handler处理所有的I/O事件】
1、All I/O operations are asynchronous. 【所有的 I/O 操作都是异步的】
All I/O operations in Netty are asynchronous. 【所有的 I/O 操作在netty中都是异步的】
It means any I/O calls will return immediately with no guarantee that the requested I/O operation has been completed at the end of the call. 【这意味着任何I/O调用都会立即返回,不能保证请求的I/O操作在调用结束时完成】
Instead, you will be returned with a ChannelFuture instance which will notify you when the requested I/O operation has succeeded, failed, or canceled.【相反,会返回一个ChannelFuture 对象,它会在I/O操作成功、失败、取消时通知你】
2、Channels are hierarchical.【Channel是有 等级/层次 的】
A Channel can have a parent depending on how it was created.【谁创建了它,谁就是它的父channel】
For instance, a SocketChannel, that was accepted by ServerSocketChannel, will return the ServerSocketChannel as its parent on parent().【例如,ServerSocketChannel通过accept()方法接受一个SocketChannel后,调用SocketChannel的 parent()会返回 ServerSocketChannel对象】
The semantics of the hierarchical structure depends on the transport implementation where the Channel belongs to. 【层次结构的语义取决于通道所属的Channel实现】
For example, you could write a new Channel implementation that creates the sub-channels that share one socket connection, as BEEP and SSH do.【例如,你可以编写一个新的Channel实现,它创建一个子Channel,它与父Channel共享一个socket的内存,例如BEEP和 SSH do】
3、Downcast to access transport-specific operations.【向下转型获得子类的特殊操作】
Some transports exposes additional operations that is specific to the transport. 【某些子类传输会提供一些特定的操作】
Down-cast the Channel to sub-type to invoke such operations.【向下转型成子类传输以获得这些操作】
For example, with the old I/O datagram transport, multicast join / leave operations are provided by DatagramChannel.【例如,UDP传输有特定的 jion() 和 leave() 操作,可以向下转型成 DatagramChannel获得这些操作】
4、Release resources.【释放资源】
It is important to call close() or close(ChannelPromise) to release all resources once you are done with the Channel. 【一旦你使用完Channel,必须调用 close() 或 close(ChannelPromise)方法释放一些重要的资源】
This ensures all resources are released in a proper way, i.e. filehandles.【确保这些资源以一个适当的方式释放,比如文件句柄】
1 public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparable<Channel>
继承了三个接口
- AttributeMap :简单理解为一个存放属性的map
- Comparable :表明Channel是可以比较的
- ChannelOutboundInvoker :网络通信
ChannelOutboundInvoker
public interface ChannelOutboundInvoker { //绑定本地地址 ChannelFuture bind(SocketAddress localAddress); ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise); //连接远程地址 ChannelFuture connect(SocketAddress remoteAddress); ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise); ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress); ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise); //解除连接 ChannelFuture disconnect(); ChannelFuture disconnect(ChannelPromise promise); //关闭Channel ChannelFuture close(); ChannelFuture close(ChannelPromise promise); //与EventLoop解除注册 ChannelFuture deregister(); ChannelFuture deregister(ChannelPromise promise); /** * Request to Read data from the {@link Channel} into the first inbound buffer, * triggers an {@link ChannelInboundHandler#channelRead(ChannelHandlerContext, Object)} event if data was read, * and triggers a {@link ChannelInboundHandler#channelReadComplete(ChannelHandlerContext) channelReadComplete} event so the * handler can decide to continue reading. If there‘s a pending read operation already, this method does nothing. * This will result in having the * {@link ChannelOutboundHandler#read(ChannelHandlerContext)} * method called of the next {@link ChannelOutboundHandler} contained in the {@link ChannelPipeline} of the * {@link Channel}. */ /** * 从channel中读取数据到第一个 InboundBuffer, * 1、触发 ChannelInboundHandler#channelRead(ChannelHandlerContext, Object)事件,在有数据的情况下 * 2、触发ChannelInboundHandler#channelReadComplete(ChannelHandlerContext)事件,handler继续读取 * 如果有read操作已经挂起,则不执行任何操作 * 实际调用:Channel——>ChannelPipeline——>ChannelOutboundHandler——>ChannelOutboundHandler#read(ChannelHandlerContext)——>read() * ChannelHandlerContext继承了ChannelOutboundInvoker,它的子类实现read()方法,最后ctx.read()。 * */ ChannelOutboundInvoker read(); //写 ChannelFuture write(Object msg); ChannelFuture write(Object msg, ChannelPromise promise); //将数据冲刷到Channel ChannelOutboundInvoker flush(); //write + flush ChannelFuture writeAndFlush(Object msg); ChannelFuture writeAndFlush(Object msg, ChannelPromise promise); ChannelPromise newPromise(); ChannelProgressivePromise newProgressivePromise(); ChannelFuture newSucceededFuture(); ChannelFuture newFailedFuture(Throwable cause); ChannelPromise voidPromise(); }
本人英语渣渣,源码中每个方法都有注释,但是每个方法中的this will result... 这一段让我困惑,捣鼓了好久才明白它的含义并写在代码上,其他方法的翻译也可以按照这个格式。
ChannelOutboundInvoker主要是定义一些 I/O的操作,扩展在Channel接口中。
Channel
public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparable<Channel> { //get属性 ChannelId id(); //获得一个唯一的channelId EventLoop eventLoop();//获得关联的EventLoop Channel parent();//父Channel ChannelConfig config();//获得配置参数 ChannelMetadata metadata();//获得元数据 SocketAddress localAddress();//获得本地地址 SocketAddress remoteAddress();//获得远端地址 ChannelFuture closeFuture();//获得Channel关闭时的异步结果 ChannelPipeline pipeline();//获得事件管道,用于处理IO事件 ByteBufAllocator alloc();//获得字节缓存分配器 Unsafe unsafe();//获得Unsafe对象 //状态查询 boolean isOpen();//是否开放 boolean isRegistered();// 是否注册到一个EventLoop boolean isActive();// 是否激活 boolean isWritable();// 是否可写 long bytesBeforeUnwritable(); long bytesBeforeWritable(); @Override Channel read(); @Override Channel flush(); }
Unsafe
public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparable<Channel> { .... interface Unsafe { RecvByteBufAllocator.Handle recvBufAllocHandle();//当接受数据时返回它,用于分配ByteBuf SocketAddress localAddress(); SocketAddress remoteAddress(); void register(EventLoop eventLoop, ChannelPromise promise); void deregister(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 beginRead(); void write(Object msg, ChannelPromise promise); void flush(); ChannelPromise voidPromise();//返回一个特殊的可重用的ChannelPromise,它仅作为一个容器不用于操作成功或失败的通知器 ChannelOutboundBuffer outboundBuffer();//返回消息发送缓冲区 } }
unsafe ? 不安全的? 一开始我是懵逼的,看了《netty权威指南》才知道这个不安全是相对于程序员而言的,不应该直接被程序员调用。它的方法与ChannelOutboundInvoker中的方法大部分重叠,实际上Channel的I/O读写操作都是由它来完成。