深入netty之三更为复杂的数据协议的decode

前面,我们讲述了一个带有包尾的数据协议包的解码过程,这种协议就已经被LengthFieldBasedFrameDecoder类能解决的数据协议复杂得多。其协议如下所示:

带包尾的数据协议包
带包尾的数据协议包

但还有更复杂的,我们遇到的,就是在上述协议的基础上,再增加三种不同于上述协议的协议,如下所示:

数据协议示例
数据协议示例

带包尾的数据协议,我们已经通过CashboxDataLengthFieldBasedFrameDecoder得到了解码。但增加了上面三个简单协议后,我们就不能只使用CashboxDataLengthFieldBasedFrameDecoder得到解决。
那该怎么解决呢?
顺理成章的思路就是:我们做一个总的粘包拆包解码器,遇到ACK等上述三种简单协议就单独解决,其他的数据协议,走CashboxDataLengthFieldBasedFrameDecoder类解决。
我们来看看实际的编码过程:

@Slf4j
public class CashboxVariableFormatDecoder extends CashboxDataLengthFieldBasedFrameDecoder{

我们是在扩展CashboxDataLengthFieldBasedFrameDecoder类的功能的,当然继承该类是一个很好的扩展方法。

private static final int INITIAL_LEN = 5;

ACK等上述三种简单协议的数据长度是5个字节,先定义成常量。

public CashboxVariableFormatDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength) {
    super(maxFrameLength, lengthFieldOffset, lengthFieldLength);
}

public CashboxVariableFormatDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip) {
    super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip);
}

public CashboxVariableFormatDecoder(int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
    super(maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
}

public CashboxVariableFormatDecoder(ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
    super(byteOrder, maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
}

上面是使用了父类的构造器,没什么好说的。
接着重载父类的decode方法:

 @Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {

如果读到的数据长度不足ACK等上述三种简单协议数据的长度,那么返回等待:

if (in.readableBytes() < INITIAL_LEN) return null;

标记一下当前的readIndex的位置,为重置index做准备:

in.markReaderIndex();

读前两个字节:

byte dle = in.readByte();

byte stx = in.readByte();

如果前两个字节是ACK等上述三种简单协议数据的标识符,则处理成ACK等上述三种简单协议数据:

if (stx == (byte) 6 || stx == (byte) 21)
{
    //必须把所有要包装的字节数读完,以保证该字节不会被反复走读。
    in.readByte();
    in.readByte();
    in.readByte();
    return in.retainedSlice(0, INITIAL_LEN);
}

处理的结果就是返回前5个字节。
如果不是,则该CashboxDataLengthFieldBasedFrameDecoder类处理了:

else
{
    //交给CashboxDataLengthFieldBasedFrameDecoder类包装,所以,已经走读过的字节要退回。
    in.resetReaderIndex();

    return super.decode(ctx, in);
}

最后,结束整个类的编写:

    }
}

以上就是整个解决过程!

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者...
    保川阅读 5,990评论 1 13
  • 1.这篇文章不是本人原创的,只是个人为了对这部分知识做一个整理和系统的输出而编辑成的,在此郑重地向本文所引用文章的...
    SOMCENT阅读 13,137评论 6 174
  • 1.谈谈 tableview 的重用机制。 为什么要“重用”?iPhone 重用机制是为了实现大量数据显示而采用的...
    大王叫我来巡山_Cong阅读 1,872评论 0 13
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,999评论 19 139
  • 今天的你我不知该如何面对,是笑着迎接,还是对你不理不睬……不敢跟你说话,我怕再次受伤!!!
    小傻傻94阅读 152评论 0 0