4、Channel簡介
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讀寫操作都是由它來完成。
4、Channel簡介