Synchronize Client Data With SyncData Class

Diarkis C# Client comes with client data serializer to help manage client-to-client data synchronization.

Overview

SyncData class takes a Dictionary<string, object> as client properties to handle data serialization and deserialization for data synchronization between clients over network.

Note

SyncData is an optional component for convenience. If you are concerned about the performance of your application and the size of data sent and received over the network, you may want to avoid using this. 

How To Set Properties To Be Serialized

The code example below shows to set data properties to be serialized:

Dictionary<string, object> props = new Dictionary<string, object>();

props.Add("x", (long)1024);
props.Add("y", (long)344);
props.Add("velocity", 4);
props.Add("dataType", 1);

Diarkis.Lib.SyncData sd = new Diarkis.Lib.SyncData();

// This is how we set data properties
sd.Properties = props;

How To Serialize

The code example below shows how to serialize data properties:

Dictionary<string, object> props = new Dictionary<string, object>();

props.Add("x", (long)1024);
props.Add("y", (long)344);
props.Add("velocity", 4);
props.Add("dataType", 1);

Diarkis.Lib.SyncData sd = new Diarkis.Lib.SyncData();

// This is how we set data properties
sd.Properties = props;

sd.Serialize();

// This is the serialized byte array to be synchronized
byte[] serialized = sd.Serialized;

How To Deserialize

The code example below shows how to deserialize the synchronized data to data properties:

Diarkis.Lib.SyncData sd = new Diarkis.Lib.SyncData();

sd.Deserialize(synchronizedBytes);

// This is the deserialized properties
Dictionary<string, object> props = sd.Properties;

long x = props.Get("x");
long y = props.Get("y");
int velocity = props.Get("velocity");
int dataType = props.Get("dataType");