2020-09-03 09:32:22 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-11-28 10:21:53 +00:00
|
|
|
using System.Linq;
|
2020-09-03 09:32:22 +00:00
|
|
|
using System.Net.WebSockets;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.HttpServer
|
|
|
|
{
|
|
|
|
public class WebSocketManager : IWebSocketManager
|
|
|
|
{
|
2020-11-28 10:21:53 +00:00
|
|
|
private readonly IWebSocketListener[] _webSocketListeners;
|
2021-04-05 13:12:47 +00:00
|
|
|
private readonly IAuthService _authService;
|
2020-09-03 09:32:22 +00:00
|
|
|
private readonly ILogger<WebSocketManager> _logger;
|
|
|
|
private readonly ILoggerFactory _loggerFactory;
|
2020-11-28 08:50:16 +00:00
|
|
|
|
2020-09-03 09:32:22 +00:00
|
|
|
public WebSocketManager(
|
2021-04-05 13:12:47 +00:00
|
|
|
IAuthService authService,
|
2020-11-28 10:21:53 +00:00
|
|
|
IEnumerable<IWebSocketListener> webSocketListeners,
|
2020-09-03 09:32:22 +00:00
|
|
|
ILogger<WebSocketManager> logger,
|
|
|
|
ILoggerFactory loggerFactory)
|
|
|
|
{
|
2020-11-28 10:21:53 +00:00
|
|
|
_webSocketListeners = webSocketListeners.ToArray();
|
2021-04-05 13:12:47 +00:00
|
|
|
_authService = authService;
|
2020-09-03 09:32:22 +00:00
|
|
|
_logger = logger;
|
|
|
|
_loggerFactory = loggerFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public async Task WebSocketRequestHandler(HttpContext context)
|
|
|
|
{
|
2021-04-05 13:12:47 +00:00
|
|
|
_ = _authService.Authenticate(context.Request);
|
2020-09-03 09:32:22 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
_logger.LogInformation("WS {IP} request", context.Connection.RemoteIpAddress);
|
|
|
|
|
|
|
|
WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
using var connection = new WebSocketConnection(
|
|
|
|
_loggerFactory.CreateLogger<WebSocketConnection>(),
|
|
|
|
webSocket,
|
|
|
|
context.Connection.RemoteIpAddress,
|
|
|
|
context.Request.Query)
|
|
|
|
{
|
|
|
|
OnReceive = ProcessWebSocketMessageReceived
|
|
|
|
};
|
|
|
|
|
2020-11-28 10:21:53 +00:00
|
|
|
var tasks = new Task[_webSocketListeners.Length];
|
|
|
|
for (var i = 0; i < _webSocketListeners.Length; ++i)
|
|
|
|
{
|
|
|
|
tasks[i] = _webSocketListeners[i].ProcessWebSocketConnectedAsync(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
await Task.WhenAll(tasks).ConfigureAwait(false);
|
2020-09-03 09:32:22 +00:00
|
|
|
|
|
|
|
await connection.ProcessAsync().ConfigureAwait(false);
|
|
|
|
_logger.LogInformation("WS {IP} closed", context.Connection.RemoteIpAddress);
|
|
|
|
}
|
|
|
|
catch (Exception ex) // Otherwise ASP.Net will ignore the exception
|
|
|
|
{
|
|
|
|
_logger.LogError(ex, "WS {IP} WebSocketRequestHandler error", context.Connection.RemoteIpAddress);
|
|
|
|
if (!context.Response.HasStarted)
|
|
|
|
{
|
|
|
|
context.Response.StatusCode = 500;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the web socket message received.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="result">The result.</param>
|
|
|
|
private Task ProcessWebSocketMessageReceived(WebSocketMessageInfo result)
|
|
|
|
{
|
2020-11-28 10:21:53 +00:00
|
|
|
var tasks = new Task[_webSocketListeners.Length];
|
|
|
|
for (var i = 0; i < _webSocketListeners.Length; ++i)
|
2020-11-28 08:50:16 +00:00
|
|
|
{
|
2020-11-28 10:21:53 +00:00
|
|
|
tasks[i] = _webSocketListeners[i].ProcessMessageAsync(result);
|
2020-11-28 08:50:16 +00:00
|
|
|
}
|
2020-09-03 09:32:22 +00:00
|
|
|
|
2020-11-28 10:21:53 +00:00
|
|
|
return Task.WhenAll(tasks);
|
2020-09-03 09:32:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|