G Sever

MIT
by fexolm

Easy for use, game developing oriented reliable udp library

( Crawled 3 months ago )
+

GServer

About GServer

Usage

Classes

Examples

What is GServer

GServer is reliable UDP networking library, designed for games. Its main advantages are:

What is reliable UDP

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 mods

RUDP supports 3 mods and their combinations. GServer implements some of the most useful of them:

  • No mode - works like standard UDP
  • Reliable - provides delivery checking and guarantees that all packages will be delivered
  • Reliable sequenced - guarantees that all packages that are delivered will be processed in correct order, but no implication of delivering all packages is made
  • Reliable ordered - guarantees that all packages will be delivered and will be processed in correct order

Why reliable udp is better than tcp

  • Reliable udp provides more flexibility than udp because of it's modes
  • Reliable ordered mode simulates tcp behavior but works faster since it doesn't need to wait for lost packages.

Classes

Host

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.

Message

Messages are used to send data from one host to another. It has 3 parameters:

  • type : short - message type which is used to add handler to specific messages.
  • mode : Mode - reliable mode (see RUDP mods)
  • body : byte[] - serialized data in an array of bytes

DataStorage

DataStorage is used to serialize data into byte array. It has 2 mods:

  • Calling the constructor without params enables write only mode.
  • Calling the constructor with byte[] or MemoryStream param enables read only mode.

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.

Plugins

In process ...

Examples

Start server

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

Handling messages

/* 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 */
});

DataStorage

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);
     }
}