提交代码
parent
f62d07f1e3
commit
78a9b98a01
File diff suppressed because it is too large
Load Diff
|
|
@ -44,6 +44,8 @@ public class RobotConnectionManager {
|
||||||
private final Map<String, Long> lastActivityTime = new ConcurrentHashMap<>();//记录最后活动时间
|
private final Map<String, Long> lastActivityTime = new ConcurrentHashMap<>();//记录最后活动时间
|
||||||
private final Map<String, ScheduledFuture<?>> connectionWatchers = new ConcurrentHashMap<>();//连接监视器
|
private final Map<String, ScheduledFuture<?>> connectionWatchers = new ConcurrentHashMap<>();//连接监视器
|
||||||
private final Map<String, Long> lastDisconnectTime = 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 MAX_RECONNECT_ATTEMPTS = 3;//最大重连次数(减少重试)
|
||||||
private final int RECONNECT_DELAY_SECONDS = 5;//重连间隔秒数(增加延迟)
|
private final int RECONNECT_DELAY_SECONDS = 5;//重连间隔秒数(增加延迟)
|
||||||
private final int CONNECTION_WATCH_INTERVAL = 15;//连接监视间隔秒数(延长检测间隔)
|
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()) {
|
if (watcher != null && !watcher.isDone()) {
|
||||||
watcher.cancel(false);
|
watcher.cancel(false);
|
||||||
}
|
}
|
||||||
System.out.println("停止连接监视器: {"+connecId+"}");
|
System.out.println("停止连接监视器定时任务: {"+connecId+"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -199,22 +220,32 @@ public class RobotConnectionManager {
|
||||||
* 断开与游戏服务器的连接(主动断开)
|
* 断开与游戏服务器的连接(主动断开)
|
||||||
*/
|
*/
|
||||||
public void disconnectFromGameServer(String connecId) {
|
public void disconnectFromGameServer(String connecId) {
|
||||||
|
System.out.println("开始主动断开连接: {"+connecId+"}");
|
||||||
|
|
||||||
//主动断开 标记为非活跃连接
|
//主动断开 标记为非活跃连接
|
||||||
activeConnections.put(connecId, false);
|
activeConnections.put(connecId, false);
|
||||||
reconnectAttempts.remove(connecId);
|
reconnectAttempts.remove(connecId);
|
||||||
lastActivityTime.remove(connecId);
|
lastActivityTime.remove(connecId);
|
||||||
|
lastDisconnectTime.put(connecId, System.currentTimeMillis());
|
||||||
|
|
||||||
//停止连接监视器
|
//停止连接监视器
|
||||||
stopConnectionWatcher(connecId);
|
stopConnectionWatcher(connecId);
|
||||||
|
|
||||||
|
//移除事件监听器
|
||||||
|
removeEventListeners(connecId);
|
||||||
|
|
||||||
TaurusClient client = gameClients.remove(connecId);
|
TaurusClient client = gameClients.remove(connecId);
|
||||||
if (client != null && client.isConnected()) {
|
if (client != null) {
|
||||||
try {
|
try {
|
||||||
client.killConnection();
|
if (client.isConnected()) {
|
||||||
System.out.println("客户端主动断开连接: {"+connecId+"}");
|
client.killConnection();
|
||||||
|
}
|
||||||
|
System.out.println("客户端主动断开连接完成: {"+connecId+"}");
|
||||||
} catch (Exception e) {
|
} 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) {
|
private void scheduleReconnect(String connecId) {
|
||||||
|
//检查是否仍然是活跃连接
|
||||||
|
if (!activeConnections.getOrDefault(connecId, false)) {
|
||||||
|
System.out.println("连接已非活跃,取消重连安排: {"+connecId+"}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Integer attempts = reconnectAttempts.getOrDefault(connecId, 0);
|
Integer attempts = reconnectAttempts.getOrDefault(connecId, 0);
|
||||||
//最大重连次数
|
//最大重连次数
|
||||||
if (attempts >= 5) {
|
if (attempts >= 5) {
|
||||||
|
|
@ -257,16 +294,19 @@ public class RobotConnectionManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
reconnectAttempts.put(connecId, attempts + 1);
|
reconnectAttempts.put(connecId, attempts + 1);
|
||||||
System.out.println("安排重连任务: {"+connecId+"}, 第{"+attempts + 1+"}次尝试");
|
System.out.println("安排重连任务: {"+connecId+"}, 第{"+(attempts + 1)+"}次尝试");
|
||||||
|
|
||||||
reconnectScheduler.schedule(() -> {
|
reconnectScheduler.schedule(() -> {
|
||||||
try {
|
try {
|
||||||
|
//再次检查连接状态
|
||||||
if (activeConnections.getOrDefault(connecId, false)) {
|
if (activeConnections.getOrDefault(connecId, false)) {
|
||||||
System.out.println("开始重连: {"+connecId+"}");
|
System.out.println("开始重连: {"+connecId+"}");
|
||||||
reconnectToGameServer(connecId);
|
reconnectToGameServer(connecId);
|
||||||
|
} else {
|
||||||
|
System.out.println("重连前检查发现连接已非活跃,取消重连: {"+connecId+"}");
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("重连失败: {"+connecId+"}");
|
System.out.println("重连失败: {"+connecId+"}, 错误: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}, 1, TimeUnit.SECONDS);//重连间隔秒
|
}, 1, TimeUnit.SECONDS);//重连间隔秒
|
||||||
}
|
}
|
||||||
|
|
@ -275,6 +315,12 @@ public class RobotConnectionManager {
|
||||||
* 重新连接到游戏服务器
|
* 重新连接到游戏服务器
|
||||||
*/
|
*/
|
||||||
private void reconnectToGameServer(String connecId) {
|
private void reconnectToGameServer(String connecId) {
|
||||||
|
//检查是否仍然是活跃连接
|
||||||
|
if (!activeConnections.getOrDefault(connecId, false)) {
|
||||||
|
System.out.println("连接已标记为非活跃,取消重连: {"+connecId+"}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//检查是否已经有连接
|
//检查是否已经有连接
|
||||||
TaurusClient existingClient = gameClients.get(connecId);
|
TaurusClient existingClient = gameClients.get(connecId);
|
||||||
|
|
@ -284,7 +330,7 @@ public class RobotConnectionManager {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//移除旧的客户端
|
//移除旧的客户端和事件监听器
|
||||||
if (existingClient != null) {
|
if (existingClient != null) {
|
||||||
try {
|
try {
|
||||||
existingClient.killConnection();
|
existingClient.killConnection();
|
||||||
|
|
@ -293,11 +339,13 @@ public class RobotConnectionManager {
|
||||||
}
|
}
|
||||||
gameClients.remove(connecId);
|
gameClients.remove(connecId);
|
||||||
}
|
}
|
||||||
|
//清理旧的事件监听器
|
||||||
|
removeEventListeners(connecId);
|
||||||
|
|
||||||
//创建新连接
|
//创建新连接
|
||||||
TaurusClient client = new TaurusClient(host + ":" + port, "game", TaurusClient.ConnectionProtocol.Tcp);
|
TaurusClient client = new TaurusClient(host + ":" + port, "game", TaurusClient.ConnectionProtocol.Tcp);
|
||||||
|
|
||||||
//事件监听器
|
//设置新的事件监听器
|
||||||
setupEventListeners(client, connecId);
|
setupEventListeners(client, connecId);
|
||||||
|
|
||||||
client.connect();
|
client.connect();
|
||||||
|
|
@ -311,8 +359,10 @@ public class RobotConnectionManager {
|
||||||
startConnectionWatcher(connecId);
|
startConnectionWatcher(connecId);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("重连过程中发生异常: " + connecId, 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) {
|
private void setupEventListeners(TaurusClient client, String connecId) {
|
||||||
// 添加事件监听器处理网络消息
|
// 先移除可能存在的旧监听器
|
||||||
client.addEventListener(TaurusClient.NetClientEvent.OnEvent, new IEventListener() {
|
removeEventListeners(connecId);
|
||||||
|
|
||||||
|
// 添加消息事件监听器
|
||||||
|
IEventListener messageListener = new IEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void handleEvent(Event event) {
|
public void handleEvent(Event event) {
|
||||||
|
//检查连接是否仍然有效
|
||||||
|
if (!activeConnections.getOrDefault(connecId, false)) {
|
||||||
|
System.out.println("消息监听器: 连接已非活跃,忽略消息: {"+connecId+"}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//获取 msg
|
//获取 msg
|
||||||
Message message = (Message) event.getParameter("msg");
|
Message message = (Message) event.getParameter("msg");
|
||||||
|
|
||||||
|
|
@ -341,14 +400,21 @@ public class RobotConnectionManager {
|
||||||
handleProtocol(command, message, client, connecId);
|
handleProtocol(command, message, client, connecId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
//添加连接状态监听器
|
//添加连接状态监听器
|
||||||
client.addEventListener(TaurusClient.NetClientEvent.Connect, new IEventListener() {
|
IEventListener connectListener = new IEventListener() {
|
||||||
@Override
|
@Override
|
||||||
public void handleEvent(Event event) {
|
public void handleEvent(Event event) {
|
||||||
|
//检查连接是否仍然应该处理事件
|
||||||
|
if (!activeConnections.getOrDefault(connecId, false)) {
|
||||||
|
System.out.println("连接监听器: 连接已标记为非活跃,忽略状态变更: {"+connecId+"}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Message message = (Message) event.getParameter("msg");
|
Message message = (Message) event.getParameter("msg");
|
||||||
SocketCode code = (SocketCode) event.getParameter("code");
|
SocketCode code = (SocketCode) event.getParameter("code");
|
||||||
|
|
||||||
if (code == SocketCode.Connect) {
|
if (code == SocketCode.Connect) {
|
||||||
System.out.println("csmj Connect connecId: " + connecId);
|
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));
|
EXGameController.removeRobotRoomInfo(String.valueOf(robotId));
|
||||||
//更新机器人剩余数量
|
//更新机器人剩余数量
|
||||||
updateLeftoverRobot(gpId, gpid, robotId);
|
updateLeftoverRobot(gpId, gpid, robotId);
|
||||||
|
disconnectFromGameServer(connecId);
|
||||||
jedis0.del(roomKey);
|
jedis0.del(roomKey);
|
||||||
System.out.println("2008结束房间,robotId: {"+robotId+"}");
|
System.out.println("2008结束房间,robotId: {"+robotId+"}");
|
||||||
}
|
}
|
||||||
|
|
@ -832,4 +907,49 @@ public class RobotConnectionManager {
|
||||||
return gameClients.get(connecId);
|
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("所有连接和资源清理完成");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue