From 9794c8fb1adc01823a9bdd469b470390fee0ebcd Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 5 Apr 2013 16:49:14 -0400 Subject: #99 - Active user list wrong --- .../Library/UserManager.cs | 38 ++++++++-------------- 1 file changed, 14 insertions(+), 24 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Library/UserManager.cs') diff --git a/MediaBrowser.Server.Implementations/Library/UserManager.cs b/MediaBrowser.Server.Implementations/Library/UserManager.cs index 682ec9a8c..9293d8199 100644 --- a/MediaBrowser.Server.Implementations/Library/UserManager.cs +++ b/MediaBrowser.Server.Implementations/Library/UserManager.cs @@ -25,8 +25,8 @@ namespace MediaBrowser.Server.Implementations.Library /// /// The _active connections /// - private readonly List _activeConnections = - new List(); + private readonly ConcurrentDictionary _activeConnections = + new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); /// /// The _users @@ -69,7 +69,7 @@ namespace MediaBrowser.Server.Implementations.Library /// All connections. public IEnumerable AllConnections { - get { return _activeConnections.Where(c => GetUserById(c.UserId) != null).OrderByDescending(c => c.LastActivityDate); } + get { return _activeConnections.Values.OrderByDescending(c => c.LastActivityDate); } } /// @@ -313,29 +313,19 @@ namespace MediaBrowser.Server.Implementations.Library /// ClientConnectionInfo. private ClientConnectionInfo GetConnection(Guid userId, string clientType, string deviceId, string deviceName) { - lock (_activeConnections) - { - var conn = _activeConnections.FirstOrDefault(c => string.Equals(c.Client, clientType, StringComparison.OrdinalIgnoreCase) && string.Equals(deviceId, c.DeviceId)); + var key = clientType + deviceId; - if (conn == null) - { - conn = new ClientConnectionInfo - { - UserId = userId, - Client = clientType, - DeviceName = deviceName, - DeviceId = deviceId - }; - - _activeConnections.Add(conn); - } - else - { - conn.UserId = userId; - } + var connection = _activeConnections.GetOrAdd(key, keyName => new ClientConnectionInfo + { + UserId = userId, + Client = clientType, + DeviceName = deviceName, + DeviceId = deviceId + }); - return conn; - } + connection.UserId = userId; + + return connection; } /// -- cgit v1.2.3