|
1 | 1 | # ReliableNetcode.NET |
2 | 2 | A pure managed C# socket agnostic reliability layer inspired by reliable.io and yojimbo |
| 3 | +ReliableNetcode.NET provides a simple and easy-to-use reliability layer designed for use in games built on unreliable UDP connections. It is a great fit for use with [Netcode.IO.NET](https://github.com/KillaMaaki/Netcode.IO.NET), my C# port of Glenn Fiedler's secure UDP communication protocol, but it can also be used with any other system you want. |
| 4 | + |
| 5 | +# Features |
| 6 | +* Multiple quality-of-service options (reliable, unreliable, and unreliable-ordered) for different use cases in a single API |
| 7 | +* Lightweight packet acking and packet resending |
| 8 | +* Supports messages up to about 16kB large using automatic message fragmentation and reassembly. |
| 9 | +* Simple congestion control changes the packet send rate according to round-trip-time. |
| 10 | + |
| 11 | +# Usage |
| 12 | +All of the API is provided under the `ReliableNetcode` namespace. |
| 13 | +Usage is really simple. Create a new instance of `ReliableEndpoint`: |
| 14 | + |
| 15 | +```c# |
| 16 | +var reliableEndpoint = new ReliableEndpoint(); |
| 17 | +reliableEndpoint.ReceiveCallback = ( buffer, size ) => |
| 18 | +{ |
| 19 | + // this will be called when the endpoint extracts messages from received packets |
| 20 | + // buffer is byte[] and size is number of bytes in the buffer. |
| 21 | + // do not keep a reference to buffer as it will be pooled after this function returns |
| 22 | +}; |
| 23 | +reliableEndpoint.TransmitCallback = ( buffer, size ) => |
| 24 | +{ |
| 25 | + // this will be called when a datagram is ready to be sent across the network. |
| 26 | + // buffer is byte[] and size is number of bytes in the buffer |
| 27 | + // do not keep a reference to the buffer as it will be pooled after this function returns |
| 28 | +}; |
| 29 | +``` |
| 30 | + |
| 31 | +To send a message, call `ReliableEndpoint.SendMessage`: |
| 32 | + |
| 33 | +```c# |
| 34 | +// Send a message through the qos channel |
| 35 | +// messageBytes is a byte array, messageSize is number of bytes in the array, and qos type is either: |
| 36 | +// QoSType.Reliable - message is guaranteed to arrive and in order. |
| 37 | +// QoSType.Unreliable - message is not guaranteed delivery. |
| 38 | +// QoSType.UnreliableOrdered - message is not guaranteed delivery, but received messages will be in order. |
| 39 | +reliableEndpoint.SendMessage( messageBytes, messageSize, qosType ); |
| 40 | +``` |
| 41 | + |
| 42 | +When you receive a datagram from your socket implementation, use `ReliableEndpoint.ReceivePacket`: |
| 43 | + |
| 44 | +```c# |
| 45 | +// when you receive a datagram, pass the byte array and the number of bytes to ReceivePacket |
| 46 | +// this will extract messages from the datagram and call your custom ReceiveCallback with any received messages. |
| 47 | +reliableEndpoint.ReceivePacket( packetBytes, packetSize ); |
| 48 | +``` |
| 49 | + |
| 50 | +And, finally, make sure you call `ReliableEndpoint.Update` at regular and frequent intervals to update the internal buffers (you could run it on a separate network thread, from your game loop, or whatever you choose): |
| 51 | +```c# |
| 52 | +// update internal buffers |
| 53 | +reliableEndpoint.Update(); |
| 54 | +``` |
0 commit comments