net.c File Reference

Networking stuff. More...

#include "iothread.h"
#include "random.h"
#include "message.h"
#include "timesync.h"
#include "net.h"
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>

Include dependency graph for net.c:

Go to the source code of this file.

Functions

static Bool ConfigIPv4Broadcast (Bool isServer)
 Opens a new socket (stored as sock) and configures it to broadcast over IPv4.
static Bool ConfigNonBlocking (int sock)
 Configures the socket to be non-blocking.
static void Disconnect (void *ignore)
 Performs a reliable disconnection from either the server or from all connected clients on the I/O Thread.
static void DiscoverConnectServer (void *srvstr)
 Attempts to connect to an existing server that is specified or to one that this function will discover.
const rtsa_charGetNetErrorString (int err)
 Returns a pointer to a string that briefly explains the last network error.
Bool MessageCliToSrv ()
 Transitions the messaging system from a client state (really anything other than the server state) to the server state.
Bool MessageSrvToCli ()
 Transitions the messaging system from the server state to the client state.
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.
static Bool SendConnectMsg (Bool bcast)
 Sends a connection message using the socket sock to the destination outgoingAddr.
int UnicastSocket (void *addr, Bool block)
 Creates a new unicast ready UDP socket that is bound to the given address.

Variables

Uint32 clientRandId
 The random ID generated by this process to uniquely identify it as a client before it has been assigned a client ID by the server.
ClientDataclients = NULL
 An array of data on each client indexed by player ID.
Uint32 localPlayer
int netError = 0
 A gobal variable used to store network errors after cetrain events, such as during connection errors.
unsigned int networkState = NETSTATE_SEARCHING
 Contains the current operating state of the networking module.
SocketAddr outgoingAddr
 The socket address of the server that the client is in communication with.
int sock = -1
 The socket used for communication.
char statHost [64]


Detailed Description

Networking stuff.

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 net.c.


Function Documentation

static Bool ConfigIPv4Broadcast ( Bool  isServer  )  [static]

Opens a new socket (stored as sock) and configures it to broadcast over IPv4.

Parameters:
isServer True if the socket should be configured with the server's port, false for the client's port.
Returns:
True on success, false on error.
Author:
Jeff Jackowski

Definition at line 146 of file net.c.

References SocketAddr_t::addr, ConfigNonBlocking(), FALSE, GetNetErrorString(), GetNetErrorVal, SocketAddr_t::ipv4, SocketAddr_t::length, outgoingAddr, PORT_CLI, PORT_SRV, and sock.

Referenced by DiscoverConnectServer(), and NetCliToSrv().

Here is the call graph for this function:

static Bool ConfigNonBlocking ( int  sock  )  [static]

Configures the socket to be non-blocking.

Parameters:
sock The socket to modify.
Returns:
True on success, false if the change could not be made.
Author:
Jeff Jackowski

Definition at line 119 of file net.c.

References FALSE.

Referenced by ConfigIPv4Broadcast(), and UnicastSocket().

static void Disconnect ( void *  ignore  )  [static]

Performs a reliable disconnection from either the server or from all connected clients on the I/O Thread.

For internal use only.

Precondition:
Only the disconnect message is in the messaging system.
Author:
Jeff Jackowski

Definition at line 370 of file net.c.

References CloseSocket, GetNumOutgoingMessages(), NetSrvToCli(), NETSTATE_INIT, NETSTATE_SRVDISCONN, networkState, ReceiveMessages(), ServiceMessages(), sock, TIMESYNC_IDLE, and timeSyncState.

Referenced by NetDisconnect(), and NetUninit().

Here is the call graph for this function:

static void DiscoverConnectServer ( void *  srvstr  )  [static]

Attempts to connect to an existing server that is specified or to one that this function will discover.

This function is intended to run on the I/O Thread. It calls ReceiveMessages(), so no other thread should use the Messaging System until this function returns. When networkState is no longer NETSTATE_SEARCHING, this function has ended.

For internal use only.

Postcondition:
If an error occurs, netError will be set to the value of the network error, or zero if the error was not network related.
Parameters:
srvstr The string with the hostname of the server to connect to, or NULL to discover a server on the LAN.
Author:
Jeff Jackowski

Todo:
Add support for IPv6.

Definition at line 252 of file net.c.

References clientRandId, CloseSocket, ConfigIPv4Broadcast(), FALSE, GetNetErrorVal, SocketAddr_t::ipv4, IsCancelationRequested(), SocketAddr_t::length, MAX_HOST_LEN, NetCliToSrv(), netError, NETSTATE_CLIENT, NETSTATE_CONERR, NETSTATE_CONNECTING, NETSTATE_RCVCFG, NETSTATE_SEARCHERR, NETSTATE_SEARCHING, NETSTATE_SRVDENY, networkState, outgoingAddr, PORT_CLI, PORT_SRV, RandNum(), ReceiveMessages(), SendConnectMsg(), sock, StartClientTimeSync(), StartNameLookup(), statHost, TRUE, and UnicastSocket().

Referenced by NetInit().

Here is the call graph for this function:

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 Bool SendConnectMsg ( Bool  bcast  )  [static]

Sends a connection message using the socket sock to the destination outgoingAddr.

Precondition:
The client's random ID is already stored in clientRandId.
Parameters:
bcast True if the connection message should be broadcast.
Returns:
True on success, false if the message could not be sent.
Author:
Jeff Jackowski

Definition at line 223 of file net.c.

References clientRandId, SocketAddr_t::length, MSG_TYPE_CONN_REQ, MSG_WRITE_32C, outgoingAddr, and sock.

Referenced by DiscoverConnectServer().


Variable Documentation

Uint32 clientRandId

The random ID generated by this process to uniquely identify it as a client before it has been assigned a client ID by the server.

Defined in net.c.

Definition at line 97 of file net.c.

Referenced by DiscoverConnectServer(), HandleConnAccept(), and SendConnectMsg().

Uint32 localPlayer

Definition at line 31 of file player.c.

Referenced by AddScoreNotice(), GameInit(), GameRun(), GetSamples(), HandleConnAccept(), HandleConnDisconn(), HandleInput(), MsgFailDisconnect(), NetCliToSrv(), NetDisconnect(), NetUninit(), ReadPlayer(), Send(), SendAck(), SendSpawnRequest(), UpdatePlayers(), and ValidateClient().

int sock = -1

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().

char statHost[64]

Definition at line 40 of file netstat.c.

Referenced by DiscoverConnectServer(), and NetStatUpdate().


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