doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AsyncServer.h
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __ASYNCSERVER_H__
30 #define __ASYNCSERVER_H__
31 
32 /*
33 ===============================================================================
34 
35  Network Server for asynchronous networking.
36 
37 ===============================================================================
38 */
39 
40 // MAX_CHALLENGES is made large to prevent a denial of service attack that could cycle
41 // all of them out before legitimate users connected
42 const int MAX_CHALLENGES = 1024;
43 
44 // if we don't hear from authorize server, assume it is down
45 const int AUTHORIZE_TIMEOUT = 5000;
46 
47 // states for the server's authorization process
48 typedef enum {
49  CDK_WAIT = 0, // we are waiting for a confirm/deny from auth
50  // this is subject to timeout if we don't hear from auth
51  // or a permanent wait if auth said so
57 } authState_t;
58 
59 // states from the auth server, while the client is in CDK_WAIT
60 typedef enum {
61  AUTH_NONE = 0, // no reply yet
62  AUTH_OK, // this client is good
63  AUTH_WAIT, // wait - keep sending me srvAuth though
64  AUTH_DENY, // denied - don't send me anything about this client anymore
66 } authReply_t;
67 
68 // message from auth to be forwarded back to the client
69 // some are locally hardcoded to save space, auth has the possibility to send a custom reply
70 typedef enum {
71  AUTH_REPLY_WAITING = 0, // waiting on an initial reply from auth
72  AUTH_REPLY_UNKNOWN, // client unknown to auth
73  AUTH_REPLY_DENIED, // access denied
74  AUTH_REPLY_PRINT, // custom message
75  AUTH_REPLY_SRVWAIT, // auth server replied and tells us he's working on it
78 
79 typedef struct challenge_s {
80  netadr_t address; // client address
81  int clientId; // client identification
82  int challenge; // challenge code
83  int time; // time the challenge was created
84  int pingTime; // time the challenge response was sent to client
85  bool connected; // true if the client is connected
86  authState_t authState; // local state regarding the client
87  authReply_t authReply; // cd key check replies
88  authReplyMsg_t authReplyMsg; // default auth messages
89  idStr authReplyPrint; // custom msg
90  char guid[12]; // guid
91  int OS;
92 } challenge_t;
93 
94 typedef enum {
95  SCS_FREE, // can be reused for a new connection
96  SCS_ZOMBIE, // client has been disconnected, but don't reuse connection for a couple seconds
97  SCS_PUREWAIT, // client needs to update it's pure checksums before we can go further
98  SCS_CONNECTED, // client is connected
99  SCS_INGAME // client is in the game
101 
102 typedef struct serverClient_s {
103  int OS;
104  int clientId;
110 
113  int gameTime;
114 
125 
126  char guid[12]; // Even Balance - M. Quinn
127 
129 
130 
132 public:
133  idAsyncServer();
134 
135  bool InitPort( void );
136  void ClosePort( void );
137  void Spawn( void );
138  void Kill( void );
139  void ExecuteMapChange( void );
140 
141  int GetPort( void ) const;
142  netadr_t GetBoundAdr( void ) const;
143  bool IsActive( void ) const { return active; }
144  int GetDelay( void ) const { return gameTimeResidual; }
145  int GetOutgoingRate( void ) const;
146  int GetIncomingRate( void ) const;
147  bool IsClientInGame( int clientNum ) const;
148  int GetClientPing( int clientNum ) const;
149  int GetClientPrediction( int clientNum ) const;
150  int GetClientTimeSinceLastPacket( int clientNum ) const;
151  int GetClientTimeSinceLastInput( int clientNum ) const;
152  int GetClientOutgoingRate( int clientNum ) const;
153  int GetClientIncomingRate( int clientNum ) const;
154  float GetClientOutgoingCompression( int clientNum ) const;
155  float GetClientIncomingCompression( int clientNum ) const;
156  float GetClientIncomingPacketLoss( int clientNum ) const;
157  int GetNumClients( void ) const;
158  int GetNumIdleClients( void ) const;
159  int GetLocalClientNum( void ) const { return localClientNum; }
160 
161  void RunFrame( void );
162  void ProcessConnectionLessMessages( void );
163  void RemoteConsoleOutput( const char *string );
164  void SendReliableGameMessage( int clientNum, const idBitMsg &msg );
165  void SendReliableGameMessageExcluding( int clientNum, const idBitMsg &msg );
166  void LocalClientSendReliableMessage( const idBitMsg &msg );
167 
168  void MasterHeartbeat( bool force = false );
169  void DropClient( int clientNum, const char *reason );
170 
171  void PacifierUpdate( void );
172 
173  void UpdateUI( int clientNum );
174 
175  void UpdateAsyncStatsAvg( void );
176  void GetAsyncStatsAvgMsg( idStr &msg );
177 
178  void PrintLocalServerInfo( void );
179 
180 private:
181  bool active; // true if server is active
182  int realTime; // absolute time
183 
184  int serverTime; // local server time
185  idPort serverPort; // UDP port
186  int serverId; // server identification
187  int serverDataChecksum; // checksum of the data used by the server
188  int localClientNum; // local client on listen server
189 
190  challenge_t challenges[MAX_CHALLENGES]; // to prevent invalid IPs from connecting
193 
194  int gameInitId; // game initialization identification
195  int gameFrame; // local game frame
196  int gameTime; // local game time
197  int gameTimeResidual; // left over time from previous frame
198 
200 
203 
204  bool serverReloadingEngine; // flip-flop to not loop over when net_serverReloadEngine is on
205 
206  bool noRconOutput; // for default rcon response when command is silent
207 
208  int lastAuthTime; // global for auth server timeout
209 
210  // track the max outgoing rate over the last few secs to watch for spikes
211  // dependent on net_serverSnapshotDelay. 50ms, for a 3 seconds backlog -> 60 samples
212  static const int stats_numsamples = 60;
218 
219  void PrintOOB( const netadr_t to, int opcode, const char *string );
220  void DuplicateUsercmds( int frame, int time );
221  void ClearClient( int clientNum );
222  void InitClient( int clientNum, int clientId, int clientRate );
223  void InitLocalClient( int clientNum );
224  void BeginLocalClient( void );
225  void LocalClientInput( void );
226  void CheckClientTimeouts( void );
227  void SendPrintBroadcast( const char *string );
228  void SendPrintToClient( int clientNum, const char *string );
229  void SendUserInfoBroadcast( int userInfoNum, const idDict &info, bool sendToAll = false );
230  void SendUserInfoToClient( int clientNum, int userInfoNum, const idDict &info );
231  void SendSyncedCvarsBroadcast( const idDict &cvars );
232  void SendSyncedCvarsToClient( int clientNum, const idDict &cvars );
233  void SendApplySnapshotToClient( int clientNum, int sequence );
234  bool SendEmptyToClient( int clientNum, bool force = false );
235  bool SendPingToClient( int clientNum );
236  void SendGameInitToClient( int clientNum );
237  bool SendSnapshotToClient( int clientNum );
238  void ProcessUnreliableClientMessage( int clientNum, const idBitMsg &msg );
239  void ProcessReliableClientMessages( int clientNum );
240  void ProcessChallengeMessage( const netadr_t from, const idBitMsg &msg );
241  void ProcessConnectMessage( const netadr_t from, const idBitMsg &msg );
242  void ProcessRemoteConsoleMessage( const netadr_t from, const idBitMsg &msg );
243  void ProcessGetInfoMessage( const netadr_t from, const idBitMsg &msg );
244  bool ConnectionlessMessage( const netadr_t from, const idBitMsg &msg );
245  bool ProcessMessage( const netadr_t from, idBitMsg &msg );
246  void ProcessAuthMessage( const idBitMsg &msg );
247  bool SendPureServerMessage( const netadr_t to, int OS ); // returns false if no pure paks on the list
248  void ProcessPureMessage( const netadr_t from, const idBitMsg &msg );
249  int ValidateChallenge( const netadr_t from, int challenge, int clientId ); // returns -1 if validate failed
250  bool SendReliablePureToClient( int clientNum );
251  void ProcessReliablePure( int clientNum, const idBitMsg &msg );
252  bool VerifyChecksumMessage( int clientNum, const netadr_t *from, const idBitMsg &msg, idStr &reply, int OS ); // if from is NULL, clientNum is used for error messages
253  void SendReliableMessage( int clientNum, const idBitMsg &msg ); // checks for overflow and disconnects the faulty client
254  int UpdateTime( int clamp );
255  void SendEnterGameToClient( int clientNum );
256  void ProcessDownloadRequestMessage( const netadr_t from, const idBitMsg &msg );
257 };
258 
259 #endif /* !__ASYNCSERVER_H__ */
const int MAX_ASYNC_CLIENTS
Definition: AsyncNetwork.h:44
int GetIncomingRate(void) const
#define OS
void Kill(void)
void ProcessPureMessage(const netadr_t from, const idBitMsg &msg)
int nextAsyncStatsTime
Definition: AsyncServer.h:202
int GetNumIdleClients(void) const
bool IsActive(void) const
Definition: AsyncServer.h:143
serverClientState_t clientState
Definition: AsyncServer.h:105
netadr_t GetBoundAdr(void) const
void ExecuteMapChange(void)
int acknowledgeSnapshotSequence
Definition: AsyncServer.h:123
bool connected
Definition: AsyncServer.h:85
authState_t authState
Definition: AsyncServer.h:86
void Spawn(void)
bool VerifyChecksumMessage(int clientNum, const netadr_t *from, const idBitMsg &msg, idStr &reply, int OS)
void SendApplySnapshotToClient(int clientNum, int sequence)
void SendReliableGameMessageExcluding(int clientNum, const idBitMsg &msg)
idStr authReplyPrint
Definition: AsyncServer.h:89
int gameTimeResidual
Definition: AsyncServer.h:197
struct serverClient_s serverClient_t
void ProcessReliableClientMessages(int clientNum)
int serverDataChecksum
Definition: AsyncServer.h:187
usercmd_t userCmds[MAX_USERCMD_BACKUP][MAX_ASYNC_CLIENTS]
Definition: AsyncServer.h:192
bool IsClientInGame(int clientNum) const
void SendUserInfoBroadcast(int userInfoNum, const idDict &info, bool sendToAll=false)
serverClientState_t
Definition: AsyncServer.h:94
void PacifierUpdate(void)
int GetNumClients(void) const
const int MAX_CHALLENGES
Definition: AsyncServer.h:42
bool serverReloadingEngine
Definition: AsyncServer.h:204
bool ConnectionlessMessage(const netadr_t from, const idBitMsg &msg)
int ValidateChallenge(const netadr_t from, int challenge, int clientId)
struct challenge_s challenge_t
int UpdateTime(int clamp)
void CheckClientTimeouts(void)
int GetClientTimeSinceLastInput(int clientNum) const
bool ProcessMessage(const netadr_t from, idBitMsg &msg)
int stats_average_sum
Definition: AsyncServer.h:215
void ProcessReliablePure(int clientNum, const idBitMsg &msg)
serverClient_t clients[MAX_ASYNC_CLIENTS]
Definition: AsyncServer.h:191
authReplyMsg_t
Definition: AsyncServer.h:70
float GetClientOutgoingCompression(int clientNum) const
void SendSyncedCvarsBroadcast(const idDict &cvars)
const int AUTHORIZE_TIMEOUT
Definition: AsyncServer.h:45
void ProcessConnectMessage(const netadr_t from, const idBitMsg &msg)
bool InitPort(void)
int nextHeartbeatTime
Definition: AsyncServer.h:201
Definition: Dict.h:65
void SendSyncedCvarsToClient(int clientNum, const idDict &cvars)
void LocalClientInput(void)
void PrintOOB(const netadr_t to, int opcode, const char *string)
void ProcessGetInfoMessage(const netadr_t from, const idBitMsg &msg)
void ProcessUnreliableClientMessage(int clientNum, const idBitMsg &msg)
void SendUserInfoToClient(int clientNum, int userInfoNum, const idDict &info)
bool SendPingToClient(int clientNum)
challenge_t challenges[MAX_CHALLENGES]
Definition: AsyncServer.h:190
void SendPrintBroadcast(const char *string)
int GetDelay(void) const
Definition: AsyncServer.h:144
void BeginLocalClient(void)
void GetAsyncStatsAvgMsg(idStr &msg)
bool SendPureServerMessage(const netadr_t to, int OS)
int GetClientPrediction(int clientNum) const
void PrintLocalServerInfo(void)
authState_t
Definition: AsyncServer.h:48
bool SendReliablePureToClient(int clientNum)
int GetClientIncomingRate(int clientNum) const
idMsgChannel channel
Definition: AsyncServer.h:115
netadr_t rconAddress
Definition: AsyncServer.h:199
void SendEnterGameToClient(int clientNum)
static const int stats_numsamples
Definition: AsyncServer.h:212
char guid[12]
Definition: AsyncServer.h:90
void ProcessDownloadRequestMessage(const netadr_t from, const idBitMsg &msg)
authReply_t authReply
Definition: AsyncServer.h:87
void LocalClientSendReliableMessage(const idBitMsg &msg)
void RunFrame(void)
void SendReliableMessage(int clientNum, const idBitMsg &msg)
int GetClientOutgoingRate(int clientNum) const
void InitClient(int clientNum, int clientId, int clientRate)
int numDuplicatedUsercmds
Definition: AsyncServer.h:124
void SendReliableGameMessage(int clientNum, const idBitMsg &msg)
void DropClient(int clientNum, const char *reason)
void ProcessChallengeMessage(const netadr_t from, const idBitMsg &msg)
void UpdateUI(int clientNum)
int GetClientTimeSinceLastPacket(int clientNum) const
void ProcessRemoteConsoleMessage(const netadr_t from, const idBitMsg &msg)
void ProcessAuthMessage(const idBitMsg &msg)
Definition: Str.h:116
int GetLocalClientNum(void) const
Definition: AsyncServer.h:159
float GetClientIncomingCompression(int clientNum) const
float GetClientIncomingPacketLoss(int clientNum) const
authReplyMsg_t authReplyMsg
Definition: AsyncServer.h:88
bool SendSnapshotToClient(int clientNum)
int stats_outrate[stats_numsamples]
Definition: AsyncServer.h:213
void SendPrintToClient(int clientNum, const char *string)
int GetPort(void) const
void UpdateAsyncStatsAvg(void)
bool SendEmptyToClient(int clientNum, bool force=false)
void MasterHeartbeat(bool force=false)
authReply_t
Definition: AsyncServer.h:60
void RemoteConsoleOutput(const char *string)
const int MAX_USERCMD_BACKUP
Definition: AsyncNetwork.h:46
void ClearClient(int clientNum)
void InitLocalClient(int clientNum)
void ClosePort(void)
void SendGameInitToClient(int clientNum)
char guid[12]
Definition: AsyncServer.h:126
netadr_t address
Definition: AsyncServer.h:80
idPort serverPort
Definition: AsyncServer.h:185
int GetClientPing(int clientNum) const
void ProcessConnectionLessMessages(void)
void DuplicateUsercmds(int frame, int time)
int GetOutgoingRate(void) const