提交代码

master
zhouwei 2026-02-04 13:21:26 +08:00
parent f62d07f1e3
commit 78a9b98a01
2 changed files with 136 additions and 1802 deletions

File diff suppressed because it is too large Load Diff

View File

@ -44,6 +44,8 @@ public class RobotConnectionManager {
private final Map<String, Long> lastActivityTime = new ConcurrentHashMap<>();//记录最后活动时间
private final Map<String, ScheduledFuture<?>> connectionWatchers = new ConcurrentHashMap<>();//连接监视器
private final Map<String, Long> lastDisconnectTime = new ConcurrentHashMap<>();//记录最后断开时间
private final Map<String, IEventListener> connectEventListeners = new ConcurrentHashMap<>();//连接事件监听器
private final Map<String, IEventListener> messageEventListeners = new ConcurrentHashMap<>();//消息事件监听器
private final int MAX_RECONNECT_ATTEMPTS = 3;//最大重连次数(减少重试)
private final int RECONNECT_DELAY_SECONDS = 5;//重连间隔秒数(增加延迟)
private final int CONNECTION_WATCH_INTERVAL = 15;//连接监视间隔秒数(延长检测间隔)
@ -138,6 +140,25 @@ public class RobotConnectionManager {
}
}
/**
*
*/
private void removeEventListeners(String connecId) {
TaurusClient client = gameClients.get(connecId);
if (client != null) {
IEventListener messageListener = messageEventListeners.remove(connecId);
IEventListener connectListener = connectEventListeners.remove(connecId);
if (messageListener != null) {
client.removeEventListener(TaurusClient.NetClientEvent.OnEvent, messageListener);
}
if (connectListener != null) {
client.removeEventListener(TaurusClient.NetClientEvent.Connect, connectListener);
}
}
System.out.println("移除事件监听器: {"+connecId+"}");
}
/**
*
*/
@ -146,7 +167,7 @@ public class RobotConnectionManager {
if (watcher != null && !watcher.isDone()) {
watcher.cancel(false);
}
System.out.println("停止连接监视器: {"+connecId+"}");
System.out.println("停止连接监视器定时任务: {"+connecId+"}");
}
/**
@ -199,22 +220,32 @@ public class RobotConnectionManager {
*
*/
public void disconnectFromGameServer(String connecId) {
System.out.println("开始主动断开连接: {"+connecId+"}");
//主动断开 标记为非活跃连接
activeConnections.put(connecId, false);
reconnectAttempts.remove(connecId);
lastActivityTime.remove(connecId);
lastDisconnectTime.put(connecId, System.currentTimeMillis());
//停止连接监视器
stopConnectionWatcher(connecId);
//移除事件监听器
removeEventListeners(connecId);
TaurusClient client = gameClients.remove(connecId);
if (client != null && client.isConnected()) {
if (client != null) {
try {
client.killConnection();
System.out.println("客户端主动断开连接: {"+connecId+"}");
if (client.isConnected()) {
client.killConnection();
}
System.out.println("客户端主动断开连接完成: {"+connecId+"}");
} catch (Exception e) {
System.out.println("断开客户端连接时发生异常" + connecId);
System.out.println("断开客户端连接时发生异常: " + connecId + ", 错误: " + e.getMessage());
}
} else {
System.out.println("客户端连接不存在: {"+connecId+"}");
}
}
@ -248,6 +279,12 @@ public class RobotConnectionManager {
*
*/
private void scheduleReconnect(String connecId) {
//检查是否仍然是活跃连接
if (!activeConnections.getOrDefault(connecId, false)) {
System.out.println("连接已非活跃,取消重连安排: {"+connecId+"}");
return;
}
Integer attempts = reconnectAttempts.getOrDefault(connecId, 0);
//最大重连次数
if (attempts >= 5) {
@ -257,16 +294,19 @@ public class RobotConnectionManager {
}
reconnectAttempts.put(connecId, attempts + 1);
System.out.println("安排重连任务: {"+connecId+"}, 第{"+attempts + 1+"}次尝试");
System.out.println("安排重连任务: {"+connecId+"}, 第{"+(attempts + 1)+"}次尝试");
reconnectScheduler.schedule(() -> {
try {
//再次检查连接状态
if (activeConnections.getOrDefault(connecId, false)) {
System.out.println("开始重连: {"+connecId+"}");
reconnectToGameServer(connecId);
} else {
System.out.println("重连前检查发现连接已非活跃,取消重连: {"+connecId+"}");
}
} catch (Exception e) {
System.out.println("重连失败: {"+connecId+"}");
System.out.println("重连失败: {"+connecId+"}, 错误: " + e.getMessage());
}
}, 1, TimeUnit.SECONDS);//重连间隔秒
}
@ -275,6 +315,12 @@ public class RobotConnectionManager {
*
*/
private void reconnectToGameServer(String connecId) {
//检查是否仍然是活跃连接
if (!activeConnections.getOrDefault(connecId, false)) {
System.out.println("连接已标记为非活跃,取消重连: {"+connecId+"}");
return;
}
try {
//检查是否已经有连接
TaurusClient existingClient = gameClients.get(connecId);
@ -284,7 +330,7 @@ public class RobotConnectionManager {
return;
}
//移除旧的客户端
//移除旧的客户端和事件监听器
if (existingClient != null) {
try {
existingClient.killConnection();
@ -293,11 +339,13 @@ public class RobotConnectionManager {
}
gameClients.remove(connecId);
}
//清理旧的事件监听器
removeEventListeners(connecId);
//创建新连接
TaurusClient client = new TaurusClient(host + ":" + port, "game", TaurusClient.ConnectionProtocol.Tcp);
//事件监听器
//设置新的事件监听器
setupEventListeners(client, connecId);
client.connect();
@ -311,8 +359,10 @@ public class RobotConnectionManager {
startConnectionWatcher(connecId);
} catch (Exception e) {
log.error("重连过程中发生异常: " + connecId, e);
//继续安排下次重连
scheduleReconnect(connecId);
//检查是否仍然是活跃连接后再安排下次重连
if (activeConnections.getOrDefault(connecId, false)) {
scheduleReconnect(connecId);
}
}
}
@ -320,10 +370,19 @@ public class RobotConnectionManager {
*
*/
private void setupEventListeners(TaurusClient client, String connecId) {
// 添加事件监听器处理网络消息
client.addEventListener(TaurusClient.NetClientEvent.OnEvent, new IEventListener() {
// 先移除可能存在的旧监听器
removeEventListeners(connecId);
// 添加消息事件监听器
IEventListener messageListener = new IEventListener() {
@Override
public void handleEvent(Event event) {
//检查连接是否仍然有效
if (!activeConnections.getOrDefault(connecId, false)) {
System.out.println("消息监听器: 连接已非活跃,忽略消息: {"+connecId+"}");
return;
}
//获取 msg
Message message = (Message) event.getParameter("msg");
@ -341,14 +400,21 @@ public class RobotConnectionManager {
handleProtocol(command, message, client, connecId);
}
}
});
};
//添加连接状态监听器
client.addEventListener(TaurusClient.NetClientEvent.Connect, new IEventListener() {
IEventListener connectListener = new IEventListener() {
@Override
public void handleEvent(Event event) {
//检查连接是否仍然应该处理事件
if (!activeConnections.getOrDefault(connecId, false)) {
System.out.println("连接监听器: 连接已标记为非活跃,忽略状态变更: {"+connecId+"}");
return;
}
Message message = (Message) event.getParameter("msg");
SocketCode code = (SocketCode) event.getParameter("code");
if (code == SocketCode.Connect) {
System.out.println("csmj Connect connecId: " + connecId);
//连接成功 标记为活跃连接并重置重连次数
@ -371,7 +437,15 @@ public class RobotConnectionManager {
}
}
}
});
};
//注册事件监听器
client.addEventListener(TaurusClient.NetClientEvent.OnEvent, messageListener);
client.addEventListener(TaurusClient.NetClientEvent.Connect, connectListener);
//保存监听器引用以便后续移除
messageEventListeners.put(connecId, messageListener);
connectEventListeners.put(connecId, connectListener);
}
/**
@ -635,6 +709,7 @@ public class RobotConnectionManager {
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
//更新机器人剩余数量
updateLeftoverRobot(gpId, gpid, robotId);
disconnectFromGameServer(connecId);
jedis0.del(roomKey);
System.out.println("2008结束房间robotId: {"+robotId+"}");
}
@ -832,4 +907,49 @@ public class RobotConnectionManager {
return gameClients.get(connecId);
}
/**
*
*/
public void cleanupAllConnections() {
System.out.println("开始清理所有连接和资源...");
//标记所有连接为非活跃
for (String connecId : activeConnections.keySet()) {
activeConnections.put(connecId, false);
}
//断开所有连接
for (String connecId : new ArrayList<>(gameClients.keySet())) {
disconnectFromGameServer(connecId);
}
//停止所有监视器
for (String connecId : new ArrayList<>(connectionWatchers.keySet())) {
stopConnectionWatcher(connecId);
}
//关闭调度器
reconnectScheduler.shutdown();
try {
if (!reconnectScheduler.awaitTermination(5, TimeUnit.SECONDS)) {
reconnectScheduler.shutdownNow();
}
} catch (InterruptedException e) {
reconnectScheduler.shutdownNow();
Thread.currentThread().interrupt();
}
//清理所有缓存数据
activeConnections.clear();
reconnectAttempts.clear();
lastActivityTime.clear();
lastDisconnectTime.clear();
connectionWatchers.clear();
connectEventListeners.clear();
messageEventListeners.clear();
gameClients.clear();
System.out.println("所有连接和资源清理完成");
}
}