| | 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 bool _disposed = false; |
| | 25 | |
|
| | 26 | | public WebSocketController( |
| | 27 | | ILogger<WebSocketController> logger, |
| | 28 | | SessionInfo session, |
| | 29 | | ISessionManager sessionManager) |
| | 30 | | { |
| 0 | 31 | | _logger = logger; |
| 0 | 32 | | _session = session; |
| 0 | 33 | | _sessionManager = sessionManager; |
| 0 | 34 | | _sockets = new List<IWebSocketConnection>(); |
| 0 | 35 | | } |
| | 36 | |
|
| 0 | 37 | | private bool HasOpenSockets => GetActiveSockets().Any(); |
| | 38 | |
|
| | 39 | | /// <inheritdoc /> |
| 0 | 40 | | public bool SupportsMediaControl => HasOpenSockets; |
| | 41 | |
|
| | 42 | | /// <inheritdoc /> |
| 0 | 43 | | public bool IsSessionActive => HasOpenSockets; |
| | 44 | |
|
| | 45 | | private IEnumerable<IWebSocketConnection> GetActiveSockets() |
| 0 | 46 | | => _sockets.Where(i => i.State == WebSocketState.Open); |
| | 47 | |
|
| | 48 | | public void AddWebSocket(IWebSocketConnection connection) |
| | 49 | | { |
| 0 | 50 | | _logger.LogDebug("Adding websocket to session {Session}", _session.Id); |
| 0 | 51 | | _sockets.Add(connection); |
| | 52 | |
|
| 0 | 53 | | connection.Closed += OnConnectionClosed; |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | private async void OnConnectionClosed(object? sender, EventArgs e) |
| | 57 | | { |
| | 58 | | var connection = sender as IWebSocketConnection ?? throw new ArgumentException($"{nameof(sender)} is not of |
| | 59 | | _logger.LogDebug("Removing websocket from session {Session}", _session.Id); |
| | 60 | | _sockets.Remove(connection); |
| | 61 | | connection.Closed -= OnConnectionClosed; |
| | 62 | | await _sessionManager.CloseIfNeededAsync(_session).ConfigureAwait(false); |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <inheritdoc /> |
| | 66 | | public Task SendMessage<T>( |
| | 67 | | SessionMessageType name, |
| | 68 | | Guid messageId, |
| | 69 | | T data, |
| | 70 | | CancellationToken cancellationToken) |
| | 71 | | { |
| 0 | 72 | | var socket = GetActiveSockets().MaxBy(i => i.LastActivityDate); |
| | 73 | |
|
| 0 | 74 | | if (socket is null) |
| | 75 | | { |
| 0 | 76 | | return Task.CompletedTask; |
| | 77 | | } |
| | 78 | |
|
| 0 | 79 | | return socket.SendAsync( |
| 0 | 80 | | new OutboundWebSocketMessage<T> |
| 0 | 81 | | { |
| 0 | 82 | | Data = data, |
| 0 | 83 | | MessageType = name, |
| 0 | 84 | | MessageId = messageId |
| 0 | 85 | | }, |
| 0 | 86 | | cancellationToken); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | /// <inheritdoc /> |
| | 90 | | public void Dispose() |
| | 91 | | { |
| 0 | 92 | | if (_disposed) |
| | 93 | | { |
| 0 | 94 | | return; |
| | 95 | | } |
| | 96 | |
|
| 0 | 97 | | foreach (var socket in _sockets) |
| | 98 | | { |
| 0 | 99 | | socket.Closed -= OnConnectionClosed; |
| 0 | 100 | | socket.Dispose(); |
| | 101 | | } |
| | 102 | |
|
| 0 | 103 | | _disposed = true; |
| 0 | 104 | | } |
| | 105 | |
|
| | 106 | | public async ValueTask DisposeAsync() |
| | 107 | | { |
| | 108 | | if (_disposed) |
| | 109 | | { |
| | 110 | | return; |
| | 111 | | } |
| | 112 | |
|
| | 113 | | foreach (var socket in _sockets) |
| | 114 | | { |
| | 115 | | socket.Closed -= OnConnectionClosed; |
| | 116 | | await socket.DisposeAsync().ConfigureAwait(false); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | _disposed = true; |
| | 120 | | } |
| | 121 | | } |
| | 122 | | } |