2016-10-25 12:02:04 -07:00
|
|
|
|
using System;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using System.Collections.Generic;
|
2014-03-24 05:47:39 -07:00
|
|
|
|
using System.Globalization;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.Net;
|
2016-10-31 20:07:45 -07:00
|
|
|
|
using MediaBrowser.Model.Threading;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
namespace MediaBrowser.Api
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts sending data over a web socket periodically when a message is received, and then stops when a corresponding stop message is received
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TReturnDataType">The type of the T return data type.</typeparam>
|
|
|
|
|
/// <typeparam name="TStateType">The type of the T state type.</typeparam>
|
2013-02-22 08:16:48 -07:00
|
|
|
|
public abstract class BasePeriodicWebSocketListener<TReturnDataType, TStateType> : IWebSocketListener, IDisposable
|
2014-05-10 10:28:03 -07:00
|
|
|
|
where TStateType : WebSocketListenerState, new()
|
2014-03-24 05:47:39 -07:00
|
|
|
|
where TReturnDataType : class
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _active connections
|
|
|
|
|
/// </summary>
|
2016-10-31 20:07:45 -07:00
|
|
|
|
protected readonly List<Tuple<IWebSocketConnection, CancellationTokenSource, ITimer, TStateType, SemaphoreSlim>> ActiveConnections =
|
|
|
|
|
new List<Tuple<IWebSocketConnection, CancellationTokenSource, ITimer, TStateType, SemaphoreSlim>>();
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <value>The name.</value>
|
|
|
|
|
protected abstract string Name { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the data to send.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>Task{`1}.</returns>
|
|
|
|
|
protected abstract Task<TReturnDataType> GetDataToSend(TStateType state);
|
|
|
|
|
|
2013-02-21 13:26:35 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected ILogger Logger;
|
|
|
|
|
|
2016-10-31 20:07:45 -07:00
|
|
|
|
protected ITimerFactory TimerFactory { get; private set; }
|
|
|
|
|
|
|
|
|
|
protected BasePeriodicWebSocketListener(ILogger logger, ITimerFactory timerFactory)
|
2013-02-21 13:26:35 -07:00
|
|
|
|
{
|
|
|
|
|
if (logger == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("logger");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger = logger;
|
2016-10-31 20:07:45 -07:00
|
|
|
|
TimerFactory = timerFactory;
|
2013-02-21 13:26:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
2013-02-22 08:16:48 -07:00
|
|
|
|
/// The null task result
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected Task NullTaskResult = Task.FromResult(true);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the message.
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-02-22 08:16:48 -07:00
|
|
|
|
public Task ProcessMessage(WebSocketMessageInfo message)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2015-03-08 13:01:56 -07:00
|
|
|
|
if (message == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("message");
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 20:04:55 -07:00
|
|
|
|
if (string.Equals(message.MessageType, Name + "Start", StringComparison.OrdinalIgnoreCase))
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
|
|
|
|
Start(message);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-29 20:04:55 -07:00
|
|
|
|
if (string.Equals(message.MessageType, Name + "Stop", StringComparison.OrdinalIgnoreCase))
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
|
|
|
|
Stop(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NullTaskResult;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-31 10:39:28 -07:00
|
|
|
|
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
|
2014-05-10 10:28:03 -07:00
|
|
|
|
|
|
|
|
|
protected virtual bool SendOnTimer
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-04 13:16:23 -07:00
|
|
|
|
protected virtual void ParseMessageParams(string[] values)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Starts sending messages over a web socket
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
private void Start(WebSocketMessageInfo message)
|
|
|
|
|
{
|
|
|
|
|
var vals = message.Data.Split(',');
|
|
|
|
|
|
2013-03-31 10:39:28 -07:00
|
|
|
|
var dueTimeMs = long.Parse(vals[0], UsCulture);
|
|
|
|
|
var periodMs = long.Parse(vals[1], UsCulture);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2015-02-04 13:16:23 -07:00
|
|
|
|
if (vals.Length > 2)
|
|
|
|
|
{
|
|
|
|
|
ParseMessageParams(vals.Skip(2).ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
2016-10-06 11:55:01 -07:00
|
|
|
|
Logger.Debug("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2014-05-10 10:28:03 -07:00
|
|
|
|
var timer = SendOnTimer ?
|
2016-10-31 20:07:45 -07:00
|
|
|
|
TimerFactory.Create(TimerCallback, message.Connection, Timeout.Infinite, Timeout.Infinite) :
|
2014-05-10 10:28:03 -07:00
|
|
|
|
null;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2014-05-10 10:28:03 -07:00
|
|
|
|
var state = new TStateType
|
|
|
|
|
{
|
|
|
|
|
IntervalMs = periodMs,
|
|
|
|
|
InitialDelayMs = dueTimeMs
|
|
|
|
|
};
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
var semaphore = new SemaphoreSlim(1, 1);
|
|
|
|
|
|
|
|
|
|
lock (ActiveConnections)
|
|
|
|
|
{
|
2016-10-31 20:07:45 -07:00
|
|
|
|
ActiveConnections.Add(new Tuple<IWebSocketConnection, CancellationTokenSource, ITimer, TStateType, SemaphoreSlim>(message.Connection, cancellationTokenSource, timer, state, semaphore));
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-10 10:28:03 -07:00
|
|
|
|
if (timer != null)
|
|
|
|
|
{
|
|
|
|
|
timer.Change(TimeSpan.FromMilliseconds(dueTimeMs), TimeSpan.FromMilliseconds(periodMs));
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Timers the callback.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
2014-05-10 10:28:03 -07:00
|
|
|
|
private void TimerCallback(object state)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2013-02-26 09:10:55 -07:00
|
|
|
|
var connection = (IWebSocketConnection)state;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2016-10-31 20:07:45 -07:00
|
|
|
|
Tuple<IWebSocketConnection, CancellationTokenSource, ITimer, TStateType, SemaphoreSlim> tuple;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
|
|
lock (ActiveConnections)
|
|
|
|
|
{
|
|
|
|
|
tuple = ActiveConnections.FirstOrDefault(c => c.Item1 == connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tuple == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (connection.State != WebSocketState.Open || tuple.Item2.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
DisposeConnection(tuple);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-10 10:28:03 -07:00
|
|
|
|
SendData(tuple);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void SendData(bool force)
|
|
|
|
|
{
|
2016-10-31 20:07:45 -07:00
|
|
|
|
List<Tuple<IWebSocketConnection, CancellationTokenSource, ITimer, TStateType, SemaphoreSlim>> tuples;
|
2014-05-10 10:28:03 -07:00
|
|
|
|
|
|
|
|
|
lock (ActiveConnections)
|
|
|
|
|
{
|
|
|
|
|
tuples = ActiveConnections
|
|
|
|
|
.Where(c =>
|
|
|
|
|
{
|
|
|
|
|
if (c.Item1.State == WebSocketState.Open && !c.Item2.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
var state = c.Item4;
|
|
|
|
|
|
|
|
|
|
if (force || (DateTime.UtcNow - state.DateLastSendUtc).TotalMilliseconds >= state.IntervalMs)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
})
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var tuple in tuples)
|
|
|
|
|
{
|
|
|
|
|
SendData(tuple);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 20:07:45 -07:00
|
|
|
|
private async void SendData(Tuple<IWebSocketConnection, CancellationTokenSource, ITimer, TStateType, SemaphoreSlim> tuple)
|
2014-05-10 10:28:03 -07:00
|
|
|
|
{
|
|
|
|
|
var connection = tuple.Item1;
|
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await tuple.Item5.WaitAsync(tuple.Item2.Token).ConfigureAwait(false);
|
|
|
|
|
|
2014-05-10 10:28:03 -07:00
|
|
|
|
var state = tuple.Item4;
|
|
|
|
|
|
|
|
|
|
var data = await GetDataToSend(state).ConfigureAwait(false);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2014-03-24 05:47:39 -07:00
|
|
|
|
if (data != null)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2014-03-24 05:47:39 -07:00
|
|
|
|
await connection.SendAsync(new WebSocketMessage<TReturnDataType>
|
|
|
|
|
{
|
|
|
|
|
MessageType = Name,
|
|
|
|
|
Data = data
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2014-03-24 05:47:39 -07:00
|
|
|
|
}, tuple.Item2.Token).ConfigureAwait(false);
|
2014-05-10 10:28:03 -07:00
|
|
|
|
|
|
|
|
|
state.DateLastSendUtc = DateTime.UtcNow;
|
2014-03-24 05:47:39 -07:00
|
|
|
|
}
|
2013-03-11 09:54:52 -07:00
|
|
|
|
|
|
|
|
|
tuple.Item5.Release();
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
if (tuple.Item2.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
DisposeConnection(tuple);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 13:26:35 -07:00
|
|
|
|
Logger.ErrorException("Error sending web socket message {0}", ex, Name);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
DisposeConnection(tuple);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Stops sending messages over a web socket
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
private void Stop(WebSocketMessageInfo message)
|
|
|
|
|
{
|
|
|
|
|
lock (ActiveConnections)
|
|
|
|
|
{
|
|
|
|
|
var connection = ActiveConnections.FirstOrDefault(c => c.Item1 == message.Connection);
|
|
|
|
|
|
|
|
|
|
if (connection != null)
|
|
|
|
|
{
|
|
|
|
|
DisposeConnection(connection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Disposes the connection.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="connection">The connection.</param>
|
2016-10-31 20:07:45 -07:00
|
|
|
|
private void DisposeConnection(Tuple<IWebSocketConnection, CancellationTokenSource, ITimer, TStateType, SemaphoreSlim> connection)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2016-10-06 11:55:01 -07:00
|
|
|
|
Logger.Debug("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2014-05-10 10:28:03 -07:00
|
|
|
|
var timer = connection.Item3;
|
|
|
|
|
|
|
|
|
|
if (timer != null)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
2014-05-10 10:28:03 -07:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
timer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2014-05-10 10:28:03 -07:00
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection.Item2.Cancel();
|
|
|
|
|
connection.Item2.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
2014-05-10 10:28:03 -07:00
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection.Item5.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2014-05-10 10:28:03 -07:00
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
|
ActiveConnections.Remove(connection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2013-02-22 08:16:48 -07:00
|
|
|
|
protected virtual void Dispose(bool dispose)
|
2013-02-20 18:33:05 -07:00
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
lock (ActiveConnections)
|
|
|
|
|
{
|
|
|
|
|
foreach (var connection in ActiveConnections.ToList())
|
|
|
|
|
{
|
|
|
|
|
DisposeConnection(connection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-22 08:16:48 -07:00
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
2013-02-22 08:16:48 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-10 10:28:03 -07:00
|
|
|
|
|
|
|
|
|
public class WebSocketListenerState
|
|
|
|
|
{
|
|
|
|
|
public DateTime DateLastSendUtc { get; set; }
|
|
|
|
|
public long InitialDelayMs { get; set; }
|
|
|
|
|
public long IntervalMs { get; set; }
|
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|