Initial commit
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Shared\Shared.csproj" />
|
||||
<ProjectReference Include="..\Server\Server.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,115 @@
|
||||
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.");
|
||||
}
|
||||
Reference in New Issue
Block a user