netty 提供的解码器
DelimiterBasedFrameDecoder 解决TCP的粘包解码器
StringDecoder 消息转成String解码器
LineBasedFrameDecoder 自动完成标识符分隔解码器
FixedLengthFrameDecoder 固定长度解码器,二进制
Base64Decoder base64 解码器
假设一包数据定义如下
image.png
netty解码部分核心代码如下
public class ObdProtocol extends ChannelInitializer<SocketChannel> {
private RabbitSender rabbitSender;
private int port;
public ObdProtocol(RabbitSender rs, int tcpPort) {
this.rabbitSender = rs;
this.port = tcpPort;
}
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast(new IdleStateHandler(6 * 60, 0, 0));
pipeline.addLast(new HeartBeatHandler());
//分割符
ByteBuf delimiter = Unpooled.copiedBuffer(new byte[]{-25});
//打印分隔符
pipeline.addLast("frameDecoder", new DelimiterBasedFrameDecoder(1024,false, delimiter));
pipeline.addLast("objectEncoder", new ObdProtocolEncoder());
pipeline.addLast("objectDecoder", new ObdServiceHandler(rabbitSender, port));
}
}
接收handler 部分代码
@Slf4j
public class ObdServiceHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf byteBuf = (ByteBuf) msg;
//转换成大写16进制字符串
String hexData = ByteBufUtil.hexDump(byteBuf).toUpperCase();
//打印接收数据
System.out.println(hexData);
}
}
测试
测试工具:NetAssistimage.png
发了两包数据 netty 已经在底层给我们处理好了粘包的问题
image.png
再测试当数据为半包的情况下
先发送数据半包数据 E712 在发送一包半包数据 11E7E7D022E7 结果会打印两包数据。很是ok 。 但是真实的需求往往不是这样子的。比如 原始数据是 E71211E7E7D022E7。往往需要解出来的是带开始和结束标识的两包数据。如下
第一包数据: E71211E7 第二包数据 : E7D022E7。 你可能说很简单,把 DelimiterBasedFrameDecoder 的构造函数里面的stripDelimiter 设置为false 不就行了吗?
image.png
但是看结果:
image.png
和想要的结果差点。应为netty 根据E7 为分隔符,把数据拆分成了四包数据。
附上netty 底层处理过程
image.png
image.png
把DelimiterBasedFrameDecoder 拷贝下来。在idexOf 加上图审所示代码(第一个if 是解决截取消息同的问题,第二个if 是为了解决粘包问题)
image.png
详细的操作步骤有空再单独写一篇文章吧!起始跟着源码调试一下就明白了 很简单的。
修改之后测试
image.png
半包测试
image.png
提示成功发送但是在handler里面并没有收到任何消息
,
下面 再发剩下的一包多的数 E7E7D022E7
image.png
依然是两包完整的数据。