Simplify websocket listeners

pull/1010/head
Claus Vium 6 years ago
parent dab8e15052
commit 71ed840944

@ -58,9 +58,8 @@ namespace MediaBrowser.Api.ScheduledTasks
/// <summary>
/// Gets the data to send.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>Task{IEnumerable{TaskInfo}}.</returns>
protected override Task<IEnumerable<TaskInfo>> GetDataToSend(WebSocketListenerState state, CancellationToken cancellationToken)
protected override Task<IEnumerable<TaskInfo>> GetDataToSend()
{
return Task.FromResult(TaskManager.ScheduledTasks
.OrderBy(i => i.Name)

@ -79,9 +79,8 @@ namespace MediaBrowser.Api.Session
/// <summary>
/// Gets the data to send.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>Task{SystemInfo}.</returns>
protected override Task<IEnumerable<SessionInfo>> GetDataToSend(WebSocketListenerState state, CancellationToken cancellationToken)
protected override Task<IEnumerable<SessionInfo>> GetDataToSend()
{
return Task.FromResult(_sessionManager.Sessions);
}

@ -38,9 +38,8 @@ namespace MediaBrowser.Api.System
/// <summary>
/// Gets the data to send.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>Task{SystemInfo}.</returns>
protected override Task<List<ActivityLogEntry>> GetDataToSend(WebSocketListenerState state, CancellationToken CancellationToken)
protected override Task<List<ActivityLogEntry>> GetDataToSend()
{
return Task.FromResult(new List<ActivityLogEntry>());
}

@ -22,8 +22,8 @@ namespace MediaBrowser.Controller.Net
/// <summary>
/// The _active connections
/// </summary>
protected readonly List<Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType>> ActiveConnections =
new List<Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType>>();
protected readonly List<Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>> ActiveConnections =
new List<Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>>();
/// <summary>
/// Gets the name.
@ -34,9 +34,8 @@ namespace MediaBrowser.Controller.Net
/// <summary>
/// Gets the data to send.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>Task{`1}.</returns>
protected abstract Task<TReturnDataType> GetDataToSend(TStateType state, CancellationToken cancellationToken);
protected abstract Task<TReturnDataType> GetDataToSend();
/// <summary>
/// The logger
@ -80,8 +79,6 @@ namespace MediaBrowser.Controller.Net
protected readonly CultureInfo UsCulture = new CultureInfo("en-US");
protected bool SendOnTimer => false;
/// <summary>
/// Starts sending messages over a web socket
/// </summary>
@ -97,10 +94,6 @@ namespace MediaBrowser.Controller.Net
Logger.LogDebug("{1} Begin transmitting over websocket to {0}", message.Connection.RemoteEndPoint, GetType().Name);
var timer = SendOnTimer ?
new Timer(TimerCallback, message.Connection, Timeout.Infinite, Timeout.Infinite) :
null;
var state = new TStateType
{
IntervalMs = periodMs,
@ -109,47 +102,13 @@ namespace MediaBrowser.Controller.Net
lock (ActiveConnections)
{
ActiveConnections.Add(new Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType>(message.Connection, cancellationTokenSource, timer, state));
}
if (timer != null)
{
timer.Change(TimeSpan.FromMilliseconds(dueTimeMs), TimeSpan.FromMilliseconds(periodMs));
}
}
/// <summary>
/// Timers the callback.
/// </summary>
/// <param name="state">The state.</param>
private void TimerCallback(object state)
{
var connection = (IWebSocketConnection)state;
Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType> tuple;
lock (ActiveConnections)
{
tuple = ActiveConnections.FirstOrDefault(c => c.Item1 == connection);
ActiveConnections.Add(new Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>(message.Connection, cancellationTokenSource, state));
}
if (tuple == null)
{
return;
}
if (connection.State != WebSocketState.Open || tuple.Item2.IsCancellationRequested)
{
DisposeConnection(tuple);
return;
}
SendData(tuple);
}
protected void SendData(bool force)
{
Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType>[] tuples;
Tuple<IWebSocketConnection, CancellationTokenSource, TStateType>[] tuples;
lock (ActiveConnections)
{
@ -158,7 +117,7 @@ namespace MediaBrowser.Controller.Net
{
if (c.Item1.State == WebSocketState.Open && !c.Item2.IsCancellationRequested)
{
var state = c.Item4;
var state = c.Item3;
if (force || (DateTime.UtcNow - state.DateLastSendUtc).TotalMilliseconds >= state.IntervalMs)
{
@ -177,17 +136,17 @@ namespace MediaBrowser.Controller.Net
}
}
private async void SendData(Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType> tuple)
private async void SendData(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> tuple)
{
var connection = tuple.Item1;
try
{
var state = tuple.Item4;
var state = tuple.Item3;
var cancellationToken = tuple.Item2.Token;
var data = await GetDataToSend(state, cancellationToken).ConfigureAwait(false);
var data = await GetDataToSend().ConfigureAwait(false);
if (data != null)
{
@ -236,24 +195,11 @@ namespace MediaBrowser.Controller.Net
/// Disposes the connection.
/// </summary>
/// <param name="connection">The connection.</param>
private void DisposeConnection(Tuple<IWebSocketConnection, CancellationTokenSource, Timer, TStateType> connection)
private void DisposeConnection(Tuple<IWebSocketConnection, CancellationTokenSource, TStateType> connection)
{
Logger.LogDebug("{1} stop transmitting over websocket to {0}", connection.Item1.RemoteEndPoint, GetType().Name);
connection.Item1.Dispose();
var timer = connection.Item3;
if (timer != null)
{
try
{
timer.Dispose();
}
catch (ObjectDisposedException)
{
//TODO Investigate and properly fix.
}
}
try
{

Loading…
Cancel
Save