Networking

Collaboration diagram for Networking:

Implements the networking portion of the game. More...


Files

file  client.h
 Definitions for server side data detailing the clients.
file  net.h
 Networking stuff.

Modules

 Network Game Configuration
 Implements the process of sending the game configuration from the server to all the clients, and receiving it on the client.
 Host Lookup
 A set of functions to perform asynchronous host lookups with the help of the I/O Thread.
 Messaging System
 A system used to send data, receive acknowledgment of sent data, and responding to acknowledgments or lack thereof.
 Time Synchronization
 A system of approximating the time the server is using to assist in having all clients show a game state that is as current as possible.

Data Structures

struct  ClientData_t
 Stores information on a client for use on the server. More...
struct  UpdateTimes_t
 Stores the time of the last acknowledged update for each of the different parts of the player data. More...

Defines

#define CloseSocket(s)   if (s != -1) { close(s); s = -1; }
 A macro used to close sockets.
#define GetNetErrorVal()   errno
 Retrives the error value from the last network operation.
#define MSG_READ_16(ptr, val)
 A macro to read a two byte number in binary form from a character buffer.
#define MSG_READ_16C(ptr, index, val)
 A macro to read a two byte value from a character buffer at a constant position within the buffer.
#define MSG_READ_32(ptr, val)
 A macro to read a four byte number in binary form from a character buffer.
#define MSG_READ_32C(ptr, index, val)
 A macro to read a four byte value from a character buffer at a constant position within the buffer.
#define MSG_READ_8(ptr, val)   (val) = *((ptr)++)
 A macro to read a byte from a character buffer.
#define MSG_WRITE_16(ptr, val)
 A macro to write a two byte numerical value in binary form to a character buffer.
#define MSG_WRITE_16C(ptr, index, val)
 A macro to write a two byte to a value character buffer at a constant position within the buffer.
#define MSG_WRITE_32(ptr, val)
 A macro to write a four byte numerical value in binary form to a character buffer.
#define MSG_WRITE_32C(ptr, index, val)
 A macro to write a four byte value to a character buffer at a constant position within the buffer.
#define MSG_WRITE_8(ptr, val)   *((ptr)++) = (val)
 A macro to write a byte to a character buffer.
#define MSG_WRITE_8C(ptr, index, val)   (ptr)[index] = (val)
 A macro to write a byte to a character buffer at a constant position within the buffer.
#define PORT_CLI   26002
 The port used by the client to receive everything but time sync messages.
#define PORT_CTSYNC   26003
 The port used by the client to send and receive time sync messages.
#define PORT_SRV   26000
 The port used by the server to receive everything but time sync messages.
#define PORT_STSYNC   26001
 The port used by the server to send receive time sync messages.

Enumerations

enum  {
  NETSTATE_INIT, NETSTATE_SEARCHING, NETSTATE_CONNECTING, NETSTATE_RCVCFG,
  NETSTATE_CLIENT, NETSTATE_CLIDISCONN, NETSTATE_SERVER, NETSTATE_SRVDISCONN,
  NETSTATE_SRVDENY, NETSTATE_DISCONN, NETSTATE_SEARCHERR, NETSTATE_SRVERR,
  NETSTATE_CONERR
}
 Codes for the operating state of the network module used with networkState. More...

Functions

const rtsa_charGetNetErrorString (int err)
 Returns a pointer to a string that briefly explains the last network error.
Bool NetCliToSrv ()
 Changes the network state from a client state to server.
void NetDisconnect ()
 Starts the process of disconnecting this client from the server, or disconnecting all clients from this server.
Bool NetInit (char *srv)
 Initalizes the networking system.
Bool NetInitSrv ()
 Initalizes the networking system and attempts to be a server without searching for one on the network.
Bool NetSrvToCli ()
 Changes the network state from server state to client.
void NetUninit ()
 Uninitalizes the networking system.
int UnicastSocket (void *addr, Bool block)
 Creates a new unicast ready UDP socket that is bound to the given address.

Variables

ClientDataclients
 An array of data on each client indexed by player ID.
int netError
 A gobal variable used to store network errors after cetrain events, such as during connection errors.
unsigned int networkState
 Contains the current operating state of the networking module.

Detailed Description

Implements the networking portion of the game.

Network Message Formats


Define Documentation

#define CloseSocket (  )     if (s != -1) { close(s); s = -1; }

A macro used to close sockets.

If the socket is -1, it will not be closed. This makes it safe to use the macro even when it is not known if the socket is already open.

Parameters:
s The socket to close.
Author:
Jeff Jackowski

Definition at line 70 of file net.h.

Referenced by ClientSync(), Disconnect(), DiscoverConnectServer(), MsgFailDropConnection(), NetCliToSrv(), NetSrvToCli(), NetUninit(), StopServerTimeSync(), and UnicastSocket().

 
#define GetNetErrorVal (  )     errno

Retrives the error value from the last network operation.

Warning:
To work with Windows, winsock2.h must be included. This can cause name conflicts with some functions, like RemoveMenu(), so winsock2.h is not included here.
Author:
Jeff Jackowski

Definition at line 393 of file net.h.

Referenced by ClientSync(), ConfigIPv4Broadcast(), DiscoverConnectServer(), NetCliToSrv(), ReceiveMessages(), and ServerTimeSyncThread().

#define MSG_READ_16 ( ptr,
val   ) 

Value:

(val) =  (Uint8)*((ptr)++) << 8; \
                               (val) |= (Uint8)*((ptr)++)
A macro to read a two byte number in binary form from a character buffer.

Parameters:
ptr The character pointer to the next readable spot in the buffer. The pointer will be incremented.
val The variable that will contain the value read from the buffer.
Author:
Jeff Jackowski

Definition at line 162 of file net.h.

Referenced by HandleAck(), HandleArea(), HandleObstacle(), HandleScoreUpdate(), and ReadPlayer().

#define MSG_READ_16C ( ptr,
index,
val   ) 

Value:

(val) =  ((Uint8*)(ptr))[index] << 8; \
                                      (val) |= ((Uint8*)(ptr))[index + 1]
A macro to read a two byte value from a character buffer at a constant position within the buffer.

Parameters:
ptr The character pointer to the buffer. The pointer will not be altered.
index The index that of the buffer with the first byte of val. The index will not be altered.
val The variable that will contain the value read from the buffer.
Author:
Jeff Jackowski

Definition at line 175 of file net.h.

Referenced by HandleGameOption(), and ReceiveMessages().

#define MSG_READ_32 ( ptr,
val   ) 

Value:

(val) =  (Uint8)*((ptr)++) << 24; \
                               (val) |= (Uint8)*((ptr)++) << 16; \
                               (val) |= (Uint8)*((ptr)++) << 8; \
                               (val) |= (Uint8)*((ptr)++)
A macro to read a four byte number in binary form from a character buffer.

Parameters:
ptr The character pointer to the next readable spot in the buffer. The pointer will be incremented.
val The variable that will contain the value read from the buffer.
Author:
Jeff Jackowski

Definition at line 185 of file net.h.

Referenced by HandlePlayerUpdate(), and ReadPlayer().

#define MSG_READ_32C ( ptr,
index,
val   ) 

Value:

(val) =  ((Uint8*)(ptr))[index] << 24; \
        (val) |= ((Uint8*)(ptr))[index + 1] << 16; \
        (val) |= ((Uint8*)(ptr))[index + 2] << 8; \
        (val) |= ((Uint8*)(ptr))[index + 3]
A macro to read a four byte value from a character buffer at a constant position within the buffer.

Parameters:
ptr The character pointer to the buffer. The pointer will not be altered.
index The index that of the buffer with the first byte of val. The index will not be altered.
val The variable that will contain the value read from the buffer.
Author:
Jeff Jackowski

Definition at line 200 of file net.h.

Referenced by GetSamples(), HandleConnAccept(), HandleConnReq(), and HandleGameOption().

#define MSG_READ_8 ( ptr,
val   )     (val) = *((ptr)++)

A macro to read a byte from a character buffer.

Parameters:
ptr The character pointer to the next readable spot in the buffer. The pointer will be incremented.
val The variable that will contain the value read from the buffer.
Author:
Jeff Jackowski

Definition at line 153 of file net.h.

Referenced by HandleArea(), HandleScoreUpdate(), and ReadPlayer().

#define MSG_WRITE_16 ( ptr,
val   ) 

Value:

*((ptr)++) = ((val) >> 8); \
                                *((ptr)++) = ((val) & 0xFF)
A macro to write a two byte numerical value in binary form to a character buffer.

Parameters:
ptr The character pointer to the next writable spot in the buffer. The pointer will be incremented.
val The value to write to the buffer.
Author:
Jeff Jackowski

Definition at line 102 of file net.h.

Referenced by AppendPlayerData(), MakeAreaBuffers(), MakeBuffers(), and SendScoreUpdate().

#define MSG_WRITE_16C ( ptr,
index,
val   ) 

Value:

(ptr)[index] = ((val) >> 8);\
                                         (ptr)[index + 1] = ((val) & 0xFF)
A macro to write a two byte to a value character buffer at a constant position within the buffer.

Parameters:
ptr The character pointer to the buffer. The pointer will not be altered.
index The index that of the buffer that will recieve the first byte of val. The index will not be altered.
val The value to write to the buffer.
Author:
Jeff Jackowski

Definition at line 115 of file net.h.

Referenced by AddMessage(), MakeBuffers(), and ReceiveMessages().

#define MSG_WRITE_32 ( ptr,
val   ) 

Value:

*((ptr)++) = ((val) >> 24); \
                                *((ptr)++) = ((val) >> 16) & 0xFF; \
                                *((ptr)++) = ((val) >> 8)  & 0xFF; \
                                *((ptr)++) = ((val) & 0xFF)
A macro to write a four byte numerical value in binary form to a character buffer.

Parameters:
ptr The character pointer to the next writable spot in the buffer. The pointer will be incremented.
val The value to write to the buffer.
Author:
Jeff Jackowski

Definition at line 126 of file net.h.

Referenced by AppendPlayerData(), and MakeBuffers().

#define MSG_WRITE_32C ( ptr,
index,
val   ) 

Value:

(ptr)[index] = ((val) >> 24); \
                                    (ptr)[index + 1] = ((val) >> 16) & 0xFF; \
                                    (ptr)[index + 2] = ((val) >> 8) & 0xFF; \
                                    (ptr)[index + 3] = ((val) & 0xFF)
A macro to write a four byte value to a character buffer at a constant position within the buffer.

Parameters:
ptr The character pointer to the buffer. The pointer will not be altered.
index The index that of the buffer that will recieve the first byte of val. The index will not be altered.
val The value to write to the buffer.
Author:
Jeff Jackowski

Definition at line 141 of file net.h.

Referenced by AppendPlayerData(), FailConnAccept(), GetSamples(), SendConnectMsg(), and ServerTimeSyncThread().

#define MSG_WRITE_8 ( ptr,
val   )     *((ptr)++) = (val)

A macro to write a byte to a character buffer.

Parameters:
ptr The character pointer to the next writable spot in the buffer. The pointer will be incremented.
val The value to write to the buffer.
Author:
Jeff Jackowski

Definition at line 80 of file net.h.

Referenced by AppendPlayerData(), MakeAreaBuffers(), and SendScoreUpdate().

#define MSG_WRITE_8C ( ptr,
index,
val   )     (ptr)[index] = (val)

A macro to write a byte to a character buffer at a constant position within the buffer.

Parameters:
ptr The character pointer to the buffer. The pointer will not be altered.
index The index that of the buffer that will recieve the first byte of val. The index will not be altered.
val The value to write to the buffer.
Author:
Jeff Jackowski

Definition at line 92 of file net.h.

#define PORT_CLI   26002

The port used by the client to receive everything but time sync messages.

Definition at line 51 of file net.h.

Referenced by ConfigIPv4Broadcast(), and DiscoverConnectServer().

#define PORT_CTSYNC   26003

The port used by the client to send and receive time sync messages.

It could have the same value as PORT_STSYNC, but that would prevent running a client and server on the same computer which is very convienient for development.

Definition at line 58 of file net.h.

Referenced by ClientSync(), and ServerTimeSyncThread().

#define PORT_SRV   26000

The port used by the server to receive everything but time sync messages.

Definition at line 43 of file net.h.

Referenced by ConfigIPv4Broadcast(), and DiscoverConnectServer().

#define PORT_STSYNC   26001

The port used by the server to send receive time sync messages.

Definition at line 47 of file net.h.

Referenced by ClientSync(), and StartServerTimeSync().


Enumeration Type Documentation

anonymous enum

Codes for the operating state of the network module used with networkState.

Enumerator:
NETSTATE_INIT  The inital state of the networking system indicating that it is ready to act as a client or transition to the server state.
NETSTATE_SEARCHING  Indicates that a server is being sought on the LAN.

This operation uses the I/O Thread.

NETSTATE_CONNECTING  Indicates that an attempt is underway to connect to a server.

This is used during the acknowledgment process. If the player selected a specific server to use, this code is also used during the initial request.

NETSTATE_RCVCFG  Indicates that process is acting as a game client and is receiving game configuation information in preperation for game play.
NETSTATE_CLIENT  Indicates this process is acting as a game client that is already connected to a server and has the game state data.
NETSTATE_CLIDISCONN  Indicates that the client process is attempting to disconnect from the server in a reliable way.

The operation uses the messaging system on the I/O Thread to implement the disconnect in a reliable way.

NETSTATE_SERVER  Indicates this process is acting as a game server.
NETSTATE_SRVDISCONN  Indicates that the server process is attempting to disconnect from all clients in a reliable way.

The operation uses the messaging system on the I/O Thread to implement the disconnect in a reliable way.

NETSTATE_SRVDENY  Indicates that the server contacted on the LAN has denied a connection.

Only valid with servers on the LAN because only one server per LAN is presently supported. The process will not transition to NETSTATE_SERVER while in this state. After a connection error with a server beyond the LAN, the state will be NETSTATE_CONERR instead of this state.

NETSTATE_DISCONN  Indicates that there is no connection.

This state will persist for some length of time to inform the user of the event, and so that a process that sends the disconnect message has some time to reliably inform the other host(s).

NETSTATE_SEARCHERR  Indicates an error occured during the attempt to locate a server.
NETSTATE_SRVERR  Indicates an error occured that prevented the process from acting as a server.
NETSTATE_CONERR  Indicates an error occured while attempting to connect to a server.

Definition at line 209 of file net.h.


Function Documentation

const rtsa_char* GetNetErrorString ( int  err  ) 

Returns a pointer to a string that briefly explains the last network error.

Parameters:
err The error code who's description is to be returned.
Returns:
A pointer to a constant string with the error description in terse English. The strings need to be changed to support other languages.
Author:
Jeff Jackowski

Definition at line 42 of file net.c.

References STRING_ERR_ADDRINUSE, STRING_ERR_BADF, STRING_ERR_CONNREFUSED, STRING_ERR_CONNRESET, STRING_ERR_DEFAULT, STRING_ERR_FAULT, STRING_ERR_HOSTUNREACH, STRING_ERR_INTR, STRING_ERR_INVAL, STRING_ERR_NETDOWN, STRING_ERR_NETUNREACH, STRING_ERR_NONE, STRING_ERR_NOTSOCK, STRING_ERR_WOULDBLOCK, and strings.

Referenced by ClientSync(), ConfigIPv4Broadcast(), NetStatUpdate(), ReceiveMessages(), and ServerTimeSyncThread().

Bool NetCliToSrv (  ) 

Changes the network state from a client state to server.

Postcondition:
The network state is set to NETSTATE_SERVER.
Todo:
Add IPv6 support.
Returns:
True on success, false on failure
Author:
Jeff Jackowski

Todo:
Complete configuring the server's local client.

Definition at line 471 of file net.c.

References ClientData_t::active, clients, CloseSocket, ConfigIPv4Broadcast(), ClientData_t::contact, FALSE, GetNetErrorVal, localPlayer, MAX_PLAYERS, MessageCliToSrv(), netError, NETSTATE_SERVER, NETSTATE_SRVERR, networkState, sock, StartServerTimeSync(), StopServerTimeSync(), and TRUE.

Referenced by DiscoverConnectServer(), and NetInitSrv().

Here is the call graph for this function:

void NetDisconnect (  ) 

Starts the process of disconnecting this client from the server, or disconnecting all clients from this server.

Postcondition:
The networkState is set to NETSTATE_CLIDISCONN or NETSTATE_SRVDISCONN and the disconnect task is queued on the I/O Thread.
Warning:
Since this uses the messaging system on another thread, the messaging system should not be used on any other thread until the network state changes.
Author:
Jeff Jackowski

Definition at line 448 of file net.c.

References CancelAllMessages(), CancelOperations(), Disconnect(), localPlayer, NETSTATE_CLIENT, NETSTATE_RCVCFG, NETSTATE_SERVER, networkState, QueueOperation(), SendDisconnectMessage(), and StopServerTimeSync().

Here is the call graph for this function:

Bool NetInit ( char *  srv  ) 

Initalizes the networking system.

Starts the process of searching for a server on the I/O Thread. Before it can be used as a server, a call must be made to NetCliToSrv(). After initialzing the networking system, it should be uninitialized before the process terminates.

Precondition:
The I/O Thread has already been started.
Postcondition:
The network state is set to NETSTATE_INIT.
Parameters:
srv The string with the hostname of the server to connect to, or NULL to discover a server on the LAN.
Returns:
True if successful, false on failure.
Author:
Jeff Jackowski

Definition at line 341 of file net.c.

References DiscoverConnectServer(), DoHostnameLookup(), MessageInit(), NETSTATE_INIT, networkState, and QueueOperation().

Referenced by main().

Here is the call graph for this function:

Bool NetInitSrv (  ) 

Initalizes the networking system and attempts to be a server without searching for one on the network.

After initialzing the networking system, it should be uninitialized before the process terminates.

Precondition:
The I/O Thread has already been started.
Postcondition:
The network state is set to NETSTATE_SERVER.
Returns:
True if successful, false on failure.
Author:
Jeff Jackowski

Definition at line 352 of file net.c.

References DoHostnameLookup(), MessageInit(), NetCliToSrv(), NETSTATE_INIT, networkState, and QueueOperation().

Referenced by main().

Here is the call graph for this function:

Bool NetSrvToCli (  ) 

Changes the network state from server state to client.

Postcondition:
The network state is set to NETSTATE_INIT.
Returns:
True on success, false on failure
Author:
Jeff Jackowski

Definition at line 508 of file net.c.

References clients, CloseSocket, MessageSrvToCli(), NETSTATE_INIT, NETSTATE_SRVDISCONN, networkState, sock, and StopServerTimeSync().

Referenced by Disconnect().

Here is the call graph for this function:

void NetUninit (  ) 

Uninitalizes the networking system.

No networking functions in this module may be called again until after a successful call to NetInit().

Author:
Jeff Jackowski

Definition at line 410 of file net.c.

References CancelAllMessages(), clients, CloseSocket, Disconnect(), localPlayer, MessageUninit(), NETSTATE_CLIDISCONN, NETSTATE_CLIENT, NETSTATE_RCVCFG, NETSTATE_SERVER, NETSTATE_SRVDISCONN, networkState, SendDisconnectMessage(), sock, and StopServerTimeSync().

Referenced by main().

Here is the call graph for this function:

int UnicastSocket ( void *  addr,
Bool  block 
)

Creates a new unicast ready UDP socket that is bound to the given address.

Parameters:
addr A pointer to a SockAddr, sockaddr_in, or sockaddr_in6 that contains the address to which the socket will be bound.
Warning:
The address family must be set correctly.
Parameters:
block True to create a blocking socket.
Returns:
The configured socket, or -1 on error.
Author:
Jeff Jackowski

Definition at line 188 of file net.c.

References CloseSocket, ConfigNonBlocking(), and sock.

Referenced by ClientSync(), DiscoverConnectServer(), and StartServerTimeSync().

Here is the call graph for this function:


Variable Documentation

ClientData* clients

An array of data on each client indexed by player ID.

This array is only allocated when the process is acting as a server, and all bytes are initialized to zero. When not a server, the pointer should be NULL. Defined in net.c.

Definition at line 93 of file net.c.

Referenced by AckConnAccept(), HandleConnDisconn(), HandleConnReq(), NetCliToSrv(), NetSrvToCli(), NetUninit(), Send(), SendAck(), and ValidateClient().

int netError

A gobal variable used to store network errors after cetrain events, such as during connection errors.

Most useful to pass an error condition from one thread to another since the operating system's error is thread local.

Definition at line 91 of file net.c.

Referenced by DiscoverConnectServer(), NetCliToSrv(), and NetStatUpdate().

unsigned int networkState

Contains the current operating state of the networking module.

Definition at line 95 of file net.c.

Referenced by AddMessage(), AppendPlayerData(), CalculateOffset(), Disconnect(), DiscoverConnectServer(), FixTankCollision(), FlagCfgMsg(), GameInit(), GameRun(), HandleAck(), HandleArea(), HandleConnAccept(), HandleConnDeny(), HandleConnDisconn(), HandleGameOption(), HandleInput(), HandleObstacle(), HandlePlayerSpawn(), HandlePlayerUpdate(), HandleScoreUpdate(), MsgFailDisconnect(), MsgFailDropConnection(), NetCliToSrv(), NetDisconnect(), NetInit(), NetInitSrv(), NetSrvToCli(), NetStatFocus(), NetStatUpdate(), NetUninit(), ProcessNetworkMessages(), ReadPlayer(), ReceiveMessages(), Send(), SendAck(), SendDisconnectMessage(), SendScoreUpdate(), ServiceMessages(), SpawnTank(), TeamSelInput(), and ValidateServer().


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