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讀寫操做都是由它來完成。
相關文章
相關標籤/搜索