Netty源码分析(三)channel,创建channel,初始化channel,绑定端口,唤醒回调事件

本文深入分析了Netty中的Channel接口及其封装层次,解释了AttributeMap的作用和实现原理。此外,详细阐述了Netty启动时创建Channel的过程,包括创建ChannelFactory、反射创建服务端Channel、初始化Channel、注册到Selector以及端口绑定的步骤,同时讨论了事件传播机制。
摘要由CSDN通过智能技术生成

Channel

Netty的抽象了一个顶层接口Channel相比原来NIO提供的Channel有更多的功能,当然也是相对复杂的。

Java NIO包下的channel

请添加图片描述
请添加图片描述
请添加图片描述

JavaNIO包下的channel 做了三层封装
请添加图片描述

  1. 最顶层的Channel只封装了关和开两种状态
  2. 第二层SelectableChannel封装了和Selector交互的状态
  3. 第三层封装了具体的业务类

Netty的channel

  • 继承图
    请添加图片描述

做了四层封装

  1. 第一层channel
    请添加图片描述

    抽象了所有的channel的公共行为

  2. 第二层channel
    请添加图片描述

    规定了channel为Nio的抽象

  3. 第三层channel
    请添加图片描述
    读写层的抽象

  4. 第四层channel 具体业务的实现,比如serverSocketChannel

AttributeMap

一个线程安全的map,附在channel或者ChannelHandlerContext上 Channel上的AttributeMap就是大家共享的,每一个ChannelHandler都能获取到
请添加图片描述

MyDefaultAttribute.java
AttributeMap可以看成是一个key为AttributeKey类型,value为Attribute类型的Map,而Attribute内存储的是一个值引用,它可以原子的更新内容,是线程安全的 pool常量池存放了key

public final class MyAttributeKey<T> extends AbstractConstant<MyAttributeKey<T>> {
   

    private static final ConstantPool<MyAttributeKey<Object>> pool = new ConstantPool<MyAttributeKey<Object>>() {
   
        @Override
        protected MyAttributeKey<Object> newConstant(int id, String name) {
   
            return new MyAttributeKey<>(id, name);
        }
    };
}

AttributeMap接口的默认实现类是DefaultAttributeMap类,该类有一个内部类DefaultAttribute,实现了Attribute接口,其定义如下:

private static final class MyDefaultAttribute<T> extends AtomicReference<T> implements MyAttribute<T> {
   

    private static final long serialVersionUID = 4618134439190501308L;
    private static final AtomicReferenceFieldUpdater<MyDefaultAttribute, MyDefaultAttributeMap> MAP_UPDATER =
            AtomicReferenceFieldUpdater.newUpdater(MyDefaultAttribute.class,
                    MyDefaultAttributeMap.class, "attributeMap");
    private final MyAttributeKey<T> key;
    private volatile MyDefaultAttributeMap attributeMap;

    MyDefaultAttribute(MyDefaultAttributeMap attributeMap, MyAttributeKey<T> key) {
   
        this.attributeMap = attributeMap;
        this.key = key;
    }

    @Override
    public MyAttributeKey<T> key() {
   
        return key;
    }

    @Override
    public T setIfAbsent(T value) {
   
        while (!compareAndSet(null, value)) {
   
            T old = get();
            if (old != null) {
   
                return old;
            }
        }
        return null;
    }

    @Override
    public T getAndRemove() {
   
        MyDefaultAttributeMap attributeMap = this.attributeMap;
        boolean removed = attributeMap != null && MAP_UPDATER.compareAndSet(this, attributeMap, null);
        T oldValue = getAndSet(null);
        if (removed) {
   
            attributeMap.removeAttributeIfMatch(key, this);
        }

        return oldValue;
    }

    @Override
    public void remove() {
   
        final MyDefaultAttributeMap attributeMap = this.attributeMap;
        final boolean removed = attributeMap != null && MAP_UPDATER.compareAndSet(this, attributeMap, null);
        set(null);
        if (removed) {
   
            attributeMap.removeAttributeIfMatch(key, this);
        }
    }

    private boolean isRemoved() {
   
        return attributeMap == null;
    }
}

该类继承了AtomicReference类,这是Netty内部实现的一个原子引用类,因此DefaultAttribute是线程安全的。它是一个双向链表的节点,也就是所有的Attribute连接成了一个双向链表的存储结构。而DefaultAttribute对象被存放在了一个AtomicReferenceArray类型的数组结构的属性attributes里面,这是AttributeMap内部的核心存储结构。

Netty启动的时候怎么创建channel

创建channelfactory

ServerBootStrap.channel 返回一个serverbootstrap, 提交一个channel类

class Test {
   
    public B channel(Class<? extends C> channelClass) {
   
        return channelFactory(new ReflectiveChannelFactory<C>(
                ObjectUtil.checkNotNull(channelClass, "channelClass")
        ));
    }
}

反射加载: 获取到类的加载器,构建一个channelfactory返回到abstractBootStrap中

class Test {
   

    public ReflectiveChannelFactory(Class<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值