GServer is reliable UDP networking library, designed for games. Its main advantages are:
Reliable udp (RUDP) is a transport layer protocol designed for situations where TCP adds too much overhead, but nonetheless we need guaranteed-order packet delivery.
RUDP supports 3 mods and their combinations. GServer implements some of the most useful of them:
Host is the main class in GServer. It provides methods to receive, send and process messages. Messages are accept automatically in listen thread.
Messages are send using the "Send" method and processed with handlers, which is registered by user using the "AddHandler" method. See examples.
Messages are used to send data from one host to another. It has 3 parameters:
DataStorage is used to serialize data into byte array. It has 2 mods:
In read only mode you could use just Read methods which are reading data from buffer. In write only mode you could use just Write methods which are writing data into buffer.
In process ...
server
Host host= new Host(portNumber); //instantiate host on portNumber port
host.StartListen(numberOfThreads); //StartListen on numberOfThreads threads
Timer timer = new Timer(o=>host.Tick());
timer.Change(10,10); // enables timer tick every 10 milliseconds
client
Host host= new Host(portNumber); //instantiate host on portNumber port
host.StartListen(numberOfThreads); //StartListen on numberOfThreads threads
Timer timer = new Timer(o=>host.Tick());
timer.Change(10,10); // enables timer tick every 10 milliseconds
host.OnConnect = () => Console.WriteLine("Connected"); // connect handler
host.BeginConnect(serverEndpoint); // connecting to server endpoint
/* host inicialization here */
//add handler to message with id == messageId
//when message with that id will arrive callback will be invoked
//connection - connection associated with sender
host.AddHanlder(messageId, (message, connection) =>
{
/* deserialize message buffer */
/* process message buffer */
/* send response if needed */
});
class SimpleClass : ISerializable, IDeserializable
{
public int IntField {get;set;}
public byte[] ByteArray {get;set} // arraySize = IntField
public byte[] Serialize()
{
var ds = DataStorage.CreateForWrite();
ds.Push(IntField);
ds.Push(ByteArray);
return ds.Serialize();
}
public void FillDeserialize(byte[] buffer)
{
var ds = DataStorage.CreateForRead(buffer);
IntField = ds.ReadInt32();
ByteArray = ds.ReadBytes(IntField);
}
}