`

netty学习之Channel接口

阅读更多

    正如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有点类似,这个会在以后进行详细的说明。

 

public interface Channel extends Comparable<Channel> {

    /**
     * The {@link #getInterestOps() interestOps} value which tells that only
     * read operation has been suspended.
     */
    static int OP_NONE = 0;

    /**
     * The {@link #getInterestOps() interestOps} value which tells that neither
     * read nor write operation has been suspended.
     */
    static int OP_READ = 1;

    /**
     * The {@link #getInterestOps() interestOps} value which tells that both
     * read and write operation has been suspended.
     */
    static int OP_WRITE = 4;

    /**
     * The {@link #getInterestOps() interestOps} value which tells that only
     * write operation has been suspended.
     */
    static int OP_READ_WRITE = OP_READ | OP_WRITE;

    /**
     * Returns the unique integer ID of this channel.
     */
    Integer getId();

    /**
     * Returns the {@link ChannelFactory} which created this channel.
     */
    ChannelFactory getFactory();

    /**
     * Returns the parent of this channel.
     *
     * @return the parent channel.
     *         {@code null} if this channel does not have a parent channel.
     */
    Channel getParent();

    /**
     * Returns the configuration of this channel.
     */
    ChannelConfig getConfig();

    /**
     * Returns the {@link ChannelPipeline} which handles {@link ChannelEvent}s
     * associated with this channel.
     */
    ChannelPipeline getPipeline();

    /**
     * Returns {@code true} if and only if this channel is open.
     */
    boolean isOpen();

    /**
     * Returns {@code true} if and only if this channel is bound to a
     * {@linkplain #getLocalAddress() local address}.
     */
    boolean isBound();

    /**
     * Returns {@code true} if and only if this channel is connected to a
     * {@linkplain #getRemoteAddress() remote address}.
     */
    boolean isConnected();

    /**
     * Returns the local address where this channel is bound to.  The returned
     * {@link SocketAddress} is supposed to be down-cast into more concrete
     * type such as {@link InetSocketAddress} to retrieve the detailed
     * information.
     *
     * @return the local address of this channel.
     *         {@code null} if this channel is not bound.
     */
    SocketAddress getLocalAddress();

    /**
     * Returns the remote address where this channel is connected to.  The
     * returned {@link SocketAddress} is supposed to be down-cast into more
     * concrete type such as {@link InetSocketAddress} to retrieve the detailed
     * information.
     *
     * @return the remote address of this channel.
     *         {@code null} if this channel is not connected.
     *         If this channel is not connected but it can receive messages
     *         from arbitrary remote addresses (e.g. {@link DatagramChannel},
     *         use {@link MessageEvent#getRemoteAddress()} to determine
     *         the origination of the received message as this method will
     *         return {@code null}.
     */
    SocketAddress getRemoteAddress();

    /**
     * Sends a message to this channel asynchronously.    If this channel was
     * created by a connectionless transport (e.g. {@link DatagramChannel})
     * and is not connected yet, you have to call {@link #write(Object, SocketAddress)}
     * instead.  Otherwise, the write request will fail with
     * {@link NotYetConnectedException} and an {@code 'exceptionCaught'} event
     * will be triggered.
     *
     * @param message the message to write
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         write request succeeds or fails
     *
     * @throws NullPointerException if the specified message is {@code null}
     */
    ChannelFuture write(Object message);

    /**
     * Sends a message to this channel asynchronously.  It has an additional
     * parameter that allows a user to specify where to send the specified
     * message instead of this channel's current remote address.  If this
     * channel was created by a connectionless transport (e.g. {@link DatagramChannel})
     * and is not connected yet, you must specify non-null address.  Otherwise,
     * the write request will fail with {@link NotYetConnectedException} and
     * an {@code 'exceptionCaught'} event will be triggered.
     *
     * @param message       the message to write
     * @param remoteAddress where to send the specified message.
     *                      This method is identical to {@link #write(Object)}
     *                      if {@code null} is specified here.
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         write request succeeds or fails
     *
     * @throws NullPointerException if the specified message is {@code null}
     */
    ChannelFuture write(Object message, SocketAddress remoteAddress);

    /**
     * Binds this channel to the specified local address asynchronously.
     *
     * @param localAddress where to bind
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         bind request succeeds or fails
     *
     * @throws NullPointerException if the specified address is {@code null}
     */
    ChannelFuture bind(SocketAddress localAddress);

    /**
     * Connects this channel to the specified remote address asynchronously.
     *
     * @param remoteAddress where to connect
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         connection request succeeds or fails
     *
     * @throws NullPointerException if the specified address is {@code null}
     */
    ChannelFuture connect(SocketAddress remoteAddress);

    /**
     * Disconnects this channel from the current remote address asynchronously.
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         disconnection request succeeds or fails
     */
    ChannelFuture disconnect();

    /**
     * Unbinds this channel from the current local address asynchronously.
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         unbind request succeeds or fails
     */
    ChannelFuture unbind();

    /**
     * Closes this channel asynchronously.  If this channel is bound or
     * connected, it will be disconnected and unbound first.  Once a channel
     * is closed, it can not be open again.  Calling this method on a closed
     * channel has no effect.  Please note that this method always returns the
     * same future instance.
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         close request succeeds or fails
     */
    ChannelFuture close();

    /**
     * Returns the {@link ChannelFuture} which will be notified when this
     * channel is closed.  This method always returns the same future instance.
     */
    ChannelFuture getCloseFuture();

    /**
     * Returns the current {@code interestOps} of this channel.
     *
     * @return {@link #OP_NONE}, {@link #OP_READ}, {@link #OP_WRITE}, or
     *         {@link #OP_READ_WRITE}
     */
    int getInterestOps();

    /**
     * Returns {@code true} if and only if the I/O thread will read a message
     * from this channel.  This method is a shortcut to the following code:
     * <pre>
     * return (getInterestOps() & OP_READ) != 0;
     * </pre>
     */
    boolean isReadable();

    /**
     * Returns {@code true} if and only if the I/O thread will perform the
     * requested write operation immediately.  Any write requests made when
     * this method returns {@code false} are queued until the I/O thread is
     * ready to process the queued write requests.  This method is a shortcut
     * to the following code:
     * <pre>
     * return (getInterestOps() & OP_WRITE) == 0;
     * </pre>
     */
    boolean isWritable();

    /**
     * Changes the {@code interestOps} of this channel asynchronously.
     *
     * @param interestOps the new {@code interestOps}
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         {@code interestOps} change request succeeds or fails
     */
    ChannelFuture setInterestOps(int interestOps);

    /**
     * Suspends or resumes the read operation of the I/O thread asynchronously.
     * This method is a shortcut to the following code:
     * <pre>
     * int interestOps = getInterestOps();
     * if (readable) {
     *     setInterestOps(interestOps | OP_READ);
     * } else {
     *     setInterestOps(interestOps & ~OP_READ);
     * }
     * </pre>
     *
     * @param readable {@code true} to resume the read operation and
     *                 {@code false} to suspend the read operation
     *
     * @return the {@link ChannelFuture} which will be notified when the
     *         {@code interestOps} change request succeeds or fails
     */
    ChannelFuture setReadable(boolean readable);
}

 

 

分享到:
评论
2 楼 jchenzeke 2016-07-12  
      
1 楼 jchenzeke 2016-07-12  

相关推荐

    netty-socketio api接口文档.7z

    Netty-SocketIO API接口文档是为开发者提供关于如何使用Netty-SocketIO框架进行通信的一个详细指南。Netty是一个高性能、异步事件驱动的网络应用程序...同时,它也是进一步学习Netty和SocketIO底层原理的重要参考资料。

    netty学习之ServerChannel

    Netty学习之ServerChannel Netty是一个高性能、异步事件驱动的网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端。在本篇中,我们将深入探讨ServerChannel这一核心概念,它是Netty中用于接收客户端...

    netty官网学习手册中文版

    这个“netty官网学习手册中文版”针对的是Netty的3.1版本,虽然现在的Netty已经发展到了5.x版本,但3.1版本的知识仍然具有历史参考价值,特别是对于那些初次接触或需要理解Netty基础概念的开发者来说。 1. **Netty...

    netty5.0官方自带的demo

    Netty的强大之处在于它能够轻松地处理自定义协议。通过示例,我们可以学习如何定义自己的消息结构,创建相应的编解码器,并将其整合进Netty的Pipeline中。 10. **异常处理** Netty提供了全面的异常处理机制,通过...

    Netty 框架学习 —— 第一个 Netty 应用(csdn)————程序.pdf

    在本篇关于“Netty框架学习——第一个Netty应用”的文章中,我们将深入理解如何使用Netty构建一个简单的Echo服务器和客户端。Netty是一个高性能、异步事件驱动的网络应用程序框架,广泛应用于Java领域的服务器开发。...

    Netty权威指南PDF书籍

    - **高度可定制**:Netty 的 ChannelHandler 接口允许用户自定义数据处理逻辑。 3. **Netty 的核心组件** - **Bootstrap**:用于启动服务器或客户端的配置类。 - **Channel**:代表一个连接,可以读写数据。 - ...

    netty4.0源码,netty例子,netty api文档

    通过阅读Javadoc,开发者可以了解每个类、接口、方法的详细说明,是学习和调试代码的重要参考资料。 - **jar** 文件夹可能包含Netty的编译后的jar包,其中可能包括了Netty的核心库、示例代码以及其他依赖。这些jar...

    Netty4.0学习笔记系列之六:多种通讯协议支持

    在本篇Netty4.0学习笔记系列之六中,我们将深入探讨Netty框架如何支持多种通讯协议,以及它在实现高效、灵活的网络通信中的关键特性。Netty是一个高性能、异步事件驱动的网络应用框架,适用于开发服务器和客户端的...

    Netty初步学习-HelloNetty.rar

    每个 Channel 都有一个 ChannelHandler,用于处理与之相关的事件。 2. **Bootstrap**:Bootstrap 是启动类,用于配置和启动客户端或服务器。在服务端,我们通常创建一个 ServerBootstrap 实例,然后添加一个或多个 ...

    Netty+设计模式

    5. **装饰者模式(Decorator Pattern)**:Netty的ChannelHandlerContext可以看作是对原始Channel接口的装饰,增加了事件处理和管道管理的功能。 6. **状态机模式(State Pattern)**:Netty中的ChannelState可以视...

    netty学习笔记

    ### Netty学习笔记知识点概述 #### 一、Netty简介 Netty是一个广泛使用的高性能、异步事件驱动的网络应用程序框架,它简化了网络编程的复杂性,使得开发者能够更轻松地开发高性能、高可靠性的网络服务器和客户端。...

    netty实战教程、netty代码demo

    以下是对 Netty 的详细介绍以及如何通过代码示例进行学习。 一、Netty 框架基础 Netty 是由 JBoss 组织开源的一个网络通信框架,基于 Java NIO(非阻塞I/O)构建,提供了一套高度抽象和优化的 API,使得开发者可以...

    Java-NIO-Netty框架学习

    学习Netty,可以从基础的Socket编程开始,然后深入理解Java NIO的基本概念,接着熟悉Netty提供的各种组件和API,通过编写简单的服务端和客户端程序来实践。随着对Netty的理解加深,可以尝试实现更复杂的网络应用,如...

    netty学习资料(java源代码实例).zip

    这个“netty学习资料(java源代码实例).zip”压缩包包含的资源显然是为了帮助Java开发者深入理解并实践Netty框架。在Netty的学习过程中,源代码实例是极其宝贵的,因为它们能让你直观地看到如何在实际应用中运用Netty...

    Java学习之IO总结及mina和netty

    这篇博客“Java学习之IO总结及mina和netty”探讨了Java IO的基础知识,并深入到两个高级网络通信框架——Mina和Netty。Mina和Netty都是基于NIO(非阻塞IO)的高性能网络应用框架,它们简化了复杂网络编程的实现。 *...

    netty框架图及netty面试知识点解析

    在Netty中,I/O操作被封装在Channel接口中,而ChannelHandler则用于处理I/O事件和数据。事件循环(EventLoop)是Netty处理事件的关键组件,它负责调度和执行事件处理器。 "粘包与半包"是网络通信中常见的问题。当...

    Netty使用与实现.pdf

    - **EventLoop**:每个线程都拥有一个EventLoop,负责处理分配给该线程的Channel上的I/O操作。 #### 五、Netty的特点与优势 - **高性能**:利用NIO技术实现了高效的I/O处理。 - **灵活性**:支持多种传输层协议(如...

    最新netty中文文档chm版

    这个“最新Netty中文文档CHM版”为中国的开发者提供了一个方便的中文学习资源,解决了阅读英文原版文档时的语言障碍,使学习过程更为轻松。 Netty 的核心特性包括: 1. **高性能**: Netty 使用了Java NIO(非阻塞I...

    netty4 api 中文

    ChannelHandler是Netty的核心组件之一,它定义了处理这些事件的接口,如读取、写入、连接和关闭事件。 Netty 4.1引入了许多增强和改进,包括: 1. **ByteBuf**:Netty的缓冲区机制,它比Java的ByteBuffer更强大,...

Global site tag (gtag.js) - Google Analytics