Netty---编解码(原理)

1.ByteToMessageDecoder

    用于将ByteBuf解码成为POJO对象

重要字段:

ByteBuf cumulation;     //缓存
private Cumulator cumulator = MERGE_CUMULATOR; //累计器
private boolean singleDecode;  
private boolean first; //是否第一次解码
private boolean firedChannelRead;
//状态码
private byte decodeState = STATE_INIT;
private int discardAfterReads = 16; //解码次数阈值,用来删除已读数据
private int numReads; //解码次数

介绍一下累计器:Cumulator类是干什么的

它的本类中的内部类,而且还是一个接口,只提供了方法。它的实现,只有匿名类,所以就是开头的静态两个字段了。

public interface Cumulator {
    ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in);
}

也就是我们默认使用的cumulator->MEGRE_CUMULATOR,我们看看它是如何实现的cumulator接口

public static final Cumulator MERGE_CUMULATOR = new Cumulator() {
    //参数:ByteBuf的分配器,本类中的ByteBuf,传递过来的ByteBuf
    @Override
    public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) {
        if (!cumulation.isReadable() && in.isContiguous()) {
            累加的不可读(比如空缓存),且新的是连续的
            cumulation.release(); //释放
            return in;
        }
        try {
            final int required = in.readableBytes(); //返回可读区域
            //可读区域,大于累加器中的可写区域, 或者累加器只能读
            if (required > cumulation.maxWritableBytes() ||
                    (required > cumulation.maxFastWritableBytes() && cumulation.refCnt() > 1) ||
                    cumulation.isReadOnly()) {
                return expandCumulation(alloc, cumulation, in); //扩充累计器
            }
            //写入到累计器中
            cumulation.writeBytes(in, in.readerIndex(), required);
            in.readerIndex(in.writerIndex()); //调整in的读指针到写的位置,那么可读区域为0
            return cumulation;
        } finally {
            in.release();  //释放ByteBuf
        }
    }
};

这个类的实现方法,很重要,因为下面的ChannelRead()方法的核心就是调用上面的方法,

重要方法:channelRead()

public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBuf) { //判断传入的 是否是ByteBuf对象
        CodecOutputList out = CodecOutputList.newInstance();
        try {
            first = cumulation == null;  //如果为null,说明是第一次
            cumulation = cumulator.cumulate(ctx.alloc(),
                    first ? Unpooled.EMPTY_BUFFER : cumulation, (ByteBuf) msg); //判断解码器是否缓存了没有解码完成的半包信息
            callDecode(ctx, cumulation, out);                               //如果为空,说明第一次解析,或者上一次的已经解析完成。
        }...
        } finally {
            try {
                if (cumulation != null && !cumulation.isReadable()) { //不为空,不可读,要释放
                    numReads = 0;
                    cumulation.release();
                    cumulation = null;
                } else if (++numReads >= discardAfterReads) {//读取数据的次数大于阈值,则尝试丢弃已读数据
                    numReads = 0;
                    discardSomeReadBytes();
                }
                int size = out.size();
                firedChannelRead |= out.insertSinceRecycled(); //有被添加或者设置,表示已经读过了
                fireChannelRead(ctx, out, size);   //尝试传递数据
            } finally {
                out.recycle();
            }
        }
    } else {
        ctx.fireChannelRead(msg);  //其他类型进行传递
    }
}

先看ctx.alloc()方法就得到的什么,它对应上面cumulator()的第一个参数,返回的自然是Bytebuf的分配器

public ByteBufAllocator alloc() {
    return channel().config().getAllocator(); //返回ByteBufAllocator,要嘛是池化的,要嘛是非池化
}

如何对msg中的信息,进行转移到本地的cumulator中,

之后调用callDecode进行解码

protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    try {
        while (in.isReadable()) {//可读
            int outSize = out.size();  //数量
            if (outSize > 0) { //一个一个的把解析出来的结果,传递下去
                fireChannelRead(ctx, out, outSize); //传递
                out.clear();  //已经传播 的,要清理掉。
                if (ctx.isRemoved()) {  //上下文被移除了,就不处理了
                    break;
                }
                outSize = 0;
            }
            //继续编解码,
            int oldInputLength = in.readableBytes();
            decodeRemovalReentryProtection(ctx, in, out); //解码  ★
            if (ctx.isRemoved()) {
                break;
            }
            if (outSize == out.size()) { //没有新生成的消息,
                if (oldInputLength == in.readableBytes()) { //没有读取数据
                    break;
                } else {  continue;  }
            }

            if (oldInputLength == in.readableBytes()) { //解码器没有读取数据
               ... }

            if (isSingleDecode()) { //是否每次只解码一条,就返回
                break;
        ...
}

  这个方法具体的逻辑就是解码+传播解码出的pojo,传播pojo就是调用context.fire..方法,没什么好看的,我们之前的pipline讲解的时候,已经讲过了事件传播的逻辑,这里我们重点看解码方法

    decodeRemovalReentryProtection(),它其实也没有实现解码,功能,我们前面说过,本类只是一个抽象类,具体的解码要交给它的子类,实现类,比如我们之前 章节,解码器的使用部分,我们自定义的Handler继承这个类,它的里面才真正实现了解码的功能。!

final void decodeRemovalReentryProtection(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
        throws Exception {
    decodeState = STATE_CALLING_CHILD_DECODE; //状态,调用子类 解码
    try {
        decode(ctx, in, out); //调用子类解码
    } finally {
        boolean removePending = decodeState == STATE_HANDLER_REMOVED_PENDING;
        decodeState = STATE_INIT; //处理完了,设置为初始化
        if (removePending) {
            fireChannelRead(ctx, out, out.size());
            out.clear();
            handlerRemoved(ctx);
        }
    }
}

再来看,丢弃已读部分的ByteBuf

protected final void discardSomeReadBytes() {
    if (cumulation != null && !first && cumulation.refCnt() == 1) {
        cumulation.discardSomeReadBytes();
    }
}

它其实是一个入口,具体的实现是在AbstractByteBuf中

public ByteBuf discardSomeReadBytes() {
    if (readerIndex > 0) {
        if (readerIndex == writerIndex) {
            ensureAccessible();
            adjustMarkers(readerIndex);
            writerIndex = readerIndex = 0;
            return this;
        }

        if (readerIndex >= capacity() >>> 1) {
            setBytes(0, this, readerIndex, writerIndex - readerIndex);
            writerIndex -= readerIndex;
            adjustMarkers(readerIndex);
            readerIndex = 0;
            return this;
        }
    }
    ensureAccessible();
    return this;
}

2.FixedLengthFrameDecoder

它是ByteToMessageDecoder的子类,也就是实现了具体的decode,解决半包,粘包问题,通过固定长度的手法。

它的字段只有一个,frameLength,固定的长度大小,

方法也就是构造方法+decoder()

protected final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    Object decoded = decode(ctx, in);
    if (decoded != null) {
        out.add(decoded);
    }
}

调用重载的方法,简单判断一下长度,然后读取

protected Object decode(
        @SuppressWarnings("UnusedParameters") ChannelHandlerContext ctx, ByteBuf in) throws Exception {
    if (in.readableBytes() < frameLength) {
        return null;
    } else {
        return in.readRetainedSlice(frameLength); //AbstracByteBuf实现的方法
    }
}

3.MessageToByteEncoder

位于outbound中,功能是将pojo编码成为Byte[]组,

两个字段:

private final TypeParameterMatcher matcher;  //类型参数匹配器,针对范型的
private final boolean preferDirect;

第一个字段更重要,是以前没见过的类型,用来处理范型进行匹配的,主要运用在构造方法中。

3.1 TypeParameterMatcher

先看字段,就一个成员Noop,匿名类,实现的是自己!也就实现了match方法,返回true。逻辑简单。

private static final TypeParameterMatcher NOOP = new TypeParameterMatcher() {
    @Override
    public boolean match(Object msg) {
        return true;
    }
};

常用方法:

get(),跟回传进来的Class对象,判断是哪个类型,如果是Object,就是上面NOOP,

public static TypeParameterMatcher get(final Class<?> parameterType) {
    final Map<Class<?>, TypeParameterMatcher> getCache =
            InternalThreadLocalMap.get().typeParameterMatcherGetCache();

    TypeParameterMatcher matcher = getCache.get(parameterType); //缓存中获取
    if (matcher == null) { //未击中
        if (parameterType == Object.class) {
            matcher = NOOP;
        } else {    //内部类,封装Class,match匹配的时候,利用反射,判断是否是这个类的实例
            matcher = new ReflectiveMatcher(parameterType);
        }
        getCache.put(parameterType, matcher); //放入缓存中
    }

    return matcher;
}

内部类,和上面的NOOP逻辑相似

private static final class ReflectiveMatcher extends TypeParameterMatcher {
    private final Class<?> type;
    ReflectiveMatcher(Class<?> type) { this.type = type; }
    @Override  //判断 msg是否是type的实现类
    public boolean match(Object msg) {
        return type.isInstance(msg);
    }
}

3.2 write()方法

public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    ByteBuf buf = null;
    try {
        if (acceptOutboundMessage(msg)) { //类型匹配
            @SuppressWarnings("unchecked")
            I cast = (I) msg;  //类型转换
            buf = allocateBuffer(ctx, cast, preferDirect); //分配空间
            try {
                encode(ctx, cast, buf); //调用子类编码方法
            } finally {
                ReferenceCountUtil.release(cast); //释放
            }

            if (buf.isReadable()) {  //可读
                ctx.write(buf, promise); //传播
            } else {
                buf.release();
                ctx.write(Unpooled.EMPTY_BUFFER, promise);
            }
            buf = null;
        } else {
            ctx.write(msg, promise);
        }
    } ...释放
}

if中的方法,就会调用上方的matcher进行匹配

public boolean acceptOutboundMessage(Object msg) throws Exception {
    return matcher.match(msg);
}

然后分配一个空间,作为ByteBuf

protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, @SuppressWarnings("unused") I msg,
                           boolean preferDirect) throws Exception {
    if (preferDirect) { //是否是直接内存
        return ctx.alloc().ioBuffer();
    } else {
        return ctx.alloc().heapBuffer();
    }
}

再调用子类,实现类的encode()方法,进行编码,同样也就是调用ByteBuf的写入方法,将对象写进去。

 

参考:https://blog.csdn.net/wangwei19871103/article/details/104521711

          https://blog.csdn.net/wangwei19871103/article/details/104535041

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netty是一个基于NIO的高性能网络通信框架,它的核心原理是通过事件驱动和异步处理机制来实现高效的网络传输。其特点是可扩展、可重用、易于维护、性能卓越。 RPC(Remote Procedure Call)是一种基于网络通信的分布式计算模型,通过这种模型,客户端调用服务端的远程方法就像调用本地方法一样简单。在RPC实践过程中,使用Netty框架可以有效提升RPC的性能和稳定性。 Netty的核心原理是基于事件驱动的模型,它的主要组成部分包括NioEventLoop、Channel、Buffer、Codec等。NioEventLoop是Netty中的核心组件之一,它是一个事件循环线程,通过不断遍历注册在它上面的Channel监听器,来处理网络传输过程中的事件,从而实现网络的异步非阻塞传输。 在RPC实践中,Netty通过对协议进行编解码,来实现远程方法的调用和响应过程。通常情况下,客户端与服务端之间需要使用一种协议来进行通信,需要对协议进行编解码处理,这个过程需要在客户端和服务端都实现一遍,使用Netty框架可以简化这个过程,使得开发人员只需要实现协议的编解码策略即可。 总之,Netty作为高性能网络通信框架,其核心原理是基于事件驱动的机制实现的。在RPC实践中,Netty可以高效地实现协议的编解码,提升RPC的性能和稳定性,因此在分布式计算中得到了广泛的应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值