This commit is contained in:
guorui 2025-10-21 21:19:58 +08:00
parent 37103ea955
commit 9a6b5aaf18
4 changed files with 8 additions and 47 deletions

View File

@ -122,11 +122,10 @@ namespace YunDa.Server.ISMSTcp.Services
{ {
try try
{ {
// 记录消息处理开始 if (messageType.ToUpper()!= "YC"&& messageType.ToUpper()!= "YX"&& "HeartBeat"!= messageType&& "CommState" != messageType)
var parsedMessageType = ISMSMessageTypeExtensions.ParseMessageType(messageType); {
//_logger.LogInformationWithFilter(_filterControlService, parsedMessageType, Log.Information(messageType + ":" + originalJson);
// "开始处理消息类型: {MessageType}", messageType); }
// 触发TCP响应接收事件 // 触发TCP响应接收事件
TcpResponseReceived?.Invoke(); TcpResponseReceived?.Invoke();
Models.ProcessResult result = messageType.ToUpper() switch Models.ProcessResult result = messageType.ToUpper() switch

View File

@ -447,7 +447,6 @@ namespace YunDa.Server.ISMSTcp.Services
return; return;
} }
// 计算延迟时间(指数退避算法)
var delay = CalculateReconnectDelay(_reconnectAttempts); var delay = CalculateReconnectDelay(_reconnectAttempts);
_logger.LogInformation("Scheduling reconnect attempt #{Attempt} in {Delay} seconds", _logger.LogInformation("Scheduling reconnect attempt #{Attempt} in {Delay} seconds",
@ -461,16 +460,11 @@ namespace YunDa.Server.ISMSTcp.Services
} }
/// <summary> /// <summary>
/// 计算重连延迟时间(指数退避算法 /// 计算重连延迟时间(固定10秒
/// </summary> /// </summary>
private TimeSpan CalculateReconnectDelay(int attemptCount) private TimeSpan CalculateReconnectDelay(int attemptCount)
{ {
// 指数退避初始延迟1秒最大延迟30秒 return TimeSpan.FromSeconds(10);
var baseDelaySeconds = 1.0;
var maxDelaySeconds = 30.0;
var delaySeconds = Math.Min(baseDelaySeconds * Math.Pow(2, attemptCount), maxDelaySeconds);
return TimeSpan.FromSeconds(delaySeconds);
} }
/// <summary> /// <summary>
@ -482,28 +476,6 @@ namespace YunDa.Server.ISMSTcp.Services
{ {
return; return;
} }
// 检查是否超过最大重连次数
if (_reconnectAttempts >= _config.MaxRetries)
{
_logger.LogError("Maximum reconnection attempts ({MaxRetries}) reached. Stopping reconnection attempts.",
_config.MaxRetries);
lock (_reconnectStateLock)
{
_reconnectTimer?.Dispose();
_reconnectTimer = null;
}
ConnectionStatusChanged?.Invoke(false, $"重连失败:已达到最大重连次数 {_config.MaxRetries}");
return;
}
_reconnectAttempts++;
_logger.LogInformation("Attempting reconnection #{Attempt}/{MaxRetries} to {IP}:{Port}",
_reconnectAttempts, _config.MaxRetries, _lastConnectedIp, _lastConnectedPort);
try try
{ {
await _reconnectLock.WaitAsync(); await _reconnectLock.WaitAsync();
@ -512,20 +484,10 @@ namespace YunDa.Server.ISMSTcp.Services
if (success) if (success)
{ {
// 重连成功,重置重连计数器
_reconnectAttempts = 0;
lock (_reconnectStateLock)
{
_reconnectTimer?.Dispose();
_reconnectTimer = null;
}
StartHeartbeat(); StartHeartbeat();
_logger.LogInformation("Successfully reconnected to {IP}:{Port} after {Attempts} attempts", _logger.LogInformation("Successfully reconnected to {IP}:{Port} after {Attempts} attempts",
_lastConnectedIp, _lastConnectedPort, _reconnectAttempts); _lastConnectedIp, _lastConnectedPort, _reconnectAttempts);
ConnectionStatusChanged?.Invoke(true, "重连成功"); ConnectionStatusChanged?.Invoke(true, "重连成功");
} }
else else

View File

@ -9,7 +9,7 @@
} }
}, },
"ISMSServer": { "ISMSServer": {
"ServerIP": "192.168.81.229", "ServerIP": "192.168.81.21",
"HeartbeatIntervalSeconds": 60, "HeartbeatIntervalSeconds": 60,
"AutoConnectDelaySeconds": 10 "AutoConnectDelaySeconds": 10
}, },

View File

@ -7,7 +7,7 @@
} }
}, },
"ISMSServer": { "ISMSServer": {
"ServerIP": "192.168.81.22", "ServerIP": "192.168.81.21",
"ServerPort": 43916, "ServerPort": 43916,
"ReconnectDelaySeconds": 5, "ReconnectDelaySeconds": 5,
"MaxRetries": 50, "MaxRetries": 50,