2026-11-10 13:07:21 +01:00
|
|
|
using System.Collections.Concurrent;
|
2026-11-10 09:23:35 +01:00
|
|
|
using System.Numerics;
|
2026-11-10 13:07:21 +01:00
|
|
|
using PipeShared;
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 13:07:21 +01:00
|
|
|
namespace PipeServer;
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 11:08:33 +01:00
|
|
|
public class ServerMethods : IPipeServerMethods
|
2026-11-10 09:23:35 +01:00
|
|
|
{
|
|
|
|
|
public int Add(int a, int b) => a + b;
|
|
|
|
|
|
|
|
|
|
public Vector3 AddVector(Vector3 a, Vector3 b) => a + b;
|
|
|
|
|
|
|
|
|
|
public double Divide(double a, double b)
|
|
|
|
|
{
|
|
|
|
|
if (b == 0)
|
2026-11-10 13:07:21 +01:00
|
|
|
throw new DivideByZeroException("Division by 0 not allowed.");
|
2026-11-10 09:23:35 +01:00
|
|
|
return a / b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string> EchoAsync(string text)
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(50);
|
|
|
|
|
if (string.IsNullOrEmpty(text))
|
2026-11-10 13:07:21 +01:00
|
|
|
throw new ArgumentException("Text must not be empty.", nameof(text));
|
2026-11-10 09:23:35 +01:00
|
|
|
return $"Echo: {text}";
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 10:16:24 +01:00
|
|
|
// Shared across instances: the handler factory may create a new Calculator per call.
|
|
|
|
|
private static readonly ConcurrentDictionary<Guid, CancellationTokenSource> RunningOperations = new();
|
|
|
|
|
|
|
|
|
|
public async Task<int> SlowOperationAsync(int delayMs, Guid operationId)
|
|
|
|
|
{
|
|
|
|
|
using var cts = new CancellationTokenSource();
|
|
|
|
|
if (!RunningOperations.TryAdd(operationId, cts))
|
|
|
|
|
throw new InvalidOperationException($"Operation {operationId} is already running.");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await Task.Delay(delayMs, cts.Token);
|
|
|
|
|
return delayMs;
|
|
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"SlowOperationAsync {operationId} canceled by client.");
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
RunningOperations.TryRemove(operationId, out _);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CancelOperation(Guid operationId)
|
|
|
|
|
{
|
|
|
|
|
if (RunningOperations.TryGetValue(operationId, out var cts))
|
|
|
|
|
cts.Cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 09:23:35 +01:00
|
|
|
public void Crash(int exitCode) => Environment.Exit(exitCode);
|
|
|
|
|
}
|