Compare commits
13
Commits
d14a20dfac
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98bdb3cf0d | ||
|
|
b7ddb46ded | ||
|
|
beb7086867 | ||
|
|
e1acf71c5d | ||
|
|
3fe7fa42e5 | ||
|
|
ae723b9bea | ||
|
|
f00e3298b1 | ||
|
|
d618bac679 | ||
|
|
23c2a9ea16 | ||
|
|
8fbb2e2e46 | ||
|
|
04e94edd64 | ||
|
|
06763d6c15 | ||
|
|
d0ef6d2039 |
@@ -0,0 +1,6 @@
|
|||||||
|
################################################################################
|
||||||
|
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
|
||||||
|
################################################################################
|
||||||
|
|
||||||
|
/.vs
|
||||||
|
.idea
|
||||||
@@ -1,115 +0,0 @@
|
|||||||
using System.Diagnostics;
|
|
||||||
using PipeMethodCalls;
|
|
||||||
using Shared;
|
|
||||||
|
|
||||||
Console.WriteLine("Client started.");
|
|
||||||
|
|
||||||
const string pipeName = "PipeDemo";
|
|
||||||
|
|
||||||
Process? serverProcess = null;
|
|
||||||
PipeClient<ICalculator> client = new(new JsonFieldsPipeSerializer(), pipeName);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Check whether a server is already running by trying a short connect
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using var probeCts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
|
|
||||||
await client.ConnectAsync(probeCts.Token);
|
|
||||||
Console.WriteLine("Server already running.");
|
|
||||||
}
|
|
||||||
catch (Exception ex) when (ex is OperationCanceledException or TimeoutException)
|
|
||||||
{
|
|
||||||
client.Dispose();
|
|
||||||
Console.WriteLine("No server running. Starting server...");
|
|
||||||
serverProcess = StartServer(pipeName);
|
|
||||||
serverProcess.EnableRaisingEvents = true;
|
|
||||||
serverProcess.Exited += (_, _) =>
|
|
||||||
{
|
|
||||||
if (serverProcess.ExitCode != 0)
|
|
||||||
Console.WriteLine($"Server process exited abnormally (exit code {serverProcess.ExitCode}).");
|
|
||||||
};
|
|
||||||
|
|
||||||
client = new PipeClient<ICalculator>(new JsonFieldsPipeSerializer(), pipeName);
|
|
||||||
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
|
|
||||||
await client.ConnectAsync(cts.Token);
|
|
||||||
}
|
|
||||||
Console.WriteLine("Connected to server.");
|
|
||||||
|
|
||||||
int sum = await client.InvokeAsync(c => c.Add(2, 3));
|
|
||||||
Console.WriteLine($"Add(2, 3) = {sum}");
|
|
||||||
|
|
||||||
var vector = await client.InvokeAsync(c => c.AddVector(new System.Numerics.Vector3(1, 2, 3), new System.Numerics.Vector3(4, 5, 6)));
|
|
||||||
Console.WriteLine($"AddVector((1,2,3), (4,5,6)) = {vector}");
|
|
||||||
|
|
||||||
double quotient = await client.InvokeAsync(c => c.Divide(10, 4));
|
|
||||||
Console.WriteLine($"Divide(10, 4) = {quotient}");
|
|
||||||
|
|
||||||
string echo = await client.InvokeAsync(c => c.EchoAsync("Hallo"));
|
|
||||||
Console.WriteLine(echo);
|
|
||||||
|
|
||||||
// Server-side exceptions are forwarded to the client
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await client.InvokeAsync(c => c.Divide(1, 0));
|
|
||||||
}
|
|
||||||
catch (PipeInvokeFailedException ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Server exception (Divide): {ex.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
await client.InvokeAsync(c => c.EchoAsync(""));
|
|
||||||
}
|
|
||||||
catch (PipeInvokeFailedException ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"Server exception (Echo): {ex.Message}");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Simulate a server crash
|
|
||||||
await client.InvokeAsync(c => c.Crash(42));
|
|
||||||
}
|
|
||||||
catch (Exception ex) when (ex is not PipeInvokeFailedException)
|
|
||||||
{
|
|
||||||
// Communication failed: check whether the server process died
|
|
||||||
if (serverProcess is not null && serverProcess.WaitForExit(2000))
|
|
||||||
Console.WriteLine($"Connection lost, server terminated with exit code {serverProcess.ExitCode}: {ex.GetType().Name}: {ex.Message}");
|
|
||||||
else
|
|
||||||
Console.WriteLine($"Communication error: {ex.GetType().Name}: {ex.Message}");
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
// Disposing the client closes the pipe, the server then exits.
|
|
||||||
client.Dispose();
|
|
||||||
if (serverProcess is not null)
|
|
||||||
{
|
|
||||||
if (!serverProcess.WaitForExit(5000))
|
|
||||||
serverProcess.Kill();
|
|
||||||
serverProcess.Dispose();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Console.WriteLine("Client finished.");
|
|
||||||
|
|
||||||
// Start server process (Server is referenced, so its output is copied next to the client).
|
|
||||||
static Process StartServer(string pipeName)
|
|
||||||
{
|
|
||||||
string baseDir = AppContext.BaseDirectory;
|
|
||||||
string serverExe = Path.Combine(baseDir, OperatingSystem.IsWindows() ? "Server.exe" : "Server");
|
|
||||||
ProcessStartInfo startInfo;
|
|
||||||
if (File.Exists(serverExe))
|
|
||||||
{
|
|
||||||
startInfo = new ProcessStartInfo(serverExe);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
startInfo = new ProcessStartInfo("dotnet");
|
|
||||||
startInfo.ArgumentList.Add(Path.Combine(baseDir, "Server.dll"));
|
|
||||||
}
|
|
||||||
startInfo.ArgumentList.Add($"{PipeConstants.PipeNameArgPrefix}{pipeName}");
|
|
||||||
startInfo.UseShellExecute = false;
|
|
||||||
|
|
||||||
return Process.Start(startInfo)
|
|
||||||
?? throw new InvalidOperationException("Server konnte nicht gestartet werden.");
|
|
||||||
}
|
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Shared\Shared.csproj" />
|
<ProjectReference Include="..\PipeShared\PipeShared.csproj" />
|
||||||
<ProjectReference Include="..\Server\Server.csproj" />
|
<ProjectReference Include="..\PipeServer\PipeServer.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
@@ -0,0 +1,138 @@
|
|||||||
|
using PipeMethodCalls;
|
||||||
|
using PipeClient;
|
||||||
|
using PipeShared;
|
||||||
|
using System.Xml.Linq;
|
||||||
|
|
||||||
|
// All output is written to an XML report next to the executable
|
||||||
|
var reportPath = Path.Combine(AppContext.BaseDirectory, "ClientReport.xml");
|
||||||
|
var report = new XElement("Report", new XAttribute("Started", DateTime.Now.ToString("O")));
|
||||||
|
var colorSwitch = false;
|
||||||
|
|
||||||
|
Log("Client started.");
|
||||||
|
|
||||||
|
// Unique pipe name per client run, so multiple clients/servers do not interfere
|
||||||
|
var pipeName = $"PipeDemo-{Environment.ProcessId}";
|
||||||
|
|
||||||
|
var server = new ServerConnection(pipeName, Log);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Log($"Starting server on pipe {pipeName}...");
|
||||||
|
await server.StartAsync();
|
||||||
|
Log("Connected to server.");
|
||||||
|
|
||||||
|
await BasicCallsExampleAsync(server.Client);
|
||||||
|
await ServerExceptionsExampleAsync(server.Client);
|
||||||
|
await CancellationExampleAsync(server.Client);
|
||||||
|
await KillOnTimeoutExampleAsync(server);
|
||||||
|
await CrashExampleAsync(server.Client);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (ex is not PipeInvokeFailedException)
|
||||||
|
{
|
||||||
|
// Communication failed: check whether the server process died
|
||||||
|
Log(server.WaitForServerExit(2000, out var exitCode)
|
||||||
|
? $"Connection lost, server terminated with exit code {exitCode}: {ex.GetType().Name}: {ex.Message}"
|
||||||
|
: $"Communication error: {ex.GetType().Name}: {ex.Message}");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
server.Dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
Log("Client finished.");
|
||||||
|
lock (report)
|
||||||
|
new XDocument(report).Save(reportPath);
|
||||||
|
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Thread-safe: also called from the Process.Exited event
|
||||||
|
|
||||||
|
void Log(string message)
|
||||||
|
{
|
||||||
|
lock (report)
|
||||||
|
report.Add(new XElement("Entry", new XAttribute("Time", DateTime.Now.ToString("O")), message));
|
||||||
|
if (message.Contains("Server exception"))
|
||||||
|
{
|
||||||
|
Console.ForegroundColor = colorSwitch ? ConsoleColor.DarkRed : ConsoleColor.Red;
|
||||||
|
colorSwitch = !colorSwitch;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
|
Console.WriteLine(message);
|
||||||
|
Console.ResetColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
async Task BasicCallsExampleAsync(PipeClient<IPipeServerMethods> client)
|
||||||
|
{
|
||||||
|
var sum = await client.InvokeAsync(c => c.Add(2, 3));
|
||||||
|
Log($"Add(2, 3) = {sum}");
|
||||||
|
|
||||||
|
var vector = await client.InvokeAsync(c => c.AddVector(new System.Numerics.Vector3(1, 2, 3), new System.Numerics.Vector3(4, 5, 6)));
|
||||||
|
Log($"AddVector((1,2,3), (4,5,6)) = {vector}");
|
||||||
|
|
||||||
|
var quotient = await client.InvokeAsync(c => c.Divide(10, 4));
|
||||||
|
Log($"Divide(10, 4) = {quotient}");
|
||||||
|
|
||||||
|
var echo = await client.InvokeAsync(c => c.EchoAsync("Hallo"));
|
||||||
|
Log(echo);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server-side exceptions are forwarded to the client
|
||||||
|
async Task ServerExceptionsExampleAsync(PipeClient<IPipeServerMethods> client)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await client.InvokeAsync(c => c.Divide(1, 0));
|
||||||
|
}
|
||||||
|
catch (PipeInvokeFailedException ex)
|
||||||
|
{
|
||||||
|
Log($"Server exception (Divide): {ex.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await client.InvokeAsync(c => c.EchoAsync(""));
|
||||||
|
}
|
||||||
|
catch (PipeInvokeFailedException ex)
|
||||||
|
{
|
||||||
|
Log($"Server exception (Echo): {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// InvokeAsync with a CancellationToken: the client stops waiting when the token is canceled.
|
||||||
|
// PipeMethodCalls does not propagate cancellation to the server, so an explicit
|
||||||
|
// CancelOperation call with the operation id stops the server-side work as well.
|
||||||
|
async Task CancellationExampleAsync(PipeClient<IPipeServerMethods> client)
|
||||||
|
{
|
||||||
|
using var invokeCts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
|
||||||
|
var operationId = Guid.NewGuid();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await client.InvokeAsync(c => c.SlowOperationAsync(2000, operationId), invokeCts.Token);
|
||||||
|
Log($"SlowOperationAsync = {result}");
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
await client.InvokeAsync(c => c.CancelOperation(operationId));
|
||||||
|
Log("SlowOperationAsync canceled after 200 ms.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kill the server process if it does not respond in time, then restart it
|
||||||
|
async Task KillOnTimeoutExampleAsync(ServerConnection connection)
|
||||||
|
{
|
||||||
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await connection.Client.InvokeAsync(c => c.SlowOperationAsync(30_000, Guid.NewGuid()), timeoutCts.Token);
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
Log("Server did not respond within 1 s. Killing server process...");
|
||||||
|
await connection.KillAndRestartAsync();
|
||||||
|
Log("Reconnected to server.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simulate a server crash
|
||||||
|
async Task CrashExampleAsync(PipeClient<IPipeServerMethods> client) => await client.InvokeAsync(c => c.Crash(42));
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
using PipeMethodCalls;
|
||||||
|
using PipeShared;
|
||||||
|
using System.Diagnostics;
|
||||||
|
|
||||||
|
namespace PipeClient;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Manages a pipe client and any server process it starts.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class ServerConnection(string pipeName, Action<string> log) : IDisposable
|
||||||
|
{
|
||||||
|
private Process? _process;
|
||||||
|
private PipeClient<IPipeServerMethods>? _client;
|
||||||
|
|
||||||
|
private string PipeName { get; } = pipeName;
|
||||||
|
|
||||||
|
public PipeClient<IPipeServerMethods> Client =>
|
||||||
|
_client ?? throw new InvalidOperationException("Not connected to server.");
|
||||||
|
|
||||||
|
public async Task StartAsync()
|
||||||
|
{
|
||||||
|
if (_client?.State == PipeState.Connected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var process = StartServerProcess(PipeName);
|
||||||
|
process.EnableRaisingEvents = true;
|
||||||
|
process.Exited += (_, _) =>
|
||||||
|
{
|
||||||
|
if (process.ExitCode != 0)
|
||||||
|
log($"Server process exited abnormally (exit code {process.ExitCode}).");
|
||||||
|
};
|
||||||
|
_process = process;
|
||||||
|
|
||||||
|
_client = new PipeClient<IPipeServerMethods>(new JsonFieldsPipeSerializer(), PipeName);
|
||||||
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
|
||||||
|
await _client.ConnectAsync(cts.Token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Kills the owned server process, if any, and starts a new one.
|
||||||
|
/// </summary>
|
||||||
|
public async Task KillAndRestartAsync()
|
||||||
|
{
|
||||||
|
if (_process is not null)
|
||||||
|
{
|
||||||
|
_process.Kill(entireProcessTree: true);
|
||||||
|
await _process.WaitForExitAsync();
|
||||||
|
_process.Dispose();
|
||||||
|
_process = null;
|
||||||
|
}
|
||||||
|
_client?.Dispose();
|
||||||
|
_client = null;
|
||||||
|
|
||||||
|
await StartAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Waits up to <paramref name="milliseconds"/> for the server process to exit.
|
||||||
|
/// </summary>
|
||||||
|
public bool WaitForServerExit(int milliseconds, out int exitCode)
|
||||||
|
{
|
||||||
|
exitCode = 0;
|
||||||
|
if (_process is null || !_process.WaitForExit(milliseconds))
|
||||||
|
return false;
|
||||||
|
exitCode = _process.ExitCode;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
// Disposing the client closes the pipe, the server then exits.
|
||||||
|
_client?.Dispose();
|
||||||
|
_client = null;
|
||||||
|
if (_process is null)
|
||||||
|
return;
|
||||||
|
if (!_process.WaitForExit(5000))
|
||||||
|
_process.Kill();
|
||||||
|
_process.Dispose();
|
||||||
|
_process = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start server process (Server is referenced, so its output is copied next to the client).
|
||||||
|
private static Process StartServerProcess(string pipeName)
|
||||||
|
{
|
||||||
|
var baseDir = AppContext.BaseDirectory;
|
||||||
|
var serverExe = Path.Combine(baseDir, OperatingSystem.IsWindows() ? "PipeServer.exe" : "PipeServer");
|
||||||
|
ProcessStartInfo startInfo;
|
||||||
|
if (File.Exists(serverExe))
|
||||||
|
{
|
||||||
|
startInfo = new ProcessStartInfo(serverExe);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
startInfo = new ProcessStartInfo("dotnet");
|
||||||
|
startInfo.ArgumentList.Add(Path.Combine(baseDir, "PipeServer.dll"));
|
||||||
|
}
|
||||||
|
startInfo.ArgumentList.Add($"{PipeConstants.PipeNameArgPrefix}{pipeName}");
|
||||||
|
startInfo.UseShellExecute = false;
|
||||||
|
|
||||||
|
return Process.Start(startInfo)
|
||||||
|
?? throw new InvalidOperationException("Server could not be started.");
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-3
@@ -1,5 +1,5 @@
|
|||||||
<Solution>
|
<Solution>
|
||||||
<Project Path="Client/Client.csproj" />
|
<Project Path="PipeClient/PipeClient.csproj" />
|
||||||
<Project Path="Server/Server.csproj" />
|
<Project Path="PipeServer/PipeServer.csproj" />
|
||||||
<Project Path="Shared/Shared.csproj" />
|
<Project Path="PipeShared/PipeShared.csproj" />
|
||||||
</Solution>
|
</Solution>
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\Shared\Shared.csproj" />
|
<ProjectReference Include="..\PipeShared\PipeShared.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
|
using PipeShared;
|
||||||
using PipeMethodCalls;
|
using PipeMethodCalls;
|
||||||
using Server;
|
using PipeServer;
|
||||||
using Shared;
|
|
||||||
|
|
||||||
string pipeName = args.FirstOrDefault(a => a.StartsWith(PipeConstants.PipeNameArgPrefix))?
|
var pipeName = args.FirstOrDefault(static a => a.StartsWith(PipeConstants.PipeNameArgPrefix, StringComparison.Ordinal))?
|
||||||
[PipeConstants.PipeNameArgPrefix.Length..] ?? "PipeDemo";
|
[PipeConstants.PipeNameArgPrefix.Length..] ?? "PipeDemo";
|
||||||
|
|
||||||
Console.WriteLine($"Server started. Pipe: {pipeName}");
|
Console.WriteLine($"Server started. Pipe: {pipeName}");
|
||||||
|
|
||||||
// Exceptions in handler methods are caught by PipeMethodCalls and sent to the client
|
// Exceptions in handler methods are caught by PipeMethodCalls and sent to the client
|
||||||
// (client receives PipeInvokeFailedException with the server exception details).
|
// (client receives PipeInvokeFailedException with the server exception details).
|
||||||
var server = new PipeServer<ICalculator>(
|
var server = new PipeServer<IPipeServerMethods>(
|
||||||
new JsonFieldsPipeSerializer(),
|
new JsonFieldsPipeSerializer(),
|
||||||
pipeName,
|
pipeName,
|
||||||
() => new Calculator());
|
() => new ServerMethods());
|
||||||
|
|
||||||
await server.WaitForConnectionAsync();
|
await server.WaitForConnectionAsync();
|
||||||
Console.WriteLine("Client connected.");
|
Console.WriteLine("Client connected.");
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
|
using System.Numerics;
|
||||||
|
using PipeShared;
|
||||||
|
|
||||||
|
namespace PipeServer;
|
||||||
|
|
||||||
|
public class ServerMethods : IPipeServerMethods
|
||||||
|
{
|
||||||
|
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)
|
||||||
|
throw new DivideByZeroException("Division by 0 not allowed.");
|
||||||
|
return a / b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<string> EchoAsync(string text)
|
||||||
|
{
|
||||||
|
await Task.Delay(50);
|
||||||
|
if (string.IsNullOrEmpty(text))
|
||||||
|
throw new ArgumentException("Text must not be empty.", nameof(text));
|
||||||
|
return $"Echo: {text}";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Crash(int exitCode) => Environment.Exit(exitCode);
|
||||||
|
}
|
||||||
@@ -1,13 +1,15 @@
|
|||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
|
|
||||||
namespace Shared;
|
namespace PipeShared;
|
||||||
|
|
||||||
public interface ICalculator
|
public interface IPipeServerMethods
|
||||||
{
|
{
|
||||||
int Add(int a, int b);
|
int Add(int a, int b);
|
||||||
Vector3 AddVector(Vector3 a, Vector3 b);
|
Vector3 AddVector(Vector3 a, Vector3 b);
|
||||||
double Divide(double a, double b);
|
double Divide(double a, double b);
|
||||||
Task<string> EchoAsync(string text);
|
Task<string> EchoAsync(string text);
|
||||||
|
Task<int> SlowOperationAsync(int delayMs, Guid operationId);
|
||||||
|
void CancelOperation(Guid operationId);
|
||||||
void Crash(int exitCode);
|
void Crash(int exitCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
using System.Text.Json;
|
|
||||||
using PipeMethodCalls;
|
using PipeMethodCalls;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
namespace Shared;
|
namespace PipeShared;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// JSON serializer that also includes public fields (required for System.Numerics.Vector3).
|
/// JSON serializer that also includes public fields (required for System.Numerics.Vector3).
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
using System.Numerics;
|
|
||||||
using Shared;
|
|
||||||
|
|
||||||
namespace Server;
|
|
||||||
|
|
||||||
public class Calculator : ICalculator
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
throw new DivideByZeroException("Division durch 0 ist nicht erlaubt.");
|
|
||||||
return a / b;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<string> EchoAsync(string text)
|
|
||||||
{
|
|
||||||
await Task.Delay(50);
|
|
||||||
if (string.IsNullOrEmpty(text))
|
|
||||||
throw new ArgumentException("Text darf nicht leer sein.", nameof(text));
|
|
||||||
return $"Echo: {text}";
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Crash(int exitCode) => Environment.Exit(exitCode);
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user