SharpChannels is a simple and lightweight communication library for .NET.
Two things:
- Gives you the higher level entities to use for messaging instead of the byte streams and sockets.
Namely: channels and messages - Implements well-known messaging scenarios
- request-response
- publisher-subscriber
- service bus
It is very expensive to develop a reliable server and client (using TCP for example), implement an application layer protocol for messaging, and then use all this stuff in a communication scenarios, like response to the requestor, broadcasts message to the subscribers etc.
SharpChannels has already done it all.
Use entities which can send and receive messages rather than write the byte sequence to or read it from stream. It also gives you a seamless switching between transport protocols.
- TCP
- In-process (inside AppDomain)
- TLS 1.2
- built-in for text messages
- built-in for binary messages
- native (using BinaryFormatter class)
- proto-buf
- custom user serialization
- version based handshake
- explicit channel closing (as in SCTP)
Нere is a simple example of request-response messaging using SharpChannels
Server side:
var serializer = new StringMessageSerializer();
var serverCommunication = new TcpCommunication<StringMessage>(
new TcpEndpointData(IPAddress.Any, 2000),
serializer);
var server = Scenarios.RequestResponse.SetupServer(serverCommunication)
.UsingNewClientHandler((sender, a) => { Console.WriteLine("channel opened"); })
.UsingRequestHandler((sender, a) =>
{
a.Response = new StringMessage(a.Request.Message.Replace("request", "response"));
})
.UsingChannelClosedHandler((sender, a) => { Console.WriteLine("channel closed"); })
.Go();
Client side:
var clientCommunication = new TcpCommunication<StringMessage>(
new TcpEndpointData(IPAddress.Loopback, 2000),
serializer);
var r = Scenarios.RequestResponse.Requester(clientCommunication);
using (r.Channel)
{
r.Channel.Open();
for (int i = 0; i < 100; i++)
{
var requestMessage = new StringMessage($"request #{i}");
Console.WriteLine(requestMessage);
var responseMessage = r.Request(requestMessage);
Console.WriteLine(responseMessage);
}
r.Channel.Close();
}
SharpChannels is licensed under The MIT License
See LICENSE for details.