File: peerPing.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 (1118 lines) | stat: -rw-r--r-- 22,893 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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
/*
GameSpy Peer SDK 
Dan "Mr. Pants" Schoenblum
dan@gamespy.com

Copyright 1999-2007 GameSpy Industries, Inc

devsupport@gamespy.com
*/

/*************
** INCLUDES **
*************/
#include <stdlib.h>
#include <stdio.h>
#include "peerPlayers.h"
#include "peerPing.h"
#include "peerGlobalCallbacks.h"
#include "peerCallbacks.h"
#include "peerMangle.h"

/************
** DEFINES **
************/
#define PI_PING_TIMEOUT            5000
#define PI_PINGS_PER_SEC           25
#define PI_PING_INTERVAL           (1000 / PI_PINGS_PER_SEC)
#define PI_XPING_INTERVAL          2000
#define PI_XPING_PLAYER_INTERVAL   5000
#define PI_PINGER_PORT             13139
#define PI_MAX_PING_PLAYERS        12
#define PI_XPING_NUM_BUCKETS       32
#define PI_DONT_PING_FLAGS         (PEER_FLAG_PLAYING | PEER_FLAG_AWAY)
#define PI_MAX_XPING_NUM_PLAYERS   32

#define PEER_CONNECTION_DATA       piConnection * connection;\
	                               assert(data);\
                                   assert(data->peer);\
                                   connection = (piConnection *)data->peer;\
								   GSI_UNUSED(connection);


/**********
** TYPES **
**********/
typedef struct piXping
{
	char nicks[2][PI_NICK_MAX_LEN];
	int ping;
} piXping;

/**************
** FUNCTIONS **
**************/
static int piXpingTableHashFn
(
	const void *param,
	int numBuckets
)
{
	piXping * xping = (piXping *)param;
	int i;
	int c;
	const char * str;
	unsigned int hash = 0;
	const char * nicks[2];

	assert(xping);
	assert(xping->nicks[0][0]);
	assert(xping->nicks[1][0]);

	nicks[0] = xping->nicks[0];
	nicks[1] = xping->nicks[1];

	// Reverse the order if the second is smaller.
	//////////////////////////////////////////////
	if(strcmp(nicks[1], nicks[0]) < 0)
	{
		const char * temp = nicks[0];
		nicks[0] = nicks[1];
		nicks[1] = temp;
	}

	// Get the hash.
	////////////////
	for(i = 0 ; i < 2 ; i++)
	{
		str = nicks[i];
		while((c = *str++) != '\0')
			hash += (unsigned int)tolower(c);
		hash %= (unsigned int)numBuckets;
	}

	return (int)hash;
}

static int GS_STATIC_CALLBACK piXpingTableCompareFn
(
	const void *param1,
	const void *param2
)
{
	piXping * xping1 = (piXping *)param1;
	piXping * xping2 = (piXping *)param2;

	int i;
	int rcode;
	const char * nicks[2][2];

	assert(xping1);
	assert(xping1->nicks[0][0]);
	assert(xping1->nicks[1][0]);
	assert(xping2);
	assert(xping2->nicks[0][0]);
	assert(xping2->nicks[1][0]);

	nicks[0][0] = xping1->nicks[0];
	nicks[0][1] = xping1->nicks[1];
	nicks[1][0] = xping2->nicks[0];
	nicks[1][1] = xping2->nicks[1];

	// Reverse the order if the second is smaller.
	//////////////////////////////////////////////
	for(i = 0 ; i < 2 ; i++)
	{
		if(strcmp(nicks[i][1], nicks[i][0]) < 0)
		{
			const char * temp = nicks[i][0];
			nicks[i][0] = nicks[i][1];
			nicks[i][1] = temp;
		}
	}

	for(i = 0 ; i < 2 ; i++)
	{
		rcode = strcasecmp(nicks[0][i], nicks[1][i]);
		if(rcode != 0)
			return rcode;
	}

	return 0;
}

static void piXpingTableElementFreeFn
(
	void *param
)
{
	piXping * xping = (piXping *)param;
	assert(xping);
	assert(xping->nicks[0][0]);
	assert(xping->nicks[1][0]);
	GSI_UNUSED(xping);
}

static void piProcessPing
(
	PEER peer,
	piPlayer * player,
	int ping
)
{
	int i;
	int total;

	//PEER_CONNECTION;

	// One more returned.
	/////////////////////
	player->pingsReturned++;
	player->pingsLostConsecutive = 0;

	// We have one more ping for this player.
	/////////////////////////////////////////
	player->numPings++;

	//Update last ping received.
	////////////////////////////
	player->lastPingRecv = current_time();

	// Update the ping history.
	///////////////////////////
	if(player->pingHistoryNum > 0)
		memmove(player->pingHistory + 1, player->pingHistory, min(player->pingHistoryNum, PI_PING_HISTORY_LEN - 1) * sizeof(int));
	player->pingHistory[0] = ping;
	if(player->pingHistoryNum < PI_PING_HISTORY_LEN)
		player->pingHistoryNum++;

	// Recompute the ping average.
	//////////////////////////////
	total = 0;
	for(i = 0 ; i < player->pingHistoryNum ; i++)
		total += player->pingHistory[i];
	player->pingAverage = (total / player->pingHistoryNum);

	// Add a ping callback.
	///////////////////////
	piAddPingCallback(peer, player->nick, ping);

	// New ping, no xping yet.
	//////////////////////////
	player->xpingSent = PEERFalse;

	// If this is a one-timer, stop pinging.
	////////////////////////////////////////
	if(player->pingOnce)
		player->pingOnce = PEERFalse;
}

static void piPinged
(
	unsigned int IP,
	unsigned short port,
	int ping,
	const char * data,
	int len,
	PEER peer
)
{
	piPlayer * player;

	// Find the player.
	///////////////////
	player = piFindPlayerByIP(peer, IP);
	if(!player)
		return;

	// Process the ping.
	////////////////////
	piProcessPing(peer, player, ping);
	
	GSI_UNUSED(port);
	GSI_UNUSED(data);
	GSI_UNUSED(len);
}

PEERBool piPingInit
(
	PEER peer
)
{
	static PEERBool noPings[NumRooms];

	PEER_CONNECTION;

	// If there are no ping rooms, skip this.
	/////////////////////////////////////////
	if(memcmp(connection->pingRoom, noPings, sizeof(noPings)) == 0)
		return PEERTrue;

	// Init the xping table.
	////////////////////////
	connection->xpings = TableNew(sizeof(piXping), PI_XPING_NUM_BUCKETS, piXpingTableHashFn, piXpingTableCompareFn, piXpingTableElementFreeFn);
	if(!connection->xpings)
		return PEERFalse;

	// Init pinger.
	///////////////
	if(!pingerInit(NULL, PI_PINGER_PORT, piPinged, peer, NULL, NULL))
		return PEERFalse;

	// No pings yet.
	////////////////
	connection->lastPingTimeMod = 0;
	connection->lastXpingSend = 0;

	// Init the random number generator.
	////////////////////////////////////
	srand(current_time());

	// We're doing pings.
	/////////////////////
	connection->doPings = PEERTrue;

	return PEERTrue;
}

void piPingCleanup
(
	PEER peer
)
{
	PEER_CONNECTION;

	// Nothing to do if we weren't pinging.
	///////////////////////////////////////
	if(!connection->doPings)
		return;

	// If we're staying in the title room, get out.
	///////////////////////////////////////////////
	if(connection->stayInTitleRoom)
		return;

	// Clear timing stuff.
	//////////////////////
	connection->lastPingTimeMod = 0;
	connection->lastXpingSend = 0;

	// gsifree the xping table.
	////////////////////////
	if(connection->xpings)
		TableFree(connection->xpings);
	connection->xpings = NULL;

	// Cleanup pinger.
	//////////////////
	pingerShutdown();

	// Not doing pings.
	///////////////////
	connection->doPings = PEERFalse;
}

typedef struct piPingerReplyData
{
	PEER peer;
	unsigned int IP;
	int ping;
} piPingerReplyData;

static void piPingerReplyMapFn
(
	void *elem,
	void *clientdata
)
{
	piPlayer * player = (piPlayer *)elem;
	piPingerReplyData * data = (piPingerReplyData *)clientdata;

	PEER_CONNECTION_DATA;

	assert(player);

	// Check if waiting for a ping.
	///////////////////////////////
	if(!player->waitingForPing)
		return;

	// Check the IP.
	////////////////
	if(!player->gotIPAndProfileID || (player->IP != data->IP))
		return;

	// Not waiting anymore.
	///////////////////////
	player->waitingForPing = PEERFalse;

	// Check timeout.
	/////////////////
	if(data->ping == PINGER_TIMEOUT)
	{
		// One more lost.
		/////////////////
		player->pingsLostConsecutive++;

		// If a one-timer drops three in a row, give up.
		////////////////////////////////////////////////
		if(player->pingOnce && (player->pingsLostConsecutive >= 3))
		{
			player->pingsLostConsecutive = 0;
			player->pingOnce = PEERFalse;
		}
	}
	else
	{
		// Process the ping.
		////////////////////
		piProcessPing(data->peer, player, data->ping);
	}
}

static void piPingerReply
(
	unsigned int IP,
	unsigned short port,
	int ping,
	const char * pingData,
	int pingDataLen,
	PEER peer
)
{
	piPingerReplyData data;

	PEER_CONNECTION;

	// Find who sent the ping.
	//////////////////////////
	data.peer = peer;
	data.IP = IP;
	data.ping = ping;
	TableMap(connection->players, piPingerReplyMapFn, &data);
	
	GSI_UNUSED(port);
	GSI_UNUSED(pingData);
	GSI_UNUSED(pingDataLen);
	
}

static void piPingPlayer
(
	PEER peer,
	piPlayer * player
)
{
	assert(player->gotIPAndProfileID);

	// Make sure we're not already waiting.
	///////////////////////////////////////
	assert(!player->waitingForPing);
	if(player->waitingForPing)
		return;

	// Do the ping.
	///////////////
	pingerPing(player->IP, PI_PINGER_PORT, piPingerReply, peer, PINGERFalse, PI_PING_TIMEOUT);

	// Waiting for a ping to return.
	////////////////////////////////
	player->waitingForPing = PEERTrue;

	// Last ping send time.
	///////////////////////
	player->lastPingSend = current_time();

	// Clear must ping flag.
	////////////////////////
	player->mustPing = PEERFalse;
}

typedef struct piPickPingPlayersData
{
	PEER peer;
	piPlayer ** players;
	int max;
	int num;
} piPickPingPlayersData;

static void piPickPingPlayersMap
(
	void * elem,
	void * clientData
)
{
	piPlayer * player = (piPlayer *)elem;
	piPickPingPlayersData * data = (piPickPingPlayersData *)clientData;
	PEER peer = (PEER)data->peer;
	piPlayer * other;
	unsigned long now;
	unsigned long delay;
	int i;
	int j;

	PEER_CONNECTION;

	// Not pinging?
	///////////////
	if(!player->inPingRoom && !player->pingOnce)
		return;

	// Don't have IP?
	/////////////////
	if(!player->gotIPAndProfileID)
		return;

	// Waiting for a ping?
	//////////////////////
	if(player->waitingForPing)
		return;

	// Don't ping the local player.
	///////////////////////////////
	if(player->local)
		return;

	// Don't ping someone who's playing or away.
	////////////////////////////////////////////
	if((player->inRoom[TitleRoom] && (player->flags[TitleRoom] & PI_DONT_PING_FLAGS)) ||
	   (player->inRoom[GroupRoom] && (player->flags[GroupRoom] & PI_DONT_PING_FLAGS)) ||
	   (player->inRoom[StagingRoom] && (player->flags[StagingRoom] & PI_DONT_PING_FLAGS)))
	   return;

	// Check if we have to ping this player.
	////////////////////////////////////////
	if(!player->mustPing)
	{
		// Get the current time.
		////////////////////////
		now = current_time();

		// Slow pings for no response.
		//////////////////////////////
		if((player->pingsLostConsecutive >= 4) && ((now - player->lastPingSend) < 120000))
			return;

		// Pinged recently?
		///////////////////
		if(player->inRoom[StagingRoom])
			delay = 2000;
		else if(player->pingsReturned < 3)
			delay = 5000;
		else
			delay = 30000;
		if((now - player->lastPingSend) < delay)
			return;
		if((now - player->lastPingRecv) < (delay + 1500))
			return;
	}

	// Go through all the spots.
	////////////////////////////
	for(i = (data->max - 1) ; i >= 0 ; i--)
	{
		// Is it empty?
		///////////////
		if(!data->players[i])
			continue;

		// The "other" player.
		//////////////////////
		other = data->players[i];

		// Is this player higher priority than us?
		//////////////////////////////////////////
		if((!other->numPings && player->numPings) ||                        // Not pinged.
			(other->inRoom[StagingRoom] && !player->inRoom[StagingRoom]) || // In staging room.
			(piIsPlayerVIP(other, StagingRoom) && !piIsPlayerVIP(player, StagingRoom)) ||  // Op or voice in the same staging room.
			(strcasecmp(other->nick, player->nick) < 0))                    // Alphabetical.
		{
			break;
		}
	}

	// Bump it up one, so it's set to our spot.
	///////////////////////////////////////////
	i++;

	// Did we not find anything?
	////////////////////////////
	if(i == data->max)
		return;

	// Bump down the other player and all below.
	////////////////////////////////////////////
	for(j = (data->max - 1) ; j > i ; j--)
		data->players[j] = data->players[j - 1];

	// Set this player.
	///////////////////
	data->players[i] = player;

	// One more.
	////////////
	if(data->num < data->max)
		data->num++;
}

// Returns an array of pointers to players to ping,
// or NULL if there's noone to ping.
// The number of players in the array will be no
// larger than min(PI_MAX_PING_PLAYERS, numPings).
///////////////////////////////////////////////////
static piPlayer ** piPickPingPlayers
(
	PEER peer,
	int * numPings
)
{
	static piPlayer * players[PI_MAX_PING_PLAYERS];
	piPickPingPlayersData data;
	PEER_CONNECTION;

	// Check for no players.
	////////////////////////
	if(!connection->players || !(*numPings) || !TableCount(connection->players))
	{
		*numPings = 0;
		return NULL;
	}

	// Setup a list of players to ping.
	///////////////////////////////////
	data.peer = peer;
	data.players = players;
	data.max = min(PI_MAX_PING_PLAYERS, *numPings);
	data.num = 0;
	memset(players, 0, sizeof(piPlayer *) * data.max);
	TableMap(connection->players, piPickPingPlayersMap, &data);

	// Set number of pings.
	///////////////////////
	*numPings = data.num;

	// Check for noone to ping.
	///////////////////////////
	if(!data.players[0])
		return NULL;

	return data.players;
}

static void piXpingPlayer
(
	PEER peer,
	piPlayer * player
)
{
	int roomType;
	char message[(PI_NICK_MAX_LEN * 2) + 32];
	char encodedIP[11];

	PEER_CONNECTION;

	assert(player->inXpingRoom);
	if(!player->inXpingRoom)
		return;

	// Setup the message.
	/////////////////////
	piMangleIP(encodedIP, player->IP);
	sprintf(message, "%s %d", encodedIP, player->pingAverage);

	// Figure out which room to send the xping in.
	//////////////////////////////////////////////
	for(roomType = 0 ; roomType < NumRooms ; roomType++)
	{
		if(player->inRoom[roomType] && connection->xpingRoom[roomType])
		{
			if(connection->numPlayers[roomType] <= PI_MAX_XPING_NUM_PLAYERS)
			{
				assert(IN_ROOM || ENTERING_ROOM);
				if(IN_ROOM || ENTERING_ROOM)
					piSendChannelUTM(peer, ROOM, PI_UTM_XPING, message, PEERFalse);
			}
		}
	}

	// Sent it.
	///////////
	player->xpingSent = PEERTrue;
	player->lastXping = current_time();
	connection->lastXpingSend = (unsigned int)player->lastXping;
}

typedef struct piPickXpingPlayerData
{
	PEER peer;
	piPlayer * player;
} piPickXpingPlayerData;

static void piPickXpingPlayerMap
(
	void * elem,
	void * clientData
)
{
	piPlayer * player = (piPlayer *)elem;
	piPickXpingPlayerData * data = (piPickXpingPlayerData *)clientData;
	PEER peer = (PEER)data->peer;
	unsigned long now;

	PEER_CONNECTION;

	// Not xpinging?
	////////////////
	if(!player->inXpingRoom)
		return;

	// Don't xping the local player.
	////////////////////////////////
	if(player->local)
		return;

	// Don't xping if no pings received.
	///////////////////////////////////
	if(!player->numPings)
		return;

	// Don't xping with the same info.
	//////////////////////////////////
	if(player->xpingSent)
		return;

	// Don't xping too often.
	/////////////////////////
	now = current_time();
	if((now - player->lastXping) < PI_XPING_PLAYER_INTERVAL)
		return;

	// First player?
	////////////////
	if(!data->player)
	{
		data->player = player;
	}
	else if((now - player->lastXping) > (now - data->player->lastXping))
	{
		data->player = player;
	}
}

// Returns a player to ping, or NULL if there's noone to ping.
//////////////////////////////////////////////////////////////
static piPlayer * piPickXpingPlayer
(
	PEER peer
)
{
	piPickXpingPlayerData data;
	PEER_CONNECTION;

	// Check for no players.
	////////////////////////
	if(!connection->players || !TableCount(connection->players))
		return NULL;

	// Find someone near the top of the list.
	/////////////////////////////////////////
	data.peer = peer;
	data.player = NULL;
	TableMap(connection->players, piPickXpingPlayerMap, &data);

	return data.player;
}

void piPingThink
(
	PEER peer
)
{
	unsigned long now;
	int pingTimeMod;
	int numPings;
	piPlayer * player;
	piPlayer ** players;
	int i;

	PEER_CONNECTION;

	// Check if we're not doing pings.
	//////////////////////////////////
	if(!connection->doPings)
		return;
	
	// Don't do pings while playing!
	////////////////////////////////
	if(connection->playing)
		return;
	
	// Don't do pings while away.
	/////////////////////////////
	if(connection->away)
		return;

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

	// Get the number of pings to send.
	///////////////////////////////////
	pingTimeMod = (int)(now / PI_PING_INTERVAL);
	if(connection->lastPingTimeMod)
		numPings = (pingTimeMod - connection->lastPingTimeMod);
	else
		numPings = 1;
	if(numPings)
		connection->lastPingTimeMod = pingTimeMod;

	// Send pings.
	//////////////
	players = piPickPingPlayers(peer, &numPings);
	if(players)
	{
		for(i = 0 ; (i < numPings) && players[i] ; i++)
			piPingPlayer(peer, players[i]);
	}

	// Check for sending a crossping.
	/////////////////////////////////
	if((now - connection->lastXpingSend) > PI_XPING_INTERVAL)
	{
		// Try and pick a player to crossping.
		//////////////////////////////////////
		player = piPickXpingPlayer(peer);
		if(player)
			piXpingPlayer(peer, player);
	}

	// Let pinger think.
	////////////////////
	pingerThink();
}

PEERBool piPingInitPlayer
(
	PEER peer,
	piPlayer * player
)
{
	int i;

	PEER_CONNECTION;

	assert(player);

	// Check if we're not doing pings.
	//////////////////////////////////
	if(!connection->doPings)
		return PEERTrue;

	player->lastPingSend = 0;
	player->lastPingRecv = 0;
	player->lastXping = 0;
	player->waitingForPing = PEERFalse;
	player->pingsReturned = 0;
	player->pingsLostConsecutive = 0;
	player->pingAverage = 0;
	for(i = 0 ; i < PI_PING_HISTORY_LEN ; i++)
		player->pingHistory[i] = 0;
	player->pingHistoryNum = 0;
	player->numPings = 0;
	player->xpingSent = PEERFalse;
	player->inPingRoom = PEERFalse;
	player->inXpingRoom = PEERFalse;
	player->mustPing = PEERFalse;
	player->pingOnce = PEERFalse;

	return PEERTrue;
}

void piPingPlayerJoinedRoom
(
	PEER peer,
	piPlayer * player,
	RoomType roomType
)
{
	PEER_CONNECTION;

	assert(player);
	ASSERT_ROOMTYPE(roomType);

	// Check if we're not doing pings.
	//////////////////////////////////
	if(!connection->doPings)
		return;

	// Is this a ping room?
	///////////////////////
	if(connection->pingRoom[roomType])
		player->inPingRoom = PEERTrue;

	// Is this an xping room?
	/////////////////////////
	if(connection->xpingRoom[roomType])
		player->inXpingRoom = PEERTrue;

	// Immediately ping players when they join a staging room.
	//////////////////////////////////////////////////////////
	if(roomType == StagingRoom)
		player->mustPing = PEERTrue;
}

static void piRemoveXping
(
	PEER peer,
	piXping * xping
)
{
	PEER_CONNECTION;
	
	assert(xping);

	TableRemove(connection->xpings, xping);
}

typedef struct piPingPlayerLeftRoomData
{
	PEER peer;
	const char * nick;
} piPingPlayerLeftRoomData;

static void piPingPlayerLeftRoomTableMapFn
(
	void *elem,
	void *clientdata
)
{
	piXping * xping = (piXping *)elem;
	piPingPlayerLeftRoomData * data = (piPingPlayerLeftRoomData *)clientdata;
	assert(xping);
	assert(data);

	// Check if this player is part of this xping.
	//////////////////////////////////////////////
	if((strcmp(xping->nicks[0], data->nick) == 0) || (strcmp(xping->nicks[1], data->nick) == 0))
		piRemoveXping(data->peer, xping);
}

void piPingPlayerLeftRoom
(
	PEER peer,
	piPlayer * player
)
{
	PEER_CONNECTION;

	assert(player);

	// Check if we're not doing pings.
	//////////////////////////////////
	if(!connection->doPings)
		return;

	// Was this player in a ping room?
	//////////////////////////////////
	if(player->inPingRoom)
	{
		int i;
		PEERBool inPingRoom = PEERFalse;

		// Is he still in a ping room?
		//////////////////////////////
		for(i = 0 ; i < NumRooms ; i++)
		{
			if(player->inRoom[i] && connection->pingRoom[i])
				inPingRoom = PEERTrue;
		}
		player->inPingRoom = inPingRoom;
	}

	// Was this player in an xping room?
	////////////////////////////////////
	if(player->inXpingRoom)
	{
		int i;
		PEERBool inXpingRoom = PEERFalse;

		// Is he still in a ping room?
		//////////////////////////////
		for(i = 0 ; i < NumRooms ; i++)
		{
			if(player->inRoom[i] && connection->xpingRoom[i])
				inXpingRoom = PEERTrue;
		}
		player->inXpingRoom = inXpingRoom;

		if(!player->inXpingRoom)
		{
			piPingPlayerLeftRoomData data;
			data.peer = peer;
			data.nick = player->nick;

			// Get rid of all this players xpings.
			//////////////////////////////////////
			TableMapSafe(connection->xpings, piPingPlayerLeftRoomTableMapFn, &data);
		}
	}
}

static piXping * piFindXping
(
	PEER peer,
	const char * nick1,
	const char * nick2
)
{
	piXping xpingMatch;

	PEER_CONNECTION;

	assert(nick1);
	assert(nick1[0]);
	assert(piGetPlayer(peer, nick1));
	assert(nick2);
	assert(nick2[0]);
	assert(piGetPlayer(peer, nick2));

	// Setup the xping match.
	/////////////////////////
	strzcpy(xpingMatch.nicks[0], nick1, PI_NICK_MAX_LEN);
	_strlwr(xpingMatch.nicks[0]);
	strzcpy(xpingMatch.nicks[1], nick2, PI_NICK_MAX_LEN);
	_strlwr(xpingMatch.nicks[0]);

	// Find the xping.
	//////////////////
	return (piXping *)TableLookup(connection->xpings, &xpingMatch);
}

static piXping * piAddXping
(
	PEER peer,
	const char * nick1,
	const char * nick2
)
{
	piXping xpingMatch;
	piXping * xping;

	PEER_CONNECTION;

	assert(nick1);
	assert(nick1[0]);
	assert(piGetPlayer(peer, nick1));
	assert(nick2);
	assert(nick2[0]);
	assert(piGetPlayer(peer, nick2));

	// Setup the one to add.
	////////////////////////
	xping = &xpingMatch;
	strzcpy(xping->nicks[0], nick1, PI_NICK_MAX_LEN);
	_strlwr(xping->nicks[0]);
	strzcpy(xping->nicks[1], nick2, PI_NICK_MAX_LEN);
	_strlwr(xping->nicks[1]);

	// Add it.
	//////////
	TableEnter(connection->xpings, xping);

	// Get it.
	//////////
	xping = (piXping *)TableLookup(connection->xpings, &xpingMatch);

	// Return it.
	/////////////
	return xping;
}

void piUpdateXping
(
	PEER peer,
	const char * nick1,
	const char * nick2,
	int ping
)
{
	piPlayer * player1;
	piPlayer * player2;
	piXping * xping;

	PEER_CONNECTION;

	assert(nick1);
	assert(nick1[0]);
	assert(piGetPlayer(peer, nick1));
	assert(nick2);
	assert(nick2[0]);
	assert(piGetPlayer(peer, nick2));
	assert(ping >= 0);

	// Check if we're not doing pings.
	//////////////////////////////////
	if(!connection->doPings)
		return;

	player1 = piGetPlayer(peer, nick1);
	if(!player1)
		return;
	if(!player1->inXpingRoom)
		return;
	player2 = piGetPlayer(peer, nick2);
	if(!player2)
		return;
	if(!player2->inXpingRoom)
		return;

	// Add it.
	//////////
	xping = piAddXping(peer, nick1, nick2);
	assert(xping);
	if(!xping)
		return;

	// Update.
	//////////
	xping->ping = ping;
}

PEERBool piGetXping
(
	PEER peer,
	const char * nick1,
	const char * nick2,
	int * ping
)
{
	piXping * xping;

	PEER_CONNECTION;

	assert(nick1);
	assert(nick1[0]);
	assert(piGetPlayer(peer, nick1));
	assert(nick2);
	assert(nick2[0]);
	assert(piGetPlayer(peer, nick2));
	assert(ping);

	// Check if we're not doing pings.
	//////////////////////////////////
	if(!connection->doPings)
		return PEERFalse;

	// Get the xping.
	/////////////////
	xping = piFindXping(peer, nick1, nick2);
	assert(xping);
	if(!xping)
		return PEERFalse;

	// Return the ping.
	///////////////////
	*ping = xping->ping;

	return PEERTrue;
}