2019-01-13 14:22:24 -05:00
|
|
|
|
using System;
|
2013-10-02 21:22:50 -04:00
|
|
|
|
using System.Collections.Generic;
|
2013-09-24 11:42:30 -04:00
|
|
|
|
using System.Linq;
|
2019-01-13 14:22:24 -05:00
|
|
|
|
using System.Net.WebSockets;
|
2013-09-24 11:42:30 -04:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 14:22:24 -05:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2013-09-24 11:42:30 -04:00
|
|
|
|
|
2016-11-03 19:35:19 -04:00
|
|
|
|
namespace Emby.Server.Implementations.Session
|
2013-09-24 11:42:30 -04:00
|
|
|
|
{
|
2014-05-17 17:23:48 -04:00
|
|
|
|
public class WebSocketController : ISessionController, IDisposable
|
2013-09-24 11:42:30 -04:00
|
|
|
|
{
|
2013-10-02 21:22:50 -04:00
|
|
|
|
public SessionInfo Session { get; private set; }
|
2014-05-17 14:37:40 -04:00
|
|
|
|
public IReadOnlyList<IWebSocketConnection> Sockets { get; private set; }
|
2013-10-02 21:22:50 -04:00
|
|
|
|
|
2014-05-17 00:24:10 -04:00
|
|
|
|
private readonly ILogger _logger;
|
2013-10-02 21:22:50 -04:00
|
|
|
|
|
2014-05-17 14:37:40 -04:00
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
2014-05-18 15:58:42 -04:00
|
|
|
|
public WebSocketController(SessionInfo session, ILogger logger, ISessionManager sessionManager)
|
2013-09-24 11:42:30 -04:00
|
|
|
|
{
|
2013-10-02 21:22:50 -04:00
|
|
|
|
Session = session;
|
2014-05-17 00:24:10 -04:00
|
|
|
|
_logger = logger;
|
2014-05-17 14:37:40 -04:00
|
|
|
|
_sessionManager = sessionManager;
|
2013-10-02 21:22:50 -04:00
|
|
|
|
Sockets = new List<IWebSocketConnection>();
|
2013-09-24 11:42:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-06 15:50:43 -05:00
|
|
|
|
private bool HasOpenSockets => GetActiveSockets().Any();
|
2015-03-21 12:10:02 -04:00
|
|
|
|
|
2019-01-06 15:50:43 -05:00
|
|
|
|
public bool SupportsMediaControl => HasOpenSockets;
|
2015-03-21 12:10:02 -04:00
|
|
|
|
|
2019-01-06 15:50:43 -05:00
|
|
|
|
public bool IsSessionActive => HasOpenSockets;
|
2013-10-02 21:22:50 -04:00
|
|
|
|
|
2014-05-17 00:24:10 -04:00
|
|
|
|
private IEnumerable<IWebSocketConnection> GetActiveSockets()
|
2013-10-02 21:22:50 -04:00
|
|
|
|
{
|
2014-05-17 00:24:10 -04:00
|
|
|
|
return Sockets
|
2013-10-02 21:22:50 -04:00
|
|
|
|
.OrderByDescending(i => i.LastActivityDate)
|
2014-05-17 00:24:10 -04:00
|
|
|
|
.Where(i => i.State == WebSocketState.Open);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-17 14:37:40 -04:00
|
|
|
|
public void AddWebSocket(IWebSocketConnection connection)
|
|
|
|
|
{
|
|
|
|
|
var sockets = Sockets.ToList();
|
|
|
|
|
sockets.Add(connection);
|
|
|
|
|
|
|
|
|
|
Sockets = sockets;
|
|
|
|
|
|
|
|
|
|
connection.Closed += connection_Closed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void connection_Closed(object sender, EventArgs e)
|
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
var connection = (IWebSocketConnection)sender;
|
|
|
|
|
var sockets = Sockets.ToList();
|
|
|
|
|
sockets.Remove(connection);
|
2015-03-21 12:10:02 -04:00
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
Sockets = sockets;
|
|
|
|
|
|
|
|
|
|
_sessionManager.CloseIfNeeded(Session);
|
2014-05-17 14:37:40 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
public Task SendMessage<T>(string name, string messageId, T data, ISessionController[] allControllers, CancellationToken cancellationToken)
|
2014-05-17 00:24:10 -04:00
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSockets()
|
|
|
|
|
.FirstOrDefault();
|
2013-09-24 15:54:42 -04:00
|
|
|
|
|
|
|
|
|
if (socket == null)
|
2013-09-24 11:42:30 -04:00
|
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
|
return Task.CompletedTask;
|
2013-09-24 11:42:30 -04:00
|
|
|
|
}
|
2013-09-24 15:54:42 -04:00
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
|
return socket.SendAsync(new WebSocketMessage<T>
|
2015-01-12 22:46:44 -05:00
|
|
|
|
{
|
|
|
|
|
Data = data,
|
2018-09-12 13:26:21 -04:00
|
|
|
|
MessageType = name,
|
|
|
|
|
MessageId = messageId
|
2015-01-12 22:46:44 -05:00
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-17 17:23:48 -04:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
foreach (var socket in Sockets.ToList())
|
|
|
|
|
{
|
|
|
|
|
socket.Closed -= connection_Closed;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-09-24 11:42:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|