diff --git a/Agent/Services/Bash.cs b/Agent/Services/Bash.cs index cdfaa320..622a786e 100644 --- a/Agent/Services/Bash.cs +++ b/Agent/Services/Bash.cs @@ -35,10 +35,7 @@ namespace Remotely.Agent.Services bash.ProcessIdleTimeout = new System.Timers.Timer(600000); // 10 minutes. bash.ProcessIdleTimeout.AutoReset = false; bash.ProcessIdleTimeout.Elapsed += bash.ProcessIdleTimeout_Elapsed; - while (!Sessions.TryAdd(connectionID, bash)) - { - Thread.Sleep(1000); - } + Sessions.AddOrUpdate(connectionID, bash, (id, b) => bash); bash.ProcessIdleTimeout.Start(); return bash; } @@ -46,11 +43,7 @@ namespace Remotely.Agent.Services private void ProcessIdleTimeout_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { - Bash outResult; - while (!Sessions.TryRemove(ConnectionID, out outResult)) - { - Thread.Sleep(1000); - } + Sessions.Remove(ConnectionID, out var outResult); outResult.BashProc.Kill(); } diff --git a/Agent/Services/CMD.cs b/Agent/Services/CMD.cs index 9885b0f7..2c8b32e6 100644 --- a/Agent/Services/CMD.cs +++ b/Agent/Services/CMD.cs @@ -35,10 +35,7 @@ namespace Remotely.Agent.Services cmd.ProcessIdleTimeout = new System.Timers.Timer(600000); // 10 minutes. cmd.ProcessIdleTimeout.AutoReset = false; cmd.ProcessIdleTimeout.Elapsed += cmd.ProcessIdleTimeout_Elapsed; - while (!Sessions.TryAdd(connectionID, cmd)) - { - Thread.Sleep(1000); - } + Sessions.AddOrUpdate(connectionID, cmd, (id, c) => cmd); cmd.ProcessIdleTimeout.Start(); return cmd; } @@ -46,11 +43,7 @@ namespace Remotely.Agent.Services private void ProcessIdleTimeout_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { - CMD outResult; - while (!Sessions.TryRemove(ConnectionID, out outResult)) - { - Thread.Sleep(1000); - } + Sessions.Remove(ConnectionID, out var outResult); outResult.CMDProc.Kill(); } diff --git a/Agent/Services/PSCore.cs b/Agent/Services/PSCore.cs index 23700b8f..a9203af8 100644 --- a/Agent/Services/PSCore.cs +++ b/Agent/Services/PSCore.cs @@ -30,15 +30,9 @@ namespace Remotely.Agent.Services psCore.ProcessIdleTimeout.AutoReset = false; psCore.ProcessIdleTimeout.Elapsed += (sender, args) => { - while (!Sessions.TryRemove(connectionID, out var pSCore)) - { - System.Threading.Thread.Sleep(1000); - } + Sessions.Remove(connectionID, out var pSCore); }; - while (!Sessions.TryAdd(connectionID, psCore)) - { - System.Threading.Thread.Sleep(1000); - } + Sessions.AddOrUpdate(connectionID, psCore, (id, p) => psCore); psCore.ProcessIdleTimeout.Start(); return psCore; } diff --git a/Agent/Services/WindowsPS.cs b/Agent/Services/WindowsPS.cs index 8ea0790c..a563217c 100644 --- a/Agent/Services/WindowsPS.cs +++ b/Agent/Services/WindowsPS.cs @@ -33,10 +33,7 @@ namespace Remotely.Agent.Services winPS.ProcessIdleTimeout = new System.Timers.Timer(600000); // 10 minutes. winPS.ProcessIdleTimeout.AutoReset = false; winPS.ProcessIdleTimeout.Elapsed += winPS.ProcessIdleTimeout_Elapsed; - while (!Sessions.TryAdd(connectionID, winPS)) - { - Thread.Sleep(1000); - } + Sessions.AddOrUpdate(connectionID, winPS, (id, w) => winPS); winPS.ProcessIdleTimeout.Start(); return winPS; } @@ -44,11 +41,7 @@ namespace Remotely.Agent.Services private void ProcessIdleTimeout_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { - WindowsPS outResult; - while (!Sessions.TryRemove(ConnectionID, out outResult)) - { - Thread.Sleep(1000); - } + Sessions.Remove(ConnectionID, out var outResult); outResult.PSProc.Kill(); } diff --git a/ScreenCast.Core/Capture/ScreenCaster.cs b/ScreenCast.Core/Capture/ScreenCaster.cs index d55b8856..5f52961e 100644 --- a/ScreenCast.Core/Capture/ScreenCaster.cs +++ b/ScreenCast.Core/Capture/ScreenCaster.cs @@ -24,7 +24,6 @@ namespace Remotely.ScreenCast.Core.Capture { Viewer viewer; byte[] encodedImageBytes; - var success = false; Logger.Write($"Starting screen cast. Requester: {requesterName}. Viewer ID: {viewerID}. Capturer: {capturer.GetType().ToString()}. App Mode: {conductor.Mode} Desktop: {conductor.CurrentDesktopName}"); @@ -38,10 +37,7 @@ namespace Remotely.ScreenCast.Core.Capture HasControl = true }; - while (!success) - { - success = conductor.Viewers.TryAdd(viewerID, viewer); - } + conductor.Viewers.AddOrUpdate(viewerID, viewer, (id, v) => viewer); if (conductor.Mode == Enums.AppMode.Normal) { @@ -123,11 +119,7 @@ namespace Remotely.ScreenCast.Core.Capture } } Logger.Write($"Ended screen cast. Requester: {requesterName}. Viewer ID: {viewerID}."); - success = false; - while (!success) - { - success = conductor.Viewers.TryRemove(viewerID, out _); - } + conductor.Viewers.TryRemove(viewerID, out _); capturer.Dispose(); diff --git a/Server/Services/BrowserSocketHub.cs b/Server/Services/BrowserSocketHub.cs index 6eeec01a..e094a306 100644 --- a/Server/Services/BrowserSocketHub.cs +++ b/Server/Services/BrowserSocketHub.cs @@ -116,22 +116,15 @@ namespace Remotely.Server.Services { return; } - while (!ConnectionIdToUserLookup.TryAdd(Context.ConnectionId, RemotelyUser)) - { - DataService.WriteEvent("Retrying ConnectionIdToUserLookup.TryAdd in BrowserSocketHub."); - await Task.Delay(100); - } - await Clients.Caller.SendAsync("UserOptions", RemotelyUser.UserOptions); + ConnectionIdToUserLookup.AddOrUpdate(Context.ConnectionId, RemotelyUser, (id, ru) => RemotelyUser); + + await Clients.Caller.SendAsync("UserOptions", RemotelyUser.UserOptions); await base.OnConnectedAsync(); } public override async Task OnDisconnectedAsync(Exception exception) { - while (!ConnectionIdToUserLookup.TryRemove(Context.ConnectionId, out _)) - { - DataService.WriteEvent("Retrying ConnectionIdToUserLookup.TryRemove in BrowserSocketHub."); - await Task.Delay(100); - } + ConnectionIdToUserLookup.Remove(Context.ConnectionId, out _); await base.OnDisconnectedAsync(exception); } diff --git a/Server/Services/DeviceSocketHub.cs b/Server/Services/DeviceSocketHub.cs index 6040c3be..301ab0fe 100644 --- a/Server/Services/DeviceSocketHub.cs +++ b/Server/Services/DeviceSocketHub.cs @@ -77,11 +77,7 @@ namespace Remotely.Server.Services if (DataService.AddOrUpdateDevice(device, out var updatedDevice)) { Device = updatedDevice; - while (!ServiceConnections.TryAdd(Context.ConnectionId, Device)) - { - DataService.WriteEvent("Retrying ServiceConnections.TryAdd in DeviceSocketHub."); - await Task.Delay(100); - } + ServiceConnections.AddOrUpdate(Context.ConnectionId, Device, (id, d) => Device); var onlineOrganizationUsers = BrowserSocketHub.ConnectionIdToUserLookup .Where(x => x.Value.OrganizationID == Device.OrganizationID); @@ -150,11 +146,7 @@ namespace Remotely.Server.Services await BrowserHub.Clients.Clients(connectionIds).SendAsync("DeviceWentOffline", Device); - while (!ServiceConnections.TryRemove(Context.ConnectionId, out _)) - { - DataService.WriteEvent("Retrying ServiceConnections.TryRemove in DeviceSocketHub."); - await Task.Delay(100); - } + ServiceConnections.Remove(Context.ConnectionId, out _); } await base.OnDisconnectedAsync(exception); diff --git a/Server/Services/RCBrowserSocketHub.cs b/Server/Services/RCBrowserSocketHub.cs index 2c36bf74..ed5bbc33 100644 --- a/Server/Services/RCBrowserSocketHub.cs +++ b/Server/Services/RCBrowserSocketHub.cs @@ -121,11 +121,7 @@ namespace Remotely.Server.Services if (Context.User.Identity.IsAuthenticated) { var user = DataService.GetUserByName(Context.User.Identity.Name); - while (!OrganizationConnectionList.TryAdd(Context.ConnectionId, user)) - { - DataService.WriteEvent("Retrying OrganizationConnectionList.TryAdd in RCBrowserSocketHub."); - await Task.Delay(100); - } + OrganizationConnectionList.AddOrUpdate(Context.ConnectionId, user, (id, r) => user); } await base.OnConnectedAsync(); } @@ -134,11 +130,7 @@ namespace Remotely.Server.Services { if (Context.User.Identity.IsAuthenticated) { - while (!OrganizationConnectionList.TryRemove(Context.ConnectionId, out _)) - { - DataService.WriteEvent("Retrying OrganizationConnectionList.TryRemove in RCBrowserSocketHub."); - await Task.Delay(100); - } + OrganizationConnectionList.Remove(Context.ConnectionId, out _); } await RCDeviceHub.Clients.Client(ScreenCasterID).SendAsync("ViewerDisconnected", Context.ConnectionId); diff --git a/Server/Services/RCDeviceSocketHub.cs b/Server/Services/RCDeviceSocketHub.cs index 346f2330..dc5ac983 100644 --- a/Server/Services/RCDeviceSocketHub.cs +++ b/Server/Services/RCDeviceSocketHub.cs @@ -125,21 +125,13 @@ namespace Remotely.Server.Services RCSocketID = Context.ConnectionId, StartTime = DateTime.Now }; - while (!SessionInfoList.TryAdd(Context.ConnectionId, SessionInfo)) - { - DataService.WriteEvent("Retrying SessionInfoList.TryAdd in RCDeviceSocketHub."); - await Task.Delay(100); - } - + SessionInfoList.AddOrUpdate(Context.ConnectionId, SessionInfo, (id, si) => SessionInfo); + await base.OnConnectedAsync(); } public override async Task OnDisconnectedAsync(Exception exception) { - while (!SessionInfoList.TryRemove(Context.ConnectionId, out _)) - { - DataService.WriteEvent("Retrying SessionInfoList.TryRemove in RCDeviceSocketHub."); - await Task.Delay(100); - } + SessionInfoList.Remove(Context.ConnectionId, out _); if (SessionInfo.Mode == Shared.Enums.RemoteControlMode.Normal) {