mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-21 03:25:12 +08:00
commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.jsowell.netty.decoder;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import io.netty.handler.codec.CorruptedFrameException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CustomDecoder extends ByteToMessageDecoder {
|
||||
private static final byte START_FLAG = (byte) 0x68;
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
// 检查可读数据长度是否大于等于起始标志符和数据长度字段的长度
|
||||
if (in.readableBytes() >= 3) {
|
||||
// 标记当前读取位置
|
||||
in.markReaderIndex();
|
||||
// 读取起始标志符
|
||||
byte startFlag = in.readByte();
|
||||
// 检查起始标志符是否正确
|
||||
if (startFlag != START_FLAG) {
|
||||
// 如果不正确,重置读取位置,并抛出异常
|
||||
in.resetReaderIndex();
|
||||
throw new CorruptedFrameException("Invalid start flag: " + startFlag);
|
||||
}
|
||||
// 读取数据长度
|
||||
byte length = in.readByte();
|
||||
// 检查可读数据长度是否大于等于数据长度字段的值
|
||||
if (in.readableBytes() >= length) {
|
||||
// 读取完整数据包
|
||||
ByteBuf frame = in.readBytes(length + 2); // 包括校验位
|
||||
out.add(frame);
|
||||
} else {
|
||||
// 如果可读数据长度不够,重置读取位置,并等待下一次读取
|
||||
in.resetReaderIndex();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.jsowell.netty.decoder;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
public class StartAndLengthFieldFrameDecoder extends ByteToMessageDecoder {
|
||||
// 起始标志
|
||||
private int HEAD_DATA;
|
||||
|
||||
public StartAndLengthFieldFrameDecoder(int HEAD_DATA) {
|
||||
this.HEAD_DATA = HEAD_DATA;
|
||||
}
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 协议开始的标准head_data,int类型,占据1个字节.
|
||||
* 表示数据的长度contentLength,int类型,占据1个字节.
|
||||
* </pre>
|
||||
*/
|
||||
public final int BASE_LENGTH = 1 + 1;
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer,
|
||||
List<Object> out) throws Exception {
|
||||
System.out.println();
|
||||
// log.info("打印ByteBuf:{}", buffer.toString());
|
||||
// 可读长度必须大于基本长度
|
||||
if (buffer.readableBytes() <= BASE_LENGTH) {
|
||||
log.warn("可读字节数:{}小于基础长度:{}", buffer.readableBytes(), BASE_LENGTH);
|
||||
return;
|
||||
}
|
||||
|
||||
// 记录包头开始的index
|
||||
int beginReader;
|
||||
|
||||
while (true) {
|
||||
// 获取包头开始的index
|
||||
beginReader = buffer.readerIndex();
|
||||
// log.info("包头开始的index:{}", beginReader);
|
||||
// 标记包头开始的index
|
||||
buffer.markReaderIndex();
|
||||
// 读到了协议的开始标志,结束while循环
|
||||
if (buffer.getUnsignedByte(beginReader) == HEAD_DATA) {
|
||||
// log.info("读到了协议的开始标志,结束while循环 byte:{}, HEAD_DATA:{}", buffer.getUnsignedByte(beginReader), HEAD_DATA);
|
||||
break;
|
||||
}
|
||||
|
||||
// 未读到包头,略过一个字节
|
||||
// 每次略过,一个字节,去读取,包头信息的开始标记
|
||||
buffer.resetReaderIndex();
|
||||
buffer.readByte();
|
||||
|
||||
// 当略过,一个字节之后,
|
||||
// 数据包的长度,又变得不满足
|
||||
// 此时,应该结束。等待后面的数据到达
|
||||
if (buffer.readableBytes() < BASE_LENGTH) {
|
||||
log.info("数据包的长度不满足 readableBytes:{}, BASE_LENGTH:{}", buffer.readableBytes(), BASE_LENGTH);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 消息的长度
|
||||
int length = buffer.getUnsignedByte(beginReader + 1);
|
||||
// 判断请求数据包数据是否到齐
|
||||
if (buffer.readableBytes() < length + 4) {
|
||||
// log.info("请求数据包数据没有到齐,还原读指针 readableBytes:{}, 消息的长度:{}", buffer.readableBytes(), length);
|
||||
// 还原读指针
|
||||
buffer.readerIndex(beginReader);
|
||||
return;
|
||||
}
|
||||
|
||||
// 读取data数据
|
||||
byte[] data = new byte[length + 4];
|
||||
buffer.readBytes(data);
|
||||
ByteBuf frame = buffer.retainedSlice(beginReader, length + 4);
|
||||
buffer.readerIndex(beginReader + length + 4);
|
||||
out.add(frame);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user