channel工具类

This commit is contained in:
Guoqs
2024-11-01 15:26:01 +08:00
parent 6b2a6ce538
commit 52d5761dbb
2 changed files with 37 additions and 14 deletions

View File

@@ -1,6 +1,11 @@
package com.jsowell.common.util; package com.jsowell.common.util;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@@ -13,7 +18,25 @@ import java.util.concurrent.ConcurrentHashMap;
public class ChannelManagerUtil { public class ChannelManagerUtil {
// 使用 ConcurrentHashMap 来保证线程安全 // 使用 ConcurrentHashMap 来保证线程安全
private static final ConcurrentHashMap<String, ChannelHandlerContext> channelMap = new ConcurrentHashMap<>(); private static final ConcurrentHashMap<ChannelId, ChannelHandlerContext> CHANNEL_MAP = new ConcurrentHashMap<>();
private static final ChannelGroup group = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
public static void add(Channel channel) {
group.add(channel);
}
public static Channel find(ChannelId channelId) {
return group.find(channelId);
}
public static void remove(Channel channel) {
group.remove(channel);
}
public static void remove(ChannelId channelId) {
group.remove(channelId);
}
/** /**
* 添加通道到 map 中 * 添加通道到 map 中
@@ -21,15 +44,15 @@ public class ChannelManagerUtil {
* @param channelId 通道 ID * @param channelId 通道 ID
* @param ctx 连接的 Channel 对象 * @param ctx 连接的 Channel 对象
*/ */
public static void addChannel(String channelId, ChannelHandlerContext ctx) { public static void addChannel(ChannelId channelId, ChannelHandlerContext ctx) {
if (channelMap.containsKey(channelId)) { if (CHANNEL_MAP.containsKey(channelId)) {
log.info("客户端【{}】是连接状态,连接通道数量: {}", channelId, channelMap.size()); log.info("客户端【{}】是连接状态,连接通道数量: {}", channelId, CHANNEL_MAP.size());
} else { } else {
channelMap.put(channelId, ctx); CHANNEL_MAP.put(channelId, ctx);
InetSocketAddress socket = (InetSocketAddress) ctx.channel().remoteAddress(); InetSocketAddress socket = (InetSocketAddress) ctx.channel().remoteAddress();
String clientIp = socket.getAddress().getHostAddress(); String clientIp = socket.getAddress().getHostAddress();
int clientPort = socket.getPort(); int clientPort = socket.getPort();
log.info("客户端【{}】, 连接netty服务器【IP:{}, PORT:{}】, 连接通道数量: {}", channelId, clientIp, clientPort, channelMap.size()); log.info("客户端【{}】, 连接netty服务器【IP:{}, PORT:{}】, 连接通道数量: {}", channelId, clientIp, clientPort, CHANNEL_MAP.size());
} }
} }
@@ -38,11 +61,11 @@ public class ChannelManagerUtil {
* *
* @param channelId 通道 ID * @param channelId 通道 ID
*/ */
public static void removeChannel(String channelId) { public static void removeChannel(ChannelId channelId) {
if (!channelMap.containsKey(channelId)) { if (!CHANNEL_MAP.containsKey(channelId)) {
return; return;
} }
channelMap.remove(channelId); CHANNEL_MAP.remove(channelId);
} }
/** /**
@@ -51,7 +74,7 @@ public class ChannelManagerUtil {
* @return 通道数量 * @return 通道数量
*/ */
public static int getChannelCount() { public static int getChannelCount() {
return channelMap.size(); return CHANNEL_MAP.size();
} }
/** /**
@@ -60,7 +83,7 @@ public class ChannelManagerUtil {
* @param channelId 通道 ID * @param channelId 通道 ID
* @return Channel 对象或 null 如果不存在 * @return Channel 对象或 null 如果不存在
*/ */
public static ChannelHandlerContext getChannel(String channelId) { public static ChannelHandlerContext getChannel(ChannelId channelId) {
return channelMap.get(channelId); return CHANNEL_MAP.get(channelId);
} }
} }

View File

@@ -45,7 +45,7 @@ public class ElectricBicyclesServerHandler extends ChannelInboundHandlerAdapter
//获取连接通道唯一标识 //获取连接通道唯一标识
ChannelId channelId = ctx.channel().id(); ChannelId channelId = ctx.channel().id();
//如果map中不包含此连接就保存连接 //如果map中不包含此连接就保存连接
ChannelManagerUtil.addChannel(channelId.asShortText(), ctx); ChannelManagerUtil.addChannel(channelId, ctx);
// if (CHANNEL_MAP.containsKey(channelId)) { // if (CHANNEL_MAP.containsKey(channelId)) {
// log.info("客户端【{}】是连接状态,连接通道数量: {}", channelId, CHANNEL_MAP.size()); // log.info("客户端【{}】是连接状态,连接通道数量: {}", channelId, CHANNEL_MAP.size());
// } else { // } else {
@@ -90,7 +90,7 @@ public class ElectricBicyclesServerHandler extends ChannelInboundHandlerAdapter
String clientIp = insocket.getAddress().getHostAddress(); String clientIp = insocket.getAddress().getHostAddress();
ChannelId channelId = ctx.channel().id(); ChannelId channelId = ctx.channel().id();
//包含此客户端才去删除 //包含此客户端才去删除
ChannelManagerUtil.removeChannel(channelId.asShortText()); ChannelManagerUtil.removeChannel(channelId);
// if (CHANNEL_MAP.containsKey(channelId)) { // if (CHANNEL_MAP.containsKey(channelId)) {
// ykcService.exit(channelId); // ykcService.exit(channelId);
// //删除连接 // //删除连接