From 9d0cfd3c8e73a1ab42894380219aa993ff540343 Mon Sep 17 00:00:00 2001 From: Matthias Heil Date: Mon, 2 Nov 2026 13:14:37 +0100 Subject: [PATCH] initial commit --- H.Pipes.Apps.ConsoleApp.MessagePack.csproj | 19 +++++ H.Pipes.Apps.ConsoleApp.MessagePack.slnx | 3 + MyClient.cs | 72 +++++++++++++++++ MyMessage.cs | 20 +++++ MyServer.cs | 90 ++++++++++++++++++++++ Program.cs | 30 ++++++++ 6 files changed, 234 insertions(+) create mode 100644 H.Pipes.Apps.ConsoleApp.MessagePack.csproj create mode 100644 H.Pipes.Apps.ConsoleApp.MessagePack.slnx create mode 100644 MyClient.cs create mode 100644 MyMessage.cs create mode 100644 MyServer.cs create mode 100644 Program.cs diff --git a/H.Pipes.Apps.ConsoleApp.MessagePack.csproj b/H.Pipes.Apps.ConsoleApp.MessagePack.csproj new file mode 100644 index 0000000..e6b704b --- /dev/null +++ b/H.Pipes.Apps.ConsoleApp.MessagePack.csproj @@ -0,0 +1,19 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + + + + + + diff --git a/H.Pipes.Apps.ConsoleApp.MessagePack.slnx b/H.Pipes.Apps.ConsoleApp.MessagePack.slnx new file mode 100644 index 0000000..a79d5e6 --- /dev/null +++ b/H.Pipes.Apps.ConsoleApp.MessagePack.slnx @@ -0,0 +1,3 @@ + + + diff --git a/MyClient.cs b/MyClient.cs new file mode 100644 index 0000000..2624ad4 --- /dev/null +++ b/MyClient.cs @@ -0,0 +1,72 @@ +// ReSharper disable AccessToDisposedClosure + +using H.Formatters; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace H.Pipes.Apps.ConsoleApp.MessagePack; + +internal static class MyClient +{ + private static void OnExceptionOccurred(Exception exception) + { + Console.Error.WriteLine($"Exception: {exception}"); + } + + public static async Task RunAsync(string pipeName) + { + try + { + using var source = new CancellationTokenSource(); + + Console.WriteLine($"Running in CLIENT mode. PipeName: {pipeName}"); + Console.WriteLine("Enter 'q' to exit"); + + await using var client = new PipeClient(pipeName, formatter: new MessagePackFormatter()); + client.MessageReceived += (o, args) => Console.WriteLine("MessageReceived: " + args.Message); + client.Disconnected += (o, args) => Console.WriteLine("Disconnected from server"); + client.Connected += (o, args) => Console.WriteLine("Connected to server"); + client.ExceptionOccurred += (o, args) => OnExceptionOccurred(args.Exception); + + // Dispose is not required + var _ = Task.Run(async () => + { + while (true) + { + try + { + var message = await Console.In.ReadLineAsync().ConfigureAwait(false); + if (message == "q") + { + source.Cancel(); + break; + } + + await client.WriteAsync(new MyMessage + { + Text = message, + }, source.Token).ConfigureAwait(false); + } + catch (Exception exception) + { + OnExceptionOccurred(exception); + } + } + }, source.Token); + + Console.WriteLine("Client connecting..."); + + await client.ConnectAsync(source.Token).ConfigureAwait(false); + + await Task.Delay(Timeout.InfiniteTimeSpan, source.Token).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + } + catch (Exception exception) + { + OnExceptionOccurred(exception); + } + } +} diff --git a/MyMessage.cs b/MyMessage.cs new file mode 100644 index 0000000..6ee8687 --- /dev/null +++ b/MyMessage.cs @@ -0,0 +1,20 @@ +using MessagePack; +using System; + +namespace H.Pipes.Apps.ConsoleApp.MessagePack; + +[MessagePackObject] +[Serializable] +public class MyMessage +{ + [Key(0)] + public Guid Id { get; set; } = Guid.NewGuid(); + + [Key(1)] + public string? Text { get; set; } + + public override string ToString() + { + return $"\"{Text}\" (message ID = {Id})"; + } +} diff --git a/MyServer.cs b/MyServer.cs new file mode 100644 index 0000000..f9a831c --- /dev/null +++ b/MyServer.cs @@ -0,0 +1,90 @@ +// ReSharper disable AccessToDisposedClosure + +using H.Formatters; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace H.Pipes.Apps.ConsoleApp.MessagePack; + +internal static class MyServer +{ + private static void OnExceptionOccurred(Exception exception) + { + Console.Error.WriteLine($"Exception: {exception}"); + } + + public static async Task RunAsync(string pipeName) + { + try + { + using var source = new CancellationTokenSource(); + + Console.WriteLine($"Running in SERVER mode. PipeName: {pipeName}"); + Console.WriteLine("Enter 'q' to exit"); + + await using var server = new PipeServer(pipeName, formatter: new MessagePackFormatter()); + server.ClientConnected += async (_, args) => + { + Console.WriteLine($"Client {args.Connection.PipeName} is now connected!"); + + try + { + await args.Connection.WriteAsync(new MyMessage + { + Text = "Welcome!" + }, source.Token).ConfigureAwait(false); + } + catch (Exception exception) + { + OnExceptionOccurred(exception); + } + }; + server.ClientDisconnected += (_, args) => Console.WriteLine($"Client {args.Connection.PipeName} disconnected"); + server.MessageReceived += (_, args) => Console.WriteLine($"Client {args.Connection.PipeName} says: {args.Message}"); + server.ExceptionOccurred += (_, args) => OnExceptionOccurred(args.Exception); + + var _ = Task.Run(async () => + { + while (true) + { + try + { + var message = await Console.In.ReadLineAsync().ConfigureAwait(false); + if (message == "q") + { + source.Cancel(); + break; + } + + Console.WriteLine($"Sent to {server.ConnectedClients.Count} clients"); + + await server.WriteAsync(new MyMessage + { + Text = message, + }, source.Token).ConfigureAwait(false); + } + catch (Exception exception) + { + OnExceptionOccurred(exception); + } + } + }, source.Token); + + Console.WriteLine("Server starting..."); + + await server.StartAsync(cancellationToken: source.Token).ConfigureAwait(false); + + Console.WriteLine("Server is started!"); + + await Task.Delay(Timeout.InfiniteTimeSpan, source.Token).ConfigureAwait(false); + } + catch (OperationCanceledException) + { + } + catch (Exception exception) + { + OnExceptionOccurred(exception); + } + } +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..57bda12 --- /dev/null +++ b/Program.cs @@ -0,0 +1,30 @@ +using H.Pipes.Apps.ConsoleApp.MessagePack; +using System; +using System.Linq; + +const string DefaultPipeName = "named_pipe_test_server"; + +string? mode; +string pipeName = DefaultPipeName; +if (args.Any()) +{ + mode = args.ElementAt(0); + pipeName = args.ElementAtOrDefault(1) ?? DefaultPipeName; +} +else +{ + Console.WriteLine("Enter mode('server' or 'client'):"); + mode = await Console.In.ReadLineAsync().ConfigureAwait(false); +} + +switch (mode?.ToUpperInvariant()) +{ + case "SERVER": + await MyServer.RunAsync(pipeName).ConfigureAwait(false); + break; + + default: + await MyClient.RunAsync(pipeName).ConfigureAwait(false); + break; +} +