update 将桩与连接channel的关系改为一对多

This commit is contained in:
Lemon
2025-05-27 10:34:35 +08:00
parent 8ec72f0f12
commit 9731a53205
6 changed files with 333 additions and 9 deletions

View File

@@ -149,6 +149,8 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
// 响应客户端
ByteBuf buffer = ctx.alloc().buffer().writeBytes(response);
this.channelWrite(channel.id(), buffer);
// 批量响应客户端
// this.channelWriteBatch(ctx, buffer);
if (!CollectionUtils.containsAny(notPrintFrameTypeList, frameType)) {
// 应答帧类型
byte[] responseFrameTypeBytes = YKCFrameTypeCode.PlatformAnswersRelation.getResponseFrameTypeBytes(frameTypeBytes);
@@ -164,6 +166,29 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
}
}
/**
* 批量发送信息
* @param pileSn
* @param ctx
* @param msg
*/
private void channelWriteBatch(String pileSn, ChannelHandlerContext ctx, Object msg) {
// 获取该桩下的所有channel
List<ChannelHandlerContext> list = PileChannelEntity.pileMap.get(pileSn);
if(CollectionUtils.isEmpty(list)) {
return;
}
// 批量写入
for (ChannelHandlerContext context : list) {
context.write(msg);
//刷新缓存区
context.flush();
}
// 如果通道不存在,则将连接删除
}
// @Override
// protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// log.info("channelRead0=== channelId:" + ctx.channel().id() + ", msg:" + msg);
@@ -179,7 +204,7 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
ChannelId channelId = ctx.channel().id();
//包含此客户端才去删除
if (CHANNEL_MAP.containsKey(channelId)) {
ykcService.exit(channelId);
ykcService.exit(ctx);
//删除连接
CHANNEL_MAP.remove(channelId);
log.info("客户端【{}】, 退出netty服务器【IP:{}, PORT:{}】, 连接通道数量: {}", channelId, clientIp, insocket.getPort(), CHANNEL_MAP.size());
@@ -278,6 +303,8 @@ public class NettyServerHandler extends ChannelInboundHandlerAdapter {
log.trace("Connection timeout 【{}】", ctx.channel().remoteAddress());
}
log.error("【{}】发生了错误, pileSn:【{}】此连接被关闭, 此时连通数量: {}", channelId, pileSn, CHANNEL_MAP.size());
// 删除连接
// PileChannelEntity.deleteChannel(pileSn, ctx);
ctx.channel().close();
}
} finally {