using CoreAgent.Domain.Interfaces; using CoreAgent.Domain.Interfaces.Network; using CoreAgent.Domain.Models.Network; using CoreAgent.Domain.Models.System; using Microsoft.Extensions.Options; namespace CoreAgent.Infrastructure.Contexts; /// /// 蜂窝网络领域上下文 /// public class CellularNetworkContext : ICellularNetworkContext, IDisposable { private readonly object _lock = new(); private CellularNetworkState _networkState; private readonly NetworkCommandConfig _networkCommandConfig; private readonly AppSettings _appSettings; private string _neConfigKey = string.Empty; private CancellationTokenSource _token; private bool _isDisposed; private bool _isInitialized; private readonly INetworkIPEndPointManager _networkIPEndPointManager; /// /// 获取取消令牌源 /// public CancellationTokenSource TokenSource => _token; /// /// 是否已初始化 /// public bool IsInitialized => _isInitialized; /// /// 网络IP端点管理器 /// public INetworkIPEndPointManager NetworkIPEndPointManager => _networkIPEndPointManager; public CellularNetworkContext( IOptions networkCommandConfig, IOptions appSettings, INetworkIPEndPointManager networkIPEndPointManager) { _isDisposed = false; _isInitialized = false; _token = new CancellationTokenSource(); _networkCommandConfig = networkCommandConfig?.Value ?? throw new ArgumentNullException(nameof(networkCommandConfig)); _appSettings = appSettings?.Value ?? throw new ArgumentNullException(nameof(appSettings)); _networkIPEndPointManager = networkIPEndPointManager ?? throw new ArgumentNullException(nameof(networkIPEndPointManager)); } /// /// 初始化上下文 /// /// 网络配置键 public void Initialize(string neConfigKey) { if (_isDisposed) { throw new ObjectDisposedException(nameof(CellularNetworkContext)); } if (_isInitialized) { return; } if (string.IsNullOrEmpty(neConfigKey)) { throw new ArgumentNullException(nameof(neConfigKey)); } lock (_lock) { _networkIPEndPointManager.Clear(); _neConfigKey = neConfigKey; _networkState = new CellularNetworkState(_neConfigKey); _isInitialized = true; } } /// /// 获取网络命令配置 /// /// 网络命令配置 public NetworkCommandConfig GetNetworkCommandConfig() { if (_isDisposed) { throw new ObjectDisposedException(nameof(CellularNetworkContext)); } if (!_isInitialized) { throw new InvalidOperationException("上下文未初始化"); } lock (_lock) { return _networkCommandConfig ?? throw new InvalidOperationException("网络命令配置未设置"); } } /// /// 获取网络配置键 /// public string GetNeConfigKey() { if (_isDisposed) { throw new ObjectDisposedException(nameof(CellularNetworkContext)); } if (!_isInitialized) { throw new InvalidOperationException("上下文未初始化"); } lock (_lock) { return _neConfigKey; } } /// /// 获取指定类型的命令配置 /// /// 命令类型 /// 命令配置列表 public List GetCommandsByType(NetworkCommandType type) { lock (_lock) { return _networkCommandConfig?.GetCommandsByType(type) ?? new List(); } } /// /// 获取所有命令类型 /// /// 命令类型数组 public NetworkCommandType[] GetCommandTypes() { lock (_lock) { return _networkCommandConfig?.GetCommandTypes() ?? Array.Empty(); } } /// /// 获取网络状态 /// /// 网络状态 public CellularNetworkState GetNetworkState() { if (_isDisposed) { throw new ObjectDisposedException(nameof(CellularNetworkContext)); } if (!_isInitialized) { throw new InvalidOperationException("上下文未初始化"); } lock (_lock) { return _networkState ?? throw new InvalidOperationException("网络状态未初始化"); } } /// /// 重置上下文状态 /// public void Reset() { if (_isDisposed) { throw new ObjectDisposedException(nameof(CellularNetworkContext)); } if (!_isInitialized) { return; } lock (_lock) { _token.Cancel(); _token = new CancellationTokenSource(); _neConfigKey = string.Empty; _isInitialized = false; _networkState = new CellularNetworkState(string.Empty); _networkIPEndPointManager.Clear(); } } /// /// 取消操作 /// public void Cancel() { if (_isDisposed) { throw new ObjectDisposedException(nameof(CellularNetworkContext)); } lock (_lock) { _token.Cancel(); } } /// /// 获取应用设置 /// public AppSettings GetAppSettings() { if (_isDisposed) { throw new ObjectDisposedException(nameof(CellularNetworkContext)); } if (!_isInitialized) { throw new InvalidOperationException("上下文未初始化"); } return _appSettings; } /// /// 释放资源 /// public void Dispose() { if (_isDisposed) { return; } lock (_lock) { if (_isDisposed) { return; } _token?.Cancel(); _token?.Dispose(); _isInitialized = false; _isDisposed = true; } } }