Merge branch 'dev-zza' into dev

This commit is contained in:
Lemon
2025-07-22 17:29:55 +08:00
6 changed files with 81 additions and 21 deletions

View File

@@ -16,6 +16,8 @@ import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.ResourceLeakDetector;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

View File

@@ -0,0 +1,47 @@
package com.jsowell.netty.server.yunkuaichong;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
/**
* 回复消息Handler
*
* @author Lemon
* @Date 2025/7/22 14:00:03
*/
@ChannelHandler.Sharable
@Slf4j
@Component
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg == null || msg == "") {
log.info("服务端响应空的消息");
return;
}
//将客户端的信息直接返回写入ctx
ctx.write(msg);
//刷新缓存区
ctx.flush();
// ByteBuf byteBuf = (ByteBuf) msg;
// ByteBuf byteBuf = Unpooled.copiedBuffer("680d01000004882300000001290100d510", StandardCharsets.UTF_8);
// // byteBuf.readBytes(bytes);
// YKCDataProtocol ykcDataProtocol = new YKCDataProtocol(new byte[]{});
//
// // 获取帧类型
// byte[] frameTypeBytes = ykcDataProtocol.getFrameType();
// String frameType = YKCUtils.frameType2Str(frameTypeBytes);
// // 获取序列号域
// int serialNumber = BytesUtil.bytesToIntLittle(ykcDataProtocol.getSerialNumber());
// if (!CollectionUtils.containsAny(NettyServerHandler.notPrintFrameTypeList, frameType)) {
// log.info("【>>>>>平台响应消息>>>>>】channel:{}, 响应帧类型:{}, 响应帧名称:{}, 序列号域:{}, response:{}",
// ctx.channel().id(), frameType, YKCFrameTypeCode.getFrameTypeStr(frameType), serialNumber,
// BytesUtil.binary(ykcDataProtocol.getBytes(), 16));
// }
}
}

View File

@@ -6,6 +6,8 @@ import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@@ -15,7 +17,13 @@ import java.util.concurrent.TimeUnit;
public class NettyServerChannelInitializer extends ChannelInitializer<SocketChannel> {
@Resource
NettyServerHandler nettyServerHandler;
private NettyServerHandler nettyServerHandler;
@Resource
private EchoServerHandler echoServerHandler;
// 引入业务线程池
final EventExecutorGroup businessGroup = new DefaultEventExecutorGroup(16);
@Override
protected void initChannel(SocketChannel channel) throws Exception {
@@ -28,7 +36,9 @@ public class NettyServerChannelInitializer extends ChannelInitializer<SocketChan
pipeline.addLast("encoder", new ByteArrayDecoder());
// 读超时时间设置为30s0表示不监控
pipeline.addLast(new IdleStateHandler(30, 0, 0, TimeUnit.SECONDS));
pipeline.addLast("handler", nettyServerHandler);
// pipeline.addLast("handler", nettyServerHandler);
pipeline.addLast(businessGroup, nettyServerHandler); // 消息先进入业务线程池
pipeline.addLast(echoServerHandler);
}
}

View File

@@ -8,6 +8,7 @@ import com.jsowell.common.util.BytesUtil;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.YKCUtils;
import com.jsowell.netty.service.yunkuaichong.YKCBusinessService;
import com.jsowell.netty.service.yunkuaichong.impl.YKCBusinessServiceImpl;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.handler.timeout.IdleState;
@@ -19,6 +20,7 @@ import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Objects;
@@ -32,7 +34,7 @@ import java.util.concurrent.ConcurrentHashMap;
@Component
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Autowired
@Resource
private YKCBusinessService ykcService;
/**
@@ -40,7 +42,9 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
*/
private static final ConcurrentHashMap<ChannelId, ChannelHandlerContext> CHANNEL_MAP = new ConcurrentHashMap<>();
private final List<String> notPrintFrameTypeList = Lists.newArrayList("0x03"); // "0x03"
protected static final List<String> notPrintFrameTypeList = Lists.newArrayList(); // "0x03"
// private final YKCBusinessService ykcService = new YKCBusinessServiceImpl();
/**
* 有客户端连接服务器会触发此函数
@@ -148,11 +152,8 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
if (Objects.nonNull(response)) {
// 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
this.channelWrite(channel.id(), buffer);
// 获取桩号
// String pileSn = PileChannelEntity.getPileSnByChannelId(ctx.channel().id().asLongText());
// 批量响应客户端
// this.channelWriteBatch(pileSn, buffer);
// this.channelWrite(channel.id(), buffer);
super.channelRead(ctx, buffer);
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// 应答帧类型
byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes);