Browse Source
- 移除不必要的公共方法(AddAndStartClient、GetAllClients、GetClient等) - 保留核心功能:StartAllClients和StopAllClients - 将CreateProtocolWsClient改为私有方法 - 更新类注释,明确单一职责 - 保持线程安全和资源管理feature/protocol-log-Perfect
12 changed files with 464 additions and 18 deletions
@ -0,0 +1,27 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using CoreAgent.ProtocolClient.Models; |
|||
using CoreAgent.ProtocolClient.ProtocolEngineCore; |
|||
using CoreAgent.WebSocketTransport.Interfaces; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace CoreAgent.Infrastructure.Services.Network |
|||
{ |
|||
public class NetworkProtocolLogObserver : IProtocolLogObserver |
|||
{ |
|||
private readonly ILogger<NetworkProtocolLogObserver> _logger; |
|||
private readonly IMessageChannelManager _ChannelManager; |
|||
public NetworkProtocolLogObserver(ILogger<NetworkProtocolLogObserver> logger, IMessageChannelManager channelManager) |
|||
{ |
|||
this._logger = logger; |
|||
this._ChannelManager = channelManager; |
|||
} |
|||
public void OnProtocolLogsReceived(IEnumerable<TransferProtocolLog> logDetails) |
|||
{ |
|||
_ChannelManager.SendChannel.TryWrite(logDetails); |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,59 @@ |
|||
using CoreAgent.ProtocolClient.Models; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace CoreAgent.Infrastructure.Services.Network |
|||
{ |
|||
/// <summary>
|
|||
/// 协议客户端配置工厂
|
|||
/// 负责从其他实体组装ProtocolClientConfig
|
|||
/// </summary>
|
|||
public class ProtocolClientConfigFactory |
|||
{ |
|||
private readonly ILogger<ProtocolClientConfigFactory> _logger; |
|||
private readonly List<ProtocolClientConfig> _configs; |
|||
|
|||
public ProtocolClientConfigFactory(ILogger<ProtocolClientConfigFactory> logger) |
|||
{ |
|||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
|||
_configs = new List<ProtocolClientConfig>(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 从多个实体创建协议客户端配置数组
|
|||
/// </summary>
|
|||
/// <param name="entities">实体列表</param>
|
|||
/// <returns>协议客户端配置数组</returns>
|
|||
public ProtocolClientConfig[] CreateFromEntities(IEnumerable<object> entities) |
|||
{ |
|||
// TODO: 实现从多个实体组装ProtocolClientConfig数组的逻辑
|
|||
_logger.LogInformation("从多个实体创建协议客户端配置数组"); |
|||
|
|||
// 清空现有配置
|
|||
_configs.Clear(); |
|||
|
|||
// TODO: 实现具体的组装逻辑
|
|||
// foreach (var entity in entities)
|
|||
// {
|
|||
// var config = CreateConfigFromEntity(entity);
|
|||
// _configs.Add(config);
|
|||
// }
|
|||
|
|||
throw new NotImplementedException("需要实现从多个实体创建协议客户端配置数组的逻辑"); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取所有协议客户端配置
|
|||
/// </summary>
|
|||
/// <returns>协议客户端配置数组</returns>
|
|||
public ProtocolClientConfig[] GetAllConfigs() |
|||
{ |
|||
_logger.LogInformation("获取所有协议客户端配置,数量: {Count}", _configs.Count); |
|||
return _configs.ToArray(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 获取配置数量
|
|||
/// </summary>
|
|||
public int ConfigCount => _configs.Count; |
|||
} |
|||
} |
@ -0,0 +1,202 @@ |
|||
using CoreAgent.ProtocolClient.Models; |
|||
using CoreAgent.ProtocolClient.ProtocolEngineCore; |
|||
using CoreAgent.ProtocolClient.ProtocolWsClient; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace CoreAgent.Infrastructure.Services.Network |
|||
{ |
|||
/// <summary>
|
|||
/// 协议WebSocket客户端管理器
|
|||
/// 负责启动和停止所有协议客户端
|
|||
/// </summary>
|
|||
public class ProtocolWsClientManager : IDisposable |
|||
{ |
|||
private readonly ILogger<ProtocolWsClientManager> _logger; |
|||
private readonly IProtocolLogObserver _protocolLogObserver; |
|||
private readonly ProtocolClientConfigFactory _configFactory; |
|||
private readonly Dictionary<string, ProtocolWsClient> _clients; |
|||
private readonly ILoggerFactory _loggerFactory; |
|||
private readonly object _lock = new object(); |
|||
private bool _disposed = false; |
|||
|
|||
public ProtocolWsClientManager( |
|||
ILogger<ProtocolWsClientManager> logger, |
|||
IProtocolLogObserver protocolLogObserver, |
|||
ProtocolClientConfigFactory configFactory, |
|||
ILoggerFactory loggerFactory) |
|||
{ |
|||
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
|||
_protocolLogObserver = protocolLogObserver ?? throw new ArgumentNullException(nameof(protocolLogObserver)); |
|||
_configFactory = configFactory ?? throw new ArgumentNullException(nameof(configFactory)); |
|||
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); |
|||
_clients = new Dictionary<string, ProtocolWsClient>(); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 创建ProtocolWsClient实例
|
|||
/// </summary>
|
|||
/// <param name="config">协议客户端配置</param>
|
|||
/// <returns>ProtocolWsClient实例</returns>
|
|||
private ProtocolWsClient CreateProtocolWsClient(ProtocolClientConfig config) |
|||
{ |
|||
if (config == null) |
|||
throw new ArgumentNullException(nameof(config)); |
|||
|
|||
if (string.IsNullOrEmpty(config.Name)) |
|||
throw new ArgumentException("配置名称不能为空", nameof(config)); |
|||
|
|||
_logger.LogInformation("创建ProtocolWsClient实例: {ClientName}", config.Name); |
|||
|
|||
return new ProtocolWsClient(config, _loggerFactory, _protocolLogObserver); |
|||
} |
|||
|
|||
|
|||
|
|||
/// <summary>
|
|||
/// 启动所有协议客户端
|
|||
/// </summary>
|
|||
public void StartAllClients() |
|||
{ |
|||
ThrowIfDisposed(); |
|||
|
|||
lock (_lock) |
|||
{ |
|||
_logger.LogInformation("开始启动所有协议客户端,配置数量: {ConfigCount}", _configFactory.ConfigCount); |
|||
var configs = _configFactory.GetAllConfigs(); |
|||
|
|||
if (configs.Length == 0) |
|||
{ |
|||
_logger.LogWarning("没有可用的协议客户端配置"); |
|||
return; |
|||
} |
|||
|
|||
var startedCount = 0; |
|||
var failedCount = 0; |
|||
|
|||
foreach (var config in configs) |
|||
{ |
|||
try |
|||
{ |
|||
if (!_clients.ContainsKey(config.Name)) |
|||
{ |
|||
var client = CreateProtocolWsClient(config); |
|||
_clients[config.Name] = client; |
|||
} |
|||
|
|||
var existingClient = _clients[config.Name]; |
|||
existingClient.Start(); |
|||
startedCount++; |
|||
_logger.LogInformation("启动协议客户端成功: {ClientName}", config.Name); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
failedCount++; |
|||
_logger.LogError(ex, "启动协议客户端失败: {ClientName}", config.Name); |
|||
} |
|||
} |
|||
|
|||
_logger.LogInformation("协议客户端启动完成 - 成功: {StartedCount}, 失败: {FailedCount}", startedCount, failedCount); |
|||
} |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 停止所有协议客户端
|
|||
/// </summary>
|
|||
public void StopAllClients() |
|||
{ |
|||
lock (_lock) |
|||
{ |
|||
if (_clients.Count == 0) |
|||
{ |
|||
_logger.LogInformation("没有运行中的协议客户端"); |
|||
return; |
|||
} |
|||
|
|||
_logger.LogInformation("开始停止所有协议客户端,客户端数量: {ClientCount}", _clients.Count); |
|||
|
|||
var stoppedCount = 0; |
|||
var failedCount = 0; |
|||
|
|||
foreach (var kvp in _clients) |
|||
{ |
|||
try |
|||
{ |
|||
kvp.Value.Stop(); |
|||
stoppedCount++; |
|||
_logger.LogInformation("停止协议客户端成功: {ClientName}", kvp.Key); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
failedCount++; |
|||
_logger.LogError(ex, "停止协议客户端失败: {ClientName}", kvp.Key); |
|||
} |
|||
} |
|||
|
|||
_logger.LogInformation("协议客户端停止完成 - 成功: {StoppedCount}, 失败: {FailedCount}", stoppedCount, failedCount); |
|||
} |
|||
} |
|||
|
|||
|
|||
|
|||
/// <summary>
|
|||
/// 释放所有客户端资源
|
|||
/// </summary>
|
|||
public void Dispose() |
|||
{ |
|||
Dispose(true); |
|||
GC.SuppressFinalize(this); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 释放资源
|
|||
/// </summary>
|
|||
/// <param name="disposing">是否正在释放托管资源</param>
|
|||
protected virtual void Dispose(bool disposing) |
|||
{ |
|||
if (_disposed) return; |
|||
|
|||
if (disposing) |
|||
{ |
|||
lock (_lock) |
|||
{ |
|||
StopAllClients(); |
|||
|
|||
var disposedCount = 0; |
|||
var failedCount = 0; |
|||
|
|||
foreach (var client in _clients.Values) |
|||
{ |
|||
try |
|||
{ |
|||
client.Dispose(); |
|||
disposedCount++; |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
failedCount++; |
|||
_logger.LogError(ex, "释放客户端资源失败"); |
|||
} |
|||
} |
|||
|
|||
_clients.Clear(); |
|||
_logger.LogInformation("协议客户端管理器释放完成 - 成功: {DisposedCount}, 失败: {FailedCount}", disposedCount, failedCount); |
|||
} |
|||
} |
|||
|
|||
_disposed = true; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// 检查是否已释放
|
|||
/// </summary>
|
|||
private void ThrowIfDisposed() |
|||
{ |
|||
if (_disposed) |
|||
{ |
|||
throw new ObjectDisposedException(nameof(ProtocolWsClientManager)); |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
|||
} |
@ -0,0 +1,17 @@ |
|||
using CoreAgent.ProtocolClient.Models; |
|||
|
|||
namespace CoreAgent.ProtocolClient.ProtocolEngineCore |
|||
{ |
|||
/// <summary>
|
|||
/// 协议日志观察者接口
|
|||
/// 用于观察和处理转换后的协议日志数据
|
|||
/// </summary>
|
|||
public interface IProtocolLogObserver |
|||
{ |
|||
/// <summary>
|
|||
/// 处理协议日志详情
|
|||
/// </summary>
|
|||
/// <param name="logDetails">协议日志详情列表</param>
|
|||
void OnProtocolLogsReceived(IEnumerable<TransferProtocolLog> logDetails); |
|||
} |
|||
} |
Loading…
Reference in new issue