Simplify websocket listeners

This commit is contained in:
Claus Vium 2019-02-27 22:09:22 +01:00
parent dab8e15052
commit 71ed840944
4 changed files with 13 additions and 70 deletions

View File

@ -58,9 +58,8 @@ namespace MediaBrowser.Api.ScheduledTasks
/// <summary> /// <summary>
/// Gets the data to send. /// Gets the data to send.
/// </summary> /// </summary>
/// <param name="state">The state.</param>
/// <returns>Task{IEnumerable{TaskInfo}}.</returns> /// <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 return Task.FromResult(TaskManager.ScheduledTasks
.OrderBy(i => i.Name) .OrderBy(i => i.Name)

View File

@ -79,9 +79,8 @@ namespace MediaBrowser.Api.Session
/// <summary> /// <summary>
/// Gets the data to send. /// Gets the data to send.
/// </summary> /// </summary>
/// <param name="state">The state.</param>
/// <returns>Task{SystemInfo}.</returns> /// <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); return Task.FromResult(_sessionManager.Sessions);
} }

View File

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

View File

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