⚡️ A fast TCP event based buffered server/client protocol for transferring data over the (inter)net in .NET 🌐
Send whole objects over the net easier and faster
- Send nearly any .NET
object
(See: supported, custom) 📦 - Send/Receive faster by buffered send/receive and ZeroFormatter's fast (de-)serializing 💨
- Automatically correct errors with TCP and Auto-reconnect ✅
- Async and Event based ⚡
- Efficient Network Discovery for other GenericProtocol Hosts 🔍
- Fast binary links for file/images/.. transfer 💾
- Made with love ❤️
Sending objects:
await client.Send(someObject);
...on the other end:
private void MyMessageReceivedCallback(SomeObject someObject) {
Console.WriteLine("I've received an object!");
}
Add GenericProtocol to your existing .NET/.NET Core 2.0+/.NET Standard 2.0+ Project via NuGet:
PM> Install-Package GenericProtocol
Use the default namespace:
using GenericProtocol;
Are you connecting to a server, or are you the server?
Connect to a server:
IClient client = await Factory.StartNewClient<MessageObject>("82.205.121.132", 1024, true);
The Factory will construct and connect a new IClient<T>
object, where <T>
is the object
you want to send over the net (Here: MessageObject
). This can be (supported)
built in types (string
, IEnumerable
, ..) or custom types marked with [ZeroFormattable]
(see here)
// MessageObject.cs
[ZeroFormattable]
public struct MessageObject {
[Index(0)]
public string Sender { get; set; }
[Index(1)]
public string Recipient { get; set; }
[Index(2)]
public string Message { get; set; }
[Index(3)]
public DateTime Timestamp { get; set; }
}
// Main.cs
IClient client = await Factory.StartNewClient<MessageObject>("82.205.121.132", 1024);
// MyMessageReceivedCallback will be called whenever this client receives a message
client.ReceivedMessage += MyMessageReceivedCallback; // void MyCallback(IPEndPoint, MessageObject)
var msgObject = new MessageObject() {
Sender = "mrousavy",
Recipient = "cbarosch",
Message = "Hi server!",
Timestamp = DateTime.Now
}
await client.Send(msgObject);
// (Optionally configure your Server so that it should redirect to the Recipient)
client.Dispose();
IClient client = await Factory.StartNewBinaryDownlink("82.205.121.132", 1024, true);
client.Send(bytes); // bytes can be a large file for example
client.Dispose();
Use BinaryDownlinks
/BinaryUplinks
when you just want to send binary content (Files, Images, ..). The binary links will skip the serialization and send buffered right away.
// Automatically try to reconnect on disconnects
client.AutoReconnect = true;
// Set the reading buffer size for incoming data
client.ReceiveBufferSize = 2048;
// Set the writing buffer size for outgoing data
client.SendBufferSize = 2048;
// Get the current Connection status
var status = client.ConnectionStatus;
// Connection to server lost handler
client.ConnectionLost += ...;
IServer server = await Factory.StartNewServer<MessageObject>(IPAddress.Any, 1024, true);
// Attach to the Message Received event
server.ReceivedMessage += MyMessageReceivedCallback; // void MyCallback(IPEndPoint, MessageObject)
var msgObject = new MessageObject() {
Sender = "server",
Recipient = "mrousavy",
Message = "Hello client!",
Timestamp = DateTime.Now
}
var clientEndPoint = server.Clients.First(); // Get first client in connected-clients enumerable
await server.Send(msgObject, clientEndPoint); // Send object to given client
// Event once a client connects
server.ClientConnected += ...; // void ClientConnectedCallback(IPEndPoint)
// Event once a client disconnects
server.ClientDisconnected += ...; // void ClientDisconnectedCallback(IPEndPoint)
// Set the reading buffer size for incoming data
server.ReceiveBufferSize = 2048;
// Set the writing buffer size for outgoing data
server.SendBufferSize = 2048;
// Set the count of maximum clients to queue on simultanious connection attempts
server.MaxConnectionsBacklog = 8;
License: MIT | Contributing | Thanks!