netty学习之Channel接口

来源:互联网 发布:java工程师 职业寿命 编辑:程序博客网 时间:2024/05/16 18:59

正如javadoc所说的,一个channel给用户提供下面四个服务:
1. 当前channel的状态,是open还是connected
2. 这个channel的一些配置信息
3. 这个channel所支持的一些io操作
4. 和这个channel相关联的ChannelPipeline
Channel中所有的操作都是异步的,当发生io操作的时候将会返回一个
ChannelFutrue的接口,在这个里面可以处理操作成功、失败、取消后的动作。
在netty里面,随着Channel的创建者的不同可以分成不同的一些channel,比如它可以被ServerSocketChannel的accept之后,返回一个SocketChannel,这个SocketChannel的parent就是ServerSocketChannel了,在netty里面应该有三类Channel,这个会在以后详细讲解。
Channel里面的interestOps的和NIO里面的SelectionKey有点类似,这个会在以后进行详细的说明。

 

Java代码  收藏代码
  1. public interface Channel extends Comparable<Channel> {  
  2.   
  3.     /** 
  4.      * The {@link #getInterestOps() interestOps} value which tells that only 
  5.      * read operation has been suspended. 
  6.      */  
  7.     static int OP_NONE = 0;  
  8.   
  9.     /** 
  10.      * The {@link #getInterestOps() interestOps} value which tells that neither 
  11.      * read nor write operation has been suspended. 
  12.      */  
  13.     static int OP_READ = 1;  
  14.   
  15.     /** 
  16.      * The {@link #getInterestOps() interestOps} value which tells that both 
  17.      * read and write operation has been suspended. 
  18.      */  
  19.     static int OP_WRITE = 4;  
  20.   
  21.     /** 
  22.      * The {@link #getInterestOps() interestOps} value which tells that only 
  23.      * write operation has been suspended. 
  24.      */  
  25.     static int OP_READ_WRITE = OP_READ | OP_WRITE;  
  26.   
  27.     /** 
  28.      * Returns the unique integer ID of this channel. 
  29.      */  
  30.     Integer getId();  
  31.   
  32.     /** 
  33.      * Returns the {@link ChannelFactory} which created this channel. 
  34.      */  
  35.     ChannelFactory getFactory();  
  36.   
  37.     /** 
  38.      * Returns the parent of this channel. 
  39.      * 
  40.      * @return the parent channel. 
  41.      *         {@code null} if this channel does not have a parent channel. 
  42.      */  
  43.     Channel getParent();  
  44.   
  45.     /** 
  46.      * Returns the configuration of this channel. 
  47.      */  
  48.     ChannelConfig getConfig();  
  49.   
  50.     /** 
  51.      * Returns the {@link ChannelPipeline} which handles {@link ChannelEvent}s 
  52.      * associated with this channel. 
  53.      */  
  54.     ChannelPipeline getPipeline();  
  55.   
  56.     /** 
  57.      * Returns {@code true} if and only if this channel is open. 
  58.      */  
  59.     boolean isOpen();  
  60.   
  61.     /** 
  62.      * Returns {@code true} if and only if this channel is bound to a 
  63.      * {@linkplain #getLocalAddress() local address}. 
  64.      */  
  65.     boolean isBound();  
  66.   
  67.     /** 
  68.      * Returns {@code true} if and only if this channel is connected to a 
  69.      * {@linkplain #getRemoteAddress() remote address}. 
  70.      */  
  71.     boolean isConnected();  
  72.   
  73.     /** 
  74.      * Returns the local address where this channel is bound to.  The returned 
  75.      * {@link SocketAddress} is supposed to be down-cast into more concrete 
  76.      * type such as {@link InetSocketAddress} to retrieve the detailed 
  77.      * information. 
  78.      * 
  79.      * @return the local address of this channel. 
  80.      *         {@code null} if this channel is not bound. 
  81.      */  
  82.     SocketAddress getLocalAddress();  
  83.   
  84.     /** 
  85.      * Returns the remote address where this channel is connected to.  The 
  86.      * returned {@link SocketAddress} is supposed to be down-cast into more 
  87.      * concrete type such as {@link InetSocketAddress} to retrieve the detailed 
  88.      * information. 
  89.      * 
  90.      * @return the remote address of this channel. 
  91.      *         {@code null} if this channel is not connected. 
  92.      *         If this channel is not connected but it can receive messages 
  93.      *         from arbitrary remote addresses (e.g. {@link DatagramChannel}, 
  94.      *         use {@link MessageEvent#getRemoteAddress()} to determine 
  95.      *         the origination of the received message as this method will 
  96.      *         return {@code null}. 
  97.      */  
  98.     SocketAddress getRemoteAddress();  
  99.   
  100.     /** 
  101.      * Sends a message to this channel asynchronously.    If this channel was 
  102.      * created by a connectionless transport (e.g. {@link DatagramChannel}) 
  103.      * and is not connected yet, you have to call {@link #write(Object, SocketAddress)} 
  104.      * instead.  Otherwise, the write request will fail with 
  105.      * {@link NotYetConnectedException} and an {@code 'exceptionCaught'} event 
  106.      * will be triggered. 
  107.      * 
  108.      * @param message the message to write 
  109.      * 
  110.      * @return the {@link ChannelFuture} which will be notified when the 
  111.      *         write request succeeds or fails 
  112.      * 
  113.      * @throws NullPointerException if the specified message is {@code null} 
  114.      */  
  115.     ChannelFuture write(Object message);  
  116.   
  117.     /** 
  118.      * Sends a message to this channel asynchronously.  It has an additional 
  119.      * parameter that allows a user to specify where to send the specified 
  120.      * message instead of this channel's current remote address.  If this 
  121.      * channel was created by a connectionless transport (e.g. {@link DatagramChannel}) 
  122.      * and is not connected yet, you must specify non-null address.  Otherwise, 
  123.      * the write request will fail with {@link NotYetConnectedException} and 
  124.      * an {@code 'exceptionCaught'} event will be triggered. 
  125.      * 
  126.      * @param message       the message to write 
  127.      * @param remoteAddress where to send the specified message. 
  128.      *                      This method is identical to {@link #write(Object)} 
  129.      *                      if {@code null} is specified here. 
  130.      * 
  131.      * @return the {@link ChannelFuture} which will be notified when the 
  132.      *         write request succeeds or fails 
  133.      * 
  134.      * @throws NullPointerException if the specified message is {@code null} 
  135.      */  
  136.     ChannelFuture write(Object message, SocketAddress remoteAddress);  
  137.   
  138.     /** 
  139.      * Binds this channel to the specified local address asynchronously. 
  140.      * 
  141.      * @param localAddress where to bind 
  142.      * 
  143.      * @return the {@link ChannelFuture} which will be notified when the 
  144.      *         bind request succeeds or fails 
  145.      * 
  146.      * @throws NullPointerException if the specified address is {@code null} 
  147.      */  
  148.     ChannelFuture bind(SocketAddress localAddress);  
  149.   
  150.     /** 
  151.      * Connects this channel to the specified remote address asynchronously. 
  152.      * 
  153.      * @param remoteAddress where to connect 
  154.      * 
  155.      * @return the {@link ChannelFuture} which will be notified when the 
  156.      *         connection request succeeds or fails 
  157.      * 
  158.      * @throws NullPointerException if the specified address is {@code null} 
  159.      */  
  160.     ChannelFuture connect(SocketAddress remoteAddress);  
  161.   
  162.     /** 
  163.      * Disconnects this channel from the current remote address asynchronously. 
  164.      * 
  165.      * @return the {@link ChannelFuture} which will be notified when the 
  166.      *         disconnection request succeeds or fails 
  167.      */  
  168.     ChannelFuture disconnect();  
  169.   
  170.     /** 
  171.      * Unbinds this channel from the current local address asynchronously. 
  172.      * 
  173.      * @return the {@link ChannelFuture} which will be notified when the 
  174.      *         unbind request succeeds or fails 
  175.      */  
  176.     ChannelFuture unbind();  
  177.   
  178.     /** 
  179.      * Closes this channel asynchronously.  If this channel is bound or 
  180.      * connected, it will be disconnected and unbound first.  Once a channel 
  181.      * is closed, it can not be open again.  Calling this method on a closed 
  182.      * channel has no effect.  Please note that this method always returns the 
  183.      * same future instance. 
  184.      * 
  185.      * @return the {@link ChannelFuture} which will be notified when the 
  186.      *         close request succeeds or fails 
  187.      */  
  188.     ChannelFuture close();  
  189.   
  190.     /** 
  191.      * Returns the {@link ChannelFuture} which will be notified when this 
  192.      * channel is closed.  This method always returns the same future instance. 
  193.      */  
  194.     ChannelFuture getCloseFuture();  
  195.   
  196.     /** 
  197.      * Returns the current {@code interestOps} of this channel. 
  198.      * 
  199.      * @return {@link #OP_NONE}, {@link #OP_READ}, {@link #OP_WRITE}, or 
  200.      *         {@link #OP_READ_WRITE} 
  201.      */  
  202.     int getInterestOps();  
  203.   
  204.     /** 
  205.      * Returns {@code true} if and only if the I/O thread will read a message 
  206.      * from this channel.  This method is a shortcut to the following code: 
  207.      * <pre> 
  208.      * return (getInterestOps() & OP_READ) != 0; 
  209.      * </pre> 
  210.      */  
  211.     boolean isReadable();  
  212.   
  213.     /** 
  214.      * Returns {@code true} if and only if the I/O thread will perform the 
  215.      * requested write operation immediately.  Any write requests made when 
  216.      * this method returns {@code false} are queued until the I/O thread is 
  217.      * ready to process the queued write requests.  This method is a shortcut 
  218.      * to the following code: 
  219.      * <pre> 
  220.      * return (getInterestOps() & OP_WRITE) == 0; 
  221.      * </pre> 
  222.      */  
  223.     boolean isWritable();  
  224.   
  225.     /** 
  226.      * Changes the {@code interestOps} of this channel asynchronously. 
  227.      * 
  228.      * @param interestOps the new {@code interestOps} 
  229.      * 
  230.      * @return the {@link ChannelFuture} which will be notified when the 
  231.      *         {@code interestOps} change request succeeds or fails 
  232.      */  
  233.     ChannelFuture setInterestOps(int interestOps);  
  234.   
  235.     /** 
  236.      * Suspends or resumes the read operation of the I/O thread asynchronously. 
  237.      * This method is a shortcut to the following code: 
  238.      * <pre> 
  239.      * int interestOps = getInterestOps(); 
  240.      * if (readable) { 
  241.      *     setInterestOps(interestOps | OP_READ); 
  242.      * } else { 
  243.      *     setInterestOps(interestOps & ~OP_READ); 
  244.      * } 
  245.      * </pre> 
  246.      * 
  247.      * @param readable {@code true} to resume the read operation and 
  248.      *                 {@code false} to suspend the read operation 
  249.      * 
  250.      * @return the {@link ChannelFuture} which will be notified when the 
  251.      *         {@code interestOps} change request succeeds or fails 
  252.      */  
  253.     ChannelFuture setReadable(boolean readable);  
  254. }  

 

 

0 0
原创粉丝点击