This commit is contained in:
Lemon
2025-07-02 10:12:56 +08:00
parent 5776bf2505
commit a951e079b7
5 changed files with 31 additions and 20 deletions

View File

@@ -114,6 +114,12 @@ public class PileChannelEntity {
return null; return null;
} }
// public static String getPileSnByCtx(ChannelHandlerContext ctx) {
// for (HashMap.Entry<String, ChannelHandlerContext> entry : manager.entrySet()) {
//
// }
// }
/** /**
* 打印 * 打印
*/ */

View File

@@ -213,6 +213,8 @@ public class YKCUtils {
// 保存桩号和channel的关系 // 保存桩号和channel的关系
PileChannelEntity.checkChannel(pileSn, ctx); PileChannelEntity.checkChannel(pileSn, ctx);
PileChannelEntity.checkChannelV2(pileSn, ctx);
} }
/** /**

View File

@@ -20,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@@ -149,8 +150,10 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
// 响应客户端 // 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response); ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
this.channelWrite(channel.id(), buffer); this.channelWrite(channel.id(), buffer);
// 获取桩号
String pileSn = PileChannelEntity.getPileSnByChannelId(ctx.channel().id().asLongText());
// 批量响应客户端 // 批量响应客户端
// this.channelWriteBatch(ctx, buffer); this.channelWriteBatch(pileSn, buffer);
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) { if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// 应答帧类型 // 应答帧类型
byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes); byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes);
@@ -172,7 +175,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
* @param ctx * @param ctx
* @param msg * @param msg
*/ */
private void channelWriteBatch(String pileSn, ChannelHandlerContext ctx, Object msg) { private void channelWriteBatch(String pileSn, Object msg) {
// 获取该桩下的所有channel // 获取该桩下的所有channel
List<ChannelHandlerContext> list = PileChannelEntity.pileMap.get(pileSn); List<ChannelHandlerContext> list = PileChannelEntity.pileMap.get(pileSn);
if(CollectionUtils.isEmpty(list)) { if(CollectionUtils.isEmpty(list)) {
@@ -304,7 +307,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
} }
log.error("【{}】发生了错误, pileSn:【{}】此连接被关闭, 此时连通数量: {}", channelId, pileSn, CHANNEL_MAP.size()); log.error("【{}】发生了错误, pileSn:【{}】此连接被关闭, 此时连通数量: {}", channelId, pileSn, CHANNEL_MAP.size());
// 删除连接 // 删除连接
// PileChannelEntity.deleteChannel(pileSn, ctx); PileChannelEntity.deleteChannel(pileSn, ctx);
ctx.channel().close(); ctx.channel().close();
} }
} finally { } finally {

View File

@@ -88,7 +88,7 @@ public class YKCBusinessServiceImpl implements YKCBusinessService {
pileMsgRecordService.save(pileSn, pileSn, type, jsonMsg, ""); pileMsgRecordService.save(pileSn, pileSn, type, jsonMsg, "");
// 删除连接 // 删除连接
// PileChannelEntity.deleteChannel(pileSn, ctx); PileChannelEntity.deleteChannel(pileSn, ctx);
// 删除桩编号和channel的关系 // 删除桩编号和channel的关系
// PileChannelEntity.removeByPileSn(pileSn); // PileChannelEntity.removeByPileSn(pileSn);

View File

@@ -126,14 +126,14 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
log.error("push发送命令失败, pileSn:{}", pileSn, cause); log.error("push发送命令失败, pileSn:{}", pileSn, cause);
// 异步删除Map中的保存关系(带关闭连接) // 异步删除Map中的保存关系(带关闭连接)
// CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
// try { try {
// // 删除Map中的保存关系 // 删除Map中的保存关系
// PileChannelEntity.deleteChannel(pileSn, ctx); PileChannelEntity.deleteChannel(pileSn, ctx);
// } catch (Exception e) { } catch (Exception e) {
// log.error("异步删除、关闭连接 error", e); log.error("异步删除、关闭连接 error", e);
// } }
// }, executor); }, executor);
} }
}); });
@@ -231,14 +231,14 @@ public class YKCPushCommandServiceImpl implements YKCPushCommandService {
log.error("push发送命令失败, pileSn:{}", pileSn, cause); log.error("push发送命令失败, pileSn:{}", pileSn, cause);
// 异步删除Map中的保存关系(带关闭连接) // 异步删除Map中的保存关系(带关闭连接)
// CompletableFuture.runAsync(() -> { CompletableFuture.runAsync(() -> {
// try { try {
// // 删除Map中的保存关系 // 删除Map中的保存关系
// PileChannelEntity.deleteChannel(pileSn, ctx); PileChannelEntity.deleteChannel(pileSn, ctx);
// } catch (Exception e) { } catch (Exception e) {
// log.error("异步删除、关闭连接 error", e); log.error("异步删除、关闭连接 error", e);
// } }
// }, executor); }, executor);
} }
}); });