内容简介:最近做项目有一个通讯协议,在其如图所示,而
最近做项目有一个通讯协议,在其 结束符 后还有两个字节的 校验符号
如图所示, 结束符 后的 校验码 的值是变化的
而 Netty 自带的 DelimiterBasedFrameDecoder 可以支持结束符拆包但是不支持目前这种情况。我们通过对 DelimiterBasedFrameDecoder 进行简单的修改实现。
新建类 io.netty.handler.codec.AdditionalDelimiterBasedFrameDecoder ,将 io.netty.handler.codec.DelimiterBasedFrameDecoder 代码拷贝其中,找到:
int minDelimLength = minDelim.capacity();
修改为:
int minDelimLength = minDelim.capacity(); final int checkLength = 2; minDelimLength = minDelimLength + checkLength;
原理很简单,将原有的 分隔符 的长度增加两个字节,将后面两位 校验码 包含进去。
声明使用:
new AdditionalDelimiterBasedFrameDecoder(4096, Unpooled.copiedBuffer(Unpooled.buffer(1).writeByte(0xCC).array())
以上所述就是小编给大家介绍的《Netty下不固定的Delimiter的拆包处理》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Art of Computer Programming, Volumes 1-3 Boxed Set
Donald E. Knuth / Addison-Wesley Professional / 1998-10-15 / USD 199.99
This multivolume work is widely recognized as the definitive description of classical computer science. The first three volumes have for decades been an invaluable resource in programming theory and p......一起来看看 《The Art of Computer Programming, Volumes 1-3 Boxed Set》 这本书的介绍吧!