Rename projects
This commit is contained in:
@@ -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,21 @@
|
||||
using ClientServerShared;
|
||||
using PipeMethodCalls;
|
||||
using Server;
|
||||
|
||||
string pipeName = args.FirstOrDefault(static a => a.StartsWith(PipeConstants.PipeNameArgPrefix, StringComparison.Ordinal))?
|
||||
[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<IServerMethods>(
|
||||
new JsonFieldsPipeSerializer(),
|
||||
pipeName,
|
||||
() => new ServerMethods());
|
||||
|
||||
await server.WaitForConnectionAsync();
|
||||
Console.WriteLine("Client connected.");
|
||||
|
||||
await server.WaitForRemotePipeCloseAsync();
|
||||
Console.WriteLine("Client disconnected. Server exiting.");
|
||||
@@ -0,0 +1,59 @@
|
||||
using ClientServerShared;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Numerics;
|
||||
|
||||
namespace Server;
|
||||
|
||||
public class ServerMethods : IServerMethods
|
||||
{
|
||||
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}";
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
Reference in New Issue
Block a user