zoukankan      html  css  js  c++  java
  • 通道Channel

    Channel是Netty网络通信的主体,由它负责同对端进行网络通信、注册和数据操作等功能。

    AbstractChannel是Channel的一个抽象类。

    1) 通道状态主要包括:打开、关闭、连接
    2) 通道主要的IO操作,读(read)、写(write)、连接(connect)、绑定(bind)。
    3) 所有的IO操作都是异步的,调用诸如read,write方法后,并不保证IO操作完成,但会返回一个凭证,在IO操作成功,取消或失败后会记录在该凭证中。
    4) channel有父子关系,SocketChannel是通过ServerSocketChannel接受创建的,故SocketChannel的parent()方法返回的就是ServerSocketChannel。
    5) 在Channel使用完毕后,请调用close方法,释放通道占用的资源。

    //返回全局唯一的channel id
        ChannelId id();
        //返回该Channel注册的线程模型,先理解为Ractor模型的Ractor线程。
        EventLoop eventLoop();
        //返回该Channel由谁创建的,ServerSocketChannel返回null,SocketChannel返回创建它的ServerSocketChannel
        Channel parent();
        //返回通道的配置信息
        ChannelConfig config();
        //通道是否打开
        boolean isOpen();
        //该通道是否已经注册在事件模型中,此处先参考Nio编程模型,一个通过需要注册在Register上
        boolean isRegistered();
        //通道是否激活
        boolean isActive();
        //通道是否支持 调用disconnect方法后,调用connect方法
        ChannelMetadata metadata();
        //返回绑定的地址,服务端的Channel返回监听的地址,而客户端的Channel返回连接到服务端的本地套接字。
        SocketAddress localAddress();
        //返回channel的远程套接字。
        SocketAddress remoteAddress();
        //通道的关闭凭证(许可),这里是多线程编程一种典型的设计模式,一个channle返回一个固定的
        ChannelFuture closeFuture();
        //是否可写,如果通道的写缓冲区未满,即返回true,表示写操作可以立即操作缓冲区,然后返回。
        boolean isWritable();
        Unsafe unsafe();
        //返回管道
        ChannelPipeline pipeline();
        //返回ByteBuf内存分配器
        ByteBufAllocator alloc();
        //诸如newPromise,newSuccessedFuture()方法,就是返回一个凭证,用来保存通知结果的
        ChannelPromise newPromise();
        ChannelProgressivePromise newProgressivePromise();
        ChannelFuture newSucceededFuture();
        ChannelFuture newFailedFuture(Throwable cause);
        ChannelPromise voidPromise();
        //绑定
        ChannelFuture bind(SocketAddress localAddress);
        //连接
        ChannelFuture connect(SocketAddress remoteAddress);
        ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress);
        //断开连接
        ChannelFuture disconnect();
        //关闭,释放通道资源
        ChannelFuture close();
        ChannelFuture deregister();
        ChannelFuture bind(SocketAddress localAddress, ChannelPromise promise);
        ChannelFuture connect(SocketAddress remoteAddress, ChannelPromise promise);
        ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress, ChannelPromise promise);
        ChannelFuture disconnect(ChannelPromise promise);
        ChannelFuture close(ChannelPromise promise);
        ChannelFuture deregister(ChannelPromise promise);
        Channel read();
        ChannelFuture write(Object msg);
        ChannelFuture write(Object msg, ChannelPromise promise);
        Channel flush();
        ChannelFuture writeAndFlush(Object msg, ChannelPromise promise);
        ChannelFuture writeAndFlush(Object msg);
        interface Unsafe {
            RecvByteBufAllocator.Handle recvBufAllocHandle();
            ChannelHandlerInvoker invoker();
            SocketAddress localAddress();
            SocketAddress remoteAddress();
            void register(EventLoop eventLoop, 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 deregister(ChannelPromise promise);
            void beginRead();
            void write(Object msg, ChannelPromise promise); 
            void flush();
            ChannelPromise voidPromise();
            //返回通道的环形缓存区
            ChannelOutboundBuffer outboundBuffer();
        }

    Channel类图:

  • 相关阅读:
    Atitit 趋势管理之道 attilax著
    Atitit 循环处理的新特性 for...else...
    Atitit 2017年的技术趋势与未来的大技术趋势
    atitit 用什么样的维度看问题.docx 如何了解 看待xxx
    atitit prj mnrs 项目中的几种经理角色.docx
    Atitit IT办公场所以及度假村以及网点以及租房点建设之道 attilax总结
    Atitit 工具选型的因素与方法 attilax总结
    Atitit.团队文化建设影响组织的的一些原理 法则 定理 效应 p826.v4
    Atiitt 管理方面的误区总结 attilax总结
    Atitit 未来趋势把控的书籍 attilax总结 v3
  • 原文地址:https://www.cnblogs.com/myitnews/p/12213637.html
Copyright © 2011-2022 走看看