2021-05-06 22:39:20 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2020-08-22 19:56:24 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 19:25:32 +00:00
|
|
|
using System;
|
2018-12-27 23:27:57 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Threading;
|
2019-01-13 19:25:32 +00:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Model.Net;
|
2020-09-25 07:25:59 +00:00
|
|
|
using MediaBrowser.Model.Session;
|
2018-12-13 13:18:25 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Controller.Net
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00: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-27 23:27:57 +00: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-15 22:37:52 +00:00
|
|
|
/// The _active connections.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
2020-02-04 01:10:44 +00:00
|
|
|
private readonly List<Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>> _activeConnections =
|
2019-02-27 21:09:22 +00:00
|
|
|
new List<Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>>();
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-09-25 07:25:59 +00:00
|
|
|
/// Gets the type used for the messages sent to the client.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
2020-09-25 07:25:59 +00: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-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the data to send.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Task{`1}.</returns>
|
2019-02-27 21:09:22 +00:00
|
|
|
protected abstract Task<TReturnDataType> GetDataToSend();
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// The logger.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
2020-06-06 00:15:56 +00:00
|
|
|
protected ILogger<BasePeriodicWebSocketListener<TReturnDataType, TStateType>> Logger;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2020-06-06 00:15:56 +00:00
|
|
|
protected BasePeriodicWebSocketListener(ILogger<BasePeriodicWebSocketListener<TReturnDataType, TStateType>> logger)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
if (logger == null)
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(logger));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the message.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
/// <returns>Task.</returns>
|
2019-02-24 02:16:19 +00:00
|
|
|
public Task ProcessMessageAsync(WebSocketMessageInfo message)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
if (message == null)
|
|
|
|
{
|
2019-01-06 20:50:43 +00:00
|
|
|
throw new ArgumentNullException(nameof(message));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 07:25:59 +00:00
|
|
|
if (message.MessageType == StartType)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
Start(message);
|
|
|
|
}
|
|
|
|
|
2020-09-25 07:25:59 +00:00
|
|
|
if (message.MessageType == StopType)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
Stop(message);
|
|
|
|
}
|
|
|
|
|
2019-02-24 02:16:19 +00:00
|
|
|
return Task.CompletedTask;
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2020-11-28 10:24:52 +00:00
|
|
|
/// <inheritdoc />
|
2020-11-28 10:21:53 +00:00
|
|
|
public Task ProcessWebSocketConnectedAsync(IWebSocketConnection connection) => Task.CompletedTask;
|
|
|
|
|
2018-12-27 23:27:57 +00:00
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Starts sending messages over a web socket.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
private void Start(WebSocketMessageInfo message)
|
|
|
|
{
|
|
|
|
var vals = message.Data.Split(',');
|
|
|
|
|
2019-12-27 14:20:27 +00:00
|
|
|
var dueTimeMs = long.Parse(vals[0], CultureInfo.InvariantCulture);
|
|
|
|
var periodMs = long.Parse(vals[1], CultureInfo.InvariantCulture);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
2019-12-27 14:20:27 +00:00
|
|
|
Logger.LogDebug("WS {1} begin transmitting to {0}", message.Connection.RemoteEndPoint, GetType().Name);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
var state = new TStateType
|
|
|
|
{
|
|
|
|
IntervalMs = periodMs,
|
|
|
|
InitialDelayMs = dueTimeMs
|
|
|
|
};
|
|
|
|
|
2020-02-04 01:10:44 +00:00
|
|
|
lock (_activeConnections)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-02-04 01:10:44 +00:00
|
|
|
_activeConnections.Add(new Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>(message.Connection, cancellationTokenSource, state));
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
protected async Task SendData(bool force)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2019-02-27 21:09:22 +00:00
|
|
|
Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>[] tuples;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2020-02-04 01:10:44 +00:00
|
|
|
lock (_activeConnections)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-02-04 01:10:44 +00:00
|
|
|
tuples = _activeConnections
|
2018-12-27 23:27:57 +00:00
|
|
|
.Where(c =>
|
|
|
|
{
|
|
|
|
if (c.Item1.State == WebSocketState.Open && !c.Item2.IsCancellationRequested)
|
|
|
|
{
|
2019-02-27 21:09:22 +00:00
|
|
|
var state = c.Item3;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
if (force || (DateTime.UtcNow - state.DateLastSendUtc).TotalMilliseconds >= state.IntervalMs)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
})
|
|
|
|
.ToArray();
|
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
IEnumerable<Task> GetTasks()
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
foreach (var tuple in tuples)
|
|
|
|
{
|
|
|
|
yield return SendData(tuple);
|
|
|
|
}
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
2020-05-25 21:52:51 +00:00
|
|
|
|
|
|
|
await Task.WhenAll(GetTasks()).ConfigureAwait(false);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 21:52:51 +00:00
|
|
|
private async Task SendData(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> tuple)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
var connection = tuple.Item1;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2019-02-27 21:09:22 +00:00
|
|
|
var state = tuple.Item3;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
var cancellationToken = tuple.Item2.Token;
|
|
|
|
|
2019-02-27 21:09:22 +00:00
|
|
|
var data = await GetDataToSend().ConfigureAwait(false);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
if (data != null)
|
|
|
|
{
|
2020-05-25 21:52:51 +00:00
|
|
|
await connection.SendAsync(
|
|
|
|
new WebSocketMessage<TReturnDataType>
|
|
|
|
{
|
2020-06-11 16:51:47 +00:00
|
|
|
MessageId = Guid.NewGuid(),
|
2020-09-25 07:25:59 +00:00
|
|
|
MessageType = Type,
|
2020-05-25 21:52:51 +00:00
|
|
|
Data = data
|
|
|
|
},
|
|
|
|
cancellationToken).ConfigureAwait(false);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
state.DateLastSendUtc = DateTime.UtcNow;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
{
|
|
|
|
if (tuple.Item2.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
DisposeConnection(tuple);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-09-25 07:25:59 +00:00
|
|
|
Logger.LogError(ex, "Error sending web socket message {Name}", Type);
|
2018-12-27 23:27:57 +00:00
|
|
|
DisposeConnection(tuple);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-15 22:37:52 +00:00
|
|
|
/// Stops sending messages over a web socket.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
private void Stop(WebSocketMessageInfo message)
|
|
|
|
{
|
2020-02-04 01:10:44 +00:00
|
|
|
lock (_activeConnections)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-02-04 01:10:44 +00:00
|
|
|
var connection = _activeConnections.FirstOrDefault(c => c.Item1 == message.Connection);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
if (connection != null)
|
|
|
|
{
|
|
|
|
DisposeConnection(connection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Disposes the connection.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="connection">The connection.</param>
|
2019-02-27 21:09:22 +00:00
|
|
|
private void DisposeConnection(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> connection)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2019-12-27 14:20:27 +00:00
|
|
|
Logger.LogDebug("WS {1} stop transmitting to {0}", connection.Item1.RemoteEndPoint, GetType().Name);
|
2018-12-27 23:27:57 +00:00
|
|
|
|
2019-03-05 06:41:41 +00: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-27 23:27:57 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
connection.Item2.Cancel();
|
|
|
|
connection.Item2.Dispose();
|
|
|
|
}
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
{
|
2020-06-14 09:11:11 +00:00
|
|
|
// TODO Investigate and properly fix.
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-04 01:10:44 +00:00
|
|
|
lock (_activeConnections)
|
2019-01-06 20:50:43 +00:00
|
|
|
{
|
2020-02-04 01:10:44 +00:00
|
|
|
_activeConnections.Remove(connection);
|
2019-01-06 20:50:43 +00:00
|
|
|
}
|
2018-12-27 23:27:57 +00: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 01:10:44 +00:00
|
|
|
lock (_activeConnections)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
2020-02-04 01:10:44 +00:00
|
|
|
foreach (var connection in _activeConnections.ToArray())
|
2018-12-27 23:27:57 +00: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 14:20:27 +00:00
|
|
|
GC.SuppressFinalize(this);
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|