Browse Source

netty错误日志打印

master
lijiuwei 8 months ago
parent
commit
529a04a567
1 changed files with 44 additions and 37 deletions
  1. +44
    -37
      src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java

+ 44
- 37
src/main/java/com/inspect/tcpserver/tcp/MyDecoder.java View File

@ -21,48 +21,55 @@ public class MyDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> 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);
}
}
}

Loading…
Cancel
Save