This commit is contained in:
2023-12-25 16:20:48 +08:00
parent 81e435e4a0
commit 3cf9da8292

View File

@@ -66,34 +66,37 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
* 有客户端发消息会触发此函数 * 有客户端发消息会触发此函数
*/ */
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
// log.info("加载客户端报文=== channelId:" + ctx.channel().id() + ", msg:" + msg); // log.info("加载客户端报文=== channelId:" + ctx.channel().id() + ", msg:" + msg);
// 下面可以解析数据保存数据生成返回报文将需要返回报文写入write函数 // 下面可以解析数据保存数据生成返回报文将需要返回报文写入write函数
byte[] arr = (byte[]) msg; byte[] msg = (byte[]) message;
// 获取帧类型 // 获取帧类型
String frameType = YKCUtils.frameType2Str(BytesUtil.copyBytes(arr, 5, 1)); String frameType = YKCUtils.frameType2Str(BytesUtil.copyBytes(msg, 5, 1));
// 获取序列号域 // 获取序列号域
int serialNumber = BytesUtil.bytesToIntLittle(BytesUtil.copyBytes(arr, 2, 2)); int serialNumber = BytesUtil.bytesToIntLittle(BytesUtil.copyBytes(msg, 2, 2));
// 获取channel
Channel channel = ctx.channel();
// new // new
String hexString = DatatypeConverter.printHexBinary(arr); String hexString = DatatypeConverter.printHexBinary(msg);
// 心跳包0x03日志太多造成日志文件过大改为不打印 // 心跳包0x03日志太多造成日志文件过大改为不打印
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) { if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
log.info("【<<<<<平台收到消息<<<<<】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, 报文:{}, new报文:{}", log.info("【<<<<<平台收到消息<<<<<】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, 报文:{}, new报文:{}",
ctx.channel().id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber, channel.id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
BytesUtil.binary(arr, 16), hexString); BytesUtil.binary(msg, 16), hexString);
} }
// 处理数据 // 处理数据
byte[] response = ykcService.process(arr, ctx.channel()); byte[] response = ykcService.process(msg, channel);
if (Objects.nonNull(response)) { if (Objects.nonNull(response)) {
// 响应客户端 // 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response); ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
this.channelWrite(ctx.channel().id(), buffer); this.channelWrite(channel.id(), buffer);
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) { if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
log.info("【>>>>>平台响应消息>>>>>】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, response:{}", log.info("【>>>>>平台响应消息>>>>>】channel:{}, 帧类型:{}, 帧名称:{}, 序列号域:{}, response:{}",
ctx.channel().id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber, channel.id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
BytesUtil.binary(response, 16)); BytesUtil.binary(response, 16));
} }
} }