Escl: Key by SharedDevice and simplify API

This commit is contained in:
Ben Olden-Cooligan 2023-12-01 18:05:39 -08:00
parent aec04111ef
commit 2021a1fa23
3 changed files with 16 additions and 15 deletions

View File

@ -23,11 +23,7 @@ public class NetworkSharingSample
using var scanServer = new ScanServer(scanningContext, new EsclServer());
// Register a device to be shared
scanServer.RegisterDevice(new SharedDevice
{
Name = device.Name,
Device = device
});
scanServer.RegisterDevice(device);
// Run the server until the user presses Enter
scanServer.Start();

View File

@ -7,7 +7,7 @@ namespace NAPS2.Remoting.Server;
public class ScanServer : IDisposable
{
private readonly ScanningContext _scanningContext;
private readonly Dictionary<(Driver, string), EsclDeviceConfig> _currentDevices = new();
private readonly Dictionary<SharedDevice, EsclDeviceConfig> _currentDevices = new();
private readonly IEsclServer _esclServer;
private byte[]? _defaultIconPng;
@ -25,19 +25,23 @@ public class ScanServer : IDisposable
public void SetDefaultIcon(byte[] iconPng) => _defaultIconPng = iconPng;
public void RegisterDevice(SharedDevice device)
public void RegisterDevice(ScanDevice device, string? displayName = null) =>
RegisterDevice(new SharedDevice { Device = device, Name = displayName ?? device.Name });
public void RegisterDevice(SharedDevice sharedDevice)
{
var key = (device.Device.Driver, device.Device.ID);
var esclDeviceConfig = MakeEsclDeviceConfig(device);
_currentDevices.Add(key, esclDeviceConfig);
var esclDeviceConfig = MakeEsclDeviceConfig(sharedDevice);
_currentDevices.Add(sharedDevice, esclDeviceConfig);
_esclServer.AddDevice(esclDeviceConfig);
}
public void UnregisterDevice(SharedDevice device)
public void UnregisterDevice(ScanDevice device, string? displayName = null) =>
UnregisterDevice(new SharedDevice { Device = device, Name = displayName ?? device.Name });
public void UnregisterDevice(SharedDevice sharedDevice)
{
var key = (device.Device.Driver, device.Device.ID);
var esclDeviceConfig = _currentDevices[key];
_currentDevices.Remove(key);
var esclDeviceConfig = _currentDevices[sharedDevice];
_currentDevices.Remove(sharedDevice);
_esclServer.RemoveDevice(esclDeviceConfig);
}

View File

@ -13,7 +13,8 @@ public record SharedDevice
{
get
{
var uniqueHash = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(Device.ID));
var key = $"{Device.Driver};{Device.ID};{Name}";
var uniqueHash = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(key));
return new Guid(uniqueHash.Take(16).ToArray()).ToString("D");
}
}