2014-12-27 22:52:41 +00:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2013-09-24 19:54:42 +00:00
|
|
|
|
using MediaBrowser.Controller.Session;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2014-05-17 04:24:10 +00:00
|
|
|
|
using MediaBrowser.Model.Logging;
|
2013-09-24 15:42:30 +00:00
|
|
|
|
using MediaBrowser.Model.Net;
|
|
|
|
|
using MediaBrowser.Model.Session;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
using MediaBrowser.Model.System;
|
2013-09-24 15:42:30 +00:00
|
|
|
|
using System;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
using System.Collections.Generic;
|
2013-09-24 15:42:30 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2016-11-03 23:35:19 +00:00
|
|
|
|
namespace Emby.Server.Implementations.Session
|
2013-09-24 15:42:30 +00:00
|
|
|
|
{
|
2014-05-17 21:23:48 +00:00
|
|
|
|
public class WebSocketController : ISessionController, IDisposable
|
2013-09-24 15:42:30 +00:00
|
|
|
|
{
|
2013-10-03 01:22:50 +00:00
|
|
|
|
public SessionInfo Session { get; private set; }
|
2014-05-17 18:37:40 +00:00
|
|
|
|
public IReadOnlyList<IWebSocketConnection> Sockets { get; private set; }
|
2013-10-03 01:22:50 +00:00
|
|
|
|
|
2014-05-17 04:24:10 +00:00
|
|
|
|
private readonly ILogger _logger;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
|
2014-05-17 18:37:40 +00:00
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
|
|
|
|
2014-05-18 19:58:42 +00:00
|
|
|
|
public WebSocketController(SessionInfo session, ILogger logger, ISessionManager sessionManager)
|
2013-09-24 15:42:30 +00:00
|
|
|
|
{
|
2013-10-03 01:22:50 +00:00
|
|
|
|
Session = session;
|
2014-05-17 04:24:10 +00:00
|
|
|
|
_logger = logger;
|
2014-05-17 18:37:40 +00:00
|
|
|
|
_sessionManager = sessionManager;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
Sockets = new List<IWebSocketConnection>();
|
2013-09-24 15:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-21 16:10:02 +00:00
|
|
|
|
private bool HasOpenSockets
|
|
|
|
|
{
|
|
|
|
|
get { return GetActiveSockets().Any(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool SupportsMediaControl
|
|
|
|
|
{
|
|
|
|
|
get { return HasOpenSockets; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool _isActive;
|
2015-03-25 03:54:32 +00:00
|
|
|
|
private DateTime _lastActivityDate;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
public bool IsSessionActive
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-03-25 03:54:32 +00:00
|
|
|
|
if (HasOpenSockets)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//return false;
|
|
|
|
|
return _isActive && (DateTime.UtcNow - _lastActivityDate).TotalMinutes <= 10;
|
2013-10-03 01:22:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-21 16:10:02 +00:00
|
|
|
|
public void OnActivity()
|
2014-05-17 18:37:40 +00:00
|
|
|
|
{
|
2015-03-21 16:10:02 +00:00
|
|
|
|
_isActive = true;
|
2015-03-25 03:54:32 +00:00
|
|
|
|
_lastActivityDate = DateTime.UtcNow;
|
2014-05-17 18:37:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-17 04:24:10 +00:00
|
|
|
|
private IEnumerable<IWebSocketConnection> GetActiveSockets()
|
2013-10-03 01:22:50 +00:00
|
|
|
|
{
|
2014-05-17 04:24:10 +00:00
|
|
|
|
return Sockets
|
2013-10-03 01:22:50 +00:00
|
|
|
|
.OrderByDescending(i => i.LastActivityDate)
|
2014-05-17 04:24:10 +00:00
|
|
|
|
.Where(i => i.State == WebSocketState.Open);
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-17 18:37:40 +00: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)
|
|
|
|
|
{
|
2014-07-12 02:31:08 +00:00
|
|
|
|
if (!GetActiveSockets().Any())
|
2014-05-17 18:37:40 +00:00
|
|
|
|
{
|
2015-03-21 16:10:02 +00:00
|
|
|
|
_isActive = false;
|
|
|
|
|
|
2014-07-12 02:31:08 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_sessionManager.ReportSessionEnded(Session.Id);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error reporting session ended.", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-05-17 18:37:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-05-17 04:24:10 +00:00
|
|
|
|
private IWebSocketConnection GetActiveSocket()
|
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSockets()
|
|
|
|
|
.FirstOrDefault();
|
2013-09-24 19:54:42 +00:00
|
|
|
|
|
|
|
|
|
if (socket == null)
|
2013-09-24 15:42:30 +00:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("The requested session does not have an open web socket.");
|
|
|
|
|
}
|
2013-09-24 19:54:42 +00:00
|
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 01:22:50 +00:00
|
|
|
|
public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
|
2013-09-24 19:54:42 +00:00
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessageInternal(new WebSocketMessage<PlayRequest>
|
2013-09-24 19:54:42 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "Play",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-03 01:22:50 +00:00
|
|
|
|
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
|
2013-09-24 19:54:42 +00:00
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessageInternal(new WebSocketMessage<PlaystateRequest>
|
2013-09-24 19:54:42 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "Playstate",
|
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
2013-09-24 15:42:30 +00:00
|
|
|
|
}
|
2013-10-03 01:22:50 +00:00
|
|
|
|
|
|
|
|
|
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<LibraryUpdateInfo>
|
2013-10-03 01:22:50 +00:00
|
|
|
|
{
|
2013-10-06 18:48:04 +00:00
|
|
|
|
MessageType = "LibraryChanged",
|
2013-10-03 01:22:50 +00:00
|
|
|
|
Data = info
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the restart required message.
|
|
|
|
|
/// </summary>
|
2014-05-18 19:58:42 +00:00
|
|
|
|
/// <param name="info">The information.</param>
|
2013-10-03 01:22:50 +00:00
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
2017-12-01 17:03:40 +00:00
|
|
|
|
public Task SendRestartRequiredNotification(CancellationToken cancellationToken)
|
2013-10-03 01:22:50 +00:00
|
|
|
|
{
|
2017-12-01 17:03:40 +00:00
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<string>
|
2013-10-03 01:22:50 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "RestartRequired",
|
2017-12-01 17:03:40 +00:00
|
|
|
|
Data = string.Empty
|
2013-10-03 01:22:50 +00:00
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
2013-10-03 18:02:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the user data change info.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="info">The info.</param>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendUserDataChangeInfo(UserDataChangeInfo info, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<UserDataChangeInfo>
|
2013-10-03 18:02:23 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "UserDataChanged",
|
|
|
|
|
Data = info
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the server shutdown notification.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendServerShutdownNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<string>
|
2013-10-03 18:02:23 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "ServerShuttingDown",
|
|
|
|
|
Data = string.Empty
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sends the server restart notification.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
|
public Task SendServerRestartNotification(CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<string>
|
2013-10-03 18:02:23 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "ServerRestarting",
|
|
|
|
|
Data = string.Empty
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
2014-03-28 19:58:18 +00:00
|
|
|
|
|
2014-03-31 21:04:22 +00:00
|
|
|
|
public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
|
2014-03-28 19:58:18 +00:00
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessageInternal(new WebSocketMessage<GeneralCommand>
|
2014-03-28 19:58:18 +00:00
|
|
|
|
{
|
2014-03-31 21:04:22 +00:00
|
|
|
|
MessageType = "GeneralCommand",
|
2014-03-28 19:58:18 +00:00
|
|
|
|
Data = command
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
2014-04-06 17:53:23 +00:00
|
|
|
|
|
|
|
|
|
public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<SessionInfoDto>
|
2014-04-06 17:53:23 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "SessionEnded",
|
|
|
|
|
Data = sessionInfo
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
2014-04-12 17:27:53 +00:00
|
|
|
|
|
|
|
|
|
public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<SessionInfoDto>
|
2014-04-12 17:27:53 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "PlaybackStart",
|
|
|
|
|
Data = sessionInfo
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2015-01-13 03:46:44 +00:00
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<SessionInfoDto>
|
2014-04-12 17:27:53 +00:00
|
|
|
|
{
|
|
|
|
|
MessageType = "PlaybackStopped",
|
|
|
|
|
Data = sessionInfo
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
2014-05-17 04:24:10 +00:00
|
|
|
|
|
2015-01-13 03:46:44 +00:00
|
|
|
|
public Task SendMessage<T>(string name, T data, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
return SendMessagesInternal(new WebSocketMessage<T>
|
|
|
|
|
{
|
|
|
|
|
Data = data,
|
|
|
|
|
MessageType = name
|
|
|
|
|
|
|
|
|
|
}, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task SendMessageInternal<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
|
2014-05-17 04:24:10 +00:00
|
|
|
|
{
|
|
|
|
|
var socket = GetActiveSocket();
|
|
|
|
|
|
|
|
|
|
return socket.SendAsync(message, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-13 03:46:44 +00:00
|
|
|
|
private Task SendMessagesInternal<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
|
2014-05-17 04:24:10 +00:00
|
|
|
|
{
|
|
|
|
|
var tasks = GetActiveSockets().Select(i => Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await i.SendAsync(message, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error sending web socket message", ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}, cancellationToken));
|
|
|
|
|
|
|
|
|
|
return Task.WhenAll(tasks);
|
|
|
|
|
}
|
2014-05-17 21:23:48 +00:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
foreach (var socket in Sockets.ToList())
|
|
|
|
|
{
|
|
|
|
|
socket.Closed -= connection_Closed;
|
|
|
|
|
}
|
2017-09-05 19:49:02 +00:00
|
|
|
|
GC.SuppressFinalize(this);
|
2014-05-17 21:23:48 +00:00
|
|
|
|
}
|
2013-09-24 15:42:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|