Merge branch 'dev-new' into dev-new-rabbitmq

This commit is contained in:
Guoqs
2024-12-30 10:12:15 +08:00
3 changed files with 22 additions and 28 deletions

View File

@@ -4,7 +4,6 @@ import com.jsowell.common.constant.Constants;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
@@ -136,9 +135,9 @@ public class YouDianProtocolDecoder extends ByteToMessageDecoder {
buffer.readerIndex(beginReader + HEADER_LENGTH_DNY + length + 2);
out.add(frame);
} finally {
if (frame != null) {
ReferenceCountUtil.release(frame);
}
// if (frame != null) {
// ReferenceCountUtil.release(frame);
// }
}
}

View File

@@ -96,6 +96,7 @@ public class NettyServerManager implements CommandLineRunner {
.handler(new LoggingHandler(LogLevel.DEBUG))
.option(ChannelOption.SO_BACKLOG, 128)
.option(ChannelOption.SO_REUSEADDR, true)
// .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) // 启用池化内存分配器
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childOption(ChannelOption.SO_REUSEADDR, true)
.childHandler(electricBicyclesServerChannelInitializer)

View File

@@ -8,7 +8,6 @@ import com.jsowell.netty.service.electricbicycles.EBikeBusinessService;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.handler.timeout.ReadTimeoutException;
import io.netty.util.ReferenceCountUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@@ -61,30 +60,25 @@ public class ElectricBicyclesServerHandler extends ChannelInboundHandlerAdapter
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
try {
byte[] msg = (byte[]) message;
// 处理数据
byte[] response = eBikeService.process(msg, ctx);
if (Objects.nonNull(response)) {
log.info("[电单车===>响应数据]:{}", BytesUtil.binary(response, 16));
// 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
// this.channelWrite(channel.id(), buffer);
ctx.writeAndFlush(buffer);
// if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// // 应答帧类型
// byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes);
// String responseFrameType = YKCUtils.frameType2Str(responseFrameTypeBytes);
// log.info("【>>>>>平台响应消息>>>>>】channel:{}, 响应帧类型:{}, 响应帧名称:{}, 原帧类型:{}, 原帧名称:{}, 序列号域:{}, response:{}",
// channel.id(), responseFrameType, YKCFrameTypeCode.getFrameTypeStr(responseFrameType),
// frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
// BytesUtil.binary(response, 16));
// }
}
} finally {
ReferenceCountUtil.release(message);
byte[] msg = (byte[]) message;
// 处理数据
byte[] response = eBikeService.process(msg, ctx);
if (Objects.nonNull(response)) {
log.info("[电单车===>响应数据]:{}", BytesUtil.binary(response, 16));
// 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
// this.channelWrite(channel.id(), buffer);
ctx.writeAndFlush(buffer);
// if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// // 应答帧类型
// byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes);
// String responseFrameType = YKCUtils.frameType2Str(responseFrameTypeBytes);
// log.info("【>>>>>平台响应消息>>>>>】channel:{}, 响应帧类型:{}, 响应帧名称:{}, 原帧类型:{}, 原帧名称:{}, 序列号域:{}, response:{}",
// channel.id(), responseFrameType, YKCFrameTypeCode.getFrameTypeStr(responseFrameType),
// frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
// BytesUtil.binary(response, 16));
// }
}
}
/**