GenericProtocol
                                
                                 GenericProtocol copied to clipboard
                                
                                    GenericProtocol copied to clipboard
                            
                            
                            
                        ⚡️ A fast TCP event based buffered server/client protocol for transferring data over the (inter)net in .NET 🌐
    <T>
    
    Generic Protocol
  
  ⚡️ A fast TCP event based buffered server/client protocol for transferring data over the (inter)net in .NET 🌐
Why?
Send whole objects over the net easier and faster
- Send nearly any .NET object(See: supported, custom) :package:
- Send/Receive faster by buffered send/receive and ZeroFormatter's fast (de-)serializing :dash:
- Automatically correct errors with TCP and Auto-reconnect :white_check_mark:
- Async and Event based :zap:
- Efficient Network Discovery for other GenericProtocol Hosts :mag:
- Fast binary links for file/images/.. transfer :floppy_disk:
- Made with love :heart:
Sending objects:
await client.Send(someObject);
...on the other end:
private void MyMessageReceivedCallback(SomeObject someObject) {
  Console.WriteLine("I've received an object!");
}
How?
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?
Client
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)
Send/Receive custom objects:
// 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();
Send large binary content
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.
Other
// 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 += ...;
Server
Start a new server:
IServer server = await Factory.StartNewServer<MessageObject>(IPAddress.Any, 1024, true);
Send/Receive your objects (MessageObject in this example):
// 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
Other
// 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!
