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 能提供:】緩存
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操做成功、失敗、取消時通知你】異步
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】工具
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得到這些操做】
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>
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(); }
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(); }
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();//返回消息發送緩衝區 } }