3.一、Channel简介

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操做,如读、写、链接和绑定。】
promise

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事件】

一、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操做成功、失败、取消时通知你异步

二、Channels are hierarchical.【Channel是有 等级/层次 的】

A Channel can have a parent depending on how it was created.【谁建立了它,谁就是它的父channel】socket

For instance, a SocketChannel, that was accepted by ServerSocketChannel, will return the ServerSocketChannel as its parent on parent().【例如,ServerSocketChannel经过accept()方法接受一个SocketChannel后,调用SocketChannel的 parent()会返回 ServerSocketChannel对象async

The semantics of the hierarchical structure depends on the transport implementation where the Channel belongs to. 层次结构的语义取决于通道所属的Channel实现ide

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】工具

三、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得到这些操做

四、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,
     * 一、触发 ChannelInboundHandler#channelRead(ChannelHandlerContext, Object)事件,在有数据的状况下
     * 二、触发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读写操做都是由它来完成。
相关文章
相关标签/搜索