From ae31ae1859ecc75c9471b1e10431f71c66718020 Mon Sep 17 00:00:00 2001 From: wangguangyuan Date: Mon, 5 Jan 2026 11:04:29 +0800 Subject: [PATCH] =?UTF-8?q?tcp=E7=B2=98=E5=8C=85=EF=BC=9A=20=E5=9C=A8?= =?UTF-8?q?=E6=89=BE=E5=88=B0=E8=B5=B7=E5=A7=8B=E6=A0=87=E5=BF=97=E5=90=8E?= =?UTF-8?q?=EF=BC=8C=E6=A3=80=E6=9F=A5=E6=98=AF=E5=90=A6=E6=9C=89=E8=B6=B3?= =?UTF-8?q?=E5=A4=9F=E7=9A=84=E6=95=B0=E6=8D=AE=E8=AF=BB=E5=8F=96=E5=9B=BA?= =?UTF-8?q?=E5=AE=9A=E5=A4=B4=E9=83=A8=E5=92=8Cxml=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E7=BB=93=E6=9D=9F=E6=A0=87=E5=BF=97=E3=80=82?= =?UTF-8?q?=20=E5=A6=82=E6=9E=9C=E6=95=B0=E6=8D=AE=E4=B8=8D=E5=A4=9F?= =?UTF-8?q?=EF=BC=8C=E5=88=99=E9=87=8D=E7=BD=AE=E8=AF=BB=E6=8C=87=E9=92=88?= =?UTF-8?q?=EF=BC=88=E5=9B=9E=E9=80=80=E5=88=B0=E8=B5=B7=E5=A7=8B=E6=A0=87?= =?UTF-8?q?=E5=BF=97=E5=89=8D=EF=BC=89=EF=BC=8C=E7=AD=89=E5=BE=85=E6=9B=B4?= =?UTF-8?q?=E5=A4=9A=E6=95=B0=E6=8D=AE=E3=80=82=20=E5=A6=82=E6=9E=9C?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=B6=B3=E5=A4=9F=EF=BC=8C=E5=88=99=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=B9=B6=E9=AA=8C=E8=AF=81=E7=BB=93=E6=9D=9F=E6=A0=87?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/inspect/tcpserver/tcp/MyDecoder.java | 91 +++++++++++++------ 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java b/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java index 7144227..d6324dd 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java +++ b/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java @@ -24,6 +24,7 @@ public class MyDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { try { + // 获取当前可读字节数 int length = in.readableBytes(); if (length < BASE_LENGTH) { // log.error("not enough readableBytes: {}", length); @@ -38,44 +39,82 @@ public class MyDecoder extends ByteToMessageDecoder { forPrint.release(); } - do { + while (in.readableBytes() >= 2) { + // 标记当前位置 + in.markReaderIndex(); + byte[] start = new byte[2]; in.readBytes(start); + if (start[0] == -21 && start[1] == -112) { + // 检查是否有足够数据读取固定头部 + if (in.readableBytes() < 21) { // 8+8+1+4 = 21字节 + in.resetReaderIndex(); + return; // 数据不足,等待下次 + } + long sendIndex = in.readLongLE(); long receiveIndex = in.readLongLE(); byte sourceFlag = in.readByte(); int xmlLength = in.readIntLE(); - 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; - binaryModel.id = ctx.channel().id().asShortText(); - out.add(binaryModel); - break; - } else { - in.readerIndex(in.readerIndex() - 23); -// log.error("wrong xml length: {}, total length: {}", xmlLength, length); + // 检查XML长度是否合理 + if (xmlLength < 0 || xmlLength > 1024 * 1024) { + log.warn("XML长度异常: {}", xmlLength); + in.resetReaderIndex(); + in.skipBytes(2); // 跳过错误的起始标志 + continue; + } + + // 检查是否有完整的XML数据 + 结束标志 + if (in.readableBytes() < xmlLength + 2) { + in.resetReaderIndex(); + return; // 数据不完整,等待更多数据 + } + + byte[] payload = new byte[xmlLength]; + in.readBytes(payload); + + // 检查结束标志 + if (in.readableBytes() < 2) { + in.resetReaderIndex(); + return; } + + byte[] end = new byte[2]; + in.readBytes(end); + + if (end[0] != -21 || end[1] != -112) { + // 结束标志错误,回退并跳过这个包 + log.warn("报文结束标志不正确"); + in.resetReaderIndex(); + in.skipBytes(2); + continue; + } + + // 成功解析一个完整包 + BinaryModel binaryModel = new BinaryModel(); + binaryModel.receiveIndex = receiveIndex; + binaryModel.sendIndex = sendIndex; + binaryModel.sourceFlag = sourceFlag; + binaryModel.dataLength = xmlLength; + binaryModel.dataBuf = Unpooled.copiedBuffer(payload); + binaryModel.uuid = uuid; + binaryModel.id = ctx.channel().id().asShortText(); + out.add(binaryModel); + } else { - in.readerIndex(in.readerIndex() - 2); -// log.error("wrong start flag: [{},{}]", start[0], start[1]); + // 不是起始标志,回退并跳过一个字节 + in.resetReaderIndex(); + in.skipBytes(1); } - - in.readByte(); - length = in.readableBytes(); - } while (length >= BASE_LENGTH); + } } catch (Exception e) { - log.error("error" , e); + log.error("解码发生异常", e); + // 异常时,如果有未处理数据,跳过所有数据,避免无限循环 + if (in.readableBytes() > 0) { + in.skipBytes(in.readableBytes()); + } } } }