mirror of
https://github.com/cyanfish/naps2.git
synced 2024-09-11 15:26:55 +03:00
Split SharedDevice into internal and config-level types
This commit is contained in:
parent
1eacb9986f
commit
8db66c0706
16
NAPS2.Lib/Remoting/Server/SharedDevice.cs
Normal file
16
NAPS2.Lib/Remoting/Server/SharedDevice.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using NAPS2.Scan;
|
||||
|
||||
namespace NAPS2.Remoting.Server;
|
||||
|
||||
public record SharedDevice
|
||||
{
|
||||
public required string Name { get; init; }
|
||||
public required ScanDevice Device { get; init; }
|
||||
public int Port { get; init; }
|
||||
|
||||
public virtual bool Equals(SharedDevice? other) =>
|
||||
other is not null && Name == other.Name && Device == other.Device;
|
||||
|
||||
public override int GetHashCode() =>
|
||||
Name.GetHashCode() * 23 + Device.GetHashCode();
|
||||
}
|
@ -108,7 +108,7 @@ public class SharedDeviceManager : ISharedDeviceManager
|
||||
}
|
||||
devices = devices.Add(device);
|
||||
SharedDevices = devices;
|
||||
_server.RegisterDevice(device);
|
||||
RegisterOnServer(device);
|
||||
if (_startTimer != null)
|
||||
{
|
||||
// If startup was deferred, don't wait for the timer before retrying since we adding a device might allow
|
||||
@ -122,7 +122,7 @@ public class SharedDeviceManager : ISharedDeviceManager
|
||||
var devices = SharedDevices;
|
||||
devices = devices.Remove(device);
|
||||
SharedDevices = devices;
|
||||
_server.UnregisterDevice(device);
|
||||
UnregisterOnServer(device);
|
||||
}
|
||||
|
||||
public void ReplaceSharedDevice(SharedDevice original, SharedDevice replacement)
|
||||
@ -136,8 +136,8 @@ public class SharedDeviceManager : ISharedDeviceManager
|
||||
}
|
||||
devices = devices.Replace(original, replacement);
|
||||
SharedDevices = devices;
|
||||
_server.UnregisterDevice(original);
|
||||
_server.RegisterDevice(replacement);
|
||||
UnregisterOnServer(original);
|
||||
RegisterOnServer(replacement);
|
||||
}
|
||||
|
||||
public ImmutableList<SharedDevice> SharedDevices
|
||||
@ -157,7 +157,13 @@ public class SharedDeviceManager : ISharedDeviceManager
|
||||
{
|
||||
foreach (var device in SharedDevices)
|
||||
{
|
||||
_server.RegisterDevice(device);
|
||||
RegisterOnServer(device);
|
||||
}
|
||||
}
|
||||
|
||||
private void RegisterOnServer(SharedDevice device) =>
|
||||
_server.RegisterDevice(device.Device, device.Name, device.Port);
|
||||
|
||||
private void UnregisterOnServer(SharedDevice device) =>
|
||||
_server.UnregisterDevice(device.Device, device.Name);
|
||||
}
|
@ -33,15 +33,15 @@ public class ScanServerIntegrationTests : ContextualTests
|
||||
var displayName = $"testName-{Guid.NewGuid()}";
|
||||
ScanningContext.Logger.LogDebug("Display name: {Name}", displayName);
|
||||
var serverDevice = new ScanDevice(ScanOptionsValidator.SystemDefaultDriver, "testID", "testName");
|
||||
var serverSharedDevice = new SharedDevice { Device = serverDevice, Name = displayName };
|
||||
_server.RegisterDevice(serverSharedDevice);
|
||||
_server.RegisterDevice(serverDevice, displayName);
|
||||
_server.Start().Wait();
|
||||
|
||||
// Set up a client ScanController for scanning through EsclScanDriver -> network -> ScanServer
|
||||
_client = new ScanController(ScanningContext);
|
||||
// This device won't match exactly the real device from GetDeviceList but it includes the UUID which is enough
|
||||
// for EsclScanDriver to correctly identify the server for scanning.
|
||||
_clientDevice = new ScanDevice(Driver.Escl, $"|{serverSharedDevice.GetUuid(_server.InstanceId)}", displayName);
|
||||
var uuid = new ScanServerDevice { Device = serverDevice, Name = displayName }.GetUuid(_server.InstanceId);
|
||||
_clientDevice = new ScanDevice(Driver.Escl, $"|{uuid}", displayName);
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
|
@ -7,7 +7,7 @@ namespace NAPS2.Remoting.Server;
|
||||
public class ScanServer : IDisposable
|
||||
{
|
||||
private readonly ScanningContext _scanningContext;
|
||||
private readonly Dictionary<SharedDevice, EsclDeviceConfig> _currentDevices = new();
|
||||
private readonly Dictionary<ScanServerDevice, EsclDeviceConfig> _currentDevices = new();
|
||||
private readonly IEsclServer _esclServer;
|
||||
private byte[]? _defaultIconPng;
|
||||
|
||||
@ -33,9 +33,9 @@ public class ScanServer : IDisposable
|
||||
public void SetDefaultIcon(byte[] iconPng) => _defaultIconPng = iconPng;
|
||||
|
||||
public void RegisterDevice(ScanDevice device, string? displayName = null, int port = 0) =>
|
||||
RegisterDevice(new SharedDevice { Device = device, Name = displayName ?? device.Name, Port = port });
|
||||
RegisterDevice(new ScanServerDevice { Device = device, Name = displayName ?? device.Name, Port = port });
|
||||
|
||||
public void RegisterDevice(SharedDevice sharedDevice)
|
||||
private void RegisterDevice(ScanServerDevice sharedDevice)
|
||||
{
|
||||
var esclDeviceConfig = MakeEsclDeviceConfig(sharedDevice);
|
||||
_currentDevices.Add(sharedDevice, esclDeviceConfig);
|
||||
@ -43,16 +43,16 @@ public class ScanServer : IDisposable
|
||||
}
|
||||
|
||||
public void UnregisterDevice(ScanDevice device, string? displayName = null) =>
|
||||
UnregisterDevice(new SharedDevice { Device = device, Name = displayName ?? device.Name });
|
||||
UnregisterDevice(new ScanServerDevice { Device = device, Name = displayName ?? device.Name });
|
||||
|
||||
public void UnregisterDevice(SharedDevice sharedDevice)
|
||||
private void UnregisterDevice(ScanServerDevice sharedDevice)
|
||||
{
|
||||
var esclDeviceConfig = _currentDevices[sharedDevice];
|
||||
_currentDevices.Remove(sharedDevice);
|
||||
_esclServer.RemoveDevice(esclDeviceConfig);
|
||||
}
|
||||
|
||||
private EsclDeviceConfig MakeEsclDeviceConfig(SharedDevice device)
|
||||
private EsclDeviceConfig MakeEsclDeviceConfig(ScanServerDevice device)
|
||||
{
|
||||
return new EsclDeviceConfig
|
||||
{
|
||||
|
@ -4,7 +4,7 @@ using NAPS2.Scan;
|
||||
|
||||
namespace NAPS2.Remoting.Server;
|
||||
|
||||
public record SharedDevice
|
||||
internal record ScanServerDevice
|
||||
{
|
||||
public required string Name { get; init; }
|
||||
public required ScanDevice Device { get; init; }
|
||||
@ -17,7 +17,7 @@ public record SharedDevice
|
||||
return new Guid(uniqueHash.Take(16).ToArray()).ToString("D");
|
||||
}
|
||||
|
||||
public virtual bool Equals(SharedDevice? other) =>
|
||||
public virtual bool Equals(ScanServerDevice? other) =>
|
||||
other is not null && Name == other.Name && Device == other.Device;
|
||||
|
||||
public override int GetHashCode() =>
|
Loading…
Reference in New Issue
Block a user