message.c File Reference

Implementation of the Messaging System. More...

#include "aatree.h"
#include "vector.h"
#include "game.h"
#include "gameconfig.h"
#include "playernet.h"
#include "net.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

Include dependency graph for message.c:

Go to the source code of this file.

Defines

#define INIT_BUFFERS   16
 The initial number of allocated network buffers.
#define INIT_MESSAGES   32
 The initial number of allocated messages (MsgDescr).
#define INIT_RESEND   192
 The initial resend interval in milliseconds.
#define INIT_TRIES   4
 The initial number of tries left (MsgDescr::triesLeft) for new messages.
#define MAX_BUFFERS   (Uint32)((networkState == NETSTATE_SERVER) ? 32 : 16)
 The maximum number of unallocated buffers that are maintained.
#define MAX_MESSAGES   (Uint32)((networkState == NETSTATE_SERVER) ? 128 : 64)
 The maximum number of unallocated messages (MsgDescr) that are maintained.
#define MIN_POINTERS   64
 The minimum number of pointers to store in the inflightTime and available vectors, and twice the minumum number of pointers used in buffers.
#define MSG_ACK_SIZE   ((MSG_MAX_ACK + 1) << 1)
 The size of a single acknowledge message in bytes.
#define MSG_MAX_ACK   15
 The maximum number of messages that are acknowledged in a single message.

Functions

static void AckConnAccept (MsgDescr *msg, int cid)
 Handles an acknowledgment of connection acceptance by bringing a client into the game.
Bool AddMessage (MsgDescr *msg)
 Adds a new message to the messaging system and sends it out immediately.
void BufferFree (NetBuffer *buff)
 Returns a no longer used network buffer to the queue of available buffers.
NetBufferBufferNew ()
 Obtains a new message buffer from the internal list of buffers.
void BufferVecDestr (void *buff)
 A Vector destructor that returns a no longer used network buffer to the queue of available buffers.
void CancelAllMessages ()
 Cancels all in-flight messages effectively killing communication with the remote end(s).
static void FailConnAccept (MsgDescr *msg, ClientData *client)
 Handles failure of a client to respond to a connection acceptance message by making available the spot that was reserved for the client.
int GetNumOutgoingMessages ()
 Returns the number of messages that have been sent but not acknowledged.
static Bool HandleAck (SocketAddr *origin, NetBuffer *buff, Uint8 *unused, Uint16 noid)
 Handles incomming acknowledgment messages (MSG_TYPE_ACK) by invoking the acknowledgment function on the corresponding messages (MsgDescr::ackFunc).
static Bool HandleConnAccept (SocketAddr *origin, NetBuffer *buff, Uint8 *unused, Uint16 noid)
 Handles a connection acceptance message from a server.
static Bool HandleConnDeny (SocketAddr *origin, NetBuffer *buff, Uint8 *unused, Uint16 noid)
 Handles a connection denied message from the server by setting the network state to NETSTATE_SRVDENY.
static Bool HandleConnDisconn (SocketAddr *origin, NetBuffer *buff, Uint8 *clientId, Uint16 noid)
 Handle an incomming disconnection message.
static Bool HandleConnReq (SocketAddr *origin, NetBuffer *buff, Uint8 *unused, Uint16 noid)
 Handles incomming connection request messages from clients.
Bool MessageCliToSrv ()
 Transitions the messaging system from a client state (really anything other than the server state) to the server state.
void MessageFree (MsgDescr *msg)
 Returns an unused message to the queue of available messages, or deallocates it if the maximum number of messages are in the available queue (MAX_MESSAGES).
Bool MessageInit ()
 Initializes the network messaging system.
MsgDescrMessageNew ()
 Gets a new uninitialized message so that it can be put to use describing a new message.
Bool MessageSrvToCli ()
 Transitions the messaging system from the server state to the client state.
void MessageUninit ()
 Uninitializes the network messaging system.
static void MsgDescrDestructor (MsgDescr **msg)
 A destructor function used to deallocate message descriptors; intended for use with Vector.
void MsgFailDisconnect (MsgDescr *descr, ClientData *client)
 A message failure function that disconnects from the remote end in the most reliable manner possible.
void MsgFailDropConnection (MsgDescr *descr, ClientData *client)
 A message failure function that immediately terminates all communication.
void ProcessNetworkMessages ()
 Handles all network I/O for the client or server.
Bool ReceiveMessages ()
 Handles all incomming messages.
static Bool Send (MsgDescr *msg, Bool inSys)
 Transmits the given message to the other end(s) and/or cancels the message.
static void SendAck (Uint8 *ack)
 Transmits an acknowledgment message.
Bool SendDisconnectMessage (Uint8 id)
 Sends a disconnect message (see MSG_TYPE_CONN_DISCONN and the data format) indicating the specified client is leaving the game.
void ServiceMessages ()
 Services the outgoing messages that are already in the system.
static Bool TimeCompare (MsgDescr **lesser, MsgDescr **greater)
 A comparison function used with Vector to maintain a binary heap sorted by resend time.
Bool ValidateClient (SocketAddr *origin, Uint8 clientId)
 Determines if incomming client data came from a valid source.
Bool ValidateServer (SocketAddr *origin)
 Determines if the inomming server data came from the server that is running the game, and if this process is a client.

Variables

static Uint8 * ackbuff = NULL
 The buffer used to store acknowledgment message(s).
static Vector available
 The vector of unused but allocated messages (MsgDescr).
static Vector buffers
 A vector of allocated but unused buffers that are for storing message data passed to or recived from the network.
Uint32 frameTime
 The current simulation time in milliseconds.
static NetBuffer inbuff
 The buffer used to receive incomming messages.
static AANodeinflightId = (&aaTerminatorNode)
 The root of the AA-tree used to reference in-flight messages by ID number.
static Vector inflightTime
 The priotity queue that references in-flight messages sorted by the next resend time.
static const HandleMsg messageHandlers [MSG_TYPE_MAX]
 The function pointer table of functions that respond to incomming messages.
static Uint16 nextMessageId = 0
 A counter containing the ID number to use for the next transmitted message.
int sock
 The socket used for communication.
int timeSyncState


Detailed Description

Implementation of the Messaging System.

Author:
Jeff Jackowski (jeffj@ro.com)
Copyright (C) 2007 Jeff Jackowski

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Or visit their website at: http://www.gnu.org/

Definition in file message.c.


Define Documentation

#define INIT_BUFFERS   16

The initial number of allocated network buffers.

More will be allocated as needed.

For internal use only.

Definition at line 90 of file message.c.

Referenced by MessageInit().

#define INIT_MESSAGES   32

The initial number of allocated messages (MsgDescr).

More will be allocated as needed.

For internal use only.

Definition at line 97 of file message.c.

Referenced by MessageInit().

#define INIT_RESEND   192

The initial resend interval in milliseconds.

Todo:
Make this 64ms plus round-trip latency.

For internal use only.

Definition at line 83 of file message.c.

Referenced by MessageNew().

#define INIT_TRIES   4

The initial number of tries left (MsgDescr::triesLeft) for new messages.

For internal use only.

Definition at line 103 of file message.c.

Referenced by MessageNew(), and SendDisconnectMessage().

#define MAX_BUFFERS   (Uint32)((networkState == NETSTATE_SERVER) ? 32 : 16)

The maximum number of unallocated buffers that are maintained.

For internal use only.

Definition at line 118 of file message.c.

Referenced by BufferFree().

#define MAX_MESSAGES   (Uint32)((networkState == NETSTATE_SERVER) ? 128 : 64)

The maximum number of unallocated messages (MsgDescr) that are maintained.

For internal use only.

Definition at line 124 of file message.c.

Referenced by MessageFree().

#define MIN_POINTERS   64

The minimum number of pointers to store in the inflightTime and available vectors, and twice the minumum number of pointers used in buffers.

The value can, and probably should, be greater than the initial number of allocated objects.

For internal use only.

Definition at line 112 of file message.c.

Referenced by MessageInit().

#define MSG_ACK_SIZE   ((MSG_MAX_ACK + 1) << 1)

The size of a single acknowledge message in bytes.

For internal use only.

Definition at line 1444 of file message.c.

Referenced by MessageCliToSrv(), MessageInit(), MessageSrvToCli(), and ReceiveMessages().

#define MSG_MAX_ACK   15

The maximum number of messages that are acknowledged in a single message.

For internal use only.

Definition at line 1438 of file message.c.

Referenced by ReceiveMessages().


Function Documentation

static void AckConnAccept ( MsgDescr msg,
int  cid 
) [static]

Handles an acknowledgment of connection acceptance by bringing a client into the game.

This is used only on the server.

For internal use only.

Parameters:
msg The descriptor for the connection acceptance message.
cid The client's ID.
Author:
Jeff Jackowski

Definition at line 912 of file message.c.

References ClientData_t::active, AddToConfigList(), clients, ClientData_t::randId, and TRUE.

Here is the call graph for this function:

static void FailConnAccept ( MsgDescr msg,
ClientData client 
) [static]

Handles failure of a client to respond to a connection acceptance message by making available the spot that was reserved for the client.

A connection denial message is sent to the client. This is used only on the server.

For internal use only.

Parameters:
msg The descriptor for the connection acceptance message.
client The information on the client that tried to connect.
Author:
Jeff Jackowski

Definition at line 945 of file message.c.

References SocketAddr_t::addr, ClientData_t::addr, ClientData_t::cid, ClientData_t::flags, SocketAddr_t::length, MSG_TYPE_CONN_DENY, MSG_WRITE_32C, ClientData_t::randId, and sock.

static Bool HandleAck ( SocketAddr origin,
NetBuffer buff,
Uint8 *  unused,
Uint16  noid 
) [static]

Handles incomming acknowledgment messages (MSG_TYPE_ACK) by invoking the acknowledgment function on the corresponding messages (MsgDescr::ackFunc).

For internal use only.

Flowchart:

inline_dotgraph_1.dot

Parameters:
origin The originating address of the message.
buff The buffer that contains the message to process.
unused A required but unused part of the function prototype.
noid A required but unused part of the function prototype.
Returns:
Always false; never acknowledge acknowledgments.
Author:
Jeff Jackowski

Definition at line 820 of file message.c.

References AATreeFind(), AANode_t::data, NetBuffer_t::data, destMap, DESTMAPLEN, DESTMAPTYPE, FALSE, GetDestMapFlag, GetDestMapIndex, inflightId, AANode_t::key, NetBuffer_t::length, MSG_READ_16, NETSTATE_CLIDISCONN, NETSTATE_CLIENT, NETSTATE_RCVCFG, NETSTATE_SERVER, NETSTATE_SRVDISCONN, networkState, ValidateClient(), and ValidateServer().

Here is the call graph for this function:

static Bool HandleConnAccept ( SocketAddr origin,
NetBuffer buff,
Uint8 *  unused,
Uint16  noid 
) [static]

Handles a connection acceptance message from a server.

Checks the random ID in the message to assure the acceptance is for this process.

For internal use only.

Precondition:
The client's random ID is stored in clientRandId.
Postcondition:
If successful, the networkState is set to NETSTATE_RCVCFG, localPlayer is set to the player's ID, outgoingAddr is set to the server's address, and sock is modified to no longer broadcast.
Parameters:
origin The originating address of the message.
buff The buffer that contains the message to process.
unused A required but unused part of the function prototype.
noid A required but unused part of the function prototype.
Returns:
True if the acceptance was for this client.
Author:
Jeff Jackowski

Definition at line 1136 of file message.c.

References clientRandId, NetBuffer_t::data, FALSE, NetBuffer_t::length, localPlayer, MAX_PLAYERS, MSG_READ_32C, NETSTATE_INIT, NETSTATE_RCVCFG, networkState, outgoingAddr, sock, and TRUE.

static Bool HandleConnDeny ( SocketAddr origin,
NetBuffer buff,
Uint8 *  unused,
Uint16  noid 
) [static]

Handles a connection denied message from the server by setting the network state to NETSTATE_SRVDENY.

For internal use only.

Parameters:
origin The originating address of the message.
buff The buffer that contains the message to process.
unused A required but unused part of the function prototype.
noid A required but unused part of the function prototype.
Returns:
Always false.
Author:
Jeff Jackowski

Definition at line 1107 of file message.c.

References FALSE, NetBuffer_t::length, NETSTATE_CONNECTING, NETSTATE_SRVDENY, networkState, and ValidateServer().

Here is the call graph for this function:

static Bool HandleConnDisconn ( SocketAddr origin,
NetBuffer buff,
Uint8 *  clientId,
Uint16  noid 
) [static]

Handle an incomming disconnection message.

Clients must handle the case of another client leaving the game and of the server killing the game. The server removes disconnecting clients from the game and sends a message to all other clients about the disconnection.

For internal use only.

Parameters:
origin The originating address of the message.
buff The buffer that contains the message to process.
clientId A pointer to the client ID that needs an acknowledgment.
noid A required but unused part of the function prototype.
Returns:
True when a valid disconnect is received.
Author:
Jeff Jackowski

Definition at line 1296 of file message.c.

References ClientData_t::active, clients, ClientData_t::contact, NetBuffer_t::data, ClientData_t::disconn, ClientData_t::disconnTime, FALSE, localPlayer, MAX_PLAYERS, NETSTATE_CLIENT, NETSTATE_DISCONN, NETSTATE_RCVCFG, NETSTATE_SERVER, networkState, players, RemoveFromConfigList(), RemovePlayer(), SendDisconnectMessage(), TRUE, ValidateClient(), and ValidateServer().

Here is the call graph for this function:

static Bool HandleConnReq ( SocketAddr origin,
NetBuffer buff,
Uint8 *  unused,
Uint16  noid 
) [static]

Handles incomming connection request messages from clients.

Finds an available spot for a new client while checking for a previous connection attempt by the same client. Used only on the server side.

For internal use only.

Flowchart:

inline_dotgraph_2.dot

Parameters:
origin The originating address of the message.
buff The buffer that contains the message to process.
unused A required but unused part of the function prototype.
noid A required but unused part of the function prototype.
Returns:
Always false; a new connection acceptance message is created to acknowledge the request message.
Author:
Jeff Jackowski

Definition at line 1019 of file message.c.

References ClientData_t::cid, clients, NetBuffer_t::data, SocketAddr_t::EqualAddresses, FALSE, MAX_PLAYERS, and MSG_READ_32C.

Bool MessageCliToSrv (  ) 

Transitions the messaging system from a client state (really anything other than the server state) to the server state.

For internal use only.

Do not call this function directly; instead, call NetCliToSrv().

Returns:
True on sucess, false on failure.
Author:
Jeff Jackowski

Definition at line 1672 of file message.c.

References CancelAllMessages(), FALSE, MAX_PLAYERS, MSG_ACK_SIZE, and TRUE.

Referenced by NetCliToSrv().

Here is the call graph for this function:

Bool MessageSrvToCli (  ) 

Transitions the messaging system from the server state to the client state.

For internal use only.

Do not call this function directly; instead, call NetSrvToCli().

Returns:
True on sucess, false on failure.
Author:
Jeff Jackowski

Definition at line 1691 of file message.c.

References CancelAllMessages(), FALSE, gameOpts, MSG_ACK_SIZE, GameOptions_t::rcvdMsgs, GameOptions_t::totalMsgs, and TRUE.

Referenced by NetSrvToCli().

Here is the call graph for this function:

static void MsgDescrDestructor ( MsgDescr **  msg  )  [static]

A destructor function used to deallocate message descriptors; intended for use with Vector.

For internal use only.

Parameters:
msg A double pointer to the message to destroy.
Author:
Jeff Jackowski

Definition at line 148 of file message.c.

References BufferFree().

Referenced by CancelAllMessages(), and MessageUninit().

Here is the call graph for this function:

static void SendAck ( Uint8 *  ack  )  [static]

Transmits an acknowledgment message.

For internal use only.

Parameters:
ack The start of the buffer that holds the acknowledge message to send.
Author:
Jeff Jackowski

Definition at line 1467 of file message.c.

References SocketAddr_t::addr, ClientData_t::addr, clients, SocketAddr_t::length, localPlayer, MSG_TYPE_ACK, NETSTATE_SERVER, networkState, outgoingAddr, and sock.

Referenced by ReceiveMessages().

static Bool TimeCompare ( MsgDescr **  lesser,
MsgDescr **  greater 
) [static]

A comparison function used with Vector to maintain a binary heap sorted by resend time.

For internal use only.

Parameters:
lesser The message that will need to be resent sooner if this function returns true.
greater The message that will need to be resent later if this function returns true.
Returns:
True if lesser needs to be resent before greater.
Author:
Jeff Jackowski

Definition at line 137 of file message.c.

Referenced by MessageInit().


Variable Documentation

Uint8* ackbuff = NULL [static]

The buffer used to store acknowledgment message(s).

For clients, its inital size should be MSG_ACK_SIZE. For servers, it should be MSG_ACK_SIZE * MAX_PLAYERS. In both cases, all bytes must be set to zero.

For internal use only.

Definition at line 1458 of file message.c.

Vector available [static]

The vector of unused but allocated messages (MsgDescr).

For internal use only.

Definition at line 61 of file message.c.

Referenced by MessageFree(), MessageInit(), MessageNew(), and MessageUninit().

Vector buffers [static]

A vector of allocated but unused buffers that are for storing message data passed to or recived from the network.

For internal use only.

Definition at line 67 of file message.c.

Referenced by BufferFree(), BufferNew(), MessageInit(), and MessageUninit().

Uint32 frameTime

The current simulation time in milliseconds.

@ingroup StatePlay

Definition at line 63 of file main.c.

NetBuffer inbuff [static]

The buffer used to receive incomming messages.

For internal use only.

Definition at line 1450 of file message.c.

Referenced by ReceiveMessages(), and TextRenderStringV().

AANode* inflightId = (&aaTerminatorNode) [static]

The root of the AA-tree used to reference in-flight messages by ID number.

For internal use only.

Definition at line 50 of file message.c.

Referenced by AddMessage(), CancelAllMessages(), HandleAck(), MessageUninit(), Send(), and ServiceMessages().

Vector inflightTime [static]

The priotity queue that references in-flight messages sorted by the next resend time.

For internal use only.

Definition at line 56 of file message.c.

Referenced by AddMessage(), CancelAllMessages(), GetNumOutgoingMessages(), MessageInit(), MessageUninit(), and ServiceMessages().

const HandleMsg messageHandlers[MSG_TYPE_MAX] [static]

Initial value:

The function pointer table of functions that respond to incomming messages.

For internal use only.

Definition at line 1417 of file message.c.

Uint16 nextMessageId = 0 [static]

A counter containing the ID number to use for the next transmitted message.

The tree used to store the messages gives special meaning to 0xFFFF (-1). Because of this, the message ID must not be 0xFFFF.

For internal use only.

Definition at line 74 of file message.c.

Referenced by AddMessage().

int sock

The socket used for communication.

Not marked as static because it is used in other networking related source files.

Definition at line 103 of file net.c.

Referenced by ClientSync(), ConfigIPv4Broadcast(), Disconnect(), DiscoverConnectServer(), FailConnAccept(), HandleConnAccept(), MsgFailDropConnection(), NetCliToSrv(), NetSrvToCli(), NetUninit(), ReceiveMessages(), Send(), SendAck(), SendConnectMsg(), and UnicastSocket().

int timeSyncState

Definition at line 45 of file timesync.c.

Referenced by ClientSync(), Disconnect(), GetSamples(), NetStatFocus(), NetStatUpdate(), StartClientTimeSync(), and StopServerTimeSync().


Generated on Mon May 28 04:41:40 2007 for Retro Tank Super Attack by  doxygen 1.5.2