| | 1 | | #pragma warning disable CS1591 |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Net.WebSockets; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using MediaBrowser.Controller.Net; |
| | 10 | | using MediaBrowser.Controller.Net.WebSocketMessages; |
| | 11 | | using MediaBrowser.Controller.Session; |
| | 12 | | using MediaBrowser.Model.Session; |
| | 13 | | using Microsoft.Extensions.Logging; |
| | 14 | |
|
| | 15 | | namespace Emby.Server.Implementations.Session |
| | 16 | | { |
| | 17 | | public sealed class WebSocketController : ISessionController, IAsyncDisposable, IDisposable |
| | 18 | | { |
| | 19 | | private readonly ILogger<WebSocketController> _logger; |
| | 20 | | private readonly ISessionManager _sessionManager; |
| | 21 | | private readonly SessionInfo _session; |
| | 22 | |
|
| | 23 | | private readonly List<IWebSocketConnection> _sockets; |
| | 24 | | private readonly ReaderWriterLockSlim _socketsLock; |
| | 25 | | private bool _disposed = false; |
| | 26 | |
|
| | 27 | | public WebSocketController( |
| | 28 | | ILogger<WebSocketController> logger, |
| | 29 | | SessionInfo session, |
| | 30 | | ISessionManager sessionManager) |
| | 31 | | { |
| 0 | 32 | | _logger = logger; |
| 0 | 33 | | _session = session; |
| 0 | 34 | | _sessionManager = sessionManager; |
| 0 | 35 | | _sockets = new(); |
| 0 | 36 | | _socketsLock = new(); |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | private bool HasOpenSockets |
| | 40 | | { |
| | 41 | | get |
| | 42 | | { |
| 0 | 43 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 44 | | try |
| | 45 | | { |
| 0 | 46 | | _socketsLock.EnterReadLock(); |
| 0 | 47 | | return _sockets.Any(i => i.State == WebSocketState.Open); |
| | 48 | | } |
| | 49 | | finally |
| | 50 | | { |
| 0 | 51 | | _socketsLock.ExitReadLock(); |
| 0 | 52 | | } |
| 0 | 53 | | } |
| | 54 | | } |
| | 55 | |
|
| | 56 | | /// <inheritdoc /> |
| 0 | 57 | | public bool SupportsMediaControl => HasOpenSockets; |
| | 58 | |
|
| | 59 | | /// <inheritdoc /> |
| 0 | 60 | | public bool IsSessionActive => HasOpenSockets; |
| | 61 | |
|
| | 62 | | public void AddWebSocket(IWebSocketConnection connection) |
| | 63 | | { |
| 0 | 64 | | _logger.LogDebug("Adding websocket to session {Session}", _session.Id); |
| 0 | 65 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 66 | | try |
| | 67 | | { |
| 0 | 68 | | _socketsLock.EnterWriteLock(); |
| 0 | 69 | | _sockets.Add(connection); |
| 0 | 70 | | connection.Closed += OnConnectionClosed; |
| 0 | 71 | | } |
| | 72 | | finally |
| | 73 | | { |
| 0 | 74 | | _socketsLock.ExitWriteLock(); |
| 0 | 75 | | } |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | private async void OnConnectionClosed(object? sender, EventArgs e) |
| | 79 | | { |
| | 80 | | var connection = sender as IWebSocketConnection ?? throw new ArgumentException($"{nameof(sender)} is not of |
| | 81 | | _logger.LogDebug("Removing websocket from session {Session}", _session.Id); |
| | 82 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 83 | | try |
| | 84 | | { |
| | 85 | | _socketsLock.EnterWriteLock(); |
| | 86 | | _sockets.Remove(connection); |
| | 87 | | connection.Closed -= OnConnectionClosed; |
| | 88 | | } |
| | 89 | | finally |
| | 90 | | { |
| | 91 | | _socketsLock.ExitWriteLock(); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | await _sessionManager.CloseIfNeededAsync(_session).ConfigureAwait(false); |
| | 95 | | } |
| | 96 | |
|
| | 97 | | /// <inheritdoc /> |
| | 98 | | public Task SendMessage<T>( |
| | 99 | | SessionMessageType name, |
| | 100 | | Guid messageId, |
| | 101 | | T data, |
| | 102 | | CancellationToken cancellationToken) |
| | 103 | | { |
| 0 | 104 | | ObjectDisposedException.ThrowIf(_disposed, this); |
| | 105 | | IWebSocketConnection? socket; |
| | 106 | | try |
| | 107 | | { |
| 0 | 108 | | _socketsLock.EnterReadLock(); |
| 0 | 109 | | socket = _sockets.Where(i => i.State == WebSocketState.Open).MaxBy(i => i.LastActivityDate); |
| 0 | 110 | | } |
| | 111 | | finally |
| | 112 | | { |
| 0 | 113 | | _socketsLock.ExitReadLock(); |
| 0 | 114 | | } |
| | 115 | |
|
| 0 | 116 | | if (socket is null) |
| | 117 | | { |
| 0 | 118 | | return Task.CompletedTask; |
| | 119 | | } |
| | 120 | |
|
| 0 | 121 | | return socket.SendAsync( |
| 0 | 122 | | new OutboundWebSocketMessage<T> |
| 0 | 123 | | { |
| 0 | 124 | | Data = data, |
| 0 | 125 | | MessageType = name, |
| 0 | 126 | | MessageId = messageId |
| 0 | 127 | | }, |
| 0 | 128 | | cancellationToken); |
| | 129 | | } |
| | 130 | |
|
| | 131 | | /// <inheritdoc /> |
| | 132 | | public void Dispose() |
| | 133 | | { |
| 0 | 134 | | if (_disposed) |
| | 135 | | { |
| 0 | 136 | | return; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | try |
| | 140 | | { |
| 0 | 141 | | _socketsLock.EnterWriteLock(); |
| 0 | 142 | | foreach (var socket in _sockets) |
| | 143 | | { |
| 0 | 144 | | socket.Closed -= OnConnectionClosed; |
| 0 | 145 | | socket.Dispose(); |
| | 146 | | } |
| | 147 | |
|
| 0 | 148 | | _sockets.Clear(); |
| 0 | 149 | | } |
| | 150 | | finally |
| | 151 | | { |
| 0 | 152 | | _socketsLock.ExitWriteLock(); |
| 0 | 153 | | } |
| | 154 | |
|
| 0 | 155 | | _socketsLock.Dispose(); |
| 0 | 156 | | _disposed = true; |
| 0 | 157 | | } |
| | 158 | |
|
| | 159 | | public async ValueTask DisposeAsync() |
| | 160 | | { |
| | 161 | | if (_disposed) |
| | 162 | | { |
| | 163 | | return; |
| | 164 | | } |
| | 165 | |
|
| | 166 | | try |
| | 167 | | { |
| | 168 | | _socketsLock.EnterWriteLock(); |
| | 169 | | foreach (var socket in _sockets) |
| | 170 | | { |
| | 171 | | socket.Closed -= OnConnectionClosed; |
| | 172 | | await socket.DisposeAsync().ConfigureAwait(false); |
| | 173 | | } |
| | 174 | |
|
| | 175 | | _sockets.Clear(); |
| | 176 | | } |
| | 177 | | finally |
| | 178 | | { |
| | 179 | | _socketsLock.ExitWriteLock(); |
| | 180 | | } |
| | 181 | |
|
| | 182 | | _socketsLock.Dispose(); |
| | 183 | | _disposed = true; |
| | 184 | | } |
| | 185 | | } |
| | 186 | | } |