Remotely/Server/Services/ServiceHubConnectionCache.cs
Jared Goodwin 3ef4cdf81a
Extract remote control functionality into separate library. (#539)
* Convert server to new single-file startup model.

* Add remote control implementations.

* Implement IViewerAuthorizer.

* Update hub endpoints.

* Implement HubEventHandler.

* Implement ViewerHubDataProvider.

* Implement page data provider.

* Implement RCL and refactor.

* Update submodule.

* Replace submodule with NuGet.

* Update copy URL.

* Update NuGet.

* Remove deprecated WebRTC.

* Remove deprecated WebRTC.

* Update Immense.RemoteControl

* Building out desktop projects.

* Bring more services into submodule.

* Update submodule.

* Update submodule.

* Refactoring for module.

* Update submodule.

* Update submodule

* Got Windows desktop app running.

* Refactor for submodule changes.

* FIx unattended session start.

* Switch desktop app out of console mode.

* Fix tests.

* Update publishing.

* Remove ClickOnce middleware.

* Remove ClickOnce remnants.

* Update submodule

* Add some logging.

* Update Linux path.

* Update submodule.

* Add cleanup service for unattended sessions that failed to start.

* Update submodule.

* Fix chat.

* Add ValidateExecutableReferencesMatchSelfContained property.

* Add other submodule projects.  Align checkbox.

* Update submodule.  Reduce deserialization in the browser, resulting in faster renders.

* Update submodule.

* Update submodule.

* Update submodule.

* Update submodule.

* Add orgId back for branding.

* Get branding loading in desktop apps.

* Update submodule.

* Create log dir.

* Refactor version check on config page.

* Update submodule.

* Update submodule.

* Change submodule URL.

* Correct namespace.

* Update submodule.

* Checkout submodules recursively.
2022-12-23 06:39:12 -08:00

74 lines
2.7 KiB
C#

using Immense.RemoteControl.Server.Models;
using Remotely.Shared.Models;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
namespace Remotely.Server.Services
{
public interface IServiceHubSessionCache
{
void AddOrUpdateByConnectionId(string connectionId, Device device);
ICollection<Device> GetAllDevices();
IEnumerable<string> GetConnectionIdsByDeviceIds(IEnumerable<string> deviceIds);
bool TryGetByDeviceId(string deviceId, out Device device);
bool TryGetConnectionId(string deviceId, out string serviceConnectionId);
bool TryRemoveByConnectionId(string connectionId, out Device device);
}
public class ServiceHubSessionCache : IServiceHubSessionCache
{
private readonly ConcurrentDictionary<string, Device> _connectionIdToDeviceLookup = new();
private readonly ConcurrentDictionary<string, string> _deviceIdToConnectionIdLookup = new();
public void AddOrUpdateByConnectionId(string connectionId, Device device)
{
_connectionIdToDeviceLookup.AddOrUpdate(connectionId, device, (k, v) => device);
_deviceIdToConnectionIdLookup.AddOrUpdate(device.ID, connectionId, (k, v) => connectionId);
}
public ICollection<Device> GetAllDevices() => _connectionIdToDeviceLookup.Values;
public IEnumerable<string> GetConnectionIdsByDeviceIds(IEnumerable<string> deviceIds)
{
foreach (var deviceId in deviceIds)
{
if (TryGetConnectionId(deviceId, out var connectionId))
{
yield return connectionId;
}
}
}
public bool TryGetByDeviceId(string deviceId, out Device device)
{
if (_deviceIdToConnectionIdLookup.TryGetValue(deviceId, out var connectionId) &&
_connectionIdToDeviceLookup.TryGetValue(connectionId, out device))
{
return true;
}
device = Device.Empty;
return false;
}
public bool TryGetConnectionId(string deviceId, out string serviceConnectionId)
{
return _deviceIdToConnectionIdLookup.TryGetValue(deviceId, out serviceConnectionId);
}
public bool TryRemoveByConnectionId(string connectionId, out Device device)
{
if (_connectionIdToDeviceLookup.TryRemove(connectionId, out var lookupResult))
{
device = lookupResult;
_ = _deviceIdToConnectionIdLookup.TryRemove(lookupResult.ID, out _);
return true;
}
device = Device.Empty;
return false;
}
}
}