Skip to content

Latest commit

 

History

History
104 lines (68 loc) · 3.72 KB

README.md

File metadata and controls

104 lines (68 loc) · 3.72 KB

ricaun.NamedPipeWrapper.Json

Visual Studio 2022 Nuke License MIT Build nuget

Named Pipe Wrapper Json for .NET Framework 4.0 and .NET Core 6.0.

This project is based of Andrew C. Dvorak's work at Named Pipe Wrapper, updated to support NET Core and Json.

PackageReference

<PackageReference Include="ricaun.NamedPipeWrapper.Json" Version="*" />

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.
  • Messages are sent and received asynchronously on a separate background thread and marshalled back to the calling thread (typically the UI).
  • Supports large messages - up to 300 MiB.
  • Design to work inside Autodesk Revit without conflict.

Json

By default, all classes gonna be serialized/deserialized by JsonExtension.JsonService with the interface IJsonService, in the end, a JSON string is passed using the PipeStream.

If Newtonsoft.Json exists in the project, the NewtonsoftJsonService will be used instead of the default JsonService.

The default JsonService for NETFRAMWORK use System.Web.Extensions, and for NETCOREAPP the System.Text.Json.

Usage

Server:

var server = new NamedPipeServer<SomeClass>("MyServerPipe");

server.ClientConnected += delegate(NamedPipeConnection<SomeClass> conn)
    {
        Console.WriteLine("Client {0} is now connected!", conn.Id);
        conn.PushMessage(new SomeClass { Text: "Welcome!" });
    };

server.ClientMessage += delegate(NamedPipeConnection<SomeClass> conn, SomeClass message)
    {
        Console.WriteLine("Client {0} says: {1}", conn.Id, message.Text);
    };

// Start up the server asynchronously and begin listening for connections.
// This method will return immediately while the server runs in a separate background thread.
server.Start();

// ...

Client:

var client = new NamedPipeClient<SomeClass>("MyServerPipe");

client.ServerMessage += delegate(NamedPipeConnection<SomeClass> conn, SomeClass message)
    {
        Console.WriteLine("Server says: {0}", message.Text);
    };

// Start up the client asynchronously and connect to the specified server pipe.
// This method will return immediately while the client runs in a separate background thread.
client.Start();

// ...

NamedPipeUtils

The NamedPipeUtils class provides some utility methods for working with named pipes.

bool pipeExists = NamedPipeUtils.PipeFileExists("MyServerPipe");

IJsonService

Override the default JsonService with the interface IJsonService:

JsonExtension.JsonService = new MyJsonService();

Release

License

This project is licensed under the MIT License.


Do you like this project? Please star this project on GitHub!