2021-05-07 00:39:20 +02:00
|
|
|
#nullable disable
|
|
|
|
|
2021-08-14 05:34:36 +02:00
|
|
|
#pragma warning disable CS1591, SA1306, SA1401
|
2020-08-22 21:56:24 +02:00
|
|
|
|
2019-01-13 20:25:32 +01:00
|
|
|
using System;
|
2018-12-28 00:27:57 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Threading;
|
2019-01-13 20:25:32 +01:00
|
|
|
using System.Threading.Tasks;
|
2023-06-29 13:44:36 +02:00
|
|
|
using MediaBrowser.Controller.Net.WebSocketMessages;
|
2020-09-25 09:25:59 +02:00
|
|
|
using MediaBrowser.Model.Session;
|
2022-01-01 17:59:25 +01:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-12-13 14:18:25 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Net
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-16 00:37:52 +02:00
|
|
|
/// Starts sending data over a web socket periodically when a message is received, and then stops when a corresponding stop message is received.
|
2018-12-28 00:27:57 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="TReturnDataType">The type of the T return data type.</typeparam>
|
|
|
|
/// <typeparam name="TStateType">The type of the T state type.</typeparam>
|
|
|
|
public abstract class BasePeriodicWebSocketListener<TReturnDataType, TStateType> : IWebSocketListener, IDisposable
|
|
|
|
where TStateType : WebSocketListenerState, new()
|
|
|
|
where TReturnDataType : class
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-16 00:37:52 +02:00
|
|
|
/// The _active connections.
|
2018-12-28 00:27:57 +01:00
|
|
|
/// </summary>
|
2020-02-04 02:10:44 +01:00
|
|
|
private readonly List<Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>> _activeConnections =
|
2019-02-27 22:09:22 +01:00
|
|
|
new List<Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>>();
|
2018-12-28 00:27:57 +01:00
|
|
|
|
2021-08-14 05:34:36 +02:00
|
|
|
/// <summary>
|
|
|
|
/// The logger.
|
|
|
|
/// </summary>
|
|
|
|
protected ILogger<BasePeriodicWebSocketListener<TReturnDataType, TStateType>> Logger;
|
|
|
|
|
|
|
|
protected BasePeriodicWebSocketListener(ILogger<BasePeriodicWebSocketListener<TReturnDataType, TStateType>> logger)
|
|
|
|
{
|
2022-10-06 20:21:23 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(logger);
|
2021-08-14 05:34:36 +02:00
|
|
|
|
|
|
|
Logger = logger;
|
|
|
|
}
|
|
|
|
|
2018-12-28 00:27:57 +01:00
|
|
|
/// <summary>
|
2020-09-25 09:25:59 +02:00
|
|
|
/// Gets the type used for the messages sent to the client.
|
2018-12-28 00:27:57 +01:00
|
|
|
/// </summary>
|
2020-09-25 09:25:59 +02:00
|
|
|
/// <value>The type.</value>
|
|
|
|
protected abstract SessionMessageType Type { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the message type received from the client to start sending messages.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The type.</value>
|
|
|
|
protected abstract SessionMessageType StartType { get; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the message type received from the client to stop sending messages.
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The type.</value>
|
|
|
|
protected abstract SessionMessageType StopType { get; }
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the data to send.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task{`1}.</returns>
|
2019-02-27 22:09:22 +01:00
|
|
|
protected abstract Task<TReturnDataType> GetDataToSend();
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2019-02-24 03:16:19 +01:00
|
|
|
public Task ProcessMessageAsync(WebSocketMessageInfo message)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2022-10-06 20:21:23 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(message);
|
2018-12-28 00:27:57 +01:00
|
|
|
|
2020-09-25 09:25:59 +02:00
|
|
|
if (message.MessageType == StartType)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
|
|
|
Start(message);
|
|
|
|
}
|
|
|
|
|
2020-09-25 09:25:59 +02:00
|
|
|
if (message.MessageType == StopType)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
|
|
|
Stop(message);
|
|
|
|
}
|
|
|
|
|
2019-02-24 03:16:19 +01:00
|
|
|
return Task.CompletedTask;
|
2018-12-28 00:27:57 +01:00
|
|
|
}
|
|
|
|
|
2020-11-28 11:24:52 +01:00
|
|
|
/// <inheritdoc />
|
2022-01-01 17:59:25 +01:00
|
|
|
public Task ProcessWebSocketConnectedAsync(IWebSocketConnection connection, HttpContext httpContext) => Task.CompletedTask;
|
2020-11-28 11:21:53 +01:00
|
|
|
|
2018-12-28 00:27:57 +01:00
|
|
|
/// <summary>
|
2020-06-16 00:37:52 +02:00
|
|
|
/// Starts sending messages over a web socket.
|
2018-12-28 00:27:57 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
private void Start(WebSocketMessageInfo message)
|
|
|
|
{
|
|
|
|
var vals = message.Data.Split(',');
|
|
|
|
|
2019-12-27 15:20:27 +01:00
|
|
|
var dueTimeMs = long.Parse(vals[0], CultureInfo.InvariantCulture);
|
|
|
|
var periodMs = long.Parse(vals[1], CultureInfo.InvariantCulture);
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
2019-12-27 15:20:27 +01:00
|
|
|
Logger.LogDebug("WS {1} begin transmitting to {0}", message.Connection.RemoteEndPoint, GetType().Name);
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
var state = new TStateType
|
|
|
|
{
|
|
|
|
IntervalMs = periodMs,
|
|
|
|
InitialDelayMs = dueTimeMs
|
|
|
|
};
|
|
|
|
|
2020-02-04 02:10:44 +01:00
|
|
|
lock (_activeConnections)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2020-02-04 02:10:44 +01:00
|
|
|
_activeConnections.Add(new Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>(message.Connection, cancellationTokenSource, state));
|
2018-12-28 00:27:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:52:51 +02:00
|
|
|
protected async Task SendData(bool force)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2019-02-27 22:09:22 +01:00
|
|
|
Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>[] tuples;
|
2018-12-28 00:27:57 +01:00
|
|
|
|
2020-02-04 02:10:44 +01:00
|
|
|
lock (_activeConnections)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2020-02-04 02:10:44 +01:00
|
|
|
tuples = _activeConnections
|
2018-12-28 00:27:57 +01:00
|
|
|
.Where(c =>
|
|
|
|
{
|
|
|
|
if (c.Item1.State == WebSocketState.Open && !c.Item2.IsCancellationRequested)
|
|
|
|
{
|
2019-02-27 22:09:22 +01:00
|
|
|
var state = c.Item3;
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
if (force || (DateTime.UtcNow - state.DateLastSendUtc).TotalMilliseconds >= state.IntervalMs)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.ToArray();
|
|
|
|
}
|
|
|
|
|
2020-05-25 23:52:51 +02:00
|
|
|
IEnumerable<Task> GetTasks()
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2020-05-25 23:52:51 +02:00
|
|
|
foreach (var tuple in tuples)
|
|
|
|
{
|
|
|
|
yield return SendData(tuple);
|
|
|
|
}
|
2018-12-28 00:27:57 +01:00
|
|
|
}
|
2020-05-25 23:52:51 +02:00
|
|
|
|
|
|
|
await Task.WhenAll(GetTasks()).ConfigureAwait(false);
|
2018-12-28 00:27:57 +01:00
|
|
|
}
|
|
|
|
|
2020-05-25 23:52:51 +02:00
|
|
|
private async Task SendData(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> tuple)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
|
|
|
var connection = tuple.Item1;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-02-27 22:09:22 +01:00
|
|
|
var state = tuple.Item3;
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
var cancellationToken = tuple.Item2.Token;
|
|
|
|
|
2019-02-27 22:09:22 +01:00
|
|
|
var data = await GetDataToSend().ConfigureAwait(false);
|
2018-12-28 00:27:57 +01:00
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (data is not null)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2020-05-25 23:52:51 +02:00
|
|
|
await connection.SendAsync(
|
2023-06-29 13:44:36 +02:00
|
|
|
new OutboundWebSocketMessage<TReturnDataType>
|
2020-05-25 23:52:51 +02:00
|
|
|
{
|
2020-06-11 18:51:47 +02:00
|
|
|
MessageId = Guid.NewGuid(),
|
2020-09-25 09:25:59 +02:00
|
|
|
MessageType = Type,
|
2020-05-25 23:52:51 +02:00
|
|
|
Data = data
|
|
|
|
},
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
state.DateLastSendUtc = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
if (tuple.Item2.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
DisposeConnection(tuple);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-09-25 09:25:59 +02:00
|
|
|
Logger.LogError(ex, "Error sending web socket message {Name}", Type);
|
2018-12-28 00:27:57 +01:00
|
|
|
DisposeConnection(tuple);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-16 00:37:52 +02:00
|
|
|
/// Stops sending messages over a web socket.
|
2018-12-28 00:27:57 +01:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
private void Stop(WebSocketMessageInfo message)
|
|
|
|
{
|
2020-02-04 02:10:44 +01:00
|
|
|
lock (_activeConnections)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2020-02-04 02:10:44 +01:00
|
|
|
var connection = _activeConnections.FirstOrDefault(c => c.Item1 == message.Connection);
|
2018-12-28 00:27:57 +01:00
|
|
|
|
2022-12-05 15:01:13 +01:00
|
|
|
if (connection is not null)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
|
|
|
DisposeConnection(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Disposes the connection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="connection">The connection.</param>
|
2019-02-27 22:09:22 +01:00
|
|
|
private void DisposeConnection(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> connection)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2019-12-27 15:20:27 +01:00
|
|
|
Logger.LogDebug("WS {1} stop transmitting to {0}", connection.Item1.RemoteEndPoint, GetType().Name);
|
2018-12-28 00:27:57 +01:00
|
|
|
|
2019-03-05 07:41:41 +01:00
|
|
|
// TODO disposing the connection seems to break websockets in subtle ways, so what is the purpose of this function really...
|
|
|
|
// connection.Item1.Dispose();
|
2018-12-28 00:27:57 +01:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connection.Item2.Cancel();
|
|
|
|
connection.Item2.Dispose();
|
|
|
|
}
|
2022-11-23 15:58:11 +01:00
|
|
|
catch (ObjectDisposedException ex)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2020-06-14 11:11:11 +02:00
|
|
|
// TODO Investigate and properly fix.
|
2022-11-23 15:58:11 +01:00
|
|
|
Logger.LogError(ex, "Object Disposed");
|
2018-12-28 00:27:57 +01:00
|
|
|
}
|
2023-02-16 03:58:49 +01:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
// TODO Investigate and properly fix.
|
2023-02-24 17:45:56 +01:00
|
|
|
Logger.LogError(ex, "Error disposing websocket");
|
2023-02-16 03:58:49 +01:00
|
|
|
}
|
2018-12-28 00:27:57 +01:00
|
|
|
|
2020-02-04 02:10:44 +01:00
|
|
|
lock (_activeConnections)
|
2019-01-06 21:50:43 +01:00
|
|
|
{
|
2020-02-04 02:10:44 +01:00
|
|
|
_activeConnections.Remove(connection);
|
2019-01-06 21:50:43 +01:00
|
|
|
}
|
2018-12-28 00:27:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
protected virtual void Dispose(bool dispose)
|
|
|
|
{
|
|
|
|
if (dispose)
|
|
|
|
{
|
2020-02-04 02:10:44 +01:00
|
|
|
lock (_activeConnections)
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
2020-02-04 02:10:44 +01:00
|
|
|
foreach (var connection in _activeConnections.ToArray())
|
2018-12-28 00:27:57 +01:00
|
|
|
{
|
|
|
|
DisposeConnection(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
2019-12-27 15:20:27 +01:00
|
|
|
GC.SuppressFinalize(this);
|
2018-12-28 00:27:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|