jellyfin/Emby.Server.Implementations/Session/SessionWebSocketListener.cs

105 lines
3.5 KiB
C#
Raw Normal View History

using System;
using System.Threading.Tasks;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
2015-03-08 12:48:30 -07:00
using MediaBrowser.Model.Events;
2014-04-15 19:17:48 -07:00
using MediaBrowser.Model.Serialization;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.Services;
using Microsoft.Extensions.Logging;
2016-11-03 16:35:19 -07:00
namespace Emby.Server.Implementations.Session
{
/// <summary>
/// Class SessionWebSocketListener
/// </summary>
2015-03-08 12:48:30 -07:00
public class SessionWebSocketListener : IWebSocketListener, IDisposable
{
/// <summary>
/// The _session manager
/// </summary>
private readonly ISessionManager _sessionManager;
/// <summary>
2013-05-10 05:18:07 -07:00
/// The _logger
/// </summary>
private readonly ILogger _logger;
2013-09-04 10:02:19 -07:00
/// <summary>
/// The _dto service
/// </summary>
2014-04-15 19:17:48 -07:00
private readonly IJsonSerializer _json;
2013-05-10 05:18:07 -07:00
2015-03-08 12:48:30 -07:00
private readonly IHttpServer _httpServer;
2013-05-10 05:18:07 -07:00
/// <summary>
/// Initializes a new instance of the <see cref="SessionWebSocketListener" /> class.
/// </summary>
/// <param name="sessionManager">The session manager.</param>
/// <param name="loggerFactory">The logger factory.</param>
2014-05-18 12:58:42 -07:00
/// <param name="json">The json.</param>
2015-03-08 12:48:30 -07:00
/// <param name="httpServer">The HTTP server.</param>
public SessionWebSocketListener(ISessionManager sessionManager, ILoggerFactory loggerFactory, IJsonSerializer json, IHttpServer httpServer)
{
_sessionManager = sessionManager;
_logger = loggerFactory.CreateLogger(GetType().Name);
2014-04-15 19:17:48 -07:00
_json = json;
2015-03-08 12:48:30 -07:00
_httpServer = httpServer;
2018-09-12 10:26:21 -07:00
httpServer.WebSocketConnected += _serverManager_WebSocketConnected;
2015-03-08 12:48:30 -07:00
}
2018-09-12 10:26:21 -07:00
void _serverManager_WebSocketConnected(object sender, GenericEventArgs<IWebSocketConnection> e)
2015-03-08 12:48:30 -07:00
{
2018-09-12 10:26:21 -07:00
var session = GetSession(e.Argument.QueryString, e.Argument.RemoteEndPoint);
2015-03-08 12:48:30 -07:00
if (session != null)
{
2018-09-12 10:26:21 -07:00
EnsureController(session, e.Argument);
2015-03-08 12:48:30 -07:00
}
else
{
_logger.LogWarning("Unable to determine session based on url: {0}", e.Argument.Url);
2015-03-08 12:48:30 -07:00
}
}
2018-09-12 10:26:21 -07:00
private SessionInfo GetSession(QueryParamCollection queryString, string remoteEndpoint)
2015-03-08 12:48:30 -07:00
{
2015-03-16 09:47:14 -07:00
if (queryString == null)
{
throw new ArgumentNullException(nameof(queryString));
2015-03-16 09:47:14 -07:00
}
2015-03-08 12:48:30 -07:00
var token = queryString["api_key"];
if (string.IsNullOrWhiteSpace(token))
{
2018-09-12 10:26:21 -07:00
return null;
}
2015-03-12 18:55:22 -07:00
var deviceId = queryString["deviceId"];
return _sessionManager.GetSessionByAuthenticationToken(token, deviceId, remoteEndpoint);
2015-03-08 12:48:30 -07:00
}
public void Dispose()
{
2018-09-12 10:26:21 -07:00
_httpServer.WebSocketConnected -= _serverManager_WebSocketConnected;
}
/// <summary>
/// Processes the message.
/// </summary>
/// <param name="message">The message.</param>
/// <returns>Task.</returns>
public Task ProcessMessage(WebSocketMessageInfo message)
{
2018-09-12 10:26:21 -07:00
return Task.CompletedTask;
}
2013-10-02 18:22:50 -07:00
2018-09-12 10:26:21 -07:00
private void EnsureController(SessionInfo session, IWebSocketConnection connection)
2014-04-15 19:17:48 -07:00
{
2018-09-12 10:26:21 -07:00
var controllerInfo = session.EnsureController<WebSocketController>(s => new WebSocketController(s, _logger, _sessionManager));
2014-04-15 19:17:48 -07:00
2018-09-12 10:26:21 -07:00
var controller = (WebSocketController)controllerInfo.Item1;
controller.AddWebSocket(connection);
2013-10-02 18:22:50 -07:00
}
}
}