| | 1 | | using System.Net.WebSockets; |
| | 2 | | using Microsoft.Extensions.Logging; |
| | 3 | | using NostrSure.Infrastructure.Client.Abstractions; |
| | 4 | |
|
| | 5 | | namespace NostrSure.Infrastructure.Client.Implementation; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Manages WebSocket connection lifecycle operations |
| | 9 | | /// </summary> |
| | 10 | | public sealed class ConnectionManager : IConnectionManager |
| | 11 | | { |
| | 12 | | private readonly IClientWebSocket _webSocket; |
| | 13 | | private readonly IConnectionStateManager _stateManager; |
| | 14 | | private readonly IConnectionErrorHandler _errorHandler; |
| | 15 | | private readonly CancellationTokenSource _cancellationTokenSource; |
| | 16 | | private readonly ILogger<ConnectionManager>? _logger; |
| | 17 | | private bool _disposed; |
| | 18 | |
|
| 4 | 19 | | public ConnectionManager( |
| 4 | 20 | | IClientWebSocket webSocket, |
| 4 | 21 | | IConnectionStateManager stateManager, |
| 4 | 22 | | IConnectionErrorHandler errorHandler, |
| 4 | 23 | | ILogger<ConnectionManager>? logger = null) |
| 4 | 24 | | { |
| 4 | 25 | | _webSocket = webSocket ?? throw new ArgumentNullException(nameof(webSocket)); |
| 4 | 26 | | _stateManager = stateManager ?? throw new ArgumentNullException(nameof(stateManager)); |
| 4 | 27 | | _errorHandler = errorHandler ?? throw new ArgumentNullException(nameof(errorHandler)); |
| 4 | 28 | | _cancellationTokenSource = new CancellationTokenSource(); |
| 4 | 29 | | _logger = logger; |
| 4 | 30 | | } |
| | 31 | |
|
| 2 | 32 | | public WebSocketState State => _webSocket.State; |
| | 33 | |
|
| | 34 | | public event EventHandler? Disconnected; |
| | 35 | |
|
| | 36 | | public async Task ConnectAsync(Uri uri, CancellationToken cancellationToken = default) |
| 2 | 37 | | { |
| | 38 | | try |
| 2 | 39 | | { |
| 2 | 40 | | _logger?.LogDebug("Attempting to connect to {Uri}", uri); |
| | 41 | |
|
| 2 | 42 | | var combinedToken = CancellationTokenSource |
| 2 | 43 | | .CreateLinkedTokenSource(cancellationToken, _cancellationTokenSource.Token) |
| 2 | 44 | | .Token; |
| | 45 | |
|
| 2 | 46 | | await _webSocket.ConnectAsync(uri, combinedToken); |
| 2 | 47 | | _stateManager.UpdateState(WebSocketState.Open); |
| | 48 | |
|
| 2 | 49 | | _logger?.LogInformation("Successfully connected to {Uri}", uri); |
| 2 | 50 | | } |
| 0 | 51 | | catch (Exception ex) |
| 0 | 52 | | { |
| 0 | 53 | | _logger?.LogError(ex, "Failed to connect to {Uri}", uri); |
| 0 | 54 | | await _errorHandler.HandleErrorAsync(ex, nameof(ConnectAsync)); |
| 0 | 55 | | throw; |
| | 56 | | } |
| 2 | 57 | | } |
| | 58 | |
|
| | 59 | | public async Task CloseAsync(WebSocketCloseStatus closeStatus = WebSocketCloseStatus.NormalClosure, |
| | 60 | | string? statusDescription = null, |
| | 61 | | CancellationToken cancellationToken = default) |
| 1 | 62 | | { |
| | 63 | | try |
| 1 | 64 | | { |
| 1 | 65 | | _logger?.LogDebug("Closing WebSocket connection with status {CloseStatus}", closeStatus); |
| | 66 | |
|
| 1 | 67 | | if (_webSocket.State == WebSocketState.Open) |
| 1 | 68 | | { |
| 1 | 69 | | await _webSocket.CloseAsync(closeStatus, statusDescription, cancellationToken); |
| 1 | 70 | | _stateManager.UpdateState(_webSocket.State); |
| 1 | 71 | | _logger?.LogInformation("WebSocket connection closed successfully"); |
| 1 | 72 | | } |
| 1 | 73 | | } |
| 0 | 74 | | catch (Exception ex) |
| 0 | 75 | | { |
| 0 | 76 | | _logger?.LogError(ex, "Error occurred while closing WebSocket connection"); |
| 0 | 77 | | await _errorHandler.HandleErrorAsync(ex, nameof(CloseAsync)); |
| 0 | 78 | | } |
| | 79 | | finally |
| 1 | 80 | | { |
| 1 | 81 | | _cancellationTokenSource.Cancel(); |
| 1 | 82 | | Disconnected?.Invoke(this, EventArgs.Empty); |
| 1 | 83 | | } |
| 1 | 84 | | } |
| | 85 | |
|
| | 86 | | public void Dispose() |
| 1 | 87 | | { |
| 1 | 88 | | if (!_disposed) |
| 1 | 89 | | { |
| 1 | 90 | | _logger?.LogDebug("Disposing ConnectionManager"); |
| 1 | 91 | | _cancellationTokenSource.Cancel(); |
| 1 | 92 | | _cancellationTokenSource.Dispose(); |
| 1 | 93 | | _webSocket.Dispose(); |
| 1 | 94 | | _disposed = true; |
| 1 | 95 | | } |
| 1 | 96 | | } |
| | 97 | | } |