You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/MediaBrowser.Server.Impleme.../Session/WebSocketController.cs

293 lines
9.1 KiB

10 years ago
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
11 years ago
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
using MediaBrowser.Model.Session;
11 years ago
using MediaBrowser.Model.System;
using System;
11 years ago
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Session
{
public class WebSocketController : ISessionController, IDisposable
{
11 years ago
public SessionInfo Session { get; private set; }
11 years ago
public IReadOnlyList<IWebSocketConnection> Sockets { get; private set; }
11 years ago
private readonly ILogger _logger;
11 years ago
11 years ago
private readonly ISessionManager _sessionManager;
11 years ago
public WebSocketController(SessionInfo session, ILogger logger, ISessionManager sessionManager)
{
11 years ago
Session = session;
_logger = logger;
11 years ago
_sessionManager = sessionManager;
11 years ago
Sockets = new List<IWebSocketConnection>();
}
11 years ago
public bool IsSessionActive
{
get
{
return Sockets.Any(i => i.State == WebSocketState.Open);
}
}
11 years ago
public bool SupportsMediaControl
{
get { return GetActiveSockets().Any(); }
}
private IEnumerable<IWebSocketConnection> GetActiveSockets()
11 years ago
{
return Sockets
11 years ago
.OrderByDescending(i => i.LastActivityDate)
.Where(i => i.State == WebSocketState.Open);
}
11 years ago
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)
{
if (!GetActiveSockets().Any())
11 years ago
{
try
{
_sessionManager.ReportSessionEnded(Session.Id);
}
catch (Exception ex)
{
_logger.ErrorException("Error reporting session ended.", ex);
}
}
11 years ago
}
private IWebSocketConnection GetActiveSocket()
{
var socket = GetActiveSockets()
.FirstOrDefault();
if (socket == null)
{
throw new InvalidOperationException("The requested session does not have an open web socket.");
}
return socket;
}
11 years ago
public Task SendPlayCommand(PlayRequest command, CancellationToken cancellationToken)
{
return SendMessageInternal(new WebSocketMessage<PlayRequest>
{
MessageType = "Play",
Data = command
}, cancellationToken);
}
11 years ago
public Task SendPlaystateCommand(PlaystateRequest command, CancellationToken cancellationToken)
{
return SendMessageInternal(new WebSocketMessage<PlaystateRequest>
{
MessageType = "Playstate",
Data = command
}, cancellationToken);
}
11 years ago
public Task SendLibraryUpdateInfo(LibraryUpdateInfo info, CancellationToken cancellationToken)
{
return SendMessagesInternal(new WebSocketMessage<LibraryUpdateInfo>
11 years ago
{
MessageType = "LibraryChanged",
11 years ago
Data = info
}, cancellationToken);
}
/// <summary>
/// Sends the restart required message.
/// </summary>
11 years ago
/// <param name="info">The information.</param>
11 years ago
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
11 years ago
public Task SendRestartRequiredNotification(SystemInfo info, CancellationToken cancellationToken)
11 years ago
{
return SendMessagesInternal(new WebSocketMessage<SystemInfo>
11 years ago
{
MessageType = "RestartRequired",
11 years ago
Data = info
11 years ago
}, cancellationToken);
}
/// <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)
{
return SendMessagesInternal(new WebSocketMessage<UserDataChangeInfo>
{
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)
{
return SendMessagesInternal(new WebSocketMessage<string>
{
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)
{
return SendMessagesInternal(new WebSocketMessage<string>
{
MessageType = "ServerRestarting",
Data = string.Empty
}, cancellationToken);
}
public Task SendGeneralCommand(GeneralCommand command, CancellationToken cancellationToken)
{
return SendMessageInternal(new WebSocketMessage<GeneralCommand>
{
MessageType = "GeneralCommand",
Data = command
}, cancellationToken);
}
public Task SendSessionEndedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return SendMessagesInternal(new WebSocketMessage<SessionInfoDto>
{
MessageType = "SessionEnded",
Data = sessionInfo
}, cancellationToken);
}
public Task SendPlaybackStartNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return SendMessagesInternal(new WebSocketMessage<SessionInfoDto>
{
MessageType = "PlaybackStart",
Data = sessionInfo
}, cancellationToken);
}
public Task SendPlaybackStoppedNotification(SessionInfoDto sessionInfo, CancellationToken cancellationToken)
{
return SendMessagesInternal(new WebSocketMessage<SessionInfoDto>
{
MessageType = "PlaybackStopped",
Data = sessionInfo
}, cancellationToken);
}
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)
{
if (SkipSending()) return Task.FromResult(true);
var socket = GetActiveSocket();
return socket.SendAsync(message, cancellationToken);
}
private Task SendMessagesInternal<T>(WebSocketMessage<T> message, CancellationToken cancellationToken)
{
if (SkipSending()) return Task.FromResult(true);
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);
}
private bool SkipSending()
{
if (Session != null)
{
if (string.Equals(Session.Client, "mb-classic", StringComparison.OrdinalIgnoreCase))
{
Version version;
if (!string.IsNullOrWhiteSpace(Session.ApplicationVersion) && Version.TryParse(Session.ApplicationVersion, out version))
{
if (version < new Version(3, 0, 196))
{
_logger.Debug("Skipping web socket message to MBC version {0}.", version);
return true;
}
}
}
}
return false;
}
public void Dispose()
{
foreach (var socket in Sockets.ToList())
{
socket.Closed -= connection_Closed;
}
}
}
}