From 59fe06519c93bf589df3544b78d5cf7e16dcd657 Mon Sep 17 00:00:00 2001 From: Eduard Dumitru Date: Thu, 28 Nov 2024 12:18:34 +0100 Subject: [PATCH] simplifications after review - decommission WaitForStop --- .../UiPath.CoreIpc.NodeInterop/Program.cs | 2 +- src/UiPath.CoreIpc/Config/IpcServer.cs | 3 - .../UiPath.Ipc.net6.0-windows.received.txt | 1 - src/UiPath.Ipc.Tests/Program.cs | 58 +++++++++++++------ 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/Clients/js/dotnet/UiPath.CoreIpc.NodeInterop/Program.cs b/src/Clients/js/dotnet/UiPath.CoreIpc.NodeInterop/Program.cs index b4476882..cba5d22e 100644 --- a/src/Clients/js/dotnet/UiPath.CoreIpc.NodeInterop/Program.cs +++ b/src/Clients/js/dotnet/UiPath.CoreIpc.NodeInterop/Program.cs @@ -155,7 +155,7 @@ IEnumerable EnumeratePings() } }); - await Task.WhenAll(ipcServers.Select(ipcServer => ipcServer.WaitForStop())); + await Task.Delay(Timeout.InfiniteTimeSpan); IpcServer CreateAndStartIpcServer(ServerTransport transport) { diff --git a/src/UiPath.CoreIpc/Config/IpcServer.cs b/src/UiPath.CoreIpc/Config/IpcServer.cs index 0fbc3d40..138d0ec3 100644 --- a/src/UiPath.CoreIpc/Config/IpcServer.cs +++ b/src/UiPath.CoreIpc/Config/IpcServer.cs @@ -9,7 +9,6 @@ public sealed class IpcServer : IpcBase, IAsyncDisposable private readonly object _lock = new(); private readonly TaskCompletionSource _listening = new(); - private readonly TaskCompletionSource _stopped = new(); private readonly CancellationTokenSource _ctsActiveConnections = new(); private bool _disposeStarted; @@ -70,7 +69,6 @@ public Task WaitForStart() Start(); return _accepter.StartedAccepting; } - public Task WaitForStop() => _stopped.Task; internal ILogger? CreateLogger(string category) => ServiceProvider.MaybeCreateLogger(category); @@ -82,7 +80,6 @@ private void OnNewConnection(Stream network) private void OnNewConnectionError(Exception ex) { Trace.TraceError($"Failed to accept new connection. Ex: {ex}"); - _stopped.TrySetException(ex); } internal RouterConfig CreateRouterConfig(IpcServer server) => RouterConfig.From( diff --git a/src/UiPath.CoreIpc/report/UiPath.Ipc.net6.0-windows.received.txt b/src/UiPath.CoreIpc/report/UiPath.Ipc.net6.0-windows.received.txt index 95fd1bdb..1290f0c3 100644 --- a/src/UiPath.CoreIpc/report/UiPath.Ipc.net6.0-windows.received.txt +++ b/src/UiPath.CoreIpc/report/UiPath.Ipc.net6.0-windows.received.txt @@ -107,7 +107,6 @@ namespace UiPath.Ipc "_accepter"})] public void Start() { } public System.Threading.Tasks.Task WaitForStart() { } - public System.Threading.Tasks.Task WaitForStop() { } } public class Message { diff --git a/src/UiPath.Ipc.Tests/Program.cs b/src/UiPath.Ipc.Tests/Program.cs index feb7a67e..4f21b49b 100644 --- a/src/UiPath.Ipc.Tests/Program.cs +++ b/src/UiPath.Ipc.Tests/Program.cs @@ -4,30 +4,50 @@ using UiPath.Ipc; using UiPath.Ipc.Tests; -if (args is not [var base64]) +using (ConsoleCancellation(out var ct)) { - Console.Error.WriteLine($"Usage: dotnet {Path.GetFileName(Assembly.GetEntryAssembly()!.Location)} "); - return 1; + return await Entry(ct); } -var externalServerParams = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(Convert.FromBase64String(base64))); -await using var asyncDisposable = externalServerParams.CreateListenerConfig(out var serverTransport); -await using var serviceProvider = new ServiceCollection() - .AddLogging(builder => builder.AddConsole()) - .AddSingleton() - .BuildServiceProvider(); - -await using var ipcServer = new IpcServer() +async Task Entry(CancellationToken ct) { - ServiceProvider = serviceProvider, - Scheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler, - Endpoints = new() + if (args is not [var base64]) + { + Console.Error.WriteLine($"Usage: dotnet {Path.GetFileName(Assembly.GetEntryAssembly()!.Location)} "); + return 1; + } + var externalServerParams = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(Convert.FromBase64String(base64))); + await using var asyncDisposable = externalServerParams.CreateListenerConfig(out var serverTransport); + + await using var serviceProvider = new ServiceCollection() + .AddLogging(builder => builder.AddConsole()) + .AddSingleton() + .BuildServiceProvider(); + + await using var ipcServer = new IpcServer() + { + ServiceProvider = serviceProvider, + Scheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler, + Endpoints = new() { { typeof(IComputingService) }, }, - Transport = serverTransport, -}; -ipcServer.Start(); -await ipcServer.WaitForStop(); + Transport = serverTransport, + }; + ipcServer.Start(); + await Task.Delay(Timeout.InfiniteTimeSpan, ct); -return 0; \ No newline at end of file + return 0; +} + +static IDisposable ConsoleCancellation(out CancellationToken ct) +{ + var cts = new CancellationTokenSource(); + ct = cts.Token; + Console.CancelKeyPress += (sender, e) => + { + e.Cancel = true; + cts.Cancel(); + }; + return cts; +} \ No newline at end of file