|
|
@ -48,20 +48,19 @@ namespace CoreAgent.Infrastructure.Services.Network |
|
|
|
return new ProtocolWsClient(config, _loggerFactory, _protocolLogObserver); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 启动所有协议客户端
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="configs">协议客户端配置数组</param>
|
|
|
|
public void StartAllClients(ProtocolClientConfig[] configs) |
|
|
|
/// <returns>是否所有客户端都成功启动并连接</returns>
|
|
|
|
public bool StartAllClients(ProtocolClientConfig[] configs) |
|
|
|
{ |
|
|
|
ThrowIfDisposed(); |
|
|
|
|
|
|
|
if (configs == null || configs.Length == 0) |
|
|
|
{ |
|
|
|
_logger.LogWarning("没有可用的协议客户端配置"); |
|
|
|
return; |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
lock (_lock) |
|
|
@ -70,6 +69,7 @@ namespace CoreAgent.Infrastructure.Services.Network |
|
|
|
|
|
|
|
var startedCount = 0; |
|
|
|
var failedCount = 0; |
|
|
|
var connectedCount = 0; |
|
|
|
|
|
|
|
foreach (var config in configs) |
|
|
|
{ |
|
|
@ -84,7 +84,15 @@ namespace CoreAgent.Infrastructure.Services.Network |
|
|
|
var existingClient = _clients[config.Name]; |
|
|
|
existingClient.Start(); |
|
|
|
startedCount++; |
|
|
|
_logger.LogInformation("启动协议客户端成功: {ClientName}", config.Name); |
|
|
|
|
|
|
|
// 检查连接状态
|
|
|
|
if (existingClient.IsConnected) |
|
|
|
{ |
|
|
|
connectedCount++; |
|
|
|
} |
|
|
|
|
|
|
|
_logger.LogInformation("启动协议客户端成功: {ClientName}, 连接状态: {IsConnected}", |
|
|
|
config.Name, existingClient.IsConnected); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
@ -93,35 +101,52 @@ namespace CoreAgent.Infrastructure.Services.Network |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_logger.LogInformation("协议客户端启动完成 - 成功: {StartedCount}, 失败: {FailedCount}", startedCount, failedCount); |
|
|
|
var allConnected = connectedCount == configs.Length; |
|
|
|
_logger.LogInformation("协议客户端启动完成 - 成功: {StartedCount}, 失败: {FailedCount}, 已连接: {ConnectedCount}, 全部连接: {AllConnected}", |
|
|
|
startedCount, failedCount, connectedCount, allConnected); |
|
|
|
|
|
|
|
return allConnected; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 停止所有协议客户端
|
|
|
|
/// </summary>
|
|
|
|
public void StopAllClients() |
|
|
|
/// <returns>是否所有客户端都成功停止并断开连接</returns>
|
|
|
|
public bool StopAllClients() |
|
|
|
{ |
|
|
|
lock (_lock) |
|
|
|
{ |
|
|
|
if (_clients.Count == 0) |
|
|
|
{ |
|
|
|
_logger.LogInformation("没有运行中的协议客户端"); |
|
|
|
return; |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
_logger.LogInformation("开始停止所有协议客户端,客户端数量: {ClientCount}", _clients.Count); |
|
|
|
|
|
|
|
var stoppedCount = 0; |
|
|
|
var failedCount = 0; |
|
|
|
var disconnectedCount = 0; |
|
|
|
|
|
|
|
foreach (var kvp in _clients) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
kvp.Value.Stop(); |
|
|
|
var client = kvp.Value; |
|
|
|
var wasConnected = client.IsConnected; |
|
|
|
|
|
|
|
client.Stop(); |
|
|
|
stoppedCount++; |
|
|
|
_logger.LogInformation("停止协议客户端成功: {ClientName}", kvp.Key); |
|
|
|
|
|
|
|
// 检查连接状态
|
|
|
|
if (!client.IsConnected) |
|
|
|
{ |
|
|
|
disconnectedCount++; |
|
|
|
} |
|
|
|
|
|
|
|
_logger.LogInformation("停止协议客户端成功: {ClientName}, 原连接状态: {WasConnected}, 当前连接状态: {IsConnected}", |
|
|
|
kvp.Key, wasConnected, client.IsConnected); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
@ -130,11 +155,37 @@ namespace CoreAgent.Infrastructure.Services.Network |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
_logger.LogInformation("协议客户端停止完成 - 成功: {StoppedCount}, 失败: {FailedCount}", stoppedCount, failedCount); |
|
|
|
var allDisconnected = disconnectedCount == _clients.Count; |
|
|
|
_logger.LogInformation("协议客户端停止完成 - 成功: {StoppedCount}, 失败: {FailedCount}, 已断开: {DisconnectedCount}, 全部断开: {AllDisconnected}", |
|
|
|
stoppedCount, failedCount, disconnectedCount, allDisconnected); |
|
|
|
|
|
|
|
return allDisconnected; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 获取所有客户端状态
|
|
|
|
/// </summary>
|
|
|
|
public void GetAllClientsStatus() |
|
|
|
{ |
|
|
|
lock (_lock) |
|
|
|
{ |
|
|
|
if (_clients.Count == 0) |
|
|
|
{ |
|
|
|
_logger.LogInformation("没有运行中的协议客户端"); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
_logger.LogInformation("获取所有协议客户端状态,客户端数量: {ClientCount}", _clients.Count); |
|
|
|
|
|
|
|
foreach (var kvp in _clients) |
|
|
|
{ |
|
|
|
var client = kvp.Value; |
|
|
|
_logger.LogInformation("客户端状态 - 名称: {ClientName}, 连接状态: {IsConnected}, 客户端状态: {State}", |
|
|
|
kvp.Key, client.IsConnected, client.State); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 释放所有客户端资源
|
|
|
@ -194,7 +245,5 @@ namespace CoreAgent.Infrastructure.Services.Network |
|
|
|
throw new ObjectDisposedException(nameof(ProtocolWsClientManager)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |