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 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 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 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 client) => await client.InvokeAsync(c => c.Crash(42));