initial commit
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFrameworks>net10.0</TargetFrameworks>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="H.Formatters" Version="15.0.0" />
|
||||||
|
<PackageReference Include="H.Formatters.MessagePack" Version="15.0.0" />
|
||||||
|
<PackageReference Include="H.Pipes" Version="15.0.0" />
|
||||||
|
<PackageReference Include="MessagePack" Version="3.1.12" />
|
||||||
|
<PackageReference Include="MessagePackAnalyzer" Version="3.1.12" PrivateAssets="all" />
|
||||||
|
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="10.0.14" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="H.Pipes.Apps.ConsoleApp.MessagePack.csproj" />
|
||||||
|
</Solution>
|
||||||
+72
@@ -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<MyMessage>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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})";
|
||||||
|
}
|
||||||
|
}
|
||||||
+90
@@ -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<MyMessage>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+30
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user