constructors
set
get
preventAutoUpdate
getServerValue
resumeAutoUpdate
The SyncData class manages a variable that's automatically synchronized between the server and clients. It doesn't actually contain any data, instead it has a pointer to a location in a DasyneServer or DasyneClient's data map. When you retrieve the variable, SyncData extracts its byte values from the data map, and uses them to create the variable. Changing the variable works in the same way; it's the Dasyne data map that is being altered.
If you try to access the SyncData variable before the data map is created, you'll get an error. DasyneClients create their data map with createData, and DasyneServers create their data maps when initialize is called. Accessing the SyncData variable after its Dasyne object has been deleted won't work either!
Servers and clients need to declare all their SyncData and SyncArray objects in exactly the same order, to make sure they reference the same location in the data map.
Example 1:
SyncData <int> testData;
DasyneServer syncEngine;
testData.assignEngine(syncEngine);
syncEngine.initialize(5555);
testData = 12;
cout << testData << endl;
Example 2:
SyncData <int> *testData;
DasyneClient syncEngine;
testData = new SyncData<int>(syncEngine);
syncEngine.initialize(5555);
testData->set(12); //use set and get when accessing the object through a pointer
cout << testData->get() << endl;
delete testData;
SyncData member functions:
template <class T>
SyncData(void);
template <class T>
SyncData(DasyneEngine &syncEngine);
There are two constructors you can use to create a SyncData object. The first contructor doesn't take any parameters. If you use this constructor, you'll have to specify the Dasyne engine that will synchronize this variable using assignEngine.
The second constructor takes either a DasyneServer or DasyneClient as its parameter, which will synchronize the SyncData's variable.
When you create a SyncData object, use a template to specify the type of variable it will hold:
SyncData <int> testData;
template <class T>
void assignEngine(DasyneEngine &newEngine);
If you didn't specify a Dasyne engine in the constructor, you can do it with this function. The engine is either a DasyneServer or DasyneClient, and will synchronize the SyncData's variable.
template <class T>
void set(T newValue);
Set the variable to the new value.
template <class T>
T get(void);
Return the variable.
template <class T>
void preventAutoUpdate(void);
Prevents this variable from being automatically updated by the server. All changes the client makes to this variable will now become permanent. (Usually client-side changes are applied temporarily, and undone if no confirmation was received from the server).
preventAutoUpdate allows clients to implement a dead-reckoning algorithm. Clients can retrieve the value the server is inviting them to use with getServerValue, and compare it to the current value of this variable. The client can then manually update the variable based on a smoothing or dead reckoning algorithm.
template <class T>
T getServerValue(void);
If auto-updates to the variable have been prevented, you can use getServerValue to retrieve the most recent value sent by the server.
template <class T>
void resumeAutoUpdate(void);
This variable will be auto-updated by the server as normal. Any changes the client makes to the variable are now temporary until they're confirmed by the server.