Collaboration diagram for Messaging System:
Files | |
file | message.c |
Implementation of the Messaging System. | |
file | message.h |
Interface to the Messaging System. | |
Data Structures | |
struct | MsgDescr_t |
Describes a message, including who should receive it and data needed to handle acknowledgments. More... | |
struct | NetBuffer_t |
The buffer type used to store messages sent and received over the network. More... | |
Defines | |
#define | DESTMAPLEN bitarraylen(MAX_PLAYERS, DESTMAPTYPE) |
The number of elements in the array MsgDescr::destMap. | |
#define | DESTMAPTYPE Uint16 |
The data type used for MsgDescr::destMap. | |
#define | GetDestMapFlag(id) bitflag(id, DESTMAPTYPE) |
Returns the bit flag that indicates the given client in the MsgDescr::destMap array at the correct index (see GetDestMapIndex()). | |
#define | GetDestMapIndex(id) bitindex(id, DESTMAPTYPE) |
Returns the index to the MsgDescr::destMap array that contains the flag for the given client. | |
#define | MSG_MAX_SIZE 510 |
The maximum size of a message in bytes. | |
#define | SetDestToAll(msg) |
Marks a message as being destined to all clients. | |
Typedefs | |
typedef void(*) | AcknowledgeMsg (MsgDescr *descr, int cid) |
The function prototype used to react to the recipient acknowledging a message. | |
typedef Bool(*) | ApproveSendMsg (MsgDescr *descr, int cid) |
Approves sending a message to a given recipient. | |
typedef void(*) | ConstructMsg (MsgDescr *descr) |
The function prototype used to construct the message in a buffer. | |
typedef void(*) | FailMsg (MsgDescr *descr, ClientData *client) |
The function prototype used to react to a recipient that did not acknowledge a message, or if an error occured when trying to send a message. | |
typedef Bool(*) | HandleMsg (SocketAddr *origin, NetBuffer *buffer, Uint8 *clientId, Uint16 messageId) |
The function prototype used to process incomming messages. | |
typedef MsgDescr_t | MsgDescr |
typedef NetBuffer_t | NetBuffer |
Enumerations | |
enum | { MSG_TYPE_ACK, MSG_TYPE_CONN_REQ, MSG_TYPE_CONN_DENY, MSG_TYPE_CONN_ACCEPT, MSG_TYPE_CONN_DISCONN, MSG_TYPE_OPTION, MSG_TYPE_OBSTACLE, MSG_TYPE_SPAWN, MSG_TYPE_GOAL, MSG_TYPE_GAME_START, MSG_TYPE_GAME_OVER, MSG_TYPE_SCORE = MSG_TYPE_GOAL + 1, MSG_TYPE_PLAYER_UPDATE, MSG_TYPE_PLAYER_SPWAN, MSG_TYPE_MAX } |
The message type identifiers that are always sent as the first byte of every message. More... | |
Functions | |
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. | |
NetBuffer * | BufferNew () |
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). | |
int | GetNumOutgoingMessages () |
Returns the number of messages that have been sent but not acknowledged. | |
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. | |
MsgDescr * | MessageNew () |
Gets a new uninitialized message so that it can be put to use describing a new message. | |
void | MessageUninit () |
Uninitializes the network messaging system. | |
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. | |
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. | |
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. |
Diagram of message data relationships:
The following macros affect how the message system operates:
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/
#define DESTMAPLEN bitarraylen(MAX_PLAYERS, DESTMAPTYPE) |
The number of elements in the array MsgDescr::destMap.
Definition at line 309 of file message.h.
Referenced by HandleAck(), MessageNew(), Send(), and ServiceGameConfig().
#define DESTMAPTYPE Uint16 |
The data type used for MsgDescr::destMap.
Definition at line 304 of file message.h.
Referenced by HandleAck(), and Send().
#define GetDestMapFlag | ( | id | ) | bitflag(id, DESTMAPTYPE) |
Returns the bit flag that indicates the given client in the MsgDescr::destMap array at the correct index (see GetDestMapIndex()).
id | The ID of the client who's bit flag is sought. |
Definition at line 327 of file message.h.
Referenced by AddToConfigList(), HandleAck(), and RemoveFromConfigList().
#define GetDestMapIndex | ( | id | ) | bitindex(id, DESTMAPTYPE) |
Returns the index to the MsgDescr::destMap array that contains the flag for the given client.
id | The ID of the client who's array position is sought. |
Definition at line 318 of file message.h.
Referenced by AddToConfigList(), HandleAck(), and RemoveFromConfigList().
#define MSG_MAX_SIZE 510 |
The maximum size of a message in bytes.
See NetBuffer for details on appropriate values.
Definition at line 199 of file message.h.
Referenced by AppendPlayerData(), MakeAreaBuffers(), and ReceiveMessages().
#define SetDestToAll | ( | msg | ) |
Value:
{ \ int loop; \ DESTMAPTYPE *map = &((msg)->destMap[0]); \ *map = ~0; \ for (loop = DESTMAPLEN - 1; loop > 0; loop--) *(++map) = ~0; \ }
msg | A pointer to the message descriptor to modify. |
Definition at line 334 of file message.h.
Referenced by AppendPlayerData(), SendDisconnectMessage(), and SendScoreUpdate().
typedef void(*) AcknowledgeMsg(MsgDescr *descr, int cid) |
The function prototype used to react to the recipient acknowledging a message.
After this function is called, the bit corresponding to the acknowledging host in MsgDescr::destMap will be cleared.
descr | The message descriptor data. | |
cid | The client's ID. |
typedef Bool(*) ApproveSendMsg(MsgDescr *descr, int cid) |
Approves sending a message to a given recipient.
If approval is not given, the recipient will be removed from the message's destination list and the corresponding FailMsg function will not be called.
descr | The message descriptor data for the message to approve. | |
cid | The destination client ID. |
typedef void(*) ConstructMsg(MsgDescr *descr) |
The function prototype used to construct the message in a buffer.
When the function is called for the first time for a given message, the MsgDescr::inflight flag will be cleared; all subsequent calls will see the flag set. The system is responsible for transmitting the message to all the recipiants.
descr | The message descriptor data. |
typedef void(*) FailMsg(MsgDescr *descr, ClientData *client) |
The function prototype used to react to a recipient that did not acknowledge a message, or if an error occured when trying to send a message.
descr | The message descriptor data. | |
client | A pointer to the client data that failed to respond, or NULL if this process is a client. |
typedef Bool(*) HandleMsg(SocketAddr *origin, NetBuffer *buffer, Uint8 *clientId, Uint16 messageId) |
The function prototype used to process incomming messages.
origin | The originating address of the message as reported by recvfrom(). | |
buffer | The buffer that contains the whole message including the message type byte. | |
clientId | A pointer used to return the ID of the client sending the message. This must be set for the message to be acknowledged by the server. For client side operation, this value should always be zero. | |
messageId | The message ID that this function is processing. This is useful to prevent re-updating player data that has been received twice because the server didn't get an acknowledgment in time. Some message types lack a message ID; see the message formats for which ones. |
typedef struct MsgDescr_t MsgDescr |
typedef struct NetBuffer_t NetBuffer |
anonymous enum |
The message type identifiers that are always sent as the first byte of every message.
The ID's must be put in the following order to simplify ReceiveMessages():
MSG_TYPE_ACK | Message acknowledgment. |
MSG_TYPE_CONN_REQ | Connection request; see design and message format. |
MSG_TYPE_CONN_DENY | Connection denial. |
MSG_TYPE_CONN_ACCEPT | Connection acceptance; see design and message format. |
MSG_TYPE_CONN_DISCONN |
Disconnection notice.
This can be sent by client and server. |
MSG_TYPE_OPTION | Game configuration options; only sent by the server. |
MSG_TYPE_OBSTACLE | A set of obstacle data; only sent by the server. |
MSG_TYPE_SPAWN | A set of spawn areas; only sent by the server. |
MSG_TYPE_GOAL | A set of goal areas for CTF; only sent by the server. |
MSG_TYPE_GAME_START | Notice that the game is about to begin. |
MSG_TYPE_GAME_OVER | Notice that the game has concluded. |
MSG_TYPE_SCORE | An update to the scores of the teams. |
MSG_TYPE_PLAYER_UPDATE | Contains updated state data for multiple players. |
MSG_TYPE_PLAYER_SPWAN | A request from a player to spwan in the game. |
MSG_TYPE_MAX |
Adds a new message to the messaging system and sends it out immediately.
The system will handle acknowledgments and re-transmissions as needed, and will assume the responsibility of deallocating the message.
Flowchart:
msg | The message to add to the system. The pointer should have been returned by a previous call to MessageNew(). A message can only be added once, after which it will represent the transmission state of the message. |
Definition at line 654 of file message.c.
References AATreeAdd(), MsgDescr_t::buffer, AANode_t::data, NetBuffer_t::data, MsgDescr_t::destMap, MsgDescr_t::doNotAddId, FALSE, inflightId, inflightTime, NetBuffer_t::length, MSG_WRITE_16C, MsgDescr_t::msgId, NETSTATE_CLIENT, NETSTATE_SERVER, networkState, nextMessageId, Send(), MsgDescr_t::triesLeft, TRUE, and Vector_t::VectorHeapPush().
Referenced by AppendPlayerData(), GameInit(), GameRun(), SendDisconnectMessage(), SendScoreUpdate(), SendSpawnRequest(), and ServiceGameConfig().
Here is the call graph for this function:
void BufferFree | ( | NetBuffer * | buff | ) |
Returns a no longer used network buffer to the queue of available buffers.
When the macro MSG_PTR_CHK is defined, the process will fail on an assert if the given buffer is already in the available buffer list.
buff | The buffer to reclaim. |
Definition at line 273 of file message.c.
References Vector_t::array, buffers, Vector_t::items, MAX_BUFFERS, and Vector_t::VectorPush().
Referenced by BufferVecDestr(), MakeAreaBuffers(), MakeBuffers(), MessageFree(), MsgDescrDestructor(), and Send().
Here is the call graph for this function:
NetBuffer* BufferNew | ( | ) |
Obtains a new message buffer from the internal list of buffers.
Intended to allow a buffer to be added to a message prior to passing its descriptor to AddMessage() rather than use a message constructor function (MsgDescr::constrFunc). For this to work, do the following:
Definition at line 247 of file message.c.
References buffers, NetBuffer_t::length, and Vector_t::VectorLast.
Referenced by AppendPlayerData(), MakeAreaBuffers(), MakeBuffers(), SendDisconnectMessage(), SendScoreUpdate(), and SendSpawnRequest().
void BufferVecDestr | ( | void * | buff | ) |
A Vector destructor that returns a no longer used network buffer to the queue of available buffers.
buff | A double pointer to the NetBuffer to reclaim. |
Definition at line 296 of file message.c.
References BufferFree().
Here is the call graph for this function:
void CancelAllMessages | ( | ) |
Cancels all in-flight messages effectively killing communication with the remote end(s).
Prepares the messaging system for use with another host. There is no need to call this function if the message system is about to be uninitialized with a call to MessageUninit().
Definition at line 162 of file message.c.
References AA_ROOT_NODE, AATreeDestroy(), Vector_t::destructOp, inflightId, inflightTime, MsgDescrDestructor(), and Vector_t::VectorClear().
Referenced by MessageCliToSrv(), MessageSrvToCli(), NetDisconnect(), and NetUninit().
Here is the call graph for this function:
int GetNumOutgoingMessages | ( | ) |
Returns the number of messages that have been sent but not acknowledged.
Definition at line 177 of file message.c.
References inflightTime, and Vector_t::items.
Referenced by Disconnect().
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).
When the macro MSG_PTR_CHK is defined, the process will fail on an assert if the given message is already in the available message list.
Definition at line 217 of file message.c.
References Vector_t::array, available, MsgDescr_t::buffer, BufferFree(), MsgDescr_t::buffLifeUnmanaged, Vector_t::items, MAX_MESSAGES, and Vector_t::VectorPush().
Referenced by AppendPlayerData(), Send(), SendDisconnectMessage(), SendScoreUpdate(), and SendSpawnRequest().
Here is the call graph for this function:
Bool MessageInit | ( | ) |
Initializes the network messaging system.
Definition at line 1603 of file message.c.
References available, buffers, Vector_t::destructOp, FALSE, GenericPointerFree(), inflightTime, INIT_BUFFERS, INIT_MESSAGES, Vector_t::itemSize, Vector_t::lessOp, MIN_POINTERS, Vector_t::minSize, MSG_ACK_SIZE, TimeCompare(), TRUE, Vector_t::VectorAdd(), and Vector_t::VectorInit().
Referenced by NetInit(), and NetInitSrv().
Here is the call graph for this function:
MsgDescr* MessageNew | ( | ) |
Gets a new uninitialized message so that it can be put to use describing a new message.
Definition at line 181 of file message.c.
References available, MsgDescr_t::destMap, DESTMAPLEN, MsgDescr_t::flags, INIT_RESEND, INIT_TRIES, MsgDescr_t::interval, Vector_t::items, MsgDescr_t::triesLeft, and Vector_t::VectorLast.
Referenced by AppendPlayerData(), SendDisconnectMessage(), SendScoreUpdate(), SendSpawnRequest(), and ServiceGameConfig().
void MessageUninit | ( | ) |
Uninitializes the network messaging system.
Definition at line 1647 of file message.c.
References AA_ROOT_NODE, AATreeDestroy(), available, buffers, Vector_t::destructOp, inflightId, inflightTime, MsgDescrDestructor(), and Vector_t::VectorDestroy().
Referenced by NetUninit().
Here is the call graph for this function:
void MsgFailDisconnect | ( | MsgDescr * | descr, | |
ClientData * | client | |||
) |
A message failure function that disconnects from the remote end in the most reliable manner possible.
It sends out the disconnect message to all possible destinations. Because a failure has occured, the disconnect message may not be received.
descr | The message descriptor data. | |
client | A pointer to the client data that failed to respond, or NULL if this process is a client. |
Definition at line 1244 of file message.c.
References ClientData_t::active, ClientData_t::cid, ClientData_t::contact, ClientData_t::disconn, ClientData_t::disconnTime, FALSE, localPlayer, MsgDescr_t::msgId, NETSTATE_CLIDISCONN, NETSTATE_CLIENT, NETSTATE_RCVCFG, NETSTATE_SERVER, networkState, players, RemoveFromConfigList(), RemovePlayer(), SendDisconnectMessage(), and TRUE.
Referenced by AppendPlayerData(), and SendScoreUpdate().
Here is the call graph for this function:
void MsgFailDropConnection | ( | MsgDescr * | descr, | |
ClientData * | client | |||
) |
A message failure function that immediately terminates all communication.
It does not send out a disconnect message unless it is on the server and the message is not a disconnect message or the failed client is the one specified in the disconnect message.
descr | The message descriptor data. | |
client | A pointer to the client data that failed to respond, or NULL if this process is a client. |
Definition at line 1180 of file message.c.
References MsgDescr_t::buffer, ClientData_t::cid, CloseSocket, NetBuffer_t::data, Player_t::flags, ClientData_t::flags, NetBuffer_t::length, MSG_TYPE_CONN_DISCONN, NETSTATE_CLIDISCONN, NETSTATE_CLIENT, NETSTATE_INIT, NETSTATE_RCVCFG, NETSTATE_SERVER, networkState, players, RemoveFromConfigList(), SendDisconnectMessage(), and sock.
Referenced by SendDisconnectMessage(), and SendSpawnRequest().
Here is the call graph for this function:
void ProcessNetworkMessages | ( | ) |
Handles all network I/O for the client or server.
Receives incomming messages, services outgoing messages, and updates player data. If running as a server, also services the game configuration messages.
Definition at line 1708 of file message.c.
References frameTime, NETSTATE_RCVCFG, NETSTATE_SERVER, networkState, ReceiveMessages(), ServiceGameConfig(), ServiceMessages(), and UpdatePlayers().
Referenced by BfldErrRun(), GameRun(), MenuRun(), and SplashRun().
Here is the call graph for this function:
Bool ReceiveMessages | ( | ) |
Handles all incomming messages.
Definition at line 1485 of file message.c.
References SocketAddr_t::addr, NetBuffer_t::data, FALSE, GetNetErrorString(), GetNetErrorVal, inbuff, NetBuffer_t::length, SocketAddr_t::length, MAX_PLAYERS, MSG_ACK_SIZE, MSG_MAX_ACK, MSG_MAX_SIZE, MSG_READ_16C, MSG_TYPE_CONN_ACCEPT, MSG_TYPE_MAX, MSG_WRITE_16C, NETSTATE_CLIENT, NETSTATE_SERVER, NETSTATE_SRVDISCONN, networkState, rounddivup, SendAck(), sock, and TRUE.
Referenced by Disconnect(), DiscoverConnectServer(), and ProcessNetworkMessages().
Here is the call graph for this function:
Transmits the given message to the other end(s) and/or cancels the message.
For internal use only.
msg | The message to transmit. | |
inSys | True if the message has been added to the heap and tree tracking messages in the system. This value will be false when called from AddMessage() because the resend time will be set while in this function. |
Definition at line 458 of file message.c.
References AATreeRemove, SocketAddr_t::addr, MsgDescr_t::approveFunc, MsgDescr_t::bcast, MsgDescr_t::buffer, BufferFree(), MsgDescr_t::buffLifeUnmanaged, clients, NetBuffer_t::data, MsgDescr_t::destMap, DESTMAPLEN, DESTMAPTYPE, MsgDescr_t::failFunc, FALSE, ClientData_t::flags, MsgDescr_t::inflight, inflightId, MsgDescr_t::interval, SocketAddr_t::ipv4, SocketAddr_t::length, NetBuffer_t::length, localPlayer, MAX_PLAYERS, MessageFree(), MSG_TYPE_CONN_DISCONN, MsgDescr_t::msgId, NETSTATE_CLIDISCONN, NETSTATE_CLIENT, NETSTATE_RCVCFG, networkState, MsgDescr_t::nextSend, MsgDescr_t::noResend, outgoingAddr, sock, MsgDescr_t::triesLeft, and TRUE.
Referenced by AddMessage(), and ServiceMessages().
Here is the call graph for this function:
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.
The message is sent using the messaging system.
id | The client ID of the host leaving the game. For servers, the value should be zero. |
Definition at line 1367 of file message.c.
References MsgDescr_t::ackFunc, AddMessage(), MsgDescr_t::approveFunc, MsgDescr_t::bcast, MsgDescr_t::buffer, BufferNew(), NetBuffer_t::data, MsgDescr_t::destMap, MsgDescr_t::failFunc, FALSE, INIT_TRIES, NetBuffer_t::length, MessageFree(), MessageNew(), MSG_TYPE_CONN_DISCONN, MsgFailDropConnection(), NETSTATE_SERVER, networkState, SetDestToAll, MsgDescr_t::triesLeft, and TRUE.
Referenced by HandleConnDisconn(), MsgFailDisconnect(), MsgFailDropConnection(), NetDisconnect(), and NetUninit().
Here is the call graph for this function:
void ServiceMessages | ( | ) |
Services the outgoing messages that are already in the system.
This includes removing messages that have been acknowledged by all recipients, and resending messages that haven't.
Definition at line 692 of file message.c.
References AATreeFind(), AANode_t::data, inflightId, inflightTime, AANode_t::key, MsgDescr_t::msgId, NETSTATE_CONNECTING, NETSTATE_SERVER, networkState, MsgDescr_t::nextSend, Send(), TRUE, Vector_t::VectorFirst, Vector_t::VectorHeapPop(), and Vector_t::VectorHeapPush().
Referenced by Disconnect(), and ProcessNetworkMessages().
Here is the call graph for this function:
Bool ValidateClient | ( | SocketAddr * | origin, | |
Uint8 | clientId | |||
) |
Determines if incomming client data came from a valid source.
This function relies on data returned from socket functions of the operating system, and as such is suseptible to things like spoofing.
origin | The originating address of the data. | |
clientId | The reported identity of the client. |
Definition at line 743 of file message.c.
References ClientData_t::addr, clients, SocketAddr_t::EqualAddresses, FALSE, localPlayer, MAX_PLAYERS, and TRUE.
Referenced by HandleAck(), HandleConnDisconn(), HandlePlayerSpawn(), HandlePlayerUpdate(), and ServerTimeSyncThread().
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.
origin | The originating address of the data. |
Definition at line 761 of file message.c.
References SocketAddr_t::EqualAddresses, FALSE, NETSTATE_CLIDISCONN, NETSTATE_CLIENT, NETSTATE_RCVCFG, networkState, outgoingAddr, and TRUE.
Referenced by HandleAck(), HandleArea(), HandleConnDeny(), HandleConnDisconn(), HandleGameOption(), HandleObstacle(), HandlePlayerUpdate(), and HandleScoreUpdate().