2024-08-20 16:13:32 +08:00
|
|
|
|
package com.jsowell.common.util;
|
|
|
|
|
|
|
2024-11-01 15:26:01 +08:00
|
|
|
|
import io.netty.channel.Channel;
|
2024-08-20 16:13:32 +08:00
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
2024-11-01 15:26:01 +08:00
|
|
|
|
import io.netty.channel.ChannelId;
|
|
|
|
|
|
import io.netty.channel.group.ChannelGroup;
|
|
|
|
|
|
import io.netty.channel.group.DefaultChannelGroup;
|
|
|
|
|
|
import io.netty.util.concurrent.GlobalEventExecutor;
|
2024-08-20 16:13:32 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
|
|
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 管理一个全局map,保存连接进服务端的通道数量
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
public class ChannelManagerUtil {
|
|
|
|
|
|
|
|
|
|
|
|
// 使用 ConcurrentHashMap 来保证线程安全
|
2024-11-01 15:26:01 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2024-08-20 16:13:32 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加通道到 map 中
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param channelId 通道 ID
|
|
|
|
|
|
* @param ctx 连接的 Channel 对象
|
|
|
|
|
|
*/
|
2024-11-01 15:26:01 +08:00
|
|
|
|
public static void addChannel(ChannelId channelId, ChannelHandlerContext ctx) {
|
|
|
|
|
|
if (CHANNEL_MAP.containsKey(channelId)) {
|
|
|
|
|
|
log.info("客户端【{}】是连接状态,连接通道数量: {}", channelId, CHANNEL_MAP.size());
|
2024-08-20 16:13:32 +08:00
|
|
|
|
} else {
|
2024-11-01 15:26:01 +08:00
|
|
|
|
CHANNEL_MAP.put(channelId, ctx);
|
2024-08-20 16:13:32 +08:00
|
|
|
|
InetSocketAddress socket = (InetSocketAddress) ctx.channel().remoteAddress();
|
|
|
|
|
|
String clientIp = socket.getAddress().getHostAddress();
|
|
|
|
|
|
int clientPort = socket.getPort();
|
2024-11-01 15:26:01 +08:00
|
|
|
|
log.info("客户端【{}】, 连接netty服务器【IP:{}, PORT:{}】, 连接通道数量: {}", channelId, clientIp, clientPort, CHANNEL_MAP.size());
|
2024-08-20 16:13:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 移除指定的通道
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param channelId 通道 ID
|
|
|
|
|
|
*/
|
2024-11-01 15:26:01 +08:00
|
|
|
|
public static void removeChannel(ChannelId channelId) {
|
|
|
|
|
|
if (!CHANNEL_MAP.containsKey(channelId)) {
|
2024-08-27 16:30:05 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-11-01 15:26:01 +08:00
|
|
|
|
CHANNEL_MAP.remove(channelId);
|
2024-08-20 16:13:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前连接的通道数量
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return 通道数量
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static int getChannelCount() {
|
2024-11-01 15:26:01 +08:00
|
|
|
|
return CHANNEL_MAP.size();
|
2024-08-20 16:13:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 根据通道 ID 获取 Channel 对象
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param channelId 通道 ID
|
|
|
|
|
|
* @return Channel 对象或 null 如果不存在
|
|
|
|
|
|
*/
|
2024-11-01 15:26:01 +08:00
|
|
|
|
public static ChannelHandlerContext getChannel(ChannelId channelId) {
|
|
|
|
|
|
return CHANNEL_MAP.get(channelId);
|
2024-08-20 16:13:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|