diff --git a/CoreAgent.Infrastructure/Services/Network/ProtocolWsClientManager.cs b/CoreAgent.Infrastructure/Services/Network/ProtocolWsClientManager.cs
index f1f732b..1d95608 100644
--- a/CoreAgent.Infrastructure/Services/Network/ProtocolWsClientManager.cs
+++ b/CoreAgent.Infrastructure/Services/Network/ProtocolWsClientManager.cs
@@ -48,20 +48,19 @@ namespace CoreAgent.Infrastructure.Services.Network
return new ProtocolWsClient(config, _loggerFactory, _protocolLogObserver);
}
-
-
///
/// 启动所有协议客户端
///
/// 协议客户端配置数组
- public void StartAllClients(ProtocolClientConfig[] configs)
+ /// 是否所有客户端都成功启动并连接
+ 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;
}
}
///
/// 停止所有协议客户端
///
- public void StopAllClients()
+ /// 是否所有客户端都成功停止并断开连接
+ 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;
}
}
-
+ ///
+ /// 获取所有客户端状态
+ ///
+ 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);
+ }
+ }
+ }
///
/// 释放所有客户端资源
@@ -194,7 +245,5 @@ namespace CoreAgent.Infrastructure.Services.Network
throw new ObjectDisposedException(nameof(ProtocolWsClientManager));
}
}
-
-
}
}
\ No newline at end of file
diff --git a/CoreAgent.ProtocolClient/Interfaces/IProtocolWsClientManager.cs b/CoreAgent.ProtocolClient/Interfaces/IProtocolWsClientManager.cs
index 8525d69..08c53b8 100644
--- a/CoreAgent.ProtocolClient/Interfaces/IProtocolWsClientManager.cs
+++ b/CoreAgent.ProtocolClient/Interfaces/IProtocolWsClientManager.cs
@@ -12,11 +12,13 @@ namespace CoreAgent.ProtocolClient.Interfaces
/// 启动所有协议客户端
///
/// 协议客户端配置数组
- void StartAllClients(ProtocolClientConfig[] configs);
+ /// 是否所有客户端都成功启动并连接
+ bool StartAllClients(ProtocolClientConfig[] configs);
///
/// 停止所有协议客户端
///
- void StopAllClients();
+ /// 是否所有客户端都成功停止并断开连接
+ bool StopAllClients();
}
}
\ No newline at end of file
diff --git a/modify.md b/modify.md
index c649662..40af7ff 100644
--- a/modify.md
+++ b/modify.md
@@ -368,4 +368,108 @@
**修改时间**: 2024年
**修改文件**:
-- `
\ No newline at end of file
+- `
+
+### ProtocolWsClientManager方法返回类型优化
+
+**修改时间**: 2024年
+**修改文件**:
+- `CoreAgent.Infrastructure/Services/Network/ProtocolWsClientManager.cs`
+- `CoreAgent.ProtocolClient/Interfaces/IProtocolWsClientManager.cs`
+
+**修改内容**:
+
+1. **StartAllClients方法返回类型修改**
+ - 将返回类型从 `void` 改为 `bool`
+ - 添加连接状态检查逻辑,使用 `client.IsConnected` 判断连接状态
+ - 统计已连接的客户端数量
+ - 返回 `bool` 值表示是否所有客户端都成功启动并连接
+ - 添加详细的日志记录,包括连接状态信息
+
+2. **StopAllClients方法返回类型修改**
+ - 将返回类型从 `void` 改为 `bool`
+ - 添加断开连接状态检查逻辑,使用 `client.IsConnected` 判断连接状态
+ - 记录停止前的连接状态和停止后的连接状态
+ - 统计已断开连接的客户端数量
+ - 返回 `bool` 值表示是否所有客户端都成功停止并断开连接
+ - 添加详细的日志记录,包括连接状态变化信息
+
+3. **GetAllClientsStatus方法修复**
+ - 修复语法错误,完善方法实现
+ - 添加线程安全锁保护
+ - 遍历所有客户端并记录其状态信息
+ - 包括客户端名称、连接状态(IsConnected)和客户端状态(State)
+ - 添加空客户端检查
+
+4. **接口定义更新**
+ - 更新 `IProtocolWsClientManager` 接口中的方法签名
+ - `StartAllClients` 方法返回 `bool` 类型
+ - `StopAllClients` 方法返回 `bool` 类型
+ - 添加详细的XML文档注释说明返回值含义
+
+5. **具体实现**:
+ ```csharp
+ // StartAllClients方法
+ public bool StartAllClients(ProtocolClientConfig[] configs)
+ {
+ // 检查连接状态
+ if (existingClient.IsConnected)
+ {
+ connectedCount++;
+ }
+
+ var allConnected = connectedCount == configs.Length;
+ return allConnected;
+ }
+
+ // StopAllClients方法
+ public bool StopAllClients()
+ {
+ var client = kvp.Value;
+ var wasConnected = client.IsConnected;
+
+ client.Stop();
+
+ // 检查连接状态
+ if (!client.IsConnected)
+ {
+ disconnectedCount++;
+ }
+
+ var allDisconnected = disconnectedCount == _clients.Count;
+ return allDisconnected;
+ }
+
+ // GetAllClientsStatus方法
+ public void GetAllClientsStatus()
+ {
+ foreach (var kvp in _clients)
+ {
+ var client = kvp.Value;
+ _logger.LogInformation("客户端状态 - 名称: {ClientName}, 连接状态: {IsConnected}, 客户端状态: {State}",
+ kvp.Key, client.IsConnected, client.State);
+ }
+ }
+ ```
+
+6. **设计优势**:
+ - **状态可追踪**:通过返回值可以明确知道操作是否完全成功
+ - **连接状态监控**:使用 `IsConnected` 属性准确判断连接状态
+ - **详细日志记录**:提供完整的操作过程和状态变化日志
+ - **线程安全**:使用锁保护共享资源访问
+ - **错误处理完善**:提供详细的错误信息和状态统计
+ - **接口一致性**:接口和实现保持完全一致
+ - **向后兼容**:保持方法签名的一致性,只改变返回类型
+
+7. **返回值含义**:
+ - `StartAllClients` 返回 `true`:所有客户端都成功启动并连接
+ - `StartAllClients` 返回 `false`:部分或全部客户端启动失败或未连接
+ - `StopAllClients` 返回 `true`:所有客户端都成功停止并断开连接
+ - `StopAllClients` 返回 `false`:部分或全部客户端停止失败或仍保持连接
+
+**影响范围**:
+- 协议客户端管理器接口契约
+- 调用方代码需要处理返回值
+- 网络启动和停止流程的状态判断
+- 日志记录详细程度提升
+- 错误处理和状态监控能力增强
\ No newline at end of file