File: gt2nat.c

package info (click to toggle)
openmohaa 0.82.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 34,192 kB
  • sloc: cpp: 315,720; ansic: 275,789; sh: 312; xml: 246; asm: 141; makefile: 7
file content (408 lines) | stat: -rw-r--r-- 9,848 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/******
gt2nat.c
GameSpy Transport 2 SDK 
  
Copyright 2000 GameSpy Industries, Inc

******

 This sample demonstrates sharing a UDP socket with the Query & Reporting 2 SDK 
 to enable developers to create games that can be hosted behind a NAT.

 Please see the GameSpy Query & Reporting 2 SDK documentation for more 
 information

******/


/********
INCLUDES
********/
#include "../gt2.h"
#include "../../qr2/qr2.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#if defined(_WIN32) && !defined(UNDER_CE)
#include <conio.h>
#endif

/********
DEFINES
********/
#define QR2_GAME_VERSION "2.00"
#define QR2_GAME_NAME "gmtest"
#define QR2_MAX_PLAYERS 32
#define QR2_BASE_PORT 26900
#define QR2_RANKINGSON_KEY 100

#ifdef _WIN32_WCE
void RetailOutputA(CHAR *tszErr, ...);
#define printf RetailOutputA
#endif

/********
TYPDEFS
********/
//representative of a game player structure
typedef struct
{
	char pname[80];
	int pfrags;
	int pdeaths;
	int pskill;
	int pping;
	char pteam[80];
} player_t;

//representative of a game data structure
typedef struct
{
	player_t players[QR2_MAX_PLAYERS];
	char mapname[20];
	char hostname[120];
	char gamemode[200];
	char gametype[30];
	int locationid;
	int numplayers;
	int maxplayers;
	int fraglimit;
	int timelimit;
	int teamplay;
	int rankingson;
	int hostport;
} gamedata_t;

/********
GLOBAL VARS
********/

//just to give us bogus data
char *constnames[QR2_MAX_PLAYERS]={"Joe Player","L33t 0n3","Raptor","Gr81","Flubber","Sarge","Void","runaway","Ph3ar","wh00t","gr1nder","Mace","stacy","lamby","Thrush"};
gamedata_t gamedata;

// Called when a server key needs to be reported
void serverkey_callback(int keyid, qr2_buffer_t outbuf, void *userdata)
{
	switch (keyid)
	{
	case HOSTNAME_KEY:
		qr2_buffer_add(outbuf, gamedata.hostname);
		break;
	case GAMEVER_KEY:
		qr2_buffer_add(outbuf, QR2_GAME_VERSION);
		break;
	case HOSTPORT_KEY:
		qr2_buffer_add_int(outbuf, gamedata.hostport);
		break;
	case MAPNAME_KEY:
		qr2_buffer_add(outbuf, gamedata.mapname);
		break;
	case GAMETYPE_KEY:
		qr2_buffer_add(outbuf, gamedata.gametype);
		break;
	case NUMPLAYERS_KEY:
		qr2_buffer_add_int(outbuf, gamedata.numplayers);
		break;
	case MAXPLAYERS_KEY:
		qr2_buffer_add_int(outbuf, gamedata.maxplayers);
		break;
	case GAMEMODE_KEY:
		qr2_buffer_add(outbuf, gamedata.gamemode);
		break;
	case TEAMPLAY_KEY:
		qr2_buffer_add_int(outbuf, gamedata.teamplay);
		break;
	case FRAGLIMIT_KEY:
		qr2_buffer_add_int(outbuf, gamedata.fraglimit);
		break;
	case TIMELIMIT_KEY:
		qr2_buffer_add_int(outbuf, gamedata.timelimit);
		break;
	case QR2_RANKINGSON_KEY:
		qr2_buffer_add_int(outbuf, gamedata.rankingson);
		break;
	default:
		qr2_buffer_add(outbuf, _T(""));
	}
	
	GSI_UNUSED(userdata);
}

// Called when a player key needs to be reported
void playerkey_callback(int keyid, int index, qr2_buffer_t outbuf, void *userdata)
{
	//check for valid index
	if (index >= gamedata.numplayers)
	{
		qr2_buffer_add(outbuf, _T(""));
		return;
	}
	switch (keyid)
	{
	case PLAYER__KEY:
		qr2_buffer_add(outbuf, gamedata.players[index].pname);
		break;
	case SKILL__KEY:
		qr2_buffer_add_int(outbuf, gamedata.players[index].pskill);
		break;
	case SCORE__KEY:
		qr2_buffer_add_int(outbuf, gamedata.players[index].pfrags);
		break;
	case DEATHS__KEY:
		qr2_buffer_add_int(outbuf, gamedata.players[index].pdeaths);
		break;
	case PING__KEY:
		qr2_buffer_add_int(outbuf, gamedata.players[index].pping);
		break;
	case TEAM__KEY:
		qr2_buffer_add(outbuf, gamedata.players[index].pteam);
		break;
	default:
		qr2_buffer_add(outbuf, _T(""));
		break;		
	}
	
	GSI_UNUSED(userdata);
}

// Called when a team key needs to be reported
void teamkey_callback(int keyid, int index, qr2_buffer_t outbuf, void *userdata)
{
	qr2_buffer_add(outbuf, _T(""));

	GSI_UNUSED(userdata);
	GSI_UNUSED(index);
	GSI_UNUSED(keyid);
}	

// Called when we need to report the list of keys we report values for
void keylist_callback(qr2_key_type keytype, qr2_keybuffer_t keybuffer, void *userdata)
{
	//need to add all the keys we support
	switch (keytype)
	{
	case key_server:
		qr2_keybuffer_add(keybuffer, HOSTNAME_KEY);
		qr2_keybuffer_add(keybuffer, GAMEVER_KEY);
		qr2_keybuffer_add(keybuffer, HOSTPORT_KEY);
		qr2_keybuffer_add(keybuffer, MAPNAME_KEY);
		qr2_keybuffer_add(keybuffer, GAMETYPE_KEY);
		qr2_keybuffer_add(keybuffer, NUMPLAYERS_KEY);
		qr2_keybuffer_add(keybuffer, NUMTEAMS_KEY);
		qr2_keybuffer_add(keybuffer, MAXPLAYERS_KEY);
		qr2_keybuffer_add(keybuffer, GAMEMODE_KEY);
		qr2_keybuffer_add(keybuffer, TEAMPLAY_KEY);
		qr2_keybuffer_add(keybuffer, FRAGLIMIT_KEY);
		qr2_keybuffer_add(keybuffer, TIMELIMIT_KEY);
		break;
	case key_player:
		qr2_keybuffer_add(keybuffer, PLAYER__KEY);
		qr2_keybuffer_add(keybuffer, SCORE__KEY);
		qr2_keybuffer_add(keybuffer, SKILL__KEY);
		qr2_keybuffer_add(keybuffer, DEATHS__KEY);
		qr2_keybuffer_add(keybuffer, PING__KEY);
		qr2_keybuffer_add(keybuffer, TEAM__KEY);
		break;
	case key_team:
		break;
	}
	
	GSI_UNUSED(userdata);
}

// Called when we need to report the number of players and teams
int count_callback(qr2_key_type keytype, void *userdata)
{
	if (keytype == key_player)
		return gamedata.numplayers;
	else if (keytype == key_team)
		return 0;
	else
		return 0;
		
	GSI_UNUSED(userdata);
}

// Called if our registration with the GameSpy master server failed
void adderror_callback(qr2_error_t error, gsi_char *errmsg, void *userdata)
{
	_tprintf(_T("Error adding server: %d, %s\n"), error, errmsg);
	
	GSI_UNUSED(userdata);
}

/***********
init_game
Initialize the sample data structures with bogus data
************/
static void init_game(void)
{
	int i;
	int team;

	srand((unsigned int) current_time() );
	gamedata.numplayers = rand() % 15;
	gamedata.maxplayers = QR2_MAX_PLAYERS;
	for (i = 0 ; i < gamedata.numplayers ; i++)
	{
		strcpy(gamedata.players[i].pname, constnames[i]);
		gamedata.players[i].pfrags = rand() % 32;
		gamedata.players[i].pdeaths = rand() % 32;
		gamedata.players[i].pskill = rand() % 1000;
		gamedata.players[i].pping = rand() % 500;
		team = rand() % 3;
		if (team == 0)
			strcpy(gamedata.players[i].pteam,"Red");
		else if (team == 1)
			strcpy(gamedata.players[i].pteam,"Blue");
		else if (team == 2)
			strcpy(gamedata.players[i].pteam,"");
	}
	strcpy(gamedata.mapname,"gmtmap1");
	strcpy(gamedata.gametype,"arena");
	strcpy(gamedata.hostname,"GameMaster Arena Server");
	strcpy(gamedata.gamemode,"openplaying");
	gamedata.fraglimit = 0;
	gamedata.timelimit = 40;
	gamedata.teamplay = 1;
	gamedata.locationid = 1;
	gamedata.rankingson = 1;
	gamedata.hostport = 25000;
}

/*******
 DoGameStuff
Simulate whatever else a game server does 
********/
void DoGameStuff(void)
{
	msleep(10);
}

GT2Bool UnrecognizedMessageCallback(GT2Socket socket, unsigned int ip, unsigned short port, GT2Byte * message, int len)
{
	static char buffer[8 * 1024];
	struct sockaddr_in saddr;

	if(!len || !message || ((message[0] != QR_MAGIC_1) && (message[1] != QR_MAGIC_2) && (message[0] != '\\')))
		return GT2False;

	// we want to make sure it is NUL-terminated
	len = min(len, (sizeof(buffer) - 1));
	memcpy(buffer, message, len);
	buffer[len] = '\0';

	memset(&saddr, 0, sizeof(saddr));
	saddr.sin_family = AF_INET;
	saddr.sin_addr.s_addr = ip;
	saddr.sin_port = htons(port);
	//qr_parse_query(NULL, buffer, len, (struct sockaddr *)&saddr);
	qr2_parse_query(NULL, buffer, len, (struct sockaddr *)&saddr);
	return GT2True;

	GSI_UNUSED(socket);
}

void ConnectAttemptCallback
(
	GT2Socket socket,
	GT2Connection connection,
	unsigned int ip,
	unsigned short port,
	int latency,
	GT2Byte * message,
	int len
)
{
	printf("Connection attempt from %s (%d ping)\n", gt2AddressToString(ip, port, NULL), latency);

	gt2Reject(connection, NULL, 0);

	GSI_UNUSED(len);
	GSI_UNUSED(message);
	GSI_UNUSED(socket);
}

/*******************
 main
Simulates a main program loop
First, initializes the Q&R items, then enters a main loop
*****************/
#if defined(_PS2)
int test_main(int argc, char **argp)
#else
int main(int argc, char **argp)
#endif
{
	char secret_key[9];
	GT2Socket socket;
	GT2Result result;
	int natNegotiate = 1;
	result = gt2CreateSocket(&socket, gt2AddressToString(0, QR2_BASE_PORT, NULL), 0, 0, NULL);
	if(result != GT2Success)
		return -1;

	gt2Listen(socket, ConnectAttemptCallback);
	
	//set the secret key, in a semi-obfuscated manner
	secret_key[0] = 'H';
	secret_key[1] = 'A';
	secret_key[2] = '6';
	secret_key[3] = 'z';
	secret_key[4] = 'k';
	secret_key[5] = 'S';
	secret_key[6] = '\0';
	
	qr2_register_key(QR2_RANKINGSON_KEY, _T("rankingson"));
	/*
	//call qr_init_socket with the socket and gamename
	if (qr_init_socket(NULL,gt2GetSocketSOCKET(socket), QR2_GAME_NAME, secret_key, basic_callback, 
		info_callback, rules_callback, players_callback, NULL) != 0)
	{
		printf("Error starting Q&R SDK\n");
		return -1;
	}
	*/

	// call the qr2_init_socket with the socket and gamename
	if (qr2_init_socket(NULL, gt2GetSocketSOCKET(socket), QR2_BASE_PORT, QR2_GAME_NAME, secret_key, 1, natNegotiate,
						serverkey_callback, playerkey_callback, teamkey_callback, keylist_callback, count_callback,
						adderror_callback, NULL) != e_qrnoerror)

	{
		printf("Error starting QR2 SDK\n");
		return -1;
	}
	// set the unrecognized message callback
	gt2SetUnrecognizedMessageCallback(socket, UnrecognizedMessageCallback);

	init_game();

	printf("Press any key to quit\n");
#if defined(_WIN32) && !defined(UNDER_CE)
	while (!_kbhit())
#else
	while (1)
#endif
	{
		DoGameStuff();
		//process our game networking
		gt2Think(socket);
		//check for / process incoming queries
		//qr_process_queries(NULL);
		qr2_think(NULL);
	}
	//let gamemaster know we are shutting down
	strcpy(gamedata.gamemode,"exiting");
	//qr_send_exiting(NULL);
	//qr_shutdown(NULL);
	qr2_shutdown(NULL);
	gt2CloseSocket(socket);
	return 0;

	GSI_UNUSED(argp);
	GSI_UNUSED(argc);
}