mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-04-20 02:55:04 +08:00
channel工具类
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
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;
|
||||
@@ -13,7 +18,25 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
public class ChannelManagerUtil {
|
||||
|
||||
// 使用 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 中
|
||||
@@ -21,15 +44,15 @@ public class ChannelManagerUtil {
|
||||
* @param channelId 通道 ID
|
||||
* @param ctx 连接的 Channel 对象
|
||||
*/
|
||||
public static void addChannel(String channelId, ChannelHandlerContext ctx) {
|
||||
if (channelMap.containsKey(channelId)) {
|
||||
log.info("客户端【{}】是连接状态,连接通道数量: {}", channelId, channelMap.size());
|
||||
public static void addChannel(ChannelId channelId, ChannelHandlerContext ctx) {
|
||||
if (CHANNEL_MAP.containsKey(channelId)) {
|
||||
log.info("客户端【{}】是连接状态,连接通道数量: {}", channelId, CHANNEL_MAP.size());
|
||||
} else {
|
||||
channelMap.put(channelId, ctx);
|
||||
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, channelMap.size());
|
||||
log.info("客户端【{}】, 连接netty服务器【IP:{}, PORT:{}】, 连接通道数量: {}", channelId, clientIp, clientPort, CHANNEL_MAP.size());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,11 +61,11 @@ public class ChannelManagerUtil {
|
||||
*
|
||||
* @param channelId 通道 ID
|
||||
*/
|
||||
public static void removeChannel(String channelId) {
|
||||
if (!channelMap.containsKey(channelId)) {
|
||||
public static void removeChannel(ChannelId channelId) {
|
||||
if (!CHANNEL_MAP.containsKey(channelId)) {
|
||||
return;
|
||||
}
|
||||
channelMap.remove(channelId);
|
||||
CHANNEL_MAP.remove(channelId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -51,7 +74,7 @@ public class ChannelManagerUtil {
|
||||
* @return 通道数量
|
||||
*/
|
||||
public static int getChannelCount() {
|
||||
return channelMap.size();
|
||||
return CHANNEL_MAP.size();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +83,7 @@ public class ChannelManagerUtil {
|
||||
* @param channelId 通道 ID
|
||||
* @return Channel 对象或 null 如果不存在
|
||||
*/
|
||||
public static ChannelHandlerContext getChannel(String channelId) {
|
||||
return channelMap.get(channelId);
|
||||
public static ChannelHandlerContext getChannel(ChannelId channelId) {
|
||||
return CHANNEL_MAP.get(channelId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user