DasyneClient, Networking



connectToHost
checkConnection
diconnectClient
createData
run
getServerAddr
getUnsentBytes
sendTCP
sendUDP
getMessage
getErrMsg

The DasyneClient class handles all client-side data synchronization. The functions listed here need to be called after initialize.

DasyneClient member functions:


bool connectToHost(string ipAddress, int portNumber);

Starts a non-blocking connect to the Dasyne server at the specified address and port. You can use checkConnection to tell when it completes. If this client is already connected to a server, that connection is terminated.

Returns false on errors, and sets errMsg with a description of the problem.


int checkConnection(uint32_t &newMapSize, bool &udpEnabled, uint16_t &dasyneIndex);

Once a non-blocking connect has been started with connectToHost, use checkConnection to tell when it completes.

If the return value is negative it will be equal to one of these error codes:

DY_NOTOPEN - The connect failed. errMsg is set with a description of the problem.
DY_BUSYUNREACHABLE - The connect is still in progress, try again later.

If the return value is zero:

The connect completed successfully. newMapsize is set to the size of the data map being hosted by the server. udpEnabled will be set to true if the server has enabled UDP, and false if it's using TCP only.

dasyneIndex will be set to the index the server is using to refer to this client. (See DasyneServer)

Once connectToHost completes successfully, you can no longer connect to a new server using this DasyneClient instance. To connect to a new server, you would need to:
-Delete this DasyneClient and create a new one (and re-initialize it).
-Delete your SyncData and SyncArray objects, and create new ones.


void disconnectClient(void);

Terminates our connection to the server.


bool createData(void);

Creates a data map the same size as the one being hosted by the server. createData should be called after you've declared all your SyncData and SyncArray variables - you'll get an error if you try to create new SyncData or SyncArray objects afterwards.

Once you call createData, you'll be able to access your SyncArray and SyncData objects, and begin synchronizing with the server by calling run.

If you haven't completely used up all the space in the data map when createData is called, an error message is written to the log. You've probably made a mistake when declaring your variables! Remember, you must declare the same synchronized variables in the same order on both the client and server. This is not treated as an error, as there may be rare cases where this was your intention.

Returns false on errors, and sets errMsg with a description of the problem.


bool run(void);

Call run to synchronize the client's SyncData and SyncArray objects with the server. run should be called whenever you update your game logic, though it doesn't hurt to call it more frequently.

If run returns false, it's typically because the connection to the server failed. It sets errMsg with a description of the problem.


string getServerAddr(void);

Returns the server's IP address, or "0.0.0.0" if we aren't connected.


long int getUnsentBytes(void);

Returns the number of bytes that are buffered for sending over TCP/IP. You can use getUnsentBytes to reduce lag if you're using sendTCP to send custom messages to the server. If getUnsentBytes returns a large number, you may wish to wait a while before sending more data. Note there may be up to queryBandwidth bytes in the send buffer from Dasyne's EDC algorithm.


void sendTCP(DyBytes &theMessage);

Sends theMessage to the server over TCP/IP. This is useful for sequential messages that don't work well with SyncData and SyncArray objects. For example, in-game chat or file transfers. You can send up to DY_MAX_MSGSIZE bytes at a time.

If you send a lot of data over TCP, it can interfere with Dasyne's operation, as you're sharing the same connection. If Dasyne notices a lot of information in the TCP/IP buffer, it will wait for it to clear before synchronizing more data. You can prevent this problem by telling Dasyne how much data you'll be sending with setMessageLimit, and by breaking up your TCP messages into smaller chunks and sending them periodically.


void sendUDP(DyBytes &theMessage);

Sends theMessage to the server over UDP. This will only work if the server has enabled UDP. (Use checkConnection to determine if UDP is enabled) You can send up to DY_MAX_MSGSIZE bytes at a time.

A possible use for sendUDP is to send player commands to the server. Eg, notify the server that the player fired his weapon, or hit the forward key.


bool getMessage(DyBytes &theMessage);

Returns true if we received a message from the server, and places it in theMessage. getMessage will retrieve both TCP and UDP messages.


string getErrMsg(void);

Return a description of the most recent error for this DasyneClient.