zoukankan      html  css  js  c++  java
  • Netty 源码 Channel(一)概述

    Netty 源码 Channel(一)概述

    Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html)

    相关文章:

    Channel 为 Netty 网络操作抽象类,EventLoop 主要是为 Channel 处理 I/O 操作,两者配合参与 I/O 操作。

    Unsafe 是个内部接口,聚合在 Channel 中协助进行网络读写相关的操作。

    1. Channel 功能

    Channel 的功能主要功能如下:

    1. 网络 IO 功能:如 read、write、connect、bind、config、isActive、isOpen 等
    2. 其它:如 eventLoop(绑定的线程)、metadata(获取 TCP 配置参数)、parent(SocketChannel 的 parent 是 ServerSocketChannel)、id(唯一标识符)

    2. Channel 创建

    以 NioServerSocketChannel 创建过程为例。

    Channel创建

    // 创建 NIO 底层的 ServerSocketChannel 对象
    public NioServerSocketChannel() {
        this(newSocket(DEFAULT_SELECTOR_PROVIDER));
    }
    // NioServerSocketChannel 需要注册 OP_ACCEPT 事件
    public NioServerSocketChannel(ServerSocketChannel channel) {
        super(null, channel, SelectionKey.OP_ACCEPT);
        config = new NioServerSocketChannelConfig(this, javaChannel().socket());
    }
    
    // 设置成非阻塞模式,并注册感兴趣的事件
    protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
        super(parent);
        this.ch = ch;
        this.readInterestOp = readInterestOp;
        ch.configureBlocking(false);
    }
    
    // 创建 channel 是创建对应的 pipeline
    protected AbstractChannel(Channel parent) {
        this.parent = parent;
        id = newId();
        unsafe = newUnsafe();
        pipeline = newChannelPipeline();
    }
    

    NioServerSocketChannel 创建过程主要干了两件事,其配置信息保存在 NioServerSocketChannelConfig 中:

    1. 创建 JDK 底层的 ServerSocketChannel 对象
    2. 创建 ChannelPipeline

    每天用心记录一点点。内容也许不重要,但习惯很重要!

  • 相关阅读:
    Prototype
    Builder Pattern
    Chain of Responsibility
    Flyweight
    HBase概念学习(九)HTablePool为何弃用?
    用web查看hadoop运行状态
    Hadoop的位置
    SQLServer的TDE加密
    Log4Net advanced pattern tips
    Forrest Gump
  • 原文地址:https://www.cnblogs.com/binarylei/p/10140143.html
Copyright © 2011-2022 走看看