File: gt2aServer.c

package info (click to toggle)
openmohaa 0.81.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: trixie
  • size: 29,124 kB
  • sloc: ansic: 270,865; cpp: 250,173; sh: 234; asm: 141; xml: 64; makefile: 7
file content (746 lines) | stat: -rw-r--r-- 15,353 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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/*
GameSpy GT2 SDK
GT2Action - sample app
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 2000 GameSpy Industries, Inc

*/

#include "gt2aMain.h"
#include "gt2aServer.h"
#include "gt2aParse.h"
#include "gt2aMath.h"
#include "gt2aSound.h"
#include "gt2aLogic.h"

#define SERVER_THINK_TIME      30

static GT2Socket Socket;
Client clients[MAX_CLIENTS];
static int numClients;
static GT2ConnectionCallbacks connectionCallbacks;
static unsigned short nextServerUpdateID;

static void Broadcast
(
	const char * message,
	int len,
	int exceptIndex,
	GT2Bool reliable
)
{
	int i;

	for(i = 0 ; i < MAX_CLIENTS ; i++)
		if(clients[i].used && (i != exceptIndex))
			gt2Send(clients[i].connection, (const GT2Byte *)message, len, reliable);
}

void BroadcastText
(
	const char * message,
	int exceptIndex,
	GT2Bool reliable
)
{
	static char messageCopy[CHAT_MAX];
	static char buffer[256];
	char * slash;
	int rcode;

	// Copy off the chat message.
	/////////////////////////////
	strncpy(messageCopy, message, CHAT_MAX);
	messageCopy[CHAT_MAX - 1] = '\0';

	// Because of the way gt2a is sending messages
	// we need to change \ to /.
	//////////////////////////////////////////////
	while(slash = strchr(messageCopy, '\\'))
		*slash = '/';

	// Encode the message.
	//////////////////////
	rcode = gtEncode(MSG_S_CHAT, MSG_S_CHAT_STR, buffer, sizeof(buffer),
		messageCopy);
	if(rcode != -1)
	{
		// Send this chat message out to everyone.
		//////////////////////////////////////////
		Broadcast(buffer, rcode, -1, reliable);
	}
}

static int AddClient
(
	void
)
{
	int i;

	// Find an open slot.
	/////////////////////
	for(i = 0 ; i < MAX_CLIENTS ; i++)
		if(!clients[i].used)
			break;

	// Nothing open?
	////////////////
	if(i == MAX_CLIENTS)
		return -1;

	// Clear it.
	////////////
	memset(&clients[i], 0, sizeof(Client));
	clients[i].index = i;
	clients[i].used = GT2True;

	// One more client.
	///////////////////
	numClients++;
	assert(numClients <= MAX_CLIENTS);

	return i;
}

static void RemoveClient
(
	int clientIndex 
)
{
	// Not in use anymore.
	//////////////////////
	clients[clientIndex].used = GT2False;

	// One less client.
	///////////////////
	numClients--;
	assert(numClients >= 0);
}

static void SendAddClient
(
	int sendClientIndex,
	int newClientIndex
)
{
	char buffer[256];
	Client * sendClient;
	Client * newClient;
	int rcode;

	assert((sendClientIndex >= 0) && (sendClientIndex < MAX_CLIENTS));
	sendClient = &clients[sendClientIndex];
	assert(sendClient->used);
	assert(sendClient->connection);

	assert((newClientIndex >= 0) && (newClientIndex < MAX_CLIENTS));
	newClient = &clients[newClientIndex];
	assert(newClient->used);
	assert(newClient->connection);

	// Encode the message.
	/////////////////////
	rcode = gtEncode(MSG_S_ADDCLIENT, MSG_S_ADDCLIENT_STR, buffer, sizeof(buffer),
		newClientIndex,
		newClient->nick);
	if(rcode != -1)
	{
		// Send the message.
		////////////////////
		gt2Send(sendClient->connection, (const GT2Byte *)buffer, rcode, GT2True);
	}
}

static void SendDelClient
(
	int sendClient,
	int delClient
)
{
	char buffer[32];
	int rcode;

	assert((sendClient >= 0) && (sendClient < MAX_CLIENTS));
	assert(clients[sendClient].used);
	assert(clients[sendClient].connection);
	assert((delClient >= 0) && (delClient < MAX_CLIENTS));

	// Encode the message.
	/////////////////////
	rcode = gtEncode(MSG_S_DELCLIENT, MSG_S_DELCLIENT_STR, buffer, sizeof(buffer),
		delClient);
	if(rcode != -1)
	{
		// Send the message.
		////////////////////
		gt2Send(clients[sendClient].connection, (const GT2Byte *)buffer, rcode, GT2True);
	}
}

static void SendStart
(
	int sendClient
)
{
	char buffer[32];
	int rcode;

	assert((sendClient >= 0) && (sendClient < MAX_CLIENTS));
	assert(clients[sendClient].used);
	assert(clients[sendClient].connection);

	// Encode the message.
	/////////////////////
	rcode = gtEncode(MSG_S_START, MSG_S_START_STR, buffer, sizeof(buffer),
		sendClient);
	if(rcode != -1)
	{
		// Send the message.
		////////////////////
		gt2Send(clients[sendClient].connection, (const GT2Byte *)buffer, rcode, GT2True);
	}
}

void SendSound
(
	int sound,
	int sendClient
)
{
	char buffer[32];
	int rcode;

	assert(sendClient >= -1);
	assert(sendClient < MAX_CLIENTS);
#ifdef _DEBUG
	if(sendClient != -1)
	{
		assert(clients[sendClient].used);
		assert(clients[sendClient].connection);
	}
#endif

	// Encode the message.
	/////////////////////
	rcode = gtEncode(MSG_S_SOUND, MSG_S_SOUND_STR, buffer, sizeof(buffer),
		sound);
	if(rcode != -1)
	{
		// Send the message.
		////////////////////
		if(sendClient == -1)
			Broadcast(buffer, rcode, -1, GT2False);
		else
			gt2Send(clients[sendClient].connection, (const GT2Byte *)buffer, rcode, GT2False);
	}
}

void SendNumAsteroids
(
	int total,
	int sendClient
)
{
	char buffer[32];
	int rcode;

	assert(sendClient >= -1);
	assert(sendClient < MAX_CLIENTS);
	assert(clients[sendClient].used);
	assert(clients[sendClient].connection);
	assert(total >= 0);
	assert(total <= PLAYER_MAX_ASTEROIDS);

	// Encode the message.
	/////////////////////
	rcode = gtEncode(MSG_S_NUMASTEROIDS, MSG_S_NUMASTEROIDS_STR, buffer, sizeof(buffer),
		(byte)total);;
	if(rcode != -1)
	{
		// Send the message.
		////////////////////
		gt2Send(clients[sendClient].connection, (const GT2Byte *)buffer, rcode, GT2True);
	}
}

static void SendUpdates
(
	void
)
{
	int i;
	char * buffer;
	int len;
	static char update[1024 * 8];
	Client * client;
	SObject * object;
	unsigned long now;
	int rcode;

	// Get the current time.
	////////////////////////
	now = current_time();

	// Setup the encoding parameters.
	/////////////////////////////////
	buffer = update;
	len = sizeof(update);

	// First encode the header.
	///////////////////////////
	rcode = gtEncode(MSG_S_UPDATE, MSG_S_UPDATE_STR, buffer, len,
		nextServerUpdateID++,
		numClients,
		numObjects);
	if(rcode == -1)
		return;
	buffer += rcode;
	len -= rcode;

	// Encode each client.
	//////////////////////
	for(i = 0 ; i < MAX_CLIENTS ; i++)
	{
		if(clients[i].used)
		{
			client = &clients[i];

			rcode = gtEncodeNoType(MSG_S_UPDATE_CLIENT_STR, buffer, len,
				i,
				PositionToUnsignedShort(client->position[0]),
				PositionToUnsignedShort(client->position[1]),
				RotationToUnsignedShort(client->rotation),
				client->score,
				client->motion == FORWARD ? 1 : 0,
				client->motion == BACKWARD ? 1 : 0,
				client->turning == RIGHT ? 1 : 0,
				client->turning == LEFT ? 1 : 0,
				client->dead);
			if(rcode == -1)
				return;
			buffer += rcode;
			len -= rcode;
		}
	}

	// Encode each object.
	//////////////////////
	for(i = 0 ; i < MAX_OBJECTS ; i++)
	{
		if(sObjects[i].used)
		{
			object = &sObjects[i];

			rcode = gtEncodeNoType(MSG_S_UPDATE_OBJECT_STR, buffer, len,
				object->type,
				PositionToUnsignedShort(object->position[0]),
				PositionToUnsignedShort(object->position[1]),
				RotationToUnsignedShort(object->rotation),
				(now - object->startTime));
			if(rcode == -1)
				return;
			buffer += rcode;
			len -= rcode;
		}
	}

	// Send the update.
	///////////////////
	Broadcast(update, sizeof(update) - len, -1, GT2False);
}

static void ServerReceivedCallback
(
	GT2Connection connection,
	GT2Byte * message,
	int len,
	GT2Bool reliable
)
{
	int clientIndex;
	Client * client;
	GTMessageType type;
	int rcode;

	// Check for no message.
	////////////////////////
	if(!message)
		return;

	// Which client was this?
	/////////////////////////
	clientIndex = (int)gt2GetConnectionData(connection);
	assert((clientIndex >= 0) && (clientIndex < MAX_CLIENTS));
	if((clientIndex < 0) || (clientIndex >= MAX_CLIENTS))
		return;
	assert(clients[clientIndex].used);
	if(!clients[clientIndex].used)
		return;
	client = &clients[clientIndex];

	// Get the message type.
	////////////////////////
	type = gtEncodedMessageType((char *)message);

	// Update?
	//////////
	if(type == MSG_C_UPDATE)
	{
		unsigned short updateID;
		unsigned long now;
		byte forward;
		byte backward;
		byte right;
		byte left;
		unsigned short packedRotation;

		// Get the time we received it.
		///////////////////////////////
		now = current_time();

		// Decode it.
		/////////////
		rcode = gtDecode(MSG_C_UPDATE_STR, (char *)message, len,
			&updateID,
			&packedRotation,
			&forward,
			&backward,
			&right,
			&left);
		if(rcode == -1)
			return;

		// Set the rotation.
		////////////////////
		client->rotation = UnsignedShortToRotation(packedRotation);

		// Interpret the motion & turning bits.
		///////////////////////////////////////
		if(forward)
			client->motion = FORWARD;
		else if(backward)
			client->motion = BACKWARD;
		else
			client->motion = STILL;
		if(right)
			client->turning = RIGHT;
		else if(left)
			client->turning = LEFT;
		else
			client->turning = STILL;

		// Check if we should move the client.
		//////////////////////////////////////
		if(client->lastUpdate && !client->dead && client->motion)
		{
			unsigned long diff;

			// Get how long since the last update.
			//////////////////////////////////////
			diff = (now - client->lastUpdate);

			// Compute the new position.
			////////////////////////////
			ComputeNewPosition(
				client->position,
				client->position,
				client->motion,
				client->rotation,
				diff,
				PLAYER_SPEED,
				GT2True);
		}

		// Set the last update time.
		////////////////////////////
		client->lastUpdate = now;
	}
	// Press?
	/////////
	else if(type == MSG_C_PRESS)
	{
		unsigned long now;
		char press[64];

		// A dead client can't fire.
		////////////////////////////
		if(client->dead)
			return;

		// Get the current time.
		////////////////////////
		now = current_time();

		// Is this too soon?
		////////////////////
		if((now - client->lastPress) < PRESS_TIME)
			return;

		// Decode it.
		/////////////
		rcode = gtDecode(MSG_C_PRESS_STR, (char *)message, len,
			press);
		if(rcode == -1)
			return;

		// The last press.
		//////////////////
		client->lastPress = now;

		// Do the press.
		////////////////
		ClientPress(clientIndex, press);
	}
	// Chat?
	////////
	else if(type == MSG_C_CHAT)
	{
		char buffer[MAX_NICK + CHAT_MAX + 16];

		// Start forming the outgoing chat message.
		///////////////////////////////////////////
		sprintf(buffer, "%s: ", client->nick);

		// Decode the incoming message.
		///////////////////////////////
		rcode = gtDecode(MSG_C_CHAT_STR, (char *)message, len,
			buffer + strlen(buffer));
		if(rcode == -1)
			return;

		// Send this chat message out to everyone.
		//////////////////////////////////////////
		BroadcastText(buffer, -1, GT2True);
	}
}
 
static void ServerClosedCallback
(
	GT2Connection connection,
	GT2CloseReason reason
)
{
	int clientIndex;
	int i;
	SObject * object;

	// Which client was this?
	/////////////////////////
	clientIndex = (int)gt2GetConnectionData(connection);
	assert((clientIndex >= 0) && (clientIndex < MAX_CLIENTS));
	if((clientIndex < 0) || (clientIndex >= MAX_CLIENTS))
		return;
	assert(clients[clientIndex].used);
	if(!clients[clientIndex].used)
		return;

	// Remove this client.
	//////////////////////
	RemoveClient(clientIndex);

	// Send the remove client message.
	//////////////////////////////////
	for(i = 0 ; i < MAX_CLIENTS ; i++)
		if(clients[i].used)
			SendDelClient(i, clientIndex);

	// Mark all owned objects as not owned.
	///////////////////////////////////////
	for(i = 0 ; i < MAX_OBJECTS ; i++)
	{
		object = &sObjects[i];
		if(object->used && (object->owner == clientIndex))
			object->owner = -1;
	}
}

static void ServerConnectAttemptCallback
(
	GT2Socket socket,
	GT2Connection connection,
	unsigned int ip,
	unsigned short port,
	int latency,
	GT2Byte * message,
	int len
)
{
	int newClientIndex;
	Client * newClient;
	int i;
	char nick[MAX_NICK];
	int rcode;

	// Make sure we got a message.
	//////////////////////////////
	if(!message)
	{
		gt2Reject(connection, (const GT2Byte *)"No initial data received", -1);
		return;
	}

	// Check the message type.
	//////////////////////////
	if(gtEncodedMessageType((char *)message) != MSG_C_INITIAL)
	{
		gt2Reject(connection, (const GT2Byte *)"Bad message type", -1);
		return;
	}

	// Decode the data.
	///////////////////
	rcode = gtDecode(MSG_C_INITIAL_STR, (char *)message, len,
		nick);
	if(rcode == -1)
	{
		gt2Reject(connection, (const GT2Byte *)"Bad message data", -1);
		return;
	}

	// Add the client to the list.
	//////////////////////////////
	newClientIndex = AddClient();
	if(newClientIndex == -1)
	{
		gt2Reject(connection, (const GT2Byte *)"Server full", -1);
		return;
	}
	newClient = &clients[newClientIndex];

	// Accept the connection.
	/////////////////////////
	if(!gt2Accept(connection, &connectionCallbacks))
	{
		// Remove the client.
		/////////////////////
		RemoveClient(newClientIndex);
		return;
	}

	// Set the connection's data to its index.
	//////////////////////////////////////////
	gt2SetConnectionData(connection, (void *)newClientIndex);

	// Set some properties for the new client.
	//////////////////////////////////////////
	newClient->connection = connection;
	strncpy(newClient->nick, nick, MAX_NICK);
	newClient->nick[MAX_NICK - 1] = '\0';

	// Spawn the client.
	////////////////////
	ClientSpawn(newClient);

	// Send new client message for the clients already connected.
	// Also send new client messages for new client to other clients.
	/////////////////////////////////////////////////////////////////
	for(i = 0 ; i < MAX_CLIENTS ; i++)
	{
		if(clients[i].used)
		{
			SendAddClient(i, newClientIndex);
			if(i != newClientIndex)
				SendAddClient(newClientIndex, i);
		}
	}

	// It has no asteroids.
	///////////////////////
	SendNumAsteroids(0, newClientIndex);

	// Tell the new client it can start.
	////////////////////////////////////
	SendStart(newClientIndex);
}

static void ServerSocketErrorCallback
(
	GT2Socket socket
)
{
	printf("Server socket error\n");
}

GT2Bool InitializeServer
(
	void
)
{
	GT2Result result;
	int i;

	// Setup callback structs.
	//////////////////////////
	memset(&connectionCallbacks, 0, sizeof(GT2ConnectionCallbacks));
	connectionCallbacks.received = ServerReceivedCallback;
	connectionCallbacks.closed = ServerClosedCallback;

	// Set client indices.
	//////////////////////
	for(i = 0 ; i < MAX_CLIENTS ; i++)
		clients[i].index = i;

	// Create the socket.
	/////////////////////
	result = gt2CreateSocket(&Socket, PORT_STRING, 0, 0, ServerSocketErrorCallback);
	if(result != GT2Success)
		return GT2False;

	// Listen.
	//////////
	gt2Listen(Socket, ServerConnectAttemptCallback);

	// Initialize the logic.
	////////////////////////
	InitializeLogic();

	return GT2True;
}

void ServerThink
(
	unsigned long now
)
{
	static GT2Bool firstTime = GT2True;
	static unsigned long last;
	unsigned long diff;

	// Think.
	/////////
	gt2Think(Socket);

	// The first time through just store the time.
	// This is so diff is accurate the second time through.
	///////////////////////////////////////////////////////
	if(firstTime)
	{
		firstTime = GT2False;
		last = now;
		return;
	}

	// How long since the last update?
	//////////////////////////////////
	diff = (now - last);

	// Check for an update.
	///////////////////////
	if(diff < SERVER_THINK_TIME)
		return;

	// New last update.
	///////////////////
	last = now;

	// Process objects.
	///////////////////
	ObjectsThink(now, diff);

	// Send updates.
	////////////////
	SendUpdates();
}