Skip to content

Latest commit

 

History

History
164 lines (143 loc) · 5.87 KB

README.md

File metadata and controls

164 lines (143 loc) · 5.87 KB

<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

  1. Send nearly any .NET object (See: supported, custom) 📦
  2. Send/Receive faster by buffered send/receive and ZeroFormatter's fast (de-)serializing 💨
  3. Automatically correct errors with TCP and Auto-reconnect
  4. Async and Event based ⚡
  5. Efficient Network Discovery for other GenericProtocol Hosts 🔍
  6. Fast binary links for file/images/.. transfer 💾
  7. 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!");
}

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!