• Stars
    star
    169
  • Rank 224,453 (Top 5 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

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.Newtonsoft.Json, H.Formatters.System.Text.Json and H.Formatters.Ceras
  • Supports PipeAccessRule's(see H.Pipes.AccessControl nuget package) or more complex code to access using the PipeServer.PipeStreamInitializeAction property

Nuget

NuGet NuGet NuGet NuGet NuGet

// All clients and servers that do not need support AccessControl.
Install-Package H.Pipes

// Servers that need support AccessControl.
Install-Package H.Pipes.AccessControl

// If you want to transfer any data that can be serialized/deserialized in json using Newtonsoft.Json.
Install-Package H.Formatters.Newtonsoft.Json

// If you want to transfer any data that can be serialized/deserialized in json using System.Text.Json.
Install-Package H.Formatters.System.Text.Json

// If you want to transfer any data that can be serialized/deserialized in binary using Ceras.
Install-Package H.Formatters.Ceras

Usage

Server:

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

    await args.Connection.WriteAsync(new MyMessage
    {
        Text = "Welcome!"
    });
};
server.ClientDisconnected += (o, args) =>
{
    Console.WriteLine($"Client {args.Connection.PipeName} disconnected");
};
server.MessageReceived += (sender, args) =>
{
    Console.WriteLine($"Client {args.Connection.PipeName} 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
{
    Text = "Hello!",
});

await Task.Delay(Timeout.InfiniteTimeSpan);

Notes:

  • To use the server inside the WinForms/WPF/Other UI application, use Task.Run() or any alternative.
  • Be careful and call Dispose before closing the program/after the end of use. Pipes are system resources and you might have problems restarting the server if you don't properly clean up the resources.

Custom Formatters

Since BinaryFormatter is used by default, you should check out this article: https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide

Install-Package H.Formatters.Newtonsoft.Json
Install-Package H.Formatters.System.Text.Json
Install-Package H.Formatters.Ceras
using H.Formatters;

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

Access Control

⚠️ Note: this is only available for the Windows platform

Install-Package H.Pipes.AccessControl
using System.IO.Pipes;
using H.Pipes.AccessControl;

await using var server = new PipeServer<string>(pipeName);

// You can set PipeSecurity
var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));

server.SetPipeSecurity(pipeSecurity);

// or just add AccessRule's (Please be careful, the server will only consider AccessRules from the last call AddAccessRules())
server.AddAccessRules(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.BuiltinUsersSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));

// or just
server.AllowUsersReadWrite();

Encryption

⚠️ Note: this is only available for the Windows platform

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

await using var server = new PipeServer<MyMessage>(pipeName, formatter: new SystemTextJsonFormatter());
server.EnableEncryption();

await using var client = new PipeClient<MyMessage>(pipeName, formatter: new SystemTextJsonFormatter());
client.EnableEncryption();

await client.ConnectAsync(source.Token).ConfigureAwait(false);
// Waits for key exchange.
await client.Connection!.WaitExchangeAsync();

server.ClientConnected += async (_, args) =>
{
    // Waits for key exchange.
    await args.Connection.WaitExchangeAsync();

    await args.Connection.WriteAsync(new MyMessage
    {
        Text = "Welcome!"
    }, source.Token).ConfigureAwait(false);
};

GetImpersonationUserName

server.ClientConnected += async (o, args) =>
{
    var name = args.Connection.GetImpersonationUserName();

    Console.WriteLine($"Client {name} is now connected!");
};

Inter-process communication

I recommend that you take a look at my other library if you plan on doing IPC. This is a SourceGenerator that will generate client and server code based on the presented interface.

Contacts

More Repositories

1

H.NotifyIcon

TrayIcon for WPF/WinUI/Uno.
C#
332
star
2

H.Socket.IO

This is the Socket.IO client for .NET, which is based on ClientWebSocket, provide a simple way to connect to the Socket.IO server. The target framework is .NET Standard 2.0
C#
83
star
3

DependencyPropertyGenerator

Dependency property, routed event and weak event source generator for WPF/UWP/WinUI/Uno/Avalonia/MAUI platforms.
C#
62
star
4

H.InputSimulator

Allows you to simulate global mouse and keyboard events.
C#
45
star
5

H.OxyPlot

UWP/WinUI/Uno support for OxyPlot
C#
20
star
6

H.Ipc

C# Source Generator library for Inter-Process Communication
C#
16
star
7

H.Hooks

Contains LowLevelKeyboardHook and LowLevelMouseHook.
C#
16
star
8

H.NSwag.Generator

C# Source Generator for NSwag.
C#
14
star
9

Mvvm.CommonInteractions

Common Interactions(like open/save file) for WPF/UWP/WinUI/Uno/Avalonia/MAUI platforms
C#
12
star
10

QtPackage

This addon fully implement Qt VS Addin
HTML
11
star
11

H.XamlExtensions

Shortest way to create rows/columns for Grid for WPF/UWP/WinUI/Uno platforms
C#
9
star
12

H.DynamicColumns

Adds support for dynamic columns to DataGrid for WPF/UWP/Uno platforms.
C#
8
star
13

OpenAIGenerator

Source generator, which allows you to add prompts from which code will be generated in deterministic mode
C#
6
star
14

NamedPipeServerStream.NetFrameworkVersion

.NET Standard version of NamedPipeServerStream that contains PipeSecurity constructor.
C#
5
star
15

H.ProxyFactory

Allows creating proxy objects that look exactly like the original objects.
C#
4
star
16

UpworkPdfGenerator

Allows you to programmatically fill out pdf forms for Upwork.
C#
4
star
17

H.Vpn

C# high-level VPN library
C#
4
star
18

EventGenerator

Generates events, OnEvent() methods and EventArgs classes
C#
3
star
19

clucene

Clucene VS 2015 fix for this job - https://www.upwork.com/jobs/~01ba8f543004940006
C++
3
star
20

WixSharp.DotNetBootstrapper

Allows you to create a bootstrapper for an MSI file in one line, simply by specifying the required .Net version
C#
3
star
21

H.Generators.Extensions

A set of extensions to simplify the code of generators
C#
3
star
22

FlaUI.Fluent

Fluent interface for FlaUI find queries
C#
2
star
23

workflows

Reusable workflows
2
star
24

SkipLocalsInit

Enables SkipLocalsInit by simply specifying the dev NuGet package
C#
2
star
25

OpenCVManager

OpenCV libraries manager
C#
2
star
26

KakaoTalkBot

C#
2
star
27

MoexIIS

C# client library and OpenAPI spec for Moex IIS
C#
2
star
28

ViewBaseGenerator

Generates boiler-plate code for constructors or ViewBase classes for WPF/UWP/WinUI/Uno projects.
C#
2
star
29

WebScraperForSpaFinder

For this job: https://www.upwork.com/jobs/~01ea04d8c223682adb
C#
1
star
30

WebScraperForAlphaBroder

C#
1
star
31

ListBoxApplication

For this job - https://www.upwork.com/jobs/_~016b3600007a4ccc17/
C#
1
star
32

Launcher

For this job - https://www.upwork.com/jobs/~016935a9668d8351ae
C#
1
star
33

FullContactDownloader

For this job - https://www.upwork.com/jobs/~01e2eb66aabf866083
C#
1
star
34

AutoSegment

C#
1
star
35

H.Controls

Common WPF, UWP and Uno controls
C#
1
star
36

Uno.Issues

Source control for various problems in Uno
C#
1
star
37

CSharpProjectTemplate

The template repository for C# projects
C#
1
star
38

ExpressionsRecognitor

C#
1
star
39

McMasterDownloader

C#
1
star
40

FileFinder

File Finder for https://www.upwork.com/jobs/~01fb3187190c8b598a
C#
1
star
41

NuGet.NativeLibraries

NuGet packages containing native DLLs.
C#
1
star
42

ClearComApi

Generated API for ClearCom and tests
C#
1
star
43

AcidBaseDiagram

For this job - https://www.upwork.com/jobs/~016f93cdfb5a9b35f3
C#
1
star
44

fluorinefx

fluorinefx with latest .Net support
C#
1
star
45

PngOutliner

C#
1
star
46

DataProtection

C#
1
star
47

veryfi-csharp

This is the official C# client library for communicating with the Veryfi OCR API.
C#
1
star
48

MakeRuler

C#
1
star
49

DanteWrapper

C
1
star
50

MotionDetector

Finds motion on a video and saves it as images
C#
1
star
51

UpworkNotifier

C#
1
star
52

H.Containers

Contains many projects that are aimed at dynamically executing code (including running code in another process)
C#
1
star
53

HavenDV

1
star
54

ImageSourceLoader

Allows you to efficiently get byte[]/Stream from ImageSource for different platforms (WPF/WinUI/Uno/MAUI)
1
star