Netty 自定义编码解码器

Netty 自定义编码解码器

入栈:InboundHandler ,出栈:OutboundHandler。Netty 构建 chain 来处理业务。

自定义一个解码器

解码器主要是对客户端发送的消息进行解码处理,所以他是一个入栈的处理器,因此他会有一个入栈的标识ibound=true;

解码器一般我都们都会基层 Netty 提供给的一个实现类来实现自己的解码逻辑 -> io.netty.handler.codec.ByteToMessageDecoder 这就是解码的抽象类,默认我们要实现一个抽象方法 com.netty.codec.custom.InboundAndOutboundHandler#decode 这个方法有大概三个参数;

  • ChannelHandlerContext channelHandlerContext 这个是上下文信息,可以获取通道管道等信息。
  • ByteBuf byteBuf 客户端发送的消息就是存在这个参数的对象里面我们要通过这个对象的read***方法读取我们需要的数据类型,可以是 LongByte 等类型的数据然后我们可以就可以转换成为我们需要的格式。
  • List<Object> list 集合,将解码后的数据传递给下一个inboundHandler 处理类。

代码示例

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
    // Long => 8 byte
    if (byteBuf.readableBytes() >= 8) {
        list.add(byteBuf.readLong());
    } else log.warn("字节数异常 => {}", byteBuf.readableBytes());
}

这样我们就实现了一个简单的解码器。

自定义一个编码器

解码器主要是对服务端将要发送给客户端的消息进行编码处理,所以他是一个出栈的处理器,因此他会有一个入栈的标识outbound=true;

使用 Netty 提供的抽象类 => io.netty.handler.codec.MessageToByteEncoder<T> 泛型 T 表示你要发送的消息的类型,实现抽象方法 => com.netty.codec.custom.OutboundHandler#encode 方法的参数有三个:

  • ChannelHandlerContext channelHandlerContext 这个是上下文信息,可以获取通道管道等信息。
  • Long msg 服务端要发送给客户端的消息
  • ByteBuf byteBuf

代码示例

@Override
protected void encode(ChannelHandlerContext channelHandlerContext, Long msg, ByteBuf byteBuf) {
    byteBuf.writeLong(msg);
    log.info("发送消息成功");
}

后续 -> io.netty.handler.codec.ReplayingDecoder

ByteToMessage 其实在使用过程中会遇到一些问题,例如:

  • 当我们的解码器中想要将字节转换为一个 Integer ,我们知道 Integer 是四个字节的,但是如果在读取的时候不够四个字节,这个时候我们就需要做一些判断逻辑 => if (byteBuf.readableBytes() >= 4) 当这个返回值为 true 的时候我么就可以继续执行解码的逻辑。那我们怎么可以跳过这一步不判断直接进行我们的转换逻辑呢?这个时候就可以使用 Netty 的 io.netty.handler.codec.ReplayingDecoder

可以不用判断可读字节数的原理

ReplayingDecoderByteToMessage 的子类,源码如下:

public abstract class ReplayingDecoder<S> extends ByteToMessageDecoder {
    ...
}

ReplayingDecoder 的秘密就是对 ByteToMessageCallDecode(...) 方法的重写,观摩一下具体实现

@Override
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
    replayable.setCumulation(in);
    try {
        while (in.isReadable()) {
            int oldReaderIndex = checkpoint = in.readerIndex();
            int outSize = out.size();

            if (outSize > 0) {
                fireChannelRead(ctx, out, outSize);
                out.clear();

                // Check if this handler was removed before continuing with decoding.
                // If it was removed, it is not safe to continue to operate on the buffer.
                //
                // See:
                // - https://github.com/netty/netty/issues/4635
                if (ctx.isRemoved()) {
                    break;
                }
                outSize = 0;
            }

            S oldState = state;
            int oldInputLength = in.readableBytes();
            try {
                decodeRemovalReentryProtection(ctx, replayable, out);

                // Check if this handler was removed before continuing the loop.
                // If it was removed, it is not safe to continue to operate on the buffer.
                //
                // See https://github.com/netty/netty/issues/1664
                if (ctx.isRemoved()) {
                    break;
                }

                if (outSize == out.size()) {
                    if (oldInputLength == in.readableBytes() && oldState == state) {
                        throw new DecoderException(
                                StringUtil.simpleClassName(getClass()) + ".decode() must consume the inbound " +
                                "data or change its state if it did not decode anything.");
                    } else {
                        // Previous data has been discarded or caused state transition.
                        // Probably it is reading on.
                        continue;
                    }
                }
            } catch (Signal replay) {
                replay.expect(REPLAY);

                // Check if this handler was removed before continuing the loop.
                // If it was removed, it is not safe to continue to operate on the buffer.
                //
                // See https://github.com/netty/netty/issues/1664
                if (ctx.isRemoved()) {
                    break;
                }

                // Return to the checkpoint (or oldPosition) and retry.
                int checkpoint = this.checkpoint;
                if (checkpoint >= 0) {
                    in.readerIndex(checkpoint);
                } else {
                    // Called by cleanup() - no need to maintain the readerIndex
                    // anymore because the buffer has been released already.
                }
                break;
            }

            if (oldReaderIndex == in.readerIndex() && oldState == state) {
                throw new DecoderException(
                       StringUtil.simpleClassName(getClass()) + ".decode() method must consume the inbound data " +
                       "or change its state if it decoded something.");
            }
            if (isSingleDecode()) {
                break;
            }
        }
    } catch (DecoderException e) {
        throw e;
    } catch (Exception cause) {
        throw new DecoderException(cause);
    }
}

实现不需要判断的逻辑就是因为 int oldReaderIndex = checkpoint = in.readerIndex(); 如果在执行过程中出现异常就会在代码中重置 index

总结

虽然 ReplayingDecoder节约了判断的逻辑,但是从他的代码实现逻辑看到是通过抛出异常来不断的重试,所以在某些特殊的情况下会造成性能的下降。所以还是再选择的时候要根据自己的实际需求来判断是使用 ByteToMessage 还是使用 ReplayingDecoder

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Netty框架提供了很多编码器和解码器,但有时候我们需要自定义编码器和解码器,以满足特定的业务需求。在Netty中,自定义编码器和解码器非常简单,只需要继承ByteToMessageDecoder或MessageToByteEncoder,并实现其中的抽象方法即可。 以下是一个自定义编码器的示例代码: ```java public class MyEncoder extends MessageToByteEncoder<MyMessage> { @Override protected void encode(ChannelHandlerContext ctx, MyMessage msg, ByteBuf out) throws Exception { byte[] data = msg.getData(); out.writeInt(data.length); out.writeBytes(data); } } ``` 在这个示例中,我们定义了一个名为MyEncoder的编码器,它继承自MessageToByteEncoder,并实现了encode方法。在encode方法中,我们首先获取消息的数据,然后将消息的数据长度写入到ByteBuf中,最后将消息的数据写入到ByteBuf中。 以下是一个自定义解码器的示例代码: ```java public class MyDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { if (in.readableBytes() < 4) { return; } int length = in.readInt(); if (in.readableBytes() < length) { in.resetReaderIndex(); return; } byte[] data = new byte[length]; in.readBytes(data); MyMessage message = new MyMessage(length, data); out.add(message); } } ``` 在这个示例中,我们定义了一个名为MyDecoder的解码器,它继承自ByteToMessageDecoder,并实现了decode方法。在decode方法中,我们首先判断ByteBuf中是否有足够的字节可读,如果不够则直接返回。然后从ByteBuf中读取消息的长度和数据,并将它们封装成MyMessage对象,加入到解码结果列表中。 在使用自定义编码器和解码器时,只需要将它们注册到ChannelPipeline中即可。以下是注册的示例代码: ```java ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast(new MyDecoder()); pipeline.addLast(new MyEncoder()); ``` 这样,在ChannelPipeline中的所有ChannelHandler都可以使用MyMessage对象,而无需关心它们与字节流的转换过程。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值