2013-03-07 05:34:00 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2013-03-07 05:34:00 +00:00
|
|
|
|
namespace MediaBrowser.Common.Net
|
2013-02-21 01:33:05 +00: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 15:16:48 +00:00
|
|
|
|
public abstract class BasePeriodicWebSocketListener<TReturnDataType, TStateType> : IWebSocketListener, IDisposable
|
2013-02-21 01:33:05 +00:00
|
|
|
|
where TStateType : class, new()
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The _active connections
|
|
|
|
|
/// </summary>
|
2013-02-26 16:10:55 +00:00
|
|
|
|
protected readonly List<Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim>> ActiveConnections =
|
|
|
|
|
new List<Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim>>();
|
2013-02-21 01:33:05 +00: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 20:26:35 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected ILogger Logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="BasePeriodicWebSocketListener{TStateType}" /> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger">The logger.</param>
|
2013-02-22 15:16:48 +00:00
|
|
|
|
/// <exception cref="System.ArgumentNullException">logger</exception>
|
2013-02-21 20:26:35 +00:00
|
|
|
|
protected BasePeriodicWebSocketListener(ILogger logger)
|
|
|
|
|
{
|
|
|
|
|
if (logger == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("logger");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// <summary>
|
2013-02-22 15:16:48 +00:00
|
|
|
|
/// The null task result
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected Task NullTaskResult = Task.FromResult(true);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Processes the message.
|
2013-02-21 01:33:05 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="message">The message.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2013-02-22 15:16:48 +00:00
|
|
|
|
public Task ProcessMessage(WebSocketMessageInfo message)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (message.MessageType.Equals(Name + "Start", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Start(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (message.MessageType.Equals(Name + "Stop", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
Stop(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NullTaskResult;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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(',');
|
|
|
|
|
|
|
|
|
|
var dueTimeMs = long.Parse(vals[0]);
|
|
|
|
|
var periodMs = long.Parse(vals[1]);
|
|
|
|
|
|
|
|
|
|
var cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
2013-02-21 20:26:35 +00:00
|
|
|
|
Logger.Info("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
var timer = new Timer(TimerCallback, message.Connection, Timeout.Infinite, Timeout.Infinite);
|
|
|
|
|
|
|
|
|
|
var state = new TStateType();
|
|
|
|
|
|
|
|
|
|
var semaphore = new SemaphoreSlim(1, 1);
|
|
|
|
|
|
|
|
|
|
lock (ActiveConnections)
|
|
|
|
|
{
|
2013-02-26 16:10:55 +00:00
|
|
|
|
ActiveConnections.Add(new Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim>(message.Connection, cancellationTokenSource, timer, state, semaphore));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timer.Change(TimeSpan.FromMilliseconds(dueTimeMs), TimeSpan.FromMilliseconds(periodMs));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Timers the callback.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
private async void TimerCallback(object state)
|
|
|
|
|
{
|
2013-02-26 16:10:55 +00:00
|
|
|
|
var connection = (IWebSocketConnection)state;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-02-26 16:10:55 +00:00
|
|
|
|
Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim> tuple;
|
2013-02-21 01:33:05 +00: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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await tuple.Item5.WaitAsync(tuple.Item2.Token).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var data = await GetDataToSend(tuple.Item4).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
await connection.SendAsync(new WebSocketMessage<TReturnDataType>
|
|
|
|
|
{
|
|
|
|
|
MessageType = Name,
|
|
|
|
|
Data = data
|
|
|
|
|
|
|
|
|
|
}, tuple.Item2.Token).ConfigureAwait(false);
|
2013-03-11 16:54:52 +00:00
|
|
|
|
|
|
|
|
|
tuple.Item5.Release();
|
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
if (tuple.Item2.IsCancellationRequested)
|
|
|
|
|
{
|
|
|
|
|
DisposeConnection(tuple);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 20:26:35 +00:00
|
|
|
|
Logger.ErrorException("Error sending web socket message {0}", ex, Name);
|
2013-02-21 01:33:05 +00: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>
|
2013-02-26 16:10:55 +00:00
|
|
|
|
private void DisposeConnection(Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType, SemaphoreSlim> connection)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
2013-02-21 20:26:35 +00:00
|
|
|
|
Logger.Info("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection.Item3.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection.Item2.Cancel();
|
|
|
|
|
connection.Item2.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
connection.Item5.Dispose();
|
|
|
|
|
}
|
|
|
|
|
catch (ObjectDisposedException)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 15:16:48 +00:00
|
|
|
|
protected virtual void Dispose(bool dispose)
|
2013-02-21 01:33:05 +00:00
|
|
|
|
{
|
|
|
|
|
if (dispose)
|
|
|
|
|
{
|
|
|
|
|
lock (ActiveConnections)
|
|
|
|
|
{
|
|
|
|
|
foreach (var connection in ActiveConnections.ToList())
|
|
|
|
|
{
|
|
|
|
|
DisposeConnection(connection);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-02-22 15:16:48 +00:00
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
|
2013-02-22 15:16:48 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|