DasyneServer, Networking



run
getMaxConnections
isConnected
isNewClient
authorize
disconnectClient
getUnsentBytes
sendTCP
sendUDP
getMessage
getClientAddr
getErrMsg

The DasyneServer class handles all server-side data synchronization. Clients are referred to by their Dasyne index - in a game with a maximum of 8 clients, players would be assigned an index between 0 and 7 when they connect.

DasyneServer member functions:


bool run(void);

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

If run ever returns false, it's because some kind of unrecoverable network error occurred. It sets errMsg with a description of the problem.


int getMaxConnections(void);

Returns the maximum number of clients that can connect to this server.


bool isConnected(int clientIndex);

Returns true if there's a client connected at the specified index.


bool isNewClient(int clientIndex);

isNewClient returns true the first time it's called after a client connects. It returns false until this client disconnects, and another client connects at the same index.


bool authorize(int clientIndex);

Once a client has connected, call authorize to allow it to begin synchronizing data. Until authorize is called, the client's synchronized variables won't be updated when you call run. This is useful to prevent clients from joining the game unless they provide a password.

You can use sendTCP and getMessage to implement any authentication. Otherwise, you can automatically call authorize whenever a client connects.

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


void disconnectClient(int clientIndex);

Disconnect the client at the specified index.


long int getUnsentBytes(int clientIndex);

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


void sendTCP(int clientIndex, DyBytes &theMessage);

Sends theMessage to the specified client 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 sending more data to that client. 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(int clientIndex, DyBytes &theMessage);

Sends theMessage to the specified client over UDP. This will only work if UDP is enabled. You can send up to DY_MAX_MSGSIZE bytes at a time.


bool getMessage(int clientIndex, DyBytes &theMessage);

Returns true if there's a message from the specified client, and places it in theMessage.


string getClientAddr(int clientIndex);

Returns the specified client's IP address, or "0.0.0.0" if there's no client at clientIndex.


string getErrMsg(void);

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