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.");
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="Client/Client.csproj" />
|
||||||
|
<Project Path="Server/Server.csproj" />
|
||||||
|
<Project Path="Shared/Shared.csproj" />
|
||||||
|
</Solution>
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
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);
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using PipeMethodCalls;
|
||||||
|
using Server;
|
||||||
|
using Shared;
|
||||||
|
|
||||||
|
string pipeName = args.FirstOrDefault(a => a.StartsWith(PipeConstants.PipeNameArgPrefix))?
|
||||||
|
[PipeConstants.PipeNameArgPrefix.Length..] ?? "PipeDemo";
|
||||||
|
|
||||||
|
Console.WriteLine($"Server started. Pipe: {pipeName}");
|
||||||
|
|
||||||
|
// Exceptions in handler methods are caught by PipeMethodCalls and sent to the client
|
||||||
|
// (client receives PipeInvokeFailedException with the server exception details).
|
||||||
|
var server = new PipeServer<ICalculator>(
|
||||||
|
new JsonFieldsPipeSerializer(),
|
||||||
|
pipeName,
|
||||||
|
() => new Calculator());
|
||||||
|
|
||||||
|
await server.WaitForConnectionAsync();
|
||||||
|
Console.WriteLine("Client connected.");
|
||||||
|
|
||||||
|
await server.WaitForRemotePipeCloseAsync();
|
||||||
|
Console.WriteLine("Client disconnected. Server exiting.");
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\Shared\Shared.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
using System.Numerics;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public interface ICalculator
|
||||||
|
{
|
||||||
|
int Add(int a, int b);
|
||||||
|
Vector3 AddVector(Vector3 a, Vector3 b);
|
||||||
|
double Divide(double a, double b);
|
||||||
|
Task<string> EchoAsync(string text);
|
||||||
|
void Crash(int exitCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class PipeConstants
|
||||||
|
{
|
||||||
|
public const string PipeNameArgPrefix = "--pipe=";
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using PipeMethodCalls;
|
||||||
|
|
||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// JSON serializer that also includes public fields (required for System.Numerics.Vector3).
|
||||||
|
/// </summary>
|
||||||
|
public class JsonFieldsPipeSerializer : IPipeSerializer
|
||||||
|
{
|
||||||
|
private static readonly JsonSerializerOptions Options = new() { IncludeFields = true };
|
||||||
|
|
||||||
|
public object Deserialize(byte[] data, Type type) => JsonSerializer.Deserialize(data, type, Options)!;
|
||||||
|
|
||||||
|
public byte[] Serialize(object o) => JsonSerializer.SerializeToUtf8Bytes(o, Options);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="PipeMethodCalls" Version="4.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user