aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Session/SessionWebSocketListener.cs
diff options
context:
space:
mode:
authorgion <oancaionutandrei@gmail.com>2020-04-21 23:37:37 +0200
committergion <oancaionutandrei@gmail.com>2020-04-27 22:39:37 +0200
commit083d3272d09395e2b7d73d886377017573e63686 (patch)
treeddeb2ae7b53417c3b564aa480a84e59ce9c2a5aa /Emby.Server.Implementations/Session/SessionWebSocketListener.cs
parentaad5058d25b3c295e9ea5b4330dde219034ba8c8 (diff)
Refactor and other minor changes
Diffstat (limited to 'Emby.Server.Implementations/Session/SessionWebSocketListener.cs')
-rw-r--r--Emby.Server.Implementations/Session/SessionWebSocketListener.cs42
1 files changed, 30 insertions, 12 deletions
diff --git a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs
index d8e02ef39..b0c6d0aa0 100644
--- a/Emby.Server.Implementations/Session/SessionWebSocketListener.cs
+++ b/Emby.Server.Implementations/Session/SessionWebSocketListener.cs
@@ -1,3 +1,4 @@
+using System.Collections.Generic;
using System;
using System.Collections.Concurrent;
using System.Linq;
@@ -25,7 +26,7 @@ namespace Emby.Server.Implementations.Session
public readonly int WebSocketLostTimeout = 60;
/// <summary>
- /// The timer factor; controls the frequency of the timer.
+ /// The keep-alive timer factor; controls how often the timer will check on the status of the WebSockets.
/// </summary>
public readonly double TimerFactor = 0.2;
@@ -136,11 +137,10 @@ namespace Emby.Server.Implementations.Session
/// </summary>
/// <param name="sender">The WebSocket.</param>
/// <param name="e">The event arguments.</param>
- private void _webSocket_Closed(object sender, EventArgs e)
+ private void OnWebSocketClosed(object sender, EventArgs e)
{
var webSocket = (IWebSocketConnection) sender;
- webSocket.Closed -= _webSocket_Closed;
- _webSockets.TryRemove(webSocket, out _);
+ RemoveWebSocket(webSocket);
}
/// <summary>
@@ -149,8 +149,12 @@ namespace Emby.Server.Implementations.Session
/// <param name="webSocket">The WebSocket to monitor.</param>
private async void KeepAliveWebSocket(IWebSocketConnection webSocket)
{
- _webSockets.TryAdd(webSocket, 0);
- webSocket.Closed += _webSocket_Closed;
+ if (!_webSockets.TryAdd(webSocket, 0))
+ {
+ _logger.LogWarning("Multiple attempts to keep alive single WebSocket {0}", webSocket);
+ return;
+ }
+ webSocket.Closed += OnWebSocketClosed;
webSocket.LastKeepAliveDate = DateTime.UtcNow;
// Notify WebSocket about timeout
@@ -160,13 +164,23 @@ namespace Emby.Server.Implementations.Session
}
catch (WebSocketException exception)
{
- _logger.LogDebug(exception, "Error sending ForceKeepAlive message to WebSocket.");
+ _logger.LogWarning(exception, "Error sending ForceKeepAlive message to WebSocket.");
}
StartKeepAliveTimer();
}
/// <summary>
+ /// Removes a WebSocket from the KeepAlive watchlist.
+ /// </summary>
+ /// <param name="webSocket">The WebSocket to remove.</param>
+ private void RemoveWebSocket(IWebSocketConnection webSocket)
+ {
+ webSocket.Closed -= OnWebSocketClosed;
+ _webSockets.TryRemove(webSocket, out _);
+ }
+
+ /// <summary>
/// Starts the KeepAlive timer.
/// </summary>
private void StartKeepAliveTimer()
@@ -195,7 +209,7 @@ namespace Emby.Server.Implementations.Session
foreach (var pair in _webSockets)
{
- pair.Key.Closed -= _webSocket_Closed;
+ pair.Key.Closed -= OnWebSocketClosed;
}
}
@@ -214,7 +228,7 @@ namespace Emby.Server.Implementations.Session
if (inactive.Any())
{
- _logger.LogDebug("Sending ForceKeepAlive message to {0} WebSockets.", inactive.Count());
+ _logger.LogDebug("Sending ForceKeepAlive message to {0} inactive WebSockets.", inactive.Count());
}
foreach (var webSocket in inactive)
@@ -225,15 +239,19 @@ namespace Emby.Server.Implementations.Session
}
catch (WebSocketException exception)
{
- _logger.LogDebug(exception, "Error sending ForceKeepAlive message to WebSocket.");
+ _logger.LogInformation(exception, "Error sending ForceKeepAlive message to WebSocket.");
lost.Append(webSocket);
}
}
if (lost.Any())
{
- // TODO: handle lost webSockets
- _logger.LogDebug("Lost {0} WebSockets.", lost.Count());
+ _logger.LogInformation("Lost {0} WebSockets.", lost.Count());
+ foreach (var webSocket in lost)
+ {
+ // TODO: handle session relative to the lost webSocket
+ RemoveWebSocket(webSocket);
+ }
}
if (!_webSockets.Any())