Skip to content

A simple, easy to use, strongly-typed, async wrapper around .NET named pipes.

License

Notifications You must be signed in to change notification settings

HavenDV/H.Pipes

Repository files navigation

Async Named Pipe Wrapper for .NET Standard 2.0

Language License Requirements Build Status

A simple, easy to use, strongly-typed, async wrapper around .NET named pipes.

Nuget

NuGet NuGet NuGet NuGet NuGet

Features

  • Create named pipe servers that can handle multiple client connections simultaneously.
  • Send strongly-typed messages between clients and servers: any serializable .NET object can be sent over a pipe and will be automatically serialized/deserialized, including cyclical references and complex object graphs.
  • Async
  • Requires .NET Standard 2.0
  • Supports large messages - up to 300 MiB.
  • Server restart automatically
  • Automatically wait for the release of the pipe for the server, if it is already in use
  • Automatically waiting for a server pipe creating when client connecting
  • Automatic reconnect with a given interval and at each client.WriteAsync, if necessary
  • Supports variable formatters, default - BinaryFormatter which uses System.Runtime.Serialization.BinaryFormatter inside
  • Also available ready formatters in separate nuget packages: H.Formatters.Json and H.Formatters.Wire

Usage

Server:

await using var server = new PipeServer<MyMessage>(pipeName);
server.ClientConnected += async (o, args) =>
{
    Console.WriteLine($"Client {args.Connection.Id} is now connected!");

    await args.Connection.WriteAsync(new MyMessage
    {
        Id = new Random().Next(),
        Text = "Welcome!"
    });
};
server.ClientDisconnected += (o, args) =>
{
    Console.WriteLine($"Client {args.Connection.Id} disconnected");
};
server.MessageReceived += (sender, args) =>
{
    Console.WriteLine($"Client {args.Connection.Id} says: {args.Message}");
};
server.ExceptionOccurred += (o, args) => OnExceptionOccurred(args.Exception);

await server.StartAsync();

await Task.Delay(Timeout.InfiniteTimeSpan);

Client:

await using var client = new PipeClient<MyMessage>(pipeName);
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);

await client.ConnectAsync();

await client.WriteAsync(new MyMessage
{
    Id = new Random().Next(),
    Text = "Hello!",
});

await Task.Delay(Timeout.InfiniteTimeSpan);

Custom Formatters

Install-Package H.Formatters.Json
using H.Formatters;

await using var server = new PipeServer<MyMessage>(pipeName, formatter: new JsonFormatter());
await using var client = new PipeClient<MyMessage>(pipeName, formatter: new JsonFormatter());