UFO: Alien Invasion
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"
Include dependency graph for sv_main.cpp:

Go to the source code of this file.

Data Structures

struct  leakyBucket_t
 

Macros

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

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

◆ HEARTBEAT_SECONDS

#define HEARTBEAT_SECONDS   30

Definition at line 690 of file sv_main.cpp.

◆ MAX_BUCKETS

#define MAX_BUCKETS   16384

Definition at line 70 of file sv_main.cpp.

◆ MAX_HASHES

#define MAX_HASHES   1024

Definition at line 71 of file sv_main.cpp.

◆ PING_SECONDS

#define PING_SECONDS   5

Definition at line 791 of file sv_main.cpp.

Function Documentation

◆ Master_Heartbeat()

◆ Master_HeartbeatThread()

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_t::string.

Referenced by Master_Heartbeat().

◆ Master_Shutdown()

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_t::integer, masterserver_url, port, cvar_t::string, sv_dedicated, sv_public, and va().

Referenced by SV_Shutdown().

◆ Rcon_Validate()

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 password, Q_streq, Q_strnull(), rcon_password, and cvar_t::string.

Referenced by SVC_RemoteCommand().

◆ SV_CheckMaxSoldiersPerPlayer()

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

◆ SV_CheckSpawnSoldiers()

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, serverInstanceGame_t::spawned, sv, SV_ClientCommand(), and SV_GetNextClient().

Referenced by SV_Frame().

◆ SV_CheckStartMatch()

◆ SV_CheckTimeouts()

static void SV_CheckTimeouts ( void  )
static

◆ SV_Clear()

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

◆ SV_ConnectionlessPacket()

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

◆ SV_CountPlayers()

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_t::initialized, SV_GetNextClient(), and svs.

Referenced by Sys_ConsoleProc().

◆ SV_DropClient()

◆ SV_FinalMessage()

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(), SV_GetNextClient(), svc_disconnect, and svc_reconnect.

Referenced by SV_Shutdown().

◆ SV_Frame()

◆ SV_GetClient()

client_t * SV_GetClient ( int  index)

◆ SV_GetConfigString()

◆ SV_GetConfigStringInteger()

int SV_GetConfigStringInteger ( int  index)

Definition at line 108 of file sv_main.cpp.

References index, and SV_GetConfigString().

Referenced by SV_Map(), and SV_Status_f().

◆ SV_GetConfigStringLength()

◆ SV_GetMapData()

mapData_t * SV_GetMapData ( void  )

Definition at line 947 of file sv_main.cpp.

References serverInstanceGame_t::mapData, and sv.

Referenced by CL_RequestNextDownload().

◆ SV_GetMapTiles()

mapTiles_t * SV_GetMapTiles ( void  )

Definition at line 952 of file sv_main.cpp.

References serverInstanceGame_t::mapTiles, and sv.

Referenced by CL_RequestNextDownload().

◆ SV_GetNextClient()

client_t * SV_GetNextClient ( client_t lastClient)

◆ SV_Init()

◆ SV_PingPlayers()

static void SV_PingPlayers ( void  )
static

◆ SV_ReadPacket()

void SV_ReadPacket ( struct net_stream s)

◆ SV_SetConfigString()

◆ SV_Shutdown()

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_t::clients, Com_Printf(), cvar_t::flags, i, serverInstanceStatic_t::initialized, Master_Shutdown(), Mem_Free, sv_model_t::name, NET_DatagramSocketClose(), serverInstanceStatic_t::netDatagramSocket, serverInstanceGame_t::numSVModels, OBJZERO, serverInstanceStatic_t::serverMutex, sv, SV_FinalMessage(), sv_mapname, sv_maxclients, SV_ShutdownGameProgs(), SV_Stop(), serverInstanceGame_t::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().

◆ SV_ShutdownWhenEmpty()

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_t::abandon, Com_SetServerState(), ss_dead, and svs.

Referenced by GAME_GetImportData().

◆ SV_UserinfoChanged()

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 cl, Com_DPrintf(), DEBUG_SERVER, serverInstanceStatic_t::ge, i, Info_IntegerForKey(), Info_ValueForKey(), Q_strncpyz(), serverInstanceStatic_t::serverMutex, and svs.

Referenced by SV_ExecuteClientMessage(), and SVC_DirectConnect().

◆ SVC_BucketForAddress()

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

◆ SVC_DirectConnect()

◆ SVC_Info()

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_t::integer, MAX_INFO_STRING, MAX_VAR, serverInstanceGame_t::name, NET_OOB_Printf(), NET_StreamToString(), outboundLeakyBucket, PROTOCOL_VERSION, cvar_t::string, sv, SV_CMD_INFO, SV_CMD_PRINT, sv_dedicated, sv_gametype, SV_GetNextClient(), sv_hostname, sv_maxclients, SVC_RateLimit(), SVC_RateLimitAddress(), and UFO_VERSION.

Referenced by SV_ConnectionlessPacket().

◆ SVC_RateLimit()

static bool SVC_RateLimit ( leakyBucket_t bucket,
int  burst = 10,
int  period = 100 
)
static

◆ SVC_RateLimitAddress()

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 SVC_BucketForAddress(), and SVC_RateLimit().

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

◆ SVC_RemoteCommand()

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

◆ SVC_Status()

static void SVC_Status ( struct net_stream s)
static

◆ SVC_TeamInfo()

Variable Documentation

◆ bucketHashes

leakyBucket_t* bucketHashes[MAX_HASHES]
static

Definition at line 74 of file sv_main.cpp.

Referenced by SVC_BucketForAddress().

◆ buckets

leakyBucket_t buckets[MAX_BUCKETS]
static

Definition at line 73 of file sv_main.cpp.

Referenced by SVC_BucketForAddress().

◆ masterServerHeartBeatThread

SDL_Thread* masterServerHeartBeatThread
static

Definition at line 692 of file sv_main.cpp.

Referenced by Master_Heartbeat(), and Master_HeartbeatThread().

◆ outboundLeakyBucket

leakyBucket_t outboundLeakyBucket
static

Definition at line 75 of file sv_main.cpp.

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

◆ rcon_password

cvar_t* rcon_password
static

password for remote server commands

Definition at line 36 of file sv_main.cpp.

Referenced by Rcon_Validate(), and SV_Init().

◆ sv_dumpmapassembly

◆ sv_enablemorale

cvar_t* sv_enablemorale
static

Definition at line 38 of file sv_main.cpp.

Referenced by SV_Init().

◆ sv_genericPool

◆ sv_hostname

cvar_t* sv_hostname
static

Definition at line 41 of file sv_main.cpp.

Referenced by SV_Init(), and SVC_Info().

◆ sv_http_downloadserver

cvar_t* sv_http_downloadserver
static

Definition at line 37 of file sv_main.cpp.

Referenced by SV_Init(), and SVC_DirectConnect().

◆ sv_mapname

cvar_t* sv_mapname

Definition at line 53 of file sv_main.cpp.

Referenced by SV_Map(), and SV_Shutdown().

◆ sv_maxclients

◆ sv_maxsoldiersperplayer

cvar_t* sv_maxsoldiersperplayer
static

Definition at line 40 of file sv_main.cpp.

Referenced by SV_Init().

◆ sv_maxsoldiersperteam

cvar_t* sv_maxsoldiersperteam
static

Definition at line 39 of file sv_main.cpp.

Referenced by SV_Init().

◆ sv_public

◆ sv_reconnect_limit

cvar_t* sv_reconnect_limit
static

minimum seconds between connect messages

Definition at line 43 of file sv_main.cpp.

Referenced by SV_Init().

◆ sv_rma

◆ sv_rmadisplaythemap

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(), SV_AddMissingTiles_r(), and SV_Init().

◆ sv_threads

cvar_t* sv_threads

◆ sv_timeout

cvar_t* sv_timeout
static

Definition at line 44 of file sv_main.cpp.

Referenced by SV_CheckTimeouts(), and SV_Init().