Refactor concurrent dictionaries.

This commit is contained in:
Jared Goodwin 2019-09-25 17:19:53 -07:00
parent f8f9437dc3
commit 771fe76fed
9 changed files with 21 additions and 87 deletions

View File

@ -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();
}

View File

@ -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();
}

View File

@ -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;
}

View File

@ -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();
}

View File

@ -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();

View File

@ -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);
}

View File

@ -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);

View File

@ -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);

View File

@ -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)
{