UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sv_main.cpp File Reference

Main server code? More...

#include "server.h"
#include "sv_log.h"
#include "../ports/system.h"
#include "../shared/scopedmutex.h"
#include "../shared/thread.h"

Go to the source code of this file.

Data Structures

struct  leakyBucket_s
 

Macros

#define MAX_BUCKETS   16384
 
#define MAX_HASHES   1024
 
#define HEARTBEAT_SECONDS   30
 
#define PING_SECONDS   5
 

Typedefs

typedef struct leakyBucket_s leakyBucket_t
 

Functions

char * SV_GetConfigString (int index)
 
int SV_GetConfigStringLength (int index)
 
int SV_GetConfigStringInteger (int index)
 
char * SV_SetConfigString (int index,...)
 
client_tSV_GetNextClient (client_t *lastClient)
 Iterates through clients. More...
 
client_tSV_GetClient (int index)
 
void SV_DropClient (client_t *drop, const char *message)
 Called when the player is totally leaving the server, either willingly or unwillingly. This is NOT called if the entire server is quitting or crashing. More...
 
static leakyBucket_tSVC_BucketForAddress (struct net_stream &address, int burst, int period)
 Find or allocate a bucket for an address. More...
 
static bool SVC_RateLimit (leakyBucket_t *bucket, int burst=10, int period=100)
 
static bool SVC_RateLimitAddress (struct net_stream &from, int burst=10, int period=1000)
 Rate limit for a particular address. More...
 
static void SVC_TeamInfo (struct net_stream *s)
 Responds with teaminfo such as free team num. More...
 
static void SVC_Status (struct net_stream *s)
 Responds with all the info that the server browser can see. More...
 
static void SVC_Info (struct net_stream *s)
 Responds with short info for broadcast scans. More...
 
static void SVC_DirectConnect (struct net_stream *stream)
 A connection request that did not come from the master. More...
 
static bool Rcon_Validate (const char *password)
 Checks whether the remote connection is allowed (rcon_password must be set on the server) - and verify the user given password with the cvar value. More...
 
static void SVC_RemoteCommand (struct net_stream *stream)
 A client issued an rcon command. Shift down the remaining args. Redirect all printfs. More...
 
static void SV_ConnectionlessPacket (struct net_stream *stream, dbuffer *msg)
 Handles a connectionless message from a client. More...
 
void SV_ReadPacket (struct net_stream *s)
 
static int Master_HeartbeatThread (void *data)
 Send a message to the master every few minutes to let it know we are alive, and log information. More...
 
static void Master_Heartbeat (void)
 
static void SV_CheckSpawnSoldiers (void)
 If all connected clients have set their ready flag the server will spawn the clients and that change the client state. More...
 
static void SV_CheckStartMatch (void)
 
static void SV_PingPlayers (void)
 
static void SV_CheckTimeouts (void)
 
void SV_Frame (int now, void *data)
 
static void Master_Shutdown (void)
 Informs all masters that this server is going down. More...
 
void SV_UserinfoChanged (client_t *cl)
 Pull specific info from a newly changed userinfo string into a more C friendly form. More...
 
static bool SV_CheckMaxSoldiersPerPlayer (cvar_t *cvar)
 
mapData_tSV_GetMapData (void)
 
mapTiles_tSV_GetMapTiles (void)
 
void SV_Init (void)
 Only called once at startup, not for each game. More...
 
static void SV_FinalMessage (const char *message, bool reconnect)
 Used by SV_Shutdown to send a final message to all connected clients before the server goes down. More...
 
void SV_Clear (void)
 Cleanup when the whole game process is shutting down. More...
 
void SV_Shutdown (const char *finalmsg, bool reconnect)
 Called when each game quits, before Sys_Quit or Sys_Error. More...
 
void SV_ShutdownWhenEmpty (void)
 Will eventually shutdown the server once all clients have disconnected. More...
 
int SV_CountPlayers (void)
 Returns the number of spawned players. More...
 

Variables

static cvar_trcon_password
 
static cvar_tsv_http_downloadserver
 
static cvar_tsv_enablemorale
 
static cvar_tsv_maxsoldiersperteam
 
static cvar_tsv_maxsoldiersperplayer
 
static cvar_tsv_hostname
 
static cvar_tsv_reconnect_limit
 
static cvar_tsv_timeout
 
cvar_tsv_maxclients = nullptr
 
cvar_tsv_dumpmapassembly
 
cvar_tsv_threads
 
cvar_tsv_rma
 
cvar_tsv_rmadisplaythemap
 display a character graphic of the tiles placed when RMA2 reaches a dead end. More...
 
cvar_tsv_public
 
cvar_tsv_mapname
 
memPool_tsv_genericPool
 
static leakyBucket_t buckets [MAX_BUCKETS]
 
static leakyBucket_tbucketHashes [MAX_HASHES]
 
static leakyBucket_t outboundLeakyBucket
 
static SDL_Thread * masterServerHeartBeatThread
 

Detailed Description

Main server code?

Definition in file sv_main.cpp.

Macro Definition Documentation

#define HEARTBEAT_SECONDS   30

Definition at line 690 of file sv_main.cpp.

Referenced by Master_Heartbeat().

#define MAX_BUCKETS   16384

Definition at line 70 of file sv_main.cpp.

Referenced by SVC_BucketForAddress().

#define MAX_HASHES   1024

Definition at line 71 of file sv_main.cpp.

Referenced by SVC_BucketForAddress().

#define PING_SECONDS   5

Definition at line 791 of file sv_main.cpp.

Referenced by SV_PingPlayers().

Typedef Documentation

typedef struct leakyBucket_s leakyBucket_t

Function Documentation

static int Master_HeartbeatThread ( void data)
static

Send a message to the master every few minutes to let it know we are alive, and log information.

Definition at line 698 of file sv_main.cpp.

References Com_Printf(), Com_sprintf(), HTTP_GetURL(), masterserver_url, masterServerHeartBeatThread, port, and cvar_s::string.

Referenced by Master_Heartbeat().

static void Master_Shutdown ( void  )
static

Informs all masters that this server is going down.

Definition at line 906 of file sv_main.cpp.

References HTTP_GetURL(), cvar_s::integer, masterserver_url, port, cvar_s::string, sv_dedicated, and va().

Referenced by SV_Shutdown().

static bool Rcon_Validate ( const char *  password)
inlinestatic

Checks whether the remote connection is allowed (rcon_password must be set on the server) - and verify the user given password with the cvar value.

Definition at line 569 of file sv_main.cpp.

References Q_streq, Q_strnull(), and cvar_s::string.

Referenced by SVC_RemoteCommand().

static bool SV_CheckMaxSoldiersPerPlayer ( cvar_t cvar)
static

Definition at line 941 of file sv_main.cpp.

References Cvar_AssertValue(), and MAX_ACTIVETEAM.

Referenced by SV_Init().

static void SV_CheckSpawnSoldiers ( void  )
static

If all connected clients have set their ready flag the server will spawn the clients and that change the client state.

See also
SV_Spawn_f

Definition at line 742 of file sv_main.cpp.

References cl, CL_SPAWNSOLDIERS, cs_began, cs_free, SrvPlayer::isReady(), client_s::player, serverInstanceGame_s::spawned, client_s::state, sv, SV_ClientCommand(), and SV_GetNextClient().

Referenced by SV_Frame().

static void SV_CheckTimeouts ( void  )
static
void SV_Clear ( void  )

Cleanup when the whole game process is shutting down.

See also
SV_Shutdown
Com_Quit

Definition at line 1030 of file sv_main.cpp.

References SV_LogShutdown(), and SV_MapcycleClear().

Referenced by Com_Quit().

static void SV_ConnectionlessPacket ( struct net_stream stream,
dbuffer msg 
)
static

Handles a connectionless message from a client.

See also
NET_OOB_Printf
Parameters
[out]streamThe stream to write to
msgThe message buffer to read the connectionless data from

Definition at line 638 of file sv_main.cpp.

References Cmd_Argv(), Cmd_TokenizeString(), Com_DPrintf(), Com_Printf(), DEBUG_SERVER, NET_ReadStringLine(), NET_StreamPeerToName(), Q_streq, SV_CMD_CONNECT, SV_CMD_INFO, SV_CMD_RCON, SV_CMD_STATUS, SV_CMD_TEAMINFO, SVC_DirectConnect(), SVC_Info(), SVC_RemoteCommand(), SVC_Status(), and SVC_TeamInfo().

Referenced by SV_ReadPacket().

int SV_CountPlayers ( void  )

Returns the number of spawned players.

See also
SV_ShutdownWhenEmpty

Definition at line 1096 of file sv_main.cpp.

References cl, count, cs_spawned, serverInstanceStatic_s::initialized, client_s::state, SV_GetNextClient(), and svs.

Referenced by Sys_ConsoleProc().

static void SV_FinalMessage ( const char *  message,
bool  reconnect 
)
static

Used by SV_Shutdown to send a final message to all connected clients before the server goes down.

See also
SV_Shutdown

Definition at line 1002 of file sv_main.cpp.

References cl, cs_connected, NET_StreamFinished(), NET_Wait(), NET_WriteByte(), NET_WriteConstMsg(), NET_WriteString(), client_s::state, client_s::stream, SV_GetNextClient(), svc_disconnect, and svc_reconnect.

Referenced by SV_Shutdown().

client_t* SV_GetClient ( int  index)
int SV_GetConfigStringInteger ( int  index)

Definition at line 108 of file sv_main.cpp.

References SV_GetConfigString().

Referenced by SV_Map(), and SV_Status_f().

mapData_t* SV_GetMapData ( void  )

Definition at line 947 of file sv_main.cpp.

References serverInstanceGame_s::mapData, and sv.

Referenced by CL_RequestNextDownload().

mapTiles_t* SV_GetMapTiles ( void  )

Definition at line 952 of file sv_main.cpp.

References serverInstanceGame_s::mapTiles, and sv.

Referenced by CL_RequestNextDownload().

client_t* SV_GetNextClient ( client_t lastClient)

Iterates through clients.

Parameters
[in]lastClientPointer of the client to iterate from. call with nullptr to get the first one.

Definition at line 152 of file sv_main.cpp.

References serverInstanceStatic_s::clients, cvar_s::integer, and svs.

Referenced by SV_BroadcastPrintf(), SV_CheckSpawnSoldiers(), SV_CheckStartMatch(), SV_CheckTimeouts(), SV_CountPlayers(), SV_DropClient(), SV_FinalMessage(), SV_GetPlayerClientStructure(), SV_Map(), SV_Multicast(), SV_PingPlayers(), SV_StartGame_f(), SV_Status_f(), SVC_DirectConnect(), SVC_Info(), SVC_Status(), and SVC_TeamInfo().

void SV_ReadPacket ( struct net_stream s)
void SV_Shutdown ( const char *  finalmsg,
bool  reconnect 
)

Called when each game quits, before Sys_Quit or Sys_Error.

Parameters
[in]finalmsgThe message all clients get as server shutdown message
[in]reconnectTrue if this is only a restart (new map or map restart), false if the server shutdown completely and you also want to disconnect all clients

Definition at line 1042 of file sv_main.cpp.

References serverInstanceStatic_s::clients, Com_Printf(), CVAR_LATCH, CVAR_NOSET, cvar_s::flags, i, serverInstanceStatic_s::initialized, Master_Shutdown(), Mem_Free, sv_model_s::name, NET_DatagramSocketClose(), serverInstanceStatic_s::netDatagramSocket, serverInstanceGame_s::numSVModels, OBJZERO, serverInstanceStatic_s::serverMutex, sv, SV_FinalMessage(), SV_ShutdownGameProgs(), SV_Stop(), serverInstanceGame_s::svModels, and svs.

Referenced by Com_Error(), Com_Quit(), Com_SetServerState(), GAME_Drop(), GAME_GetImportData(), Qcommon_Frame(), SV_Frame(), SV_KillServer_f(), and TEST_Shutdown().

void SV_ShutdownWhenEmpty ( void  )

Will eventually shutdown the server once all clients have disconnected.

See also
SV_CountPlayers

Definition at line 1085 of file sv_main.cpp.

References serverInstanceStatic_s::abandon, Com_SetServerState(), ss_dead, and svs.

Referenced by GAME_GetImportData().

void SV_UserinfoChanged ( client_t cl)

Pull specific info from a newly changed userinfo string into a more C friendly form.

Definition at line 921 of file sv_main.cpp.

References Com_DPrintf(), DEBUG_SERVER, serverInstanceStatic_s::ge, i, Info_IntegerForKey(), Info_ValueForKey(), client_s::messagelevel, client_s::name, client_s::player, Q_strncpyz(), serverInstanceStatic_s::serverMutex, svs, and client_s::userinfo.

Referenced by SV_ExecuteClientMessage(), and SVC_DirectConnect().

static leakyBucket_t* SVC_BucketForAddress ( struct net_stream address,
int  burst,
int  period 
)
static
static void SVC_Info ( struct net_stream s)
static

Responds with short info for broadcast scans.

Note
The second parameter should be the current protocol version number.
Only a short server description - the user can determine whether he is interested in a full status
See also
CL_ParseStatusMessage
CL_ProcessPingReply

Definition at line 411 of file sv_main.cpp.

References cl, Cmd_Argv(), Com_DPrintf(), Com_sprintf(), count, cs_spawning, DEBUG_SERVER, DOUBLEQUOTE, Info_SetValueForKey(), Info_SetValueForKeyAsInteger(), cvar_s::integer, MAX_INFO_STRING, MAX_VAR, serverInstanceGame_s::name, NET_OOB_Printf(), NET_StreamToString(), PROTOCOL_VERSION, client_s::state, cvar_s::string, sv, SV_CMD_INFO, SV_CMD_PRINT, sv_dedicated, sv_gametype, SV_GetNextClient(), SVC_RateLimit(), SVC_RateLimitAddress(), and UFO_VERSION.

Referenced by SV_ConnectionlessPacket().

static bool SVC_RateLimit ( leakyBucket_t bucket,
int  burst = 10,
int  period = 100 
)
static
static bool SVC_RateLimitAddress ( struct net_stream from,
int  burst = 10,
int  period = 1000 
)
static

Rate limit for a particular address.

Definition at line 313 of file sv_main.cpp.

References leakyBucket_s::burst, SVC_BucketForAddress(), and SVC_RateLimit().

Referenced by SVC_Info(), SVC_RemoteCommand(), SVC_Status(), and SVC_TeamInfo().

static void SVC_RemoteCommand ( struct net_stream stream)
static

A client issued an rcon command. Shift down the remaining args. Redirect all printfs.

Definition at line 585 of file sv_main.cpp.

References BAD_RCON_PASSWORD, Cmd_Argc(), Cmd_Argv(), Cmd_ExecuteString(), Com_BeginRedirect(), Com_DPrintf(), Com_EndRedirect(), Com_Printf(), DEBUG_SERVER, i, NET_StreamPeerToName(), Q_strcat(), Rcon_Validate(), SVC_RateLimit(), and SVC_RateLimitAddress().

Referenced by SV_ConnectionlessPacket().

Variable Documentation

leakyBucket_t* bucketHashes[MAX_HASHES]
static

Definition at line 74 of file sv_main.cpp.

leakyBucket_t buckets[MAX_BUCKETS]
static

Definition at line 73 of file sv_main.cpp.

SDL_Thread* masterServerHeartBeatThread
static

Definition at line 692 of file sv_main.cpp.

Referenced by Master_Heartbeat(), and Master_HeartbeatThread().

leakyBucket_t outboundLeakyBucket
static

Definition at line 75 of file sv_main.cpp.

cvar_t* rcon_password
static

password for remote server commands

Definition at line 36 of file sv_main.cpp.

cvar_t* sv_enablemorale
static

Definition at line 38 of file sv_main.cpp.

cvar_t* sv_hostname
static

Definition at line 41 of file sv_main.cpp.

cvar_t* sv_http_downloadserver
static

Definition at line 37 of file sv_main.cpp.

cvar_t* sv_mapname

Definition at line 53 of file sv_main.cpp.

Referenced by SV_Map().

cvar_t* sv_maxsoldiersperplayer
static

Definition at line 40 of file sv_main.cpp.

cvar_t* sv_maxsoldiersperteam
static

Definition at line 39 of file sv_main.cpp.

cvar_t* sv_reconnect_limit
static

minimum seconds between connect messages

Definition at line 43 of file sv_main.cpp.

cvar_t* sv_rma
cvar_t* sv_rmadisplaythemap

display a character graphic of the tiles placed when RMA2 reaches a dead end.

Definition at line 50 of file sv_main.cpp.

Referenced by RandomMapAssemblyTest::SetUpTestCase(), SV_AddMapTiles(), and SV_AddMissingTiles_r().

cvar_t* sv_threads

run the game lib threaded

Definition at line 48 of file sv_main.cpp.

Referenced by RandomMapAssemblyTest::SetUpTestCase(), SV_AddMapTiles(), SV_DoMapAssemble(), SV_InitGameProgs(), SV_ParallelSearch(), and TEST_F().

cvar_t* sv_timeout
static

Definition at line 44 of file sv_main.cpp.