2026-11-10 11:06:10 +01:00
|
|
|
using PipeMethodCalls;
|
2026-11-10 13:07:21 +01:00
|
|
|
using PipeClient;
|
2026-11-10 11:06:10 +01:00
|
|
|
using PipeShared;
|
2026-11-10 10:38:20 +01:00
|
|
|
using System.Xml.Linq;
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 10:20:18 +01:00
|
|
|
// All output is written to an XML report next to the executable
|
2026-11-10 13:07:21 +01:00
|
|
|
var reportPath = Path.Combine(AppContext.BaseDirectory, "ClientReport.xml");
|
2026-11-10 10:20:18 +01:00
|
|
|
var report = new XElement("Report", new XAttribute("Started", DateTime.Now.ToString("O")));
|
2026-11-10 13:07:21 +01:00
|
|
|
var colorSwitch = false;
|
2026-11-10 10:20:18 +01:00
|
|
|
|
|
|
|
|
Log("Client started.");
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 10:16:24 +01:00
|
|
|
// Unique pipe name per client run, so multiple clients/servers do not interfere
|
2026-11-10 13:07:21 +01:00
|
|
|
var pipeName = $"PipeDemo-{Environment.ProcessId}";
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 13:07:21 +01:00
|
|
|
var server = new ServerConnection(pipeName, Log);
|
2026-11-10 09:23:35 +01:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 10:20:18 +01:00
|
|
|
Log($"Starting server on pipe {pipeName}...");
|
2026-11-10 13:07:21 +01:00
|
|
|
await server.StartAsync();
|
2026-11-10 10:20:18 +01:00
|
|
|
Log("Connected to server.");
|
2026-11-10 10:16:24 +01:00
|
|
|
|
2026-11-10 13:07:21 +01:00
|
|
|
await BasicCallsExampleAsync(server.Client);
|
|
|
|
|
await ServerExceptionsExampleAsync(server.Client);
|
|
|
|
|
await CancellationExampleAsync(server.Client);
|
|
|
|
|
await KillOnTimeoutExampleAsync(server);
|
|
|
|
|
await CrashExampleAsync(server.Client);
|
2026-11-10 10:16:24 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception ex) when (ex is not PipeInvokeFailedException)
|
|
|
|
|
{
|
|
|
|
|
// Communication failed: check whether the server process died
|
2026-11-10 13:07:21 +01:00
|
|
|
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}");
|
2026-11-10 10:16:24 +01:00
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2026-11-10 13:07:21 +01:00
|
|
|
server.Dispose();
|
2026-11-10 10:16:24 +01:00
|
|
|
}
|
|
|
|
|
|
2026-11-10 10:20:18 +01:00
|
|
|
Log("Client finished.");
|
|
|
|
|
lock (report)
|
|
|
|
|
new XDocument(report).Save(reportPath);
|
|
|
|
|
|
2026-11-10 13:07:21 +01:00
|
|
|
return;
|
|
|
|
|
|
2026-11-10 10:20:18 +01:00
|
|
|
// Thread-safe: also called from the Process.Exited event
|
2026-11-10 11:06:10 +01:00
|
|
|
|
2026-11-10 10:20:18 +01:00
|
|
|
void Log(string message)
|
|
|
|
|
{
|
|
|
|
|
lock (report)
|
|
|
|
|
report.Add(new XElement("Entry", new XAttribute("Time", DateTime.Now.ToString("O")), message));
|
2026-11-10 11:06:10 +01:00
|
|
|
if (message.Contains("Server exception"))
|
|
|
|
|
{
|
|
|
|
|
Console.ForegroundColor = colorSwitch ? ConsoleColor.DarkRed : ConsoleColor.Red;
|
|
|
|
|
colorSwitch = !colorSwitch;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
2026-11-10 10:38:20 +01:00
|
|
|
Console.WriteLine(message);
|
|
|
|
|
Console.ResetColor();
|
2026-11-10 10:20:18 +01:00
|
|
|
}
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 13:07:21 +01:00
|
|
|
async Task BasicCallsExampleAsync(PipeClient<IPipeServerMethods> client)
|
2026-11-10 10:16:24 +01:00
|
|
|
{
|
2026-11-10 13:07:21 +01:00
|
|
|
var sum = await client.InvokeAsync(c => c.Add(2, 3));
|
2026-11-10 10:20:18 +01:00
|
|
|
Log($"Add(2, 3) = {sum}");
|
2026-11-10 09:23:35 +01:00
|
|
|
|
|
|
|
|
var vector = await client.InvokeAsync(c => c.AddVector(new System.Numerics.Vector3(1, 2, 3), new System.Numerics.Vector3(4, 5, 6)));
|
2026-11-10 10:20:18 +01:00
|
|
|
Log($"AddVector((1,2,3), (4,5,6)) = {vector}");
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 13:07:21 +01:00
|
|
|
var quotient = await client.InvokeAsync(c => c.Divide(10, 4));
|
2026-11-10 10:20:18 +01:00
|
|
|
Log($"Divide(10, 4) = {quotient}");
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 13:07:21 +01:00
|
|
|
var echo = await client.InvokeAsync(c => c.EchoAsync("Hallo"));
|
2026-11-10 10:20:18 +01:00
|
|
|
Log(echo);
|
2026-11-10 10:16:24 +01:00
|
|
|
}
|
2026-11-10 09:23:35 +01:00
|
|
|
|
2026-11-10 10:16:24 +01:00
|
|
|
// Server-side exceptions are forwarded to the client
|
2026-11-10 13:07:21 +01:00
|
|
|
async Task ServerExceptionsExampleAsync(PipeClient<IPipeServerMethods> client)
|
2026-11-10 10:16:24 +01:00
|
|
|
{
|
2026-11-10 09:23:35 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await client.InvokeAsync(c => c.Divide(1, 0));
|
|
|
|
|
}
|
|
|
|
|
catch (PipeInvokeFailedException ex)
|
|
|
|
|
{
|
2026-11-10 10:20:18 +01:00
|
|
|
Log($"Server exception (Divide): {ex.Message}");
|
2026-11-10 09:23:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await client.InvokeAsync(c => c.EchoAsync(""));
|
|
|
|
|
}
|
|
|
|
|
catch (PipeInvokeFailedException ex)
|
|
|
|
|
{
|
2026-11-10 10:20:18 +01:00
|
|
|
Log($"Server exception (Echo): {ex.Message}");
|
2026-11-10 09:23:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
2026-11-10 10:16:24 +01:00
|
|
|
|
|
|
|
|
// 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.
|
2026-11-10 13:07:21 +01:00
|
|
|
async Task CancellationExampleAsync(PipeClient<IPipeServerMethods> client)
|
2026-11-10 09:23:35 +01:00
|
|
|
{
|
2026-11-10 10:16:24 +01:00
|
|
|
using var invokeCts = new CancellationTokenSource(TimeSpan.FromMilliseconds(200));
|
|
|
|
|
var operationId = Guid.NewGuid();
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-11-10 13:07:21 +01:00
|
|
|
var result = await client.InvokeAsync(c => c.SlowOperationAsync(2000, operationId), invokeCts.Token);
|
2026-11-10 10:20:18 +01:00
|
|
|
Log($"SlowOperationAsync = {result}");
|
2026-11-10 10:16:24 +01:00
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
await client.InvokeAsync(c => c.CancelOperation(operationId));
|
2026-11-10 10:20:18 +01:00
|
|
|
Log("SlowOperationAsync canceled after 200 ms.");
|
2026-11-10 10:16:24 +01:00
|
|
|
}
|
2026-11-10 09:23:35 +01:00
|
|
|
}
|
2026-11-10 10:16:24 +01:00
|
|
|
|
|
|
|
|
// Kill the server process if it does not respond in time, then restart it
|
2026-11-10 13:07:21 +01:00
|
|
|
async Task KillOnTimeoutExampleAsync(ServerConnection connection)
|
2026-11-10 09:23:35 +01:00
|
|
|
{
|
2026-11-10 10:16:24 +01:00
|
|
|
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
|
|
|
|
|
try
|
2026-11-10 09:23:35 +01:00
|
|
|
{
|
2026-11-10 13:07:21 +01:00
|
|
|
await connection.Client.InvokeAsync(c => c.SlowOperationAsync(30_000, Guid.NewGuid()), timeoutCts.Token);
|
2026-11-10 10:16:24 +01:00
|
|
|
}
|
2026-11-10 13:07:21 +01:00
|
|
|
catch (OperationCanceledException)
|
2026-11-10 10:16:24 +01:00
|
|
|
{
|
2026-11-10 10:20:18 +01:00
|
|
|
Log("Server did not respond within 1 s. Killing server process...");
|
2026-11-10 13:07:21 +01:00
|
|
|
await connection.KillAndRestartAsync();
|
2026-11-10 10:20:18 +01:00
|
|
|
Log("Reconnected to server.");
|
2026-11-10 09:23:35 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-10 10:16:24 +01:00
|
|
|
// Simulate a server crash
|
2026-11-10 13:07:21 +01:00
|
|
|
async Task CrashExampleAsync(PipeClient<IPipeServerMethods> client) => await client.InvokeAsync(c => c.Crash(42));
|