Skip to content

Commit

Permalink
Simplify twain type names
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish authored and fabiante committed Mar 18, 2024
1 parent dbffcbd commit 310fb3d
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 38 deletions.
8 changes: 4 additions & 4 deletions NAPS2.Lib/Modules/WorkerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ protected override void Load(ContainerBuilder builder)
{
builder.Register(ctx => new ScanningContext(ctx.Resolve<ImageContext>())).SingleInstance();
#if MAC
builder.RegisterType<StubTwainSessionController>().As<ITwainSessionController>();
builder.RegisterType<StubTwainController>().As<ITwainController>();
#elif NET6_0_OR_GREATER
if (OperatingSystem.IsWindows())
{
builder.RegisterType<LocalTwainSessionController>().As<ITwainSessionController>();
builder.RegisterType<LocalTwainController>().As<ITwainController>();
}
else
{
builder.RegisterType<StubTwainSessionController>().As<ITwainSessionController>();
builder.RegisterType<StubTwainController>().As<ITwainController>();
}
#else
builder.RegisterType<LocalTwainSessionController>().As<ITwainSessionController>();
builder.RegisterType<LocalTwainController>().As<ITwainController>();
#endif
}
}
10 changes: 5 additions & 5 deletions NAPS2.Sdk.Tests/Worker/WorkerChannelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ namespace NAPS2.Sdk.Tests.Worker;
public class WorkerChannelTests : ContextualTests
{
private Channel Start(IRemoteScanController remoteScanController = null, ThumbnailRenderer thumbnailRenderer = null,
IMapiWrapper mapiWrapper = null, ITwainSessionController twainSessionController = null)
IMapiWrapper mapiWrapper = null, ITwainController twainController = null)
{
string pipeName = $"WorkerNamedPipeTests.{Path.GetRandomFileName()}";
NamedPipeServer server = new NamedPipeServer(pipeName);
WorkerService.BindService(server.ServiceBinder,
new WorkerServiceImpl(ScanningContext, remoteScanController, thumbnailRenderer, mapiWrapper,
twainSessionController));
twainController));
server.Start();
var client = new WorkerServiceAdapter(new NamedPipeChannel(".", pipeName));
return new Channel
Expand Down Expand Up @@ -135,9 +135,9 @@ public async Task ScanException()
public async Task TwainScan()
{
var twainEvents = Substitute.For<ITwainEvents>();
var sessionController = Substitute.For<ITwainSessionController>();
var twainController = Substitute.For<ITwainController>();

sessionController.StartScan(Arg.Any<ScanOptions>(), Arg.Any<TwainEvents>(), Arg.Any<CancellationToken>())
twainController.StartScan(Arg.Any<ScanOptions>(), Arg.Any<TwainEvents>(), Arg.Any<CancellationToken>())
.Returns(
x =>
{
Expand All @@ -149,7 +149,7 @@ public async Task TwainScan()
return Task.CompletedTask;
});

using var channel = Start(twainSessionController: sessionController);
using var channel = Start(twainController: twainController);
await channel.Client.TwainScan(new ScanOptions(), CancellationToken.None, twainEvents);

twainEvents.Received().PageStart(Arg.Any<TwainPageStart>());
Expand Down
4 changes: 2 additions & 2 deletions NAPS2.Sdk/Remoting/Worker/WorkerServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public static async Task Run(ScanningContext scanningContext, CancellationToken
var serviceImpl = new WorkerServiceImpl(scanningContext,
new ThumbnailRenderer(scanningContext.ImageContext), new StubMapiWrapper(),
#if MAC
new StubTwainSessionController());
new StubTwainController());
#else
new LocalTwainSessionController(scanningContext));
new LocalTwainController(scanningContext));
#endif
serviceImpl.OnStop += (_, _) => tcs.SetResult(true);
WorkerService.BindService(server.ServiceBinder, serviceImpl);
Expand Down
14 changes: 7 additions & 7 deletions NAPS2.Sdk/Remoting/Worker/WorkerServiceImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,27 @@ internal class WorkerServiceImpl : WorkerService.WorkerServiceBase
private readonly IRemoteScanController _remoteScanController;
private readonly ThumbnailRenderer _thumbnailRenderer;
private readonly IMapiWrapper _mapiWrapper;
private readonly ITwainSessionController _twainSessionController;
private readonly ITwainController _twainController;

private readonly AutoResetEvent _ongoingCallFinished = new(false);
private int _ongoingCallCount;

public WorkerServiceImpl(ScanningContext scanningContext, ThumbnailRenderer thumbnailRenderer,
IMapiWrapper mapiWrapper, ITwainSessionController twainSessionController)
IMapiWrapper mapiWrapper, ITwainController twainController)
: this(scanningContext, new RemoteScanController(scanningContext),
thumbnailRenderer, mapiWrapper, twainSessionController)
thumbnailRenderer, mapiWrapper, twainController)
{
}

internal WorkerServiceImpl(ScanningContext scanningContext, IRemoteScanController remoteScanController,
ThumbnailRenderer thumbnailRenderer,
IMapiWrapper mapiWrapper, ITwainSessionController twainSessionController)
IMapiWrapper mapiWrapper, ITwainController twainController)
{
_scanningContext = scanningContext;
_remoteScanController = remoteScanController;
_thumbnailRenderer = thumbnailRenderer;
_mapiWrapper = mapiWrapper;
_twainSessionController = twainSessionController;
_twainController = twainController;
}

public override Task<InitResponse> Init(InitRequest request, ServerCallContext context)
Expand Down Expand Up @@ -315,7 +315,7 @@ public override async Task<GetDeviceListResponse> TwainGetDeviceList(GetDeviceLi
try
{
var options = request.OptionsXml.FromXml<ScanOptions>();
var deviceList = await _twainSessionController.GetDeviceList(options);
var deviceList = await _twainController.GetDeviceList(options);
return new GetDeviceListResponse
{
DeviceListXml = deviceList.ToXml()
Expand Down Expand Up @@ -353,7 +353,7 @@ public override async Task TwainScan(TwainScanRequest request,
})
);
var options = request.OptionsXml.FromXml<ScanOptions>();
await _twainSessionController.StartScan(options, twainEvents, context.CancellationToken);
await _twainController.StartScan(options, twainEvents, context.CancellationToken);
}
catch (Exception e)
{
Expand Down
2 changes: 1 addition & 1 deletion NAPS2.Sdk/Scan/Internal/ScanBridgeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public IScanBridge Create(ScanOptions options)
if (options is { Driver: Driver.Twain, TwainOptions.Adapter: TwainAdapter.Legacy })
{
// Legacy twain needs to run in a 32-bit worker
// (Normal twain also does, but it runs the worker at a lower level via RemoteTwainSessionController)
// (Normal twain also does, but it runs the worker at a lower level via RemoteTwainController)
return new WorkerScanBridge(_scanningContext, WorkerType.WinX86);
}
return new InProcScanBridge(_scanningContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace NAPS2.Scan.Internal.Twain;
/// Interface for interacting with a Twain session that might happen in the current (local) process or a worker (remote)
/// process.
/// </summary>
internal interface ITwainSessionController
internal interface ITwainController
{
Task<List<ScanDevice>> GetDeviceList(ScanOptions options);
Task StartScan(ScanOptions options, ITwainEvents twainEvents, CancellationToken cancelToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
namespace NAPS2.Scan.Internal.Twain;

/// <summary>
/// Real implementation of ITwainSessionController that interacts with a Twain session in the current process.
/// Real implementation of ITwainController that interacts with a Twain session in the current process.
/// </summary>
internal class LocalTwainSessionController : ITwainSessionController
internal class LocalTwainController : ITwainController
{
public static readonly TWIdentity TwainAppId;

static LocalTwainSessionController()
static LocalTwainController()
{
PlatformInfo.Current.Log.IsDebugEnabled = true;
TwainAppId = TWIdentity.Create(DataGroups.Image | DataGroups.Control, AssemblyHelper.Version,
Expand All @@ -32,7 +32,7 @@ static LocalTwainSessionController()

private readonly ILogger _logger;

public LocalTwainSessionController(ScanningContext scanningContext)
public LocalTwainController(ScanningContext scanningContext)
{
_logger = scanningContext.Logger;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public async Task StartScan(ScanOptions options, ITwainEvents twainEvents, Cance
private async Task InternalScan(TwainDsm dsm, ScanOptions options, CancellationToken cancelToken,
ITwainEvents twainEvents)
{
var runner = new TwainSessionScanRunner(_logger, TwainAppId, dsm, options, cancelToken, twainEvents);
var runner = new TwainScanRunner(_logger, TwainAppId, dsm, options, cancelToken, twainEvents);
await runner.Run();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
namespace NAPS2.Scan.Internal.Twain;

/// <summary>
/// Proxy implementation of ITwainSessionController that interacts with a Twain session in a worker process.
/// Proxy implementation of ITwainController that interacts with a Twain session in a worker process.
/// </summary>
internal class RemoteTwainSessionController : ITwainSessionController
internal class RemoteTwainController : ITwainController
{
private readonly ScanningContext _scanningContext;

public RemoteTwainSessionController(ScanningContext scanningContext)
public RemoteTwainController(ScanningContext scanningContext)
{
_scanningContext = scanningContext;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace NAPS2.Scan.Internal.Twain;

/// <summary>
/// Stub implementation of ITwainSessionController for unsupported platforms.
/// Stub implementation of ITwainController for unsupported platforms.
/// </summary>
internal class StubTwainSessionController : ITwainSessionController
internal class StubTwainController : ITwainController
{
public Task<List<ScanDevice>> GetDeviceList(ScanOptions options)
{
Expand Down
12 changes: 6 additions & 6 deletions NAPS2.Sdk/Scan/Internal/Twain/TwainScanDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace NAPS2.Scan.Internal.Twain;

/// <summary>
/// Implementation of IScanDriver for Twain. Delegates to RemoteTwainSessionController in most cases which runs Twain
/// Implementation of IScanDriver for Twain. Delegates to RemoteTwainController in most cases which runs Twain
/// in a 32-bit worker process as Twain drivers are generally 32-bit only.
/// </summary>
internal class TwainScanDriver : IScanDriver
Expand All @@ -22,7 +22,7 @@ public Task GetDevices(ScanOptions options, CancellationToken cancelToken, Actio
CheckArch(options);
return Task.Run(async () =>
{
var controller = GetSessionController(options);
var controller = GetTwainController(options);
foreach (var device in await controller.GetDeviceList(options))
{
callback(device);
Expand All @@ -36,7 +36,7 @@ public Task Scan(ScanOptions options, CancellationToken cancelToken, IScanEvents
CheckArch(options);
return Task.Run(async () =>
{
var controller = GetSessionController(options);
var controller = GetTwainController(options);
using var state = new TwainImageProcessor(_scanningContext, options, scanEvents, callback);
try
{
Expand Down Expand Up @@ -86,16 +86,16 @@ private void CheckArch(ScanOptions options)
}
}

private ITwainSessionController GetSessionController(ScanOptions options)
private ITwainController GetTwainController(ScanOptions options)
{
if (_scanningContext.WorkerFactory == null)
{
// If we don't have a worker, we assume the configuration has already been validated by CheckArch and will
// run in the current process.
// In general we always prefer to run TWAIN in a worker though.
return new LocalTwainSessionController(_scanningContext);
return new LocalTwainController(_scanningContext);
}
return new RemoteTwainSessionController(_scanningContext);
return new RemoteTwainController(_scanningContext);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace NAPS2.Scan.Internal.Twain;
/// ITwainEvents interface. This logic involves quite a bit of complicated state management related to the Twain spec.
/// https://twain.org/wp-content/uploads/2015/05/TWAIN-2.3-Specification.pdf
/// </summary>
internal class TwainSessionScanRunner
internal class TwainScanRunner
{
private readonly ILogger _logger;
private readonly TwainDsm _dsm;
Expand All @@ -27,7 +27,7 @@ internal class TwainSessionScanRunner
private readonly TaskCompletionSource<bool> _sourceDisabledTcs;
private DataSource? _source;

public TwainSessionScanRunner(ILogger logger, TWIdentity twainAppId, TwainDsm dsm, ScanOptions options,
public TwainScanRunner(ILogger logger, TWIdentity twainAppId, TwainDsm dsm, ScanOptions options,
CancellationToken cancelToken, ITwainEvents twainEvents)
{
_logger = logger;
Expand Down

0 comments on commit 310fb3d

Please sign in to comment.