title | date | categories | tags | description | ||||
---|---|---|---|---|---|---|---|---|
17.Channel |
2020-03-15 |
|
|
|
channel文档说明
/**
* A nexus to a network socket or a component which is capable of I/O
* operations such as read, write, connect, and bind.
Channel是对于网络socket的一个连接点,或者可以理解为有读写操作的组件
* <p>
* A channel provides a user: 一个Channel 可以为用户提供:
* <ul>
* <li>the current state of the channel (e.g. is it open? is it connected?),</li>
当前channel的状态,是否打开,是否已经连接上
判断状态的方法: isopen isactive
* <li>the {@linkplain ChannelConfig configuration parameters} of the channel (e.g. receive buffer size),</li>
提供关于channel的配置参数。
* <li>the I/O operations that the channel supports (e.g. read, write, connect, and bind), and</li>
提供Channel 支持的IO操作,比如读写,连接等
* <li>the {@link ChannelPipeline} which handles all I/O events and requests
* associated with the channel.</li>
最后,还提供了一个pipeline,这个pipeline可以处理与当前channel关联的所有的事件和请求。
channel. pipeline 可以拿到channel关联的pipeline
* </ul>
*
* <h3>All I/O operations are asynchronous.</h3> 所有的IO操作都是异步的
* <p>
* All I/O operations in Netty are asynchronous. 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. Instead, you will be returned with
* a {@link ChannelFuture} instance which will notify you when the requested I/O
* operation has succeeded, failed, or canceled.
在Netty中,所有的IO操作都是异步的。
这意味着任何IO的调用都会立即返回,并且不保证所请求的I / O操作在调用结束时已完成。
反而,将返回一个{@link ChannelFuture}对象,该对象将在请求的I / O操作成功,失败或取消时通知你, 就是通过回调方法来通知的。
*
* <h3>Channels are hierarchical</h3>channel是可继承的
* <p>
* A {@link Channel} can have a {@linkplain #parent() parent} depending on
* how it was created. For instance, a {@link SocketChannel}, that was accepted
* by {@link ServerSocketChannel}, will return the {@link ServerSocketChannel}
* as its parent on {@link #parent()}.
channel能够有个parent,取决于它如何被创建的。比如,socketxhannel是通过serversocketchannel的accept方法来产生的,就认为socketxhannel的parent是serversocketchannel。 socketchannel.parent()方法就会返回ServerSocketChannel对象
* <p>
* The semantics of the hierarchical structure depends on the transport
* implementation where the {@link Channel} belongs to. For example, you could
* write a new {@link Channel} implementation that creates the sub-channels that
* share one socket connection, as <a href="http://beepcore.org/">BEEP</a> and
* <a href="http://en.wikipedia.org/wiki/Secure_Shell">SSH</a> do.
*
* <h3>Downcast to access transport-specific operations</h3>
* <p>
* Some transports exposes additional operations that is specific to the
* transport. Down-cast the {@link Channel} to sub-type to invoke such
* operations. For example, with the old I/O datagram transport, multicast
* join / leave operations are provided by {@link DatagramChannel}.
根据不同的传输,不同的协议,用的channel对象不同。比如udp的就用DatagramChannel
*
* <h3>Release resources</h3>释放资源
* <p>
* It is important to call {@link #close()} or {@link #close(ChannelPromise)} to release all
* resources once you are done with the {@link Channel}. This ensures all resources are
* released in a proper way, i.e. filehandles.
当你使用完Channel,记得调用 close 或者 close()去释放所有的资源。
这样可以确保以适当的方式(即文件句柄)释放所有资源。
*/
public interface Channel extends AttributeMap, ChannelOutboundInvoker, Comparable<Channel> {}
eventloop
注册到哪个事件循环上了。其实实质是注册到一个事件循环里面的selector上了。每个eventloop 里都有一个自己的selector对象
netty中所有的io都是异步的。因此所有的io操作都会立即返回,不保证所进行的io操作在返回的时候已经完成了。相反,在io操作返回的时候,拿到一个channel future对象,然后io操作的成功失败取消都会通过这个channel future通知你
channel是可继承的。channel能够有个parent,取决于它如何被创建的。比如,socketxhannel是通过serversocketchannel的accept方法来产生的,就认为socketxhannel的parent是serversocketchannel
根据不同的传输,不同的协议,用的channel对象不同。比如udp的