Debian Codegraph @

netcon/mtclient/mtgametrack.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/mtclient/mtgametrack.cpp $
21 * $Revision: 1.1.1.1 $
22 * $Date: 2003-08-26 03:58:40 $
23 * $Author: kevinb $
24 *
25 * Game Tracker client code
26 *
27 * $Log: not supported by cvs2svn $
28 *
29 * 17 10/03/01 12:44a Kevin
30 * Smaller version of pxo packets
31 *
32 * 16 9/02/99 3:16p Kevin
33 * Macintosh byte ordering fixes
34 *
35 * 15 8/25/99 2:38p Samir
36 * added byteswap.h for INTEL_INT.
37 *
38 * 14 8/24/99 5:47p Kevin
39 * Macintosh crap
40 *
41 * 13 7/06/99 5:52p Kevin
42 * PXO & multiplayer fixes for the patch
43 *
44 * 12 5/05/99 5:23p Kevin
45 * fixed some bugs on the gamelist
46 *
47 * 11 5/04/99 7:29p Kevin
48 * Fixed bug that caused the server to send his game info twice whenever
49 * the level starts.
50 *
51 * 10 5/04/99 5:39p Kevin
52 * connection dll & pxo game tracking improvements (added server type to
53 * list)
54 *
55 * 9 4/19/99 8:35p Jeff
56 * Compile under Linux
57 *
58 * 8 3/25/99 3:26p Kevin
59 * Made PXO games be based on your chat channel
60 *
61 * 7 1/07/99 11:51a Kevin
62 * Added support for joining servers on alternate ports and hosting behind
63 * a proxy/firewall
64 *
65 * 6 12/30/98 12:16p Kevin
66 * Auto Mission Download system
67 *
68 * 5 12/23/98 6:38p Kevin
69 * All UDP data (except gamespy) now uses one (registered) port number
70 *
71 * 4 10/19/98 9:50a Kevin
72 * Added nettest application
73 *
74 * 3 7/27/98 5:31p Kevin
75 * Sound/Bitmap exchange system
76 *
77 * 2 6/01/98 10:10a Kevin
78 * Added DLL connection interface and auto update DLL
79 *
80 * 1 5/21/98 11:08a Kevin
81 *
82 * 6 5/14/98 11:07a Kevin
83 * Made gameover packet to the tracker reliable
84 *
85 * 5 5/04/98 12:53p Kevin
86 * Fixed mastertracker resend stuff
87 *
88 * 4 4/30/98 3:49p Kevin
89 * Mastertracker pilot stats
90 *
91 * 3 4/24/98 3:50p Kevin
92 * Added mastertracker game tracking support
93 *
94 * 2 4/23/98 4:49p Kevin
95 * Added Master tracker Game tracker support
96 *
97 * 1 4/23/98 3:26p Kevin
98 * Initial version
99 *
100 * $NoKeywords: $
101 */
102
103 /*
104 InitGameTracker(int game_type); //D3 or Freespace
105 //Call periodically so we can send our update, and make sure it is received
106 IdleGameTracker();
107 StartTrackerGame(void *buffer) //Call with a freespace_net_game_data or d3_net_game_data structure
108 //Call with a freespace_net_game_data or d3_net_game_data structure
109 //Updates our memory so when it is time, we send the latest game data
110 UpdateGameData(gamedata);
111 //Call to signify end of game
112 RequestGameList();//Sends a GNT_GAMELIST_REQ packet to the server.
113 game_list * GetGameList();//returns a pointer to a game_list struct
114
115 */
116 #ifdef WIN32
117 #include <windows.h>
118 #endif
119
120 #include <cstring>
121 #include <ctime>
122
123 #include "pstypes.h"
124 #include "networking.h"
125 #include "mtgametrack.h"
126 #include "byteswap.h"
127 #include "mt_net.h"
128
129 // extern float DLLtimer_GetTime(void);
130 typedef float (*timer_GetTime_fp)();
131
132 extern timer_GetTime_fp DLLtimer_GetTime;
133
134 typedef void (*HandleGamePacket_fp)(uint8_t *data, int len, network_address *from);
135
136 typedef int (*nw_RegisterCallback_fp)(HandleGamePacket_fp, uint8_t id);
137 extern nw_RegisterCallback_fp DLLnw_RegisterCallback;
138
139 typedef int (*nw_DoReceiveCallbacks_fp)();
140 extern nw_DoReceiveCallbacks_fp DLLnw_DoReceiveCallbacks;
141
142 #if (defined(LOGGER) && (!defined(RELEASE)))
143 #define DLLmprintf(...) DLLDebug_ConsolePrintf(__VA_ARGS__)
144 #else
145 #define DLLmprintf(...)
146 #endif
147
148 typedef void (*Debug_ConsolePrintf_fp)(int n, const char *format, ...);
149
150 extern Debug_ConsolePrintf_fp DLLDebug_ConsolePrintf;
151
152 // Variables
153
154 game_list GameBuffer[MAX_GAME_BUFFERS];
155 int GameType; // d3 or fs
156
157 float LastTrackerUpdate;
158 float LastSentToTracker;
159
160 float LastGameOverPacket;
161 float FirstGameOverPacket;
162
163 int SendingGameOver;
164
165 uint32_t TrackerAckdUs;
166 uint32_t TrackerGameIsRunning;
167
168 game_packet_header TrackerGameData;
169 game_packet_header GameListReq;
170 game_packet_header TrackAckPacket;
171 game_packet_header GameOverPacket;
172
173 game_packet_header inpacket;
174
175 d3_net_game_data_tiny *D3TrackerGameData;
176
177 extern int DLLnw_ListenPort;
178 extern uint16_t DLLPXOPort;
179 extern char *DLLPXO_hosted_lobby_name;
180
181 int InitGameTrackerClient(int gametype) {
182 GameType = gametype;
183 LastTrackerUpdate = 0;
184 switch (gametype) {
185 case GT_FREESPACE:
186 // TrackerGameData.len = GAME_HEADER_ONLY_SIZE+sizeof(freespace_net_game_data);
187 break;
188 case GT_DESCENT3:
189 TrackerGameData.len = INTEL_INT(GAME_HEADER_ONLY_SIZE + sizeof(d3_net_game_data_short));
190 break;
191 case GT_D3TNG:
192 TrackerGameData.len = INTEL_INT(GAME_HEADER_ONLY_SIZE + sizeof(d3_net_game_data_tiny));
193 break;
194 }
195 TrackerGameData.game_type = gametype; // 1==freespace (GT_FREESPACE), 2==D3, 3==tuberacer, etc.
196 TrackerGameData.type = INTEL_INT(GNT_GAMEUPDATE_TINY); // Used to specify what to do ie. Add a new net game
197 // (GNT_GAMESTARTED), remove a net game (game over), etc.
198
199 D3TrackerGameData = (d3_net_game_data_tiny *)&TrackerGameData.data;
200 if (DLLPXOPort) {
201 D3TrackerGameData->options = 1;
202 D3TrackerGameData->arg1 = DLLPXOPort;
203 }
204 GameListReq.game_type = gametype;
205 GameListReq.type = INTEL_INT(GNT_GAMELIST_LITE_FILTER_REQ);
206 GameListReq.len = INTEL_INT(GAME_HEADER_ONLY_SIZE);
207
208 TrackAckPacket.game_type = gametype;
209 TrackAckPacket.len = INTEL_INT(GAME_HEADER_ONLY_SIZE);
210 TrackAckPacket.type = INTEL_INT(GNT_CLIENT_ACK);
211
212 GameOverPacket.game_type = gametype;
213 GameOverPacket.len = INTEL_INT(GAME_HEADER_ONLY_SIZE);
214 GameOverPacket.type = INTEL_INT(GNT_GAMEOVER);
215
216 SendingGameOver = 0;
217
218 DLLnw_RegisterCallback(HandleGamePacket, PXO_NETID_GAME_TRACKER);
219 return 1;
220 }
221
222 void IdleGameTracker() {
223 DLLnw_DoReceiveCallbacks();
224 // uint32_t bytesin;
225 // mprintf(0,"IdleGameTracker() entered.\n");
226 if ((TrackerGameIsRunning) && ((DLLtimer_GetTime() - LastTrackerUpdate) > TRACKER_UPDATE_INTERVAL)) {
227
228 struct tm *newtime;
229 time_t aclock;
230 time(&aclock); /* Get time in seconds */
231 newtime = localtime(&aclock);
232 // Time to update the tracker again
233 DLLmprintf(0, "[%.24s] Updating the PXO game tracker!\n", asctime(newtime));
234 SendGameTrackerPacker(&TrackerGameData);
235 // sendto(gamesock,(const char *)&TrackerGameData,TrackerGameData.len,0,(SOCKADDR
236 //*)&gtrackaddr,sizeof(SOCKADDR_IN));
237 TrackerAckdUs = 0;
238 LastTrackerUpdate = DLLtimer_GetTime();
239 LastSentToTracker = DLLtimer_GetTime();
240 } else if ((TrackerGameIsRunning) && (!TrackerAckdUs) &&
241 ((DLLtimer_GetTime() - LastSentToTracker) > TRACKER_RESEND_TIME)) {
242 // We still haven't been acked by the last packet and it's time to resend.
243
244 struct tm *newtime;
245 time_t aclock;
246 time(&aclock); /* Get time in seconds */
247 newtime = localtime(&aclock);
248 // Time to update the tracker again
249 DLLmprintf(0, "[%.24s] (Resending) Updating the PXO game tracker!\n", asctime(newtime));
250
251 SendGameTrackerPacker(&TrackerGameData);
252 TrackerAckdUs = 0;
253 LastTrackerUpdate = DLLtimer_GetTime();
254 LastSentToTracker = DLLtimer_GetTime();
255 }
256
257 if (SendingGameOver) {
258 if ((DLLtimer_GetTime() - LastGameOverPacket) > TRACKER_RESEND_TIME) {
259 // resend
260 LastGameOverPacket = DLLtimer_GetTime();
261 SendGameTrackerPacker(&GameOverPacket);
262 } else if ((DLLtimer_GetTime() - FirstGameOverPacket) > TRACKER_TIMEOUT) {
263 // Giving up, it timed out.
264 SendingGameOver = 3;
265 }
266 }
267 }
268
269 void HandleGamePacket(uint8_t *data, int len, network_address *from) {
270
271 memcpy(&inpacket, data, sizeof(inpacket));
272
273 // Check to make sure the packets ok
274 if (len) {
275 switch (INTEL_INT(inpacket.type)) {
276 case GNT_SERVER_ACK:
277 // The server got our packet so we can stop sending now
278 TrackerAckdUs = 1;
279 if (SendingGameOver) {
280 SendingGameOver = 2;
281 }
282 break;
283 case GNT_GAMELIST_LITE_DATA:
284 if (len < (GAME_HEADER_ONLY_SIZE + sizeof(game_list))) {
285 DLLmprintf(0, "(%d) packet is too small!\n", len);
286 }
287 // Woohoo! Game data! put it in the buffer (if one's free)
288 // DLLmprintf(0,"GNT_GAMELIST_LITE_DATA received.\n");
289 for (int i = 0; i < MAX_GAME_BUFFERS; i++) {
290 if (GameBuffer[i].game_type == GT_UNUSED) {
291 memset(&GameBuffer[i], 0, sizeof(game_list));
292 memcpy(&GameBuffer[i], inpacket.data, len - GAME_HEADER_ONLY_SIZE);
293 i = MAX_GAME_BUFFERS + 1;
294 }
295 }
296 break;
297 }
298 // mprintf(0,"Sending ACK.\n");
299 AckPacket(inpacket.sig);
300 }
301 }
302
303 void UpdateGameData(void *buffer) {
304 if (GameType == GT_FREESPACE) {
305 // memcpy(FreeSpaceTrackerGameData,buffer,sizeof(freespace_net_game_data));
306 } else if (GameType == GT_DESCENT3) {
307 memcpy(D3TrackerGameData, buffer, sizeof(d3_net_game_data_short));
308 } else if (GameType == GT_D3TNG) {
309 memcpy(D3TrackerGameData, buffer, sizeof(d3_net_game_data_tiny));
310 }
311 }
312
313 game_list gl;
314
315 game_list *GetGameList() {
316 memset(&gl, 0, sizeof(gl));
317 for (auto & i : GameBuffer) {
318 if (i.game_type != GT_UNUSED) {
319 memcpy(&gl, &i, sizeof(gl));
320 i.game_type = GT_UNUSED;
321 return &gl;
322 }
323 }
324 return nullptr;
325 }
326
327 void RequestGameList() {
328 // Zero out all games
329 for (auto & i : GameBuffer) {
330 memset(&i, 0, sizeof(game_list));
331 }
332
333 int len = strlen(DLLPXO_hosted_lobby_name) + 1;
334 memcpy(GameListReq.data, DLLPXO_hosted_lobby_name, len);
335 GameListReq.len = INTEL_INT((uint32_t)(GAME_HEADER_ONLY_SIZE + len));
336 SendGameTrackerPacker(&GameListReq);
337 }
338
339 int SendGameOver() {
340 IdleGameTracker();
341
342 if (SendingGameOver == 3) {
343 // Request timed out.
344 return 2;
345 }
346 if (SendingGameOver == 2) {
347 SendingGameOver = 0;
348 return 1;
349 }
350 if (SendingGameOver == 1) {
351 // Wait until it's sent.
352 return 0;
353 }
354 if (SendingGameOver == 0) {
355 LastGameOverPacket = DLLtimer_GetTime();
356 FirstGameOverPacket = DLLtimer_GetTime();
357 SendingGameOver = 1;
358 TrackerGameIsRunning = 0;
359 SendGameTrackerPacker(&GameOverPacket);
360 return 0;
361 }
362 return 0;
363 }
364
365 void AckPacket(int sig) {
366 TrackAckPacket.sig = sig;
367 SendGameTrackerPacker(&TrackAckPacket);
368 }
369
370 void StartTrackerGame(void *buffer) {
371
372 struct tm *newtime;
373 time_t aclock;
374 time(&aclock); /* Get time in seconds */
375 newtime = localtime(&aclock);
376 // Time to update the tracker again
377 DLLmprintf(0, "[%.24s] Updating the PXO game tracker with a new game!\n", asctime(newtime));
378
379 if (GameType == GT_FREESPACE) {
380 // memcpy(FreeSpaceTrackerGameData,buffer,sizeof(freespace_net_game_data));
381 } else if (GameType == GT_DESCENT3) {
382 memcpy(D3TrackerGameData, buffer, sizeof(d3_net_game_data_short));
383 SendGameTrackerPacker(&TrackerGameData);
384 TrackerAckdUs = 0;
385 LastTrackerUpdate = DLLtimer_GetTime();
386 LastSentToTracker = DLLtimer_GetTime();
387
388 } else if (GameType == GT_D3TNG) {
389 memcpy(D3TrackerGameData, buffer, sizeof(d3_net_game_data_tiny));
390 SendGameTrackerPacker(&TrackerGameData);
391 TrackerAckdUs = 0;
392 LastTrackerUpdate = DLLtimer_GetTime();
393 LastSentToTracker = DLLtimer_GetTime();
394 }
395 TrackerGameIsRunning = 1;
396 // LastTrackerUpdate = 0;
397 }