diff --git a/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs b/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs
index b2422f8e6e..e43f5c2c31 100644
--- a/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs
+++ b/Emby.Server.Implementations/SyncPlay/SyncPlayManager.cs
@@ -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
///
/// The map between sessions and groups.
///
- private readonly Dictionary _sessionToGroupMap =
- new Dictionary(StringComparer.OrdinalIgnoreCase);
+ private readonly ConcurrentDictionary _sessionToGroupMap =
+ new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
///
/// The groups.
///
- private readonly Dictionary _groups =
- new Dictionary();
+ private readonly ConcurrentDictionary _groups =
+ new ConcurrentDictionary();
///
- /// Lock used for accessing any group.
+ /// Lock used for accessing multiple groups at once.
///
///
- /// Always lock before and before locking on any .
+ /// This lock has priority on locks made on .
///
private readonly object _groupsLock = new object();
- ///
- /// Lock used for accessing the session-to-group map.
- ///
- ///
- /// Always lock after and before locking on any .
- ///
- private readonly object _mapsLock = new object();
-
private bool _disposed = false;
///
@@ -102,31 +94,51 @@ namespace Emby.Server.Implementations.SyncPlay
///
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)
+ // Make sure that session has not joined another group.
+ if (_sessionToGroupMap.ContainsKey(session.Id))
{
- if (IsSessionInGroup(session))
- {
- var leaveGroupRequest = new LeaveGroupRequest();
- LeaveGroup(session, leaveGroupRequest, cancellationToken);
- }
+ var leaveGroupRequest = new LeaveGroupRequest();
+ LeaveGroup(session, leaveGroupRequest, cancellationToken);
+ }
- var group = new Group(_loggerFactory, _userManager, _sessionManager, _libraryManager);
- _groups[group.GroupId] = group;
+ 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);
}
}
///
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,37 +155,37 @@ 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)
{
- // Group lock required to let other requests end first.
- lock (group)
+ if (!group.HasAccessToPlayQueue(user))
{
- if (!group.HasAccessToPlayQueue(user))
- {
- _logger.LogWarning("Session {SessionId} tried to join group {GroupId} but does not have access to some content of the playing queue.", session.Id, group.GroupId.ToString());
+ _logger.LogWarning("Session {SessionId} tried to join group {GroupId} but does not have access to some content of the playing queue.", session.Id, group.GroupId.ToString());
- var error = new GroupUpdate(group.GroupId, GroupUpdateType.LibraryAccessDenied, string.Empty);
- _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
- return;
- }
+ var error = new GroupUpdate(group.GroupId, GroupUpdateType.LibraryAccessDenied, string.Empty);
+ _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
+ return;
+ }
- if (IsSessionInGroup(session))
+ if (_sessionToGroupMap.TryGetValue(session.Id, out var existingGroup))
+ {
+ if (existingGroup.GroupId.Equals(request.GroupId))
{
- if (FindJoinedGroupId(session).Equals(request.GroupId))
- {
- // Restore session.
- group.SessionJoin(session, request, cancellationToken);
- return;
- }
-
- var leaveGroupRequest = new LeaveGroupRequest();
- LeaveGroup(session, leaveGroupRequest, cancellationToken);
+ // Restore session.
+ group.SessionJoin(session, request, cancellationToken);
+ return;
}
- AddSessionToGroup(session, group);
- group.SessionJoin(session, request, cancellationToken);
+ var leaveGroupRequest = new LeaveGroupRequest();
+ LeaveGroup(session, leaveGroupRequest, 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
///
public void LeaveGroup(SessionInfo session, LeaveGroupRequest 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.
- lock (_mapsLock)
+ if (_sessionToGroupMap.TryGetValue(session.Id, out var group))
{
- var group = FindJoinedGroup(session);
- if (group == null)
- {
- _logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
-
- var error = new GroupUpdate(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
- _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
- return;
- }
-
// 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,27 +232,41 @@ namespace Emby.Server.Implementations.SyncPlay
}
}
}
+ else
+ {
+ _logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
+
+ var error = new GroupUpdate(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
+ _sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
+ return;
+ }
}
}
///
public List 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 list = new List();
- // Locking required to access list of groups.
- lock (_groupsLock)
+ foreach (var group in _groups.Values)
{
- foreach (var group in _groups.Values)
+ // Locking required as group is not thread-safe.
+ lock (group)
{
- // Locking required as group is not thread-safe.
- lock (group)
+ if (group.HasAccessToPlayQueue(user))
{
- if (group.HasAccessToPlayQueue(user))
- {
- list.Add(group.GetInfo());
- }
+ list.Add(group.GetInfo());
}
}
}
@@ -241,25 +277,43 @@ namespace Emby.Server.Implementations.SyncPlay
///
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)
+ {
+ 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)
+ {
+ // 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;
+ }
+
+ // Drop request if group is empty.
+ if (group.IsGroupEmpty())
+ {
+ return;
+ }
+
+ group.HandleRequest(session, request, cancellationToken);
+ }
+ }
+ else
{
_logger.LogWarning("Session {SessionId} does not belong to any group.", session.Id);
var error = new GroupUpdate(Guid.Empty, GroupUpdateType.NotInGroup, string.Empty);
_sessionManager.SendSyncPlayGroupUpdate(session, error, CancellationToken.None);
- return;
- }
-
- // Group lock required as Group is not thread-safe.
- lock (group)
- {
- group.HandleRequest(session, request, cancellationToken);
}
}
@@ -282,115 +336,10 @@ namespace Emby.Server.Implementations.SyncPlay
{
var session = e.SessionInfo;
- Guid groupId = Guid.Empty;
- lock (_mapsLock)
- {
- groupId = FindJoinedGroupId(session);
- }
-
- if (groupId.Equals(Guid.Empty))
- {
- return;
- }
-
- var request = new JoinGroupRequest(groupId);
- JoinGroup(session, request, CancellationToken.None);
- }
-
- ///
- /// Checks if a given session has joined a group.
- ///
- ///
- /// Method is not thread-safe, external locking on is required.
- ///
- /// The session.
- /// true if the session has joined a group, false otherwise.
- private bool IsSessionInGroup(SessionInfo session)
- {
- return _sessionToGroupMap.ContainsKey(session.Id);
- }
-
- ///
- /// Gets the group joined by the given session, if any.
- ///
- ///
- /// Method is not thread-safe, external locking on is required.
- ///
- /// The session.
- /// The group.
- private Group FindJoinedGroup(SessionInfo session)
- {
- _sessionToGroupMap.TryGetValue(session.Id, out var group);
- return group;
- }
-
- ///
- /// Gets the group identifier joined by the given session, if any.
- ///
- ///
- /// Method is not thread-safe, external locking on is required.
- ///
- /// The session.
- /// The group identifier if the session has joined a group, an empty identifier otherwise.
- private Guid FindJoinedGroupId(SessionInfo session)
- {
- return FindJoinedGroup(session)?.GroupId ?? Guid.Empty;
- }
-
- ///
- /// Maps a session to a group.
- ///
- ///
- /// Method is not thread-safe, external locking on is required.
- ///
- /// The session.
- /// The group.
- /// Thrown when the user is in another group already.
- private void AddSessionToGroup(SessionInfo session, Group group)
- {
- if (session == null)
- {
- throw new InvalidOperationException("Session is null!");
- }
-
- if (IsSessionInGroup(session))
- {
- throw new InvalidOperationException("Session in other group already!");
- }
-
- _sessionToGroupMap[session.Id] = group ?? throw new InvalidOperationException("Group is null!");
- }
-
- ///
- /// Unmaps a session from a group.
- ///
- ///
- /// Method is not thread-safe, external locking on is required.
- ///
- /// The session.
- /// The group.
- /// Thrown when the user is not found in the specified group.
- private void RemoveSessionFromGroup(SessionInfo session, Group group)
- {
- if (session == null)
- {
- throw new InvalidOperationException("Session is null!");
- }
-
- if (group == null)
- {
- throw new InvalidOperationException("Group is null!");
- }
-
- if (!IsSessionInGroup(session))
- {
- throw new InvalidOperationException("Session not in any group!");
- }
-
- _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);
}
}
}