diff --git a/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java b/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java index d124db1..9636bbf 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java +++ b/src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java @@ -21,48 +21,55 @@ public class MyDecoder extends ByteToMessageDecoder { @Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { - int length = in.readableBytes(); - if (length < BASE_LENGTH) { - return; - } + try { + int length = in.readableBytes(); + if (length < BASE_LENGTH) { + log.error("not enough readableBytes: {}", 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(); - 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(); - length = in.readableBytes(); - if (length >= xmlLength + 2) { - byte[] payload = new byte[xmlLength]; - in.readBytes(payload); - in.skipBytes(2); + 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(); + 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; + 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 { + in.readerIndex(in.readerIndex() - 23); + log.error("wrong xml length: {}, total length: {}", xmlLength, length); + } } else { - in.readerIndex(in.readerIndex() - 23); + in.readerIndex(in.readerIndex() - 2); + log.error("wrong start flag: [{},{}]", start[0], start[1]); } - } else { - in.readerIndex(in.readerIndex() - 2); - } - in.readByte(); - length = in.readableBytes(); - } while(length >= BASE_LENGTH); + in.readByte(); + length = in.readableBytes(); + } while (length >= BASE_LENGTH); + } catch (Exception e) { + log.error("error" , e); + } } }