server.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. // server.h
  19. #include "../game/q_shared.h"
  20. #include "../qcommon/qcommon.h"
  21. #include "../game/g_public.h"
  22. #include "../game/bg_public.h"
  23. //=============================================================================
  24. #define PERS_SCORE 0 // !!! MUST NOT CHANGE, SERVER AND
  25. // GAME BOTH REFERENCE !!!
  26. #define MAX_ENT_CLUSTERS 16
  27. typedef struct svEntity_s {
  28. struct worldSector_s *worldSector;
  29. struct svEntity_s *nextEntityInWorldSector;
  30. entityState_t baseline; // for delta compression of initial sighting
  31. int numClusters; // if -1, use headnode instead
  32. int clusternums[MAX_ENT_CLUSTERS];
  33. int lastCluster; // if all the clusters don't fit in clusternums
  34. int areanum, areanum2;
  35. int snapshotCounter; // used to prevent double adding from portal views
  36. } svEntity_t;
  37. typedef enum {
  38. SS_DEAD, // no map loaded
  39. SS_LOADING, // spawning level entities
  40. SS_GAME // actively running
  41. } serverState_t;
  42. typedef struct {
  43. serverState_t state;
  44. qboolean restarting; // if true, send configstring changes during SS_LOADING
  45. int serverId; // changes each server start
  46. int restartedServerId; // serverId before a map_restart
  47. int checksumFeed; // the feed key that we use to compute the pure checksum strings
  48. // https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=475
  49. // the serverId associated with the current checksumFeed (always <= serverId)
  50. int checksumFeedServerId;
  51. int snapshotCounter; // incremented for each snapshot built
  52. int timeResidual; // <= 1000 / sv_frame->value
  53. int nextFrameTime; // when time > nextFrameTime, process world
  54. struct cmodel_s *models[MAX_MODELS];
  55. char *configstrings[MAX_CONFIGSTRINGS];
  56. svEntity_t svEntities[MAX_GENTITIES];
  57. char *entityParsePoint; // used during game VM init
  58. // the game virtual machine will update these on init and changes
  59. sharedEntity_t *gentities;
  60. int gentitySize;
  61. int num_entities; // current number, <= MAX_GENTITIES
  62. playerState_t *gameClients;
  63. int gameClientSize; // will be > sizeof(playerState_t) due to game private data
  64. int restartTime;
  65. } server_t;
  66. typedef struct {
  67. int areabytes;
  68. byte areabits[MAX_MAP_AREA_BYTES]; // portalarea visibility bits
  69. playerState_t ps;
  70. int num_entities;
  71. int first_entity; // into the circular sv_packet_entities[]
  72. // the entities MUST be in increasing state number
  73. // order, otherwise the delta compression will fail
  74. int messageSent; // time the message was transmitted
  75. int messageAcked; // time the message was acked
  76. int messageSize; // used to rate drop packets
  77. } clientSnapshot_t;
  78. typedef enum {
  79. CS_FREE, // can be reused for a new connection
  80. CS_ZOMBIE, // client has been disconnected, but don't reuse
  81. // connection for a couple seconds
  82. CS_CONNECTED, // has been assigned to a client_t, but no gamestate yet
  83. CS_PRIMED, // gamestate has been sent, but client hasn't sent a usercmd
  84. CS_ACTIVE // client is fully in game
  85. } clientState_t;
  86. typedef struct netchan_buffer_s {
  87. msg_t msg;
  88. byte msgBuffer[MAX_MSGLEN];
  89. struct netchan_buffer_s *next;
  90. } netchan_buffer_t;
  91. typedef struct client_s {
  92. clientState_t state;
  93. char userinfo[MAX_INFO_STRING]; // name, etc
  94. char reliableCommands[MAX_RELIABLE_COMMANDS][MAX_STRING_CHARS];
  95. int reliableSequence; // last added reliable message, not necesarily sent or acknowledged yet
  96. int reliableAcknowledge; // last acknowledged reliable message
  97. int reliableSent; // last sent reliable message, not necesarily acknowledged yet
  98. int messageAcknowledge;
  99. int gamestateMessageNum; // netchan->outgoingSequence of gamestate
  100. int challenge;
  101. usercmd_t lastUsercmd;
  102. int lastMessageNum; // for delta compression
  103. int lastClientCommand; // reliable client message sequence
  104. char lastClientCommandString[MAX_STRING_CHARS];
  105. sharedEntity_t *gentity; // SV_GentityNum(clientnum)
  106. char name[MAX_NAME_LENGTH]; // extracted from userinfo, high bits masked
  107. // downloading
  108. char downloadName[MAX_QPATH]; // if not empty string, we are downloading
  109. fileHandle_t download; // file being downloaded
  110. int downloadSize; // total bytes (can't use EOF because of paks)
  111. int downloadCount; // bytes sent
  112. int downloadClientBlock; // last block we sent to the client, awaiting ack
  113. int downloadCurrentBlock; // current block number
  114. int downloadXmitBlock; // last block we xmited
  115. unsigned char *downloadBlocks[MAX_DOWNLOAD_WINDOW]; // the buffers for the download blocks
  116. int downloadBlockSize[MAX_DOWNLOAD_WINDOW];
  117. qboolean downloadEOF; // We have sent the EOF block
  118. int downloadSendTime; // time we last got an ack from the client
  119. int deltaMessage; // frame last client usercmd message
  120. int nextReliableTime; // svs.time when another reliable command will be allowed
  121. int lastPacketTime; // svs.time when packet was last received
  122. int lastConnectTime; // svs.time when connection started
  123. int nextSnapshotTime; // send another snapshot when svs.time >= nextSnapshotTime
  124. qboolean rateDelayed; // true if nextSnapshotTime was set based on rate instead of snapshotMsec
  125. int timeoutCount; // must timeout a few frames in a row so debugging doesn't break
  126. clientSnapshot_t frames[PACKET_BACKUP]; // updates can be delta'd from here
  127. int ping;
  128. int rate; // bytes / second
  129. int snapshotMsec; // requests a snapshot every snapshotMsec unless rate choked
  130. int pureAuthentic;
  131. qboolean gotCP; // TTimo - additional flag to distinguish between a bad pure checksum, and no cp command at all
  132. netchan_t netchan;
  133. // TTimo
  134. // queuing outgoing fragmented messages to send them properly, without udp packet bursts
  135. // in case large fragmented messages are stacking up
  136. // buffer them into this queue, and hand them out to netchan as needed
  137. netchan_buffer_t *netchan_start_queue;
  138. netchan_buffer_t **netchan_end_queue;
  139. } client_t;
  140. //=============================================================================
  141. // MAX_CHALLENGES is made large to prevent a denial
  142. // of service attack that could cycle all of them
  143. // out before legitimate users connected
  144. #define MAX_CHALLENGES 1024
  145. #define AUTHORIZE_TIMEOUT 5000
  146. typedef struct {
  147. netadr_t adr;
  148. int challenge;
  149. int time; // time the last packet was sent to the autherize server
  150. int pingTime; // time the challenge response was sent to client
  151. int firstTime; // time the adr was first used, for authorize timeout checks
  152. qboolean connected;
  153. } challenge_t;
  154. #define MAX_MASTERS 8 // max recipients for heartbeat packets
  155. // this structure will be cleared only when the game dll changes
  156. typedef struct {
  157. qboolean initialized; // sv_init has completed
  158. int time; // will be strictly increasing across level changes
  159. int snapFlagServerBit; // ^= SNAPFLAG_SERVERCOUNT every SV_SpawnServer()
  160. client_t *clients; // [sv_maxclients->integer];
  161. int numSnapshotEntities; // sv_maxclients->integer*PACKET_BACKUP*MAX_PACKET_ENTITIES
  162. int nextSnapshotEntities; // next snapshotEntities to use
  163. entityState_t *snapshotEntities; // [numSnapshotEntities]
  164. int nextHeartbeatTime;
  165. challenge_t challenges[MAX_CHALLENGES]; // to prevent invalid IPs from connecting
  166. netadr_t redirectAddress; // for rcon return messages
  167. netadr_t authorizeAddress; // for rcon return messages
  168. } serverStatic_t;
  169. //=============================================================================
  170. extern serverStatic_t svs; // persistant server info across maps
  171. extern server_t sv; // cleared each map
  172. extern vm_t *gvm; // game virtual machine
  173. #define MAX_MASTER_SERVERS 5
  174. extern cvar_t *sv_fps;
  175. extern cvar_t *sv_timeout;
  176. extern cvar_t *sv_zombietime;
  177. extern cvar_t *sv_rconPassword;
  178. extern cvar_t *sv_privatePassword;
  179. extern cvar_t *sv_allowDownload;
  180. extern cvar_t *sv_maxclients;
  181. extern cvar_t *sv_privateClients;
  182. extern cvar_t *sv_hostname;
  183. extern cvar_t *sv_master[MAX_MASTER_SERVERS];
  184. extern cvar_t *sv_reconnectlimit;
  185. extern cvar_t *sv_showloss;
  186. extern cvar_t *sv_padPackets;
  187. extern cvar_t *sv_killserver;
  188. extern cvar_t *sv_mapname;
  189. extern cvar_t *sv_mapChecksum;
  190. extern cvar_t *sv_serverid;
  191. extern cvar_t *sv_maxRate;
  192. extern cvar_t *sv_minPing;
  193. extern cvar_t *sv_maxPing;
  194. extern cvar_t *sv_gametype;
  195. extern cvar_t *sv_pure;
  196. extern cvar_t *sv_floodProtect;
  197. extern cvar_t *sv_lanForceRate;
  198. extern cvar_t *sv_strictAuth;
  199. //===========================================================
  200. //
  201. // sv_main.c
  202. //
  203. void SV_FinalMessage (char *message);
  204. void QDECL SV_SendServerCommand( client_t *cl, const char *fmt, ...);
  205. void SV_AddOperatorCommands (void);
  206. void SV_RemoveOperatorCommands (void);
  207. void SV_MasterHeartbeat (void);
  208. void SV_MasterShutdown (void);
  209. //
  210. // sv_init.c
  211. //
  212. void SV_SetConfigstring( int index, const char *val );
  213. void SV_GetConfigstring( int index, char *buffer, int bufferSize );
  214. void SV_SetUserinfo( int index, const char *val );
  215. void SV_GetUserinfo( int index, char *buffer, int bufferSize );
  216. void SV_ChangeMaxClients( void );
  217. void SV_SpawnServer( char *server, qboolean killBots );
  218. //
  219. // sv_client.c
  220. //
  221. void SV_GetChallenge( netadr_t from );
  222. void SV_DirectConnect( netadr_t from );
  223. void SV_AuthorizeIpPacket( netadr_t from );
  224. void SV_ExecuteClientMessage( client_t *cl, msg_t *msg );
  225. void SV_UserinfoChanged( client_t *cl );
  226. void SV_ClientEnterWorld( client_t *client, usercmd_t *cmd );
  227. void SV_DropClient( client_t *drop, const char *reason );
  228. void SV_ExecuteClientCommand( client_t *cl, const char *s, qboolean clientOK );
  229. void SV_ClientThink (client_t *cl, usercmd_t *cmd);
  230. void SV_WriteDownloadToClient( client_t *cl , msg_t *msg );
  231. //
  232. // sv_ccmds.c
  233. //
  234. void SV_Heartbeat_f( void );
  235. //
  236. // sv_snapshot.c
  237. //
  238. void SV_AddServerCommand( client_t *client, const char *cmd );
  239. void SV_UpdateServerCommandsToClient( client_t *client, msg_t *msg );
  240. void SV_WriteFrameToClient (client_t *client, msg_t *msg);
  241. void SV_SendMessageToClient( msg_t *msg, client_t *client );
  242. void SV_SendClientMessages( void );
  243. void SV_SendClientSnapshot( client_t *client );
  244. //
  245. // sv_game.c
  246. //
  247. int SV_NumForGentity( sharedEntity_t *ent );
  248. sharedEntity_t *SV_GentityNum( int num );
  249. playerState_t *SV_GameClientNum( int num );
  250. svEntity_t *SV_SvEntityForGentity( sharedEntity_t *gEnt );
  251. sharedEntity_t *SV_GEntityForSvEntity( svEntity_t *svEnt );
  252. void SV_InitGameProgs ( void );
  253. void SV_ShutdownGameProgs ( void );
  254. void SV_RestartGameProgs( void );
  255. qboolean SV_inPVS (const vec3_t p1, const vec3_t p2);
  256. //
  257. // sv_bot.c
  258. //
  259. void SV_BotFrame( int time );
  260. int SV_BotAllocateClient(void);
  261. void SV_BotFreeClient( int clientNum );
  262. void SV_BotInitCvars(void);
  263. int SV_BotLibSetup( void );
  264. int SV_BotLibShutdown( void );
  265. int SV_BotGetSnapshotEntity( int client, int ent );
  266. int SV_BotGetConsoleMessage( int client, char *buf, int size );
  267. int BotImport_DebugPolygonCreate(int color, int numPoints, vec3_t *points);
  268. void BotImport_DebugPolygonDelete(int id);
  269. //============================================================
  270. //
  271. // high level object sorting to reduce interaction tests
  272. //
  273. void SV_ClearWorld (void);
  274. // called after the world model has been loaded, before linking any entities
  275. void SV_UnlinkEntity( sharedEntity_t *ent );
  276. // call before removing an entity, and before trying to move one,
  277. // so it doesn't clip against itself
  278. void SV_LinkEntity( sharedEntity_t *ent );
  279. // Needs to be called any time an entity changes origin, mins, maxs,
  280. // or solid. Automatically unlinks if needed.
  281. // sets ent->v.absmin and ent->v.absmax
  282. // sets ent->leafnums[] for pvs determination even if the entity
  283. // is not solid
  284. clipHandle_t SV_ClipHandleForEntity( const sharedEntity_t *ent );
  285. void SV_SectorList_f( void );
  286. int SV_AreaEntities( const vec3_t mins, const vec3_t maxs, int *entityList, int maxcount );
  287. // fills in a table of entity numbers with entities that have bounding boxes
  288. // that intersect the given area. It is possible for a non-axial bmodel
  289. // to be returned that doesn't actually intersect the area on an exact
  290. // test.
  291. // returns the number of pointers filled in
  292. // The world entity is never returned in this list.
  293. int SV_PointContents( const vec3_t p, int passEntityNum );
  294. // returns the CONTENTS_* value from the world and all entities at the given point.
  295. void SV_Trace( trace_t *results, const vec3_t start, vec3_t mins, vec3_t maxs, const vec3_t end, int passEntityNum, int contentmask, int capsule );
  296. // mins and maxs are relative
  297. // if the entire move stays in a solid volume, trace.allsolid will be set,
  298. // trace.startsolid will be set, and trace.fraction will be 0
  299. // if the starting point is in a solid, it will be allowed to move out
  300. // to an open area
  301. // passEntityNum is explicitly excluded from clipping checks (normally ENTITYNUM_NONE)
  302. void SV_ClipToEntity( trace_t *trace, const vec3_t start, const vec3_t mins, const vec3_t maxs, const vec3_t end, int entityNum, int contentmask, int capsule );
  303. // clip to a specific entity
  304. //
  305. // sv_net_chan.c
  306. //
  307. void SV_Netchan_Transmit( client_t *client, msg_t *msg);
  308. void SV_Netchan_TransmitNextFragment( client_t *client );
  309. qboolean SV_Netchan_Process( client_t *client, msg_t *msg );