mirror of
https://github.com/cyanfish/naps2.git
synced 2024-09-11 15:26:55 +03:00
Escl: Key by SharedDevice and simplify API
This commit is contained in:
parent
aec04111ef
commit
2021a1fa23
@ -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();
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user