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