|
|
|
@ -16,66 +16,53 @@ import java.util.List; |
|
|
|
public class MyDecoder extends ByteToMessageDecoder { |
|
|
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(MyDecoder.class); |
|
|
|
private final String PACKET_FLAG = "EB90"; |
|
|
|
|
|
|
|
private final int BASE_LENGTH = 2 + 8 + 8 + 1 + 4 + 2; |
|
|
|
|
|
|
|
@Override |
|
|
|
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception { |
|
|
|
if (in.readableBytes() >= BASE_LENGTH) { |
|
|
|
try { |
|
|
|
//skip |
|
|
|
if (in.readableBytes() > 512 * 1024) { |
|
|
|
in.skipBytes(in.readableBytes()); |
|
|
|
} |
|
|
|
int length = in.readableBytes(); |
|
|
|
if (length < BASE_LENGTH) { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
final String uuid = RandomStringUtils.randomAlphanumeric(16); |
|
|
|
// ByteBuf forPrint = in.copy(); |
|
|
|
// log.info("###### 会话:{}, 客户:{}, 上行原始报文 ######\n {}", uuid, ctx.channel().id().asShortText(), ByteBufUtil.hexDump(forPrint)); |
|
|
|
// forPrint.release(); |
|
|
|
final String uuid = RandomStringUtils.randomAlphanumeric(16); |
|
|
|
// ByteBuf forPrint = in.copy(); |
|
|
|
// log.info("###### 会话:{}, 客户:{}, 上行原始报文 ######\n {}", uuid, ctx.channel().id().asShortText(), ByteBufUtil.hexDump(forPrint)); |
|
|
|
// forPrint.release(); |
|
|
|
|
|
|
|
int index; |
|
|
|
String flag; |
|
|
|
while (true) { |
|
|
|
index = in.readerIndex(); |
|
|
|
in.markReaderIndex(); |
|
|
|
byte[] dst = new byte[2]; |
|
|
|
in.readBytes(dst, 0, 2); |
|
|
|
flag = ByteUtils.byte2Hex(dst); |
|
|
|
if (PACKET_FLAG.equalsIgnoreCase(flag)) { |
|
|
|
break; |
|
|
|
} |
|
|
|
in.resetReaderIndex(); |
|
|
|
if (in.readableBytes() < BASE_LENGTH) { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
do { |
|
|
|
byte[] start = new byte[2]; |
|
|
|
in.readBytes(start); |
|
|
|
if (start[0]==-21 && start[1]==-112) { |
|
|
|
long sendIndex = in.readLongLE(); |
|
|
|
long receiveIndex = in.readLongLE(); |
|
|
|
byte sourceFlag = in.readByte(); |
|
|
|
int xmlLength = in.readIntLE(); |
|
|
|
if (in.readableBytes() < xmlLength) { |
|
|
|
in.readerIndex(index); |
|
|
|
return; |
|
|
|
} |
|
|
|
byte[] payload = new byte[xmlLength]; |
|
|
|
in.readBytes(payload); |
|
|
|
if(in.readableBytes() > 0) { |
|
|
|
in.readShortLE(); |
|
|
|
length = in.readableBytes(); |
|
|
|
if (length >= xmlLength + 2) { |
|
|
|
byte[] payload = new byte[xmlLength]; |
|
|
|
in.readBytes(payload); |
|
|
|
in.skipBytes(2); |
|
|
|
|
|
|
|
BinaryModel binaryModel = new BinaryModel(); |
|
|
|
binaryModel.receiveIndex = receiveIndex; |
|
|
|
binaryModel.sendIndex = sendIndex; |
|
|
|
binaryModel.sourceFlag = sourceFlag; |
|
|
|
binaryModel.dataLength = xmlLength; |
|
|
|
binaryModel.dataBuf = Unpooled.copiedBuffer(payload); |
|
|
|
binaryModel.uuid = uuid; |
|
|
|
out.add(binaryModel); |
|
|
|
break; |
|
|
|
} else { |
|
|
|
log.error("readShortLE() but readableBytes = 0"); |
|
|
|
in.readerIndex(in.readerIndex() - 23); |
|
|
|
} |
|
|
|
BinaryModel binaryModel = new BinaryModel(); |
|
|
|
binaryModel.receiveIndex = receiveIndex; |
|
|
|
binaryModel.sendIndex = sendIndex; |
|
|
|
binaryModel.sourceFlag = sourceFlag; |
|
|
|
binaryModel.dataLength = xmlLength; |
|
|
|
binaryModel.dataBuf = Unpooled.copiedBuffer(payload); |
|
|
|
binaryModel.uuid = uuid; |
|
|
|
out.add(binaryModel); |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("error" , e); |
|
|
|
} else { |
|
|
|
in.readerIndex(in.readerIndex() - 2); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
in.readByte(); |
|
|
|
length = in.readableBytes(); |
|
|
|
} while(length >= BASE_LENGTH); |
|
|
|
} |
|
|
|
} |