|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Jellyfin.Data.Enums;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Session;
|
|
|
|
|
using MediaBrowser.Controller.SyncPlay;
|
|
|
|
@ -44,31 +44,23 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The map between sessions and groups.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly Dictionary<string, Group> _sessionToGroupMap =
|
|
|
|
|
new Dictionary<string, Group>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
private readonly ConcurrentDictionary<string, Group> _sessionToGroupMap =
|
|
|
|
|
new ConcurrentDictionary<string, Group>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The groups.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly Dictionary<Guid, Group> _groups =
|
|
|
|
|
new Dictionary<Guid, Group>();
|
|
|
|
|
private readonly ConcurrentDictionary<Guid, Group> _groups =
|
|
|
|
|
new ConcurrentDictionary<Guid, Group>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Lock used for accessing any group.
|
|
|
|
|
/// Lock used for accessing multiple groups at once.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Always lock before <see cref="_mapsLock"/> and before locking on any <see cref="Group"/>.
|
|
|
|
|
/// This lock has priority on locks made on <see cref="Group"/>.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
private readonly object _groupsLock = new object();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Lock used for accessing the session-to-group map.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Always lock after <see cref="_groupsLock"/> and before locking on any <see cref="Group"/>.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
private readonly object _mapsLock = new object();
|
|
|
|
|
|
|
|
|
|
private bool _disposed = false;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
@ -102,14 +94,21 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void NewGroup(SessionInfo session, NewGroupRequest request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (session == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Request is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Locking required to access list of groups.
|
|
|
|
|
lock (_groupsLock)
|
|
|
|
|
{
|
|
|
|
|
// Locking required as session-to-group map will be edited.
|
|
|
|
|
// Locking the group is not required as it is not visible yet.
|
|
|
|
|
lock (_mapsLock)
|
|
|
|
|
{
|
|
|
|
|
if (IsSessionInGroup(session))
|
|
|
|
|
// Make sure that session has not joined another group.
|
|
|
|
|
if (_sessionToGroupMap.ContainsKey(session.Id))
|
|
|
|
|
{
|
|
|
|
|
var leaveGroupRequest = new LeaveGroupRequest();
|
|
|
|
|
LeaveGroup(session, leaveGroupRequest, cancellationToken);
|
|
|
|
@ -118,15 +117,28 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
var group = new Group(_loggerFactory, _userManager, _sessionManager, _libraryManager);
|
|
|
|
|
_groups[group.GroupId] = group;
|
|
|
|
|
|
|
|
|
|
AddSessionToGroup(session, group);
|
|
|
|
|
group.CreateGroup(session, request, cancellationToken);
|
|
|
|
|
if (!_sessionToGroupMap.TryAdd(session.Id, group))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Could not add session to group!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.CreateGroup(session, request, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void JoinGroup(SessionInfo session, JoinGroupRequest request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (session == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Request is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var user = _userManager.GetUserById(session.UserId);
|
|
|
|
|
|
|
|
|
|
// Locking required to access list of groups.
|
|
|
|
@ -143,9 +155,6 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Locking required as session-to-group map will be edited.
|
|
|
|
|
lock (_mapsLock)
|
|
|
|
|
{
|
|
|
|
|
// Group lock required to let other requests end first.
|
|
|
|
|
lock (group)
|
|
|
|
|
{
|
|
|
|
@ -158,9 +167,9 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (IsSessionInGroup(session))
|
|
|
|
|
if (_sessionToGroupMap.TryGetValue(session.Id, out var existingGroup))
|
|
|
|
|
{
|
|
|
|
|
if (FindJoinedGroupId(session).Equals(request.GroupId))
|
|
|
|
|
if (existingGroup.GroupId.Equals(request.GroupId))
|
|
|
|
|
{
|
|
|
|
|
// Restore session.
|
|
|
|
|
group.SessionJoin(session, request, cancellationToken);
|
|
|
|
@ -171,9 +180,12 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
LeaveGroup(session, leaveGroupRequest, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AddSessionToGroup(session, group);
|
|
|
|
|
group.SessionJoin(session, request, cancellationToken);
|
|
|
|
|
if (!_sessionToGroupMap.TryAdd(session.Id, group))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Could not add session to group!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.SessionJoin(session, request, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -181,26 +193,36 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void LeaveGroup(SessionInfo session, LeaveGroupRequest request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
// Locking required to access list of groups.
|
|
|
|
|
lock (_groupsLock)
|
|
|
|
|
{
|
|
|
|
|
// Locking required as session-to-group map will be edited.
|
|
|
|
|
lock (_mapsLock)
|
|
|
|
|
{
|
|
|
|
|
var group = FindJoinedGroup(session);
|
|
|
|
|
if (group == null)
|
|
|
|
|
if (session == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
|
|
|
|
|
throw new InvalidOperationException("Session is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
|
|
|
|
|
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
|
|
|
|
|
return;
|
|
|
|
|
if (request == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Request is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Locking required to access list of groups.
|
|
|
|
|
lock (_groupsLock)
|
|
|
|
|
{
|
|
|
|
|
if (_sessionToGroupMap.TryGetValue(session.Id, out var group))
|
|
|
|
|
{
|
|
|
|
|
// Group lock required to let other requests end first.
|
|
|
|
|
lock (group)
|
|
|
|
|
{
|
|
|
|
|
RemoveSessionFromGroup(session, group);
|
|
|
|
|
if (_sessionToGroupMap.TryRemove(session.Id, out var tempGroup))
|
|
|
|
|
{
|
|
|
|
|
if (!tempGroup.GroupId.Equals(group.GroupId))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session was in wrong group!");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Could not remove session from group!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group.SessionLeave(session, request, cancellationToken);
|
|
|
|
|
|
|
|
|
|
if (group.IsGroupEmpty())
|
|
|
|
@ -210,18 +232,33 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
|
|
|
|
|
|
|
|
|
|
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
|
|
|
|
|
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public List<GroupInfoDto> ListGroups(SessionInfo session, ListGroupsRequest request)
|
|
|
|
|
{
|
|
|
|
|
if (session == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Request is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var user = _userManager.GetUserById(session.UserId);
|
|
|
|
|
List<GroupInfoDto> list = new List<GroupInfoDto>();
|
|
|
|
|
|
|
|
|
|
// Locking required to access list of groups.
|
|
|
|
|
lock (_groupsLock)
|
|
|
|
|
{
|
|
|
|
|
foreach (var group in _groups.Values)
|
|
|
|
|
{
|
|
|
|
|
// Locking required as group is not thread-safe.
|
|
|
|
@ -233,7 +270,6 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
@ -241,156 +277,69 @@ namespace Emby.Server.Implementations.SyncPlay
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void HandleRequest(SessionInfo session, IGroupPlaybackRequest request, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
Group group;
|
|
|
|
|
lock (_mapsLock)
|
|
|
|
|
if (session == null)
|
|
|
|
|
{
|
|
|
|
|
group = FindJoinedGroup(session);
|
|
|
|
|
throw new InvalidOperationException("Session is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (group == null)
|
|
|
|
|
if (request == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
|
|
|
|
|
|
|
|
|
|
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
|
|
|
|
|
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
|
|
|
|
|
return;
|
|
|
|
|
throw new InvalidOperationException("Request is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_sessionToGroupMap.TryGetValue(session.Id, out var group))
|
|
|
|
|
{
|
|
|
|
|
// Group lock required as Group is not thread-safe.
|
|
|
|
|
lock (group)
|
|
|
|
|
{
|
|
|
|
|
group.HandleRequest(session, request, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Releases unmanaged and optionally managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (_disposed)
|
|
|
|
|
// Make sure that session still belongs to this group.
|
|
|
|
|
if (_sessionToGroupMap.TryGetValue(session.Id, out var checkGroup) && !checkGroup.GroupId.Equals(group.GroupId))
|
|
|
|
|
{
|
|
|
|
|
// Drop request.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
|
|
|
|
|
_disposed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnSessionManagerSessionStarted(object sender, SessionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var session = e.SessionInfo;
|
|
|
|
|
|
|
|
|
|
Guid groupId = Guid.Empty;
|
|
|
|
|
lock (_mapsLock)
|
|
|
|
|
{
|
|
|
|
|
groupId = FindJoinedGroupId(session);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (groupId.Equals(Guid.Empty))
|
|
|
|
|
// Drop request if group is empty.
|
|
|
|
|
if (group.IsGroupEmpty())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var request = new JoinGroupRequest(groupId);
|
|
|
|
|
JoinGroup(session, request, CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if a given session has joined a group.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="session">The session.</param>
|
|
|
|
|
/// <returns><c>true</c> if the session has joined a group, <c>false</c> otherwise.</returns>
|
|
|
|
|
private bool IsSessionInGroup(SessionInfo session)
|
|
|
|
|
{
|
|
|
|
|
return _sessionToGroupMap.ContainsKey(session.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the group joined by the given session, if any.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="session">The session.</param>
|
|
|
|
|
/// <returns>The group.</returns>
|
|
|
|
|
private Group FindJoinedGroup(SessionInfo session)
|
|
|
|
|
{
|
|
|
|
|
_sessionToGroupMap.TryGetValue(session.Id, out var group);
|
|
|
|
|
return group;
|
|
|
|
|
group.HandleRequest(session, request, cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the group identifier joined by the given session, if any.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="session">The session.</param>
|
|
|
|
|
/// <returns>The group identifier if the session has joined a group, an empty identifier otherwise.</returns>
|
|
|
|
|
private Guid FindJoinedGroupId(SessionInfo session)
|
|
|
|
|
{
|
|
|
|
|
return FindJoinedGroup(session)?.GroupId ?? Guid.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Maps a session to a group.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="session">The session.</param>
|
|
|
|
|
/// <param name="group">The group.</param>
|
|
|
|
|
/// <exception cref="InvalidOperationException">Thrown when the user is in another group already.</exception>
|
|
|
|
|
private void AddSessionToGroup(SessionInfo session, Group group)
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (session == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session is null!");
|
|
|
|
|
}
|
|
|
|
|
_logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
|
|
|
|
|
|
|
|
|
|
if (IsSessionInGroup(session))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session in other group already!");
|
|
|
|
|
var error = new GroupUpdate<string>(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
|
|
|
|
|
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_sessionToGroupMap[session.Id] = group ?? throw new InvalidOperationException("Group is null!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Unmaps a session from a group.
|
|
|
|
|
/// Releases unmanaged and optionally managed resources.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Method is not thread-safe, external locking on <see cref="_mapsLock"/> is required.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
/// <param name="session">The session.</param>
|
|
|
|
|
/// <param name="group">The group.</param>
|
|
|
|
|
/// <exception cref="InvalidOperationException">Thrown when the user is not found in the specified group.</exception>
|
|
|
|
|
private void RemoveSessionFromGroup(SessionInfo session, Group group)
|
|
|
|
|
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (session == null)
|
|
|
|
|
if (_disposed)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session is null!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (group == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Group is null!");
|
|
|
|
|
_sessionManager.SessionStarted -= OnSessionManagerSessionStarted;
|
|
|
|
|
_disposed = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!IsSessionInGroup(session))
|
|
|
|
|
private void OnSessionManagerSessionStarted(object sender, SessionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session not in any group!");
|
|
|
|
|
}
|
|
|
|
|
var session = e.SessionInfo;
|
|
|
|
|
|
|
|
|
|
_sessionToGroupMap.Remove(session.Id, out var tempGroup);
|
|
|
|
|
if (!tempGroup.GroupId.Equals(group.GroupId))
|
|
|
|
|
if (_sessionToGroupMap.TryGetValue(session.Id, out var group))
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Session was in wrong group!");
|
|
|
|
|
var request = new JoinGroupRequest(group.GroupId);
|
|
|
|
|
JoinGroup(session, request, CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|