-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dpthread.c
1742 lines (1449 loc) · 39.8 KB
/
Dpthread.c
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
// #define INITGUID
#include <windows.h>
#include <dplay.h>
#include <dplobby.h>
#include <stdio.h>
#include <time.h>
#include <stdarg.h>
#include "typedefs.h"
#include "New3D.h"
#include "typedefs.h"
#include <dplay.h>
#include "new3d.h"
#include "quat.h"
#include "CompObjects.h"
#include "bgobjects.h"
#include "Object.h"
#include "mydplay.h"
#include "ships.h"
#include "main.h"
#include "Title.h"
#include "Text.h"
#include "screenpolys.h"
#include "magic.h"
#include "XMem.h"
#include "ddsurfhand.h"
#include "pickups.h"
#include "local.h"
#include "comm.h"
#include "gsutils.h"
#include "dpthread.h"
#include "demo_id.h"
#include "primary.h"
//#define DPTEST
extern LPDIRECTPLAYLOBBY2A lpDPlayLobby; //Lobby stuff...
extern LPDIRECTPLAY3 glpDP;
extern BYTE WhoIAm;
extern BYTE GameStatus[];
extern char IPAddressText[16];
extern LPDPSESSIONDESC2 glpdpSD;
extern SHORTNAMETYPE Names; // all the players short Names....
extern BOOL IsHost;
extern GLOBALSHIP Ships[];
extern GUID ServiceProvidersGuids[];
extern LIST ServiceProvidersList;
extern uint16 PingTimes[MAX_PLAYERS];
extern SLIDER MaxServerPlayersSlider;
extern BYTE TeamNumber[];
extern LIST TrackersList;
extern BOOL IsServerGame;
extern BOOL IsLobbyLaunched;
extern BOOL NoMenuBack;
// TEMP?
extern char MissionTextNames[MAXLEVELS][128];
extern LIST LevelList;
extern SLIDER PacketsSlider;
void DebugPrintf( const char * format, ... );
extern void SetMultiplayerPrefs( void );
//
// modify this struction with what ever you want.
// just make sure you modify the sprintf in ProcessForsakenInfo
// to print the values
//
typedef struct playerlist_tag {
char PlayerName[ 8 ];
int CurScore;
DWORD Ping;
int16 Team;
} playerlist_t;
playerlist_t PlayerInfo[ MAX_PLAYERS ];
BOOL SendShutdownPacket;
uint16 PlayerPort[ MAX_PLAYERS ];
char GamespyEchoText[ 128 ];
char GamespySecureText[ 128 ];
BOOL bGameSpy = FALSE;
BOOL ServerHeartbeat = FALSE;
BOOL PeerPeerHeartbeat = FALSE;
BOOL ForceHeartbeat = FALSE;
BOOL DoHeartbeat = FALSE;
BOOL TrackerOveride = FALSE;
char heartbeat_location[ 32 ];
char heartbeat_servername[ 32 ];
char heartbeat_serverspec[ 32 ];
char heartbeat_serverconnection[ 32 ];
char heartbeat_serveradmin[ 32 ];
CRITICAL_SECTION PlayerInfoKey;
BOOL WinsockActive = FALSE;
BOOL WSA_Active = FALSE;
SOCKET sockfd;
HANDLE DPSendThreadEvent = NULL;
HANDLE DPSendThreadKillEvent = NULL;
HANDLE DPListenThreadEvent = NULL;
HANDLE DPListenThreadKillEvent = NULL;
HANDLE ghDPSendThread = NULL;
HANDLE ghDPListenThread = NULL;
char *udp_header = "\377\377\377\377";
char *UDP_MSG_Ping = "ping\n";
char *UDP_MSG_BouncedPing = "gnip\n";
char *UDP_MSG_Status = "status\n";
char *UDP_MSG_Forsaken = "forsaken\n";
#define THIS_GAMENAME "Forsaken"
DWORD WINAPI DPThread(LPVOID p);
BOOL LoadDP(void);
void PostServerInfo( struct sockaddr_in *addr );
char pBuf[4096];
char *srv_info;
HANDLE SendMutex;
struct sockaddr_in HostIP, TrackerIP;
struct sockaddr_in PlayerIP[ MAX_PLAYERS ];
u_short host_port;
u_short tracker_port;
char host_ip[ 16 ];
char tracker_ip[ 16 ];
uint32 tracker_addr = 0;
char tracker_name[ 128 ];
DWORD heartbeat_freq;
int heartbeat_type;
static const BYTE GuidMap[] = { 3, 2, 1, 0, '-', 5, 4, '-', 7, 6, '-',
8, 9, '-', 10, 11, 12, 13, 14, 15 };
static const char szDigits[] = "0123456789ABCDEF";
char Gamespy_Secret_Key[] = "znoJ6k";
BOOL StringFromGUID(LPCGUID lpguid, LPSTR lpsz)
{
int i;
const BYTE * pBytes = (const BYTE *) lpguid;
*lpsz++ = '{';
for (i = 0; i < sizeof(GuidMap); i++)
{
if (GuidMap[i] == '-')
{
*lpsz++ = '-';
}
else
{
*lpsz++ = szDigits[ (pBytes[GuidMap[i]] & 0xF0) >> 4 ];
*lpsz++ = szDigits[ (pBytes[GuidMap[i]] & 0x0F) ];
}
}
*lpsz++ = '}';
*lpsz = '\0';
return TRUE;
}
void GrabMutex( HANDLE *mutex, char *name) {
if (*mutex == NULL) {
*mutex = CreateMutex (NULL, FALSE, name);
}
// Enter Mutex
WaitForSingleObject( *mutex, INFINITE );
}
void UpdatePlayerInfo( void )
{
int i;
if ( !WSA_Active )
return;
EnterCriticalSection( &PlayerInfoKey );
for ( i = 0; i < MAX_PLAYERS; i++ )
{
if ( ( i == WhoIAm ) || ( GameStatus[ i ] == STATUS_Normal ) || ( GameStatus[ i ] == STATUS_StartingMultiplayer ) )
{
PlayerInfo[ i ].CurScore = Ships[ i ].Kills;
// copy names / pings here ( inside critical section ) in case of simultaneous access by seperate threads
strcpy( PlayerInfo[ i ].PlayerName, Names[ i ] );
PlayerInfo[ i ].Ping = PingTimes[ i ];
PlayerInfo[ i ].Team = TeamNumber[ i ];
}
}
LeaveCriticalSection( &PlayerInfoKey );
}
void RequestPings( void )
{
int i;
char buf[ 256 ];
char *pCh;
if ( !ghDPSendThread ) // if no send thread active, no point in trying to send pings
return;
GrabMutex(&SendMutex, NULL);
for ( i = 0; i < MAX_PLAYERS; i++ )
{
if ( i != WhoIAm )
{
PlayerInfo[ i ].Ping = 999999;
if ( GameStatus[ i ] == STATUS_Normal )
{
if ( !PlayerIP[ i ].sin_port ) // get IP address if neccesary
{
char add[ 16 ];
memset(&PlayerIP[ i ], 0, sizeof(PlayerIP[ i ]));
if ( GetIPFromDP( add, Ships[ i ].dcoID ) )
{
PlayerIP[ i ].sin_family = AF_INET;
PlayerIP[ i ].sin_port = htons( DEF_PORT );
PlayerIP[ i ].sin_addr.s_addr = inet_addr( add );
}
}
if ( PlayerIP[ i ].sin_port )
{
pCh = buf;
pCh += sprintf( pCh, "%s", udp_header );
pCh += sprintf( pCh, UDP_MSG_Ping );
pCh += sprintf( pCh, "\\player\\%d", i );
pCh += sprintf( pCh, "\\time\\%d", timeGetTime() );
*(++pCh) = 0; // ensure NULL terminated
sendto(sockfd, buf, pCh - buf, 0,
(struct sockaddr*)&PlayerIP[ i ], sizeof(struct sockaddr_in));
}
}
}else
{
PlayerInfo[ i ].Ping = 0;
}
}
ReleaseMutex(SendMutex);
}
void GetUpTime( char *str, DWORD dwTime )
{
FILETIME FileTime;
FILETIME sysTime;
SYSTEMTIME _sysTime;
// use of 64bit integers should be fine as long
// as you compile with MS Devstudio.
__int64 t1, t2;
int uptime;
if (dwTime &&
DosDateTimeToFileTime((WORD)(dwTime & 0xffff),
(WORD)((dwTime >> 16) & 0xffff), &FileTime)) {
GetSystemTime(&_sysTime);
if (SystemTimeToFileTime(&_sysTime, &sysTime)) {
t1 = ((__int64)FileTime.dwLowDateTime +
((__int64)FileTime.dwHighDateTime << 32));
t2 = ((__int64)sysTime.dwLowDateTime +
((__int64)sysTime.dwHighDateTime << 32));
// t1 = start time, t2 = Current time
// Get the difference then divide by 10,000,000 to get seconds
// MS Devstudio will convert the 64bit operations to multiple
// 32bit operations.
uptime = (int)((t2 - t1)/10000000);
// Add an uptime key to the srv_info in the form of
// "hours:minutes:seconds"
sprintf(str, "\\Uptime\\%dh:%2.2dm:%2.2ds",
uptime / 3600, (uptime/60) % 60, uptime % 60);
}
}
}
void GetGameType( char *buf, LPDPSESSIONDESC2 pSD )
{
// game type
if ( pSD->dwUser3 & BombGameBit ) {
strcpy( buf, "bomb tag" );
} else if ( pSD->dwUser3 & CTFGameBit ) {
strcpy( buf, "ctf" );
} else if ( pSD->dwUser3 & FlagGameBit ) {
strcpy( buf, "flag chase" );
} else if ( pSD->dwUser3 & BountyGameBit ) {
if ( pSD->dwUser3 & TeamGameBit )
{
strcpy( buf, "team bounty hunt" );
}else
{
strcpy( buf, "bounty hunt" );
}
} else {
if ( pSD->dwUser3 & TeamGameBit )
{
strcpy( buf, "team game" );
}else
{
strcpy( buf, "free for all" );
}
}
}
void GetGameSubType( char *buf, LPDPSESSIONDESC2 pSD )
{
// CTF sub type
if ( pSD->dwUser3 & CTFGameBit )
{
switch ( CTF_Type_Decode( pSD->dwUser3 ) )
{
case CTF_STANDARD:
strcpy( buf, LT_CTF_Normal/*"normal"*/ );
break;
case CTF_MUST_CARRY:
strcpy( buf, LT_CTF_MustCarry/*"carry it back"*/ );
break;
case CTF_CAN_CARRY:
strcpy( buf, LT_CTF_CanCarry/*"can pick up"*/ );
break;
case CTF_ONE_TOUCH:
strcpy( buf, LT_CTF_OneTouch/*"1 touch return"*/ );
break;
case CTF_NO_RETURN:
strcpy( buf, LT_CTF_NoReturn/*"can't pick up"*/ );
break;
default:
strcpy( buf, LT_CTF_Standard/*"standard"*/ );
}
}
}
BOOL IsGameJoinable( LPDPSESSIONDESC2 pSD )
{
return ( ( pSD->dwFlags & DPSESSION_JOINDISABLED ) ? FALSE : TRUE );
#if 0
// if server game & host choosing, return FALSE
if ( ( pSD->dwUser3 & ServerGameStateBits ) == SERVER_STATE_HostChoosing )
return FALSE;
switch( MyGameStatus )
{
case STATUS_StartingMultiplayer:
case STATUS_Normal:
return TRUE;
default:
return FALSE;
}
#endif
}
BOOL ProcessGamespy( int type )
{
LPDPSESSIONDESC2 pSD = NULL;
DWORD dwSize;
HRESULT hr;
char *levelname;
char buf[ 128 ];
int16 i, j;
char *pCh;
// first get the size for the session desc
if ((hr = IDirectPlay3_GetSessionDesc(glpDP, NULL, &dwSize)) == DPERR_BUFFERTOOSMALL)
{
// allocate memory for it
pSD = (LPDPSESSIONDESC2) malloc(dwSize);
if (pSD)
{
// now get the session desc
hr = IDirectPlay3_GetSessionDesc(glpDP, pSD, &dwSize);
}
else
{
hr = E_OUTOFMEMORY;
}
}
if ( !pSD )
return FALSE;
if ( hr != DP_OK )
{
free( pSD );
return FALSE;
}
GrabMutex(&SendMutex, NULL);
srv_info = &pBuf[0];
if ( type & HEARTBEAT_GAMESPY_Pulse )
{
srv_info += sprintf( srv_info, "\\heartbeat\\%d\\gamename\\forsaken", DEF_PORT );
ReleaseMutex(SendMutex);
free( pSD );
return TRUE; // no need to append query ID / packet num / security code
}
if ( ( type & HEARTBEAT_GAMESPY_Basic ) || ( type & HEARTBEAT_GAMESPY_Status ) )
{
srv_info += sprintf( srv_info, "\\gamename\\forsaken\\gamever\\%1.2f\\location\\%s", PATCH_VERSION, heartbeat_location );
}
if ( ( type & HEARTBEAT_GAMESPY_Info ) || ( type & HEARTBEAT_GAMESPY_Status ) )
{
// get map name
levelname = strchr( pSD->lpszSessionNameA, '~' );
if ( levelname )
{
*levelname++ = 0;
}else
{
levelname = "unknown";
}
GetGameType( buf, pSD );
srv_info += sprintf( srv_info, "\\hostname\\%s\\hostport\\%d\\mapname\\%s\\gametype\\%s\\numplayers\\%d\\maxplayers\\%d",
heartbeat_servername[ 0 ] ? heartbeat_servername : pSD->lpszSessionNameA,
DEF_PORT,
levelname,
buf,
IsServerGame ? ( pSD->dwCurrentPlayers - 1 ) : pSD->dwCurrentPlayers,
IsServerGame ? ( pSD->dwMaxPlayers - 1 ) : pSD->dwMaxPlayers );
srv_info += sprintf( srv_info, "\\gamemode\\%s",
( pSD->dwUser3 & ServerGameStateBits ) ? "server" : "peer-peer" );
}
if ( ( type & HEARTBEAT_GAMESPY_Rules ) || ( type & HEARTBEAT_GAMESPY_Status ) )
{
// session GUID
StringFromGUID(&pSD->guidInstance, buf );
srv_info += sprintf(srv_info, "\\cl_session\\%s", buf );
srv_info += sprintf(srv_info, "\\cl_TCP\\%s", IPAddressText );
srv_info += sprintf(srv_info, "\\cl_AutoStart\\" );
// application GUID
//StringFromGUID(&pSD->guidApplication, buf );
//srv_info += sprintf(srv_info, "\\cl_application\\%s", buf );
// up time
GetUpTime( buf, pSD->dwUser1 );
srv_info += sprintf( srv_info, "%s", buf );
srv_info += sprintf( srv_info, "\\joinable\\%d", IsGameJoinable( pSD ) );
buf[ 0 ] = 0;
GetGameSubType( buf, pSD );
if ( buf[ 0 ] )
{
srv_info += sprintf( srv_info, "\\ctftype\\%s", buf );
}
if ( pSD->dwUser3 & TeamGameBit )
{
srv_info += sprintf( srv_info, "\\HarmTM\\%d",( pSD->dwUser3 & HarmTeamMatesBit ) );
}
srv_info += sprintf( srv_info, "\\shortpackets\\%d",( pSD->dwUser3 & ShortPacketsBit ) ? 1 : 0 );
srv_info += sprintf( srv_info, "\\bigpackets\\%d",( pSD->dwUser3 & BigPacketsBit ) ? 1 : 0 );
srv_info += sprintf( srv_info, "\\packetrate\\%d", PacketsSlider.value );
srv_info += sprintf( srv_info, "\\maxkills\\%d", ( pSD->dwUser2 & MaxKillsBits ) >> MaxKills_Shift );
srv_info += sprintf( srv_info, "\\timelimit\\%d", ( pSD->dwUser3 & GameTimeBit ) >> GameTimeBit_Shift );
switch( ( pSD->dwUser3 & CollisionTypeBits ) >> Collision_Type_BitShift )
{
case COLPERS_Forsaken:
pCh = "on";
break;
case COLPERS_Descent:
pCh = "off";
break;
case COLPERS_Server:
pCh = "server";
break;
srv_info += sprintf(srv_info, "\\lagtolerance\\%s", pCh );
}
srv_info += sprintf(srv_info, "\\brightbikes\\%d", ( pSD->dwUser3 & BrightShipsBit ) ? 1 : 0 );
srv_info += sprintf(srv_info, "\\exhausts\\%d", ( pSD->dwUser3 & BikeExhaustBit ) ? 1 : 0 );
srv_info += sprintf(srv_info, "\\serverspec\\%s", heartbeat_serverspec );
srv_info += sprintf(srv_info, "\\serverconnection\\%s", heartbeat_serverconnection );
srv_info += sprintf(srv_info, "\\serveradmin\\%s", heartbeat_serveradmin );
}
if ( ( type & HEARTBEAT_GAMESPY_Players ) || ( type & HEARTBEAT_GAMESPY_Status ) )
{
j = 0;
for ( i = IsServerGame ? 1 : 0 ; i < MAX_PLAYERS; i++ )
{
if ( ( i == WhoIAm ) || ( ( GameStatus[ i ] != STATUS_Null ) && ( GameStatus[ i ] != STATUS_Left ) && ( GameStatus[ i ] != STATUS_LeftCrashed ) ) )
{
srv_info += sprintf( srv_info, "\\player_%d\\%s", j, PlayerInfo[ i ].PlayerName );
srv_info += sprintf( srv_info, "\\frags_%d\\%d", j, PlayerInfo[ i ].CurScore );
srv_info += sprintf( srv_info, "\\ping_%d\\%d", j, PlayerInfo[ i ].Ping );
srv_info += sprintf( srv_info, "\\team_%d\\%d", j, PlayerInfo[ i ].Team );
j++;
}
}
}
if ( type & HEARTBEAT_GAMESPY_Echo )
{
srv_info += sprintf( srv_info, "\\echo\\%s", GamespyEchoText );
}
if ( type & HEARTBEAT_GAMESPY_Secure )
{
char encoded_val[(GAMESPY_VALIDATE_SIZE * 4) / 3 + 1];
gs_encrypt(GamespySecureText, GAMESPY_VALIDATE_SIZE, Gamespy_Secret_Key);
gs_encode(GamespySecureText, GAMESPY_VALIDATE_SIZE, encoded_val);
srv_info += sprintf( srv_info, "\\validate\\%s", encoded_val );
}
ReleaseMutex(SendMutex);
free( pSD );
return TRUE;
}
BOOL ProcessForsakenInfo( int type )
{
static char strAppGuid[128], strInstGuid[128];
char *pChar;
char buf[ 128 ];
FILE *f = NULL;
int i;
LPDPSESSIONDESC2 pSD = NULL;
DWORD dwSize;
HRESULT hr;
// first get the size for the session desc
if ((hr = IDirectPlay3_GetSessionDesc(glpDP, NULL, &dwSize)) == DPERR_BUFFERTOOSMALL)
{
// allocate memory for it
pSD = (LPDPSESSIONDESC2) malloc(dwSize);
if (pSD)
{
// now get the session desc
hr = IDirectPlay3_GetSessionDesc(glpDP, pSD, &dwSize);
}
else
{
hr = E_OUTOFMEMORY;
}
}
if( !pSD )
return FALSE;
if ( hr != DP_OK )
{
free( pSD );
return FALSE;
}
GrabMutex(&SendMutex, NULL);
srv_info = &pBuf[0];
strcpy ( srv_info, udp_header );
srv_info += strlen( udp_header );
if ( type & HEARTBEAT_Raw )
{
srv_info += sprintf( srv_info, "heartbeat\n" );
ReleaseMutex(SendMutex);
free( pSD );
return TRUE;
}
srv_info += sprintf( srv_info, "forsaken\n" );
StringFromGUID(&pSD->guidApplication, strAppGuid);
StringFromGUID(&pSD->guidInstance, strInstGuid);
srv_info += sprintf(srv_info, "\\AppGuid\\%s", strAppGuid);
srv_info += sprintf(srv_info, "\\InstGuid\\%s", strInstGuid);
if ( !type || ( type & HEARTBEAT_BasicInfo ) )
{
// session name
pChar = srv_info + sprintf( srv_info, "\\session\\%s", pSD->lpszSessionNameA );
srv_info = strchr( srv_info, '~' );
if ( !srv_info )
{
srv_info = pChar;
}
/*
// session creation time / date
srv_info += sprintf( srv_info, "\\created\\%x", pSD->dwUser1 );
*/
// session up time
GetUpTime( buf, pSD->dwUser1 );
srv_info += sprintf( srv_info, "%s", buf );
strncpy( buf, pSD->lpszSessionNameA, 128 );
buf[ 127 ] = 0;
pChar = strchr( buf, '~' );
if ( pChar )
{
srv_info += sprintf( srv_info, "\\level\\%s", ++pChar );
}
// num players
srv_info += sprintf( srv_info, "\\curplayers\\%d", IsServerGame ? ( pSD->dwCurrentPlayers - 1 ) : pSD->dwCurrentPlayers );
// max players
if ( ( ( pSD->dwUser3 & ServerGameStateBits ) == SERVER_STATE_NeedHost ) ||
( ( pSD->dwUser3 & ServerGameStateBits ) == SERVER_STATE_HostChoosing ) )
{
//server waiting for pseudohost )
srv_info += sprintf( srv_info, "\\maxplayers\\%d", MaxServerPlayersSlider.value );
}else
{
srv_info += sprintf( srv_info, "\\maxplayers\\%d", IsServerGame ? ( pSD->dwMaxPlayers - 1 ) : pSD->dwMaxPlayers );
}
// server game?
srv_info += sprintf( srv_info, "\\server\\%d",
( ( pSD->dwUser3 & ServerGameStateBits ) > 0 ) );
}
if ( !type || ( type & HEARTBEAT_GameType ) )
{
GetGameType( buf, pSD );
srv_info += sprintf( srv_info, "\\game\\%s", buf );
buf[ 0 ] = 0;
GetGameSubType( buf, pSD );
if ( buf[ 0 ] )
{
srv_info += sprintf( srv_info, "\\ctftype\\%s", buf );
}
// harm team mates?
if ( pSD->dwUser3 & TeamGameBit )
{
srv_info += sprintf( srv_info, "\\HarmTM\\%d",( pSD->dwUser3 & HarmTeamMatesBit ) );
}
}
if ( !type || ( type & HEARTBEAT_PacketInfo ) )
{
// short packets
srv_info += sprintf( srv_info, "\\shortpackets\\%d",( pSD->dwUser3 & ShortPacketsBit ) ? 1 : 0 );
// group packets
srv_info += sprintf( srv_info, "\\bigpackets\\%d",( pSD->dwUser3 & BigPacketsBit ) ? 1 : 0 );
// packet rate
srv_info += sprintf( srv_info, "\\packetrate\\%d", PacketsSlider.value );
}
if ( !type || ( type & HEARTBEAT_KillsInfo ) )
{
// max kills
srv_info += sprintf( srv_info, "\\maxkills\\%d", ( pSD->dwUser2 & MaxKillsBits ) >> MaxKills_Shift );
// current top
srv_info += sprintf( srv_info, "\\topkills\\%d", ( ( pSD->dwUser2 & CurrentMaxKillsBits ) >> CurrentMaxKills_Shift ) );
}
if ( !type || ( type & HEARTBEAT_TimeInfo ) )
{
// time limit
srv_info += sprintf( srv_info, "\\timelimit\\%d", ( pSD->dwUser3 & GameTimeBit ) >> GameTimeBit_Shift );
// current top
srv_info += sprintf( srv_info, "\\elapsedtime\\%d",
( ( pSD->dwUser3 & CurrentGameTimeBits ) >> CurrentGameTime_Shift ) +
( ( ( pSD->dwUser3 & GameTimeBit ) >> GameTimeBit_Shift == ( pSD->dwUser3 & CurrentGameTimeBits ) >> CurrentGameTime_Shift ) ? 0 : 1 ) );
}
if ( !type || ( type & HEARTBEAT_ValidPickups ) )
{
uint32 pickupinfo[ MAX_PICKUPFLAGS ];
PackPickupInfo( pickupinfo );
srv_info += sprintf(srv_info, "\\validpickups\\" );
for ( i = 0; i < MAX_PICKUPFLAGS; i++ )
{
srv_info += sprintf(srv_info, "%x ", pickupinfo[ i ] );
}
}
if ( !type || ( type & HEARTBEAT_ExtraInfo ) )
{
switch( ( pSD->dwUser3 & CollisionTypeBits ) >> Collision_Type_BitShift )
{
case COLPERS_Forsaken:
pChar = "on";
break;
case COLPERS_Descent:
pChar = "off";
break;
case COLPERS_Server:
pChar = "server";
break;
srv_info += sprintf(srv_info, "\\lagtolerance\\%s ", pChar );
}
srv_info += sprintf(srv_info, "\\brightbikes\\%d ", ( pSD->dwUser3 & BrightShipsBit ) ? 1 : 0 );
srv_info += sprintf(srv_info, "\\exhausts\\%d ", ( pSD->dwUser3 & BikeExhaustBit ) ? 1 : 0 );
}
if ( !type || ( type & HEARTBEAT_PlayersList ) )
{
srv_info += sprintf( srv_info, "\n" );
EnterCriticalSection( &PlayerInfoKey );
for ( i = 0; i < MAX_PLAYERS; i++ )
{
if ( ( i == WhoIAm ) || ( GameStatus[ i ] == STATUS_Normal ) || ( GameStatus[ i ] == STATUS_StartingMultiplayer ) )
{
srv_info += sprintf(srv_info, "\"%s\" %d %d\n", PlayerInfo[ i ].PlayerName, PlayerInfo[ i ].CurScore, PlayerInfo[ i ].Ping);
}
}
LeaveCriticalSection( &PlayerInfoKey );
}
ReleaseMutex(SendMutex);
free( pSD );
return TRUE;
}
char *GAMESPY_KEY_Basic = "basic";
char *GAMESPY_KEY_Info = "info";
char *GAMESPY_KEY_Rules = "rules";
char *GAMESPY_KEY_Players = "players";
char *GAMESPY_KEY_Status = "status";
char *GAMESPY_KEY_Echo = "echo";
char *GAMESPY_KEY_Secure = "secure";
void ProcessGamespyKey( char *key, char *value, int *type )
{
if ( !_strnicmp( key, GAMESPY_KEY_Basic, sizeof( GAMESPY_KEY_Basic ) ) )
{
*type |= HEARTBEAT_GAMESPY_Basic;
}else if ( !_strnicmp( key, GAMESPY_KEY_Info, sizeof( GAMESPY_KEY_Info ) ) )
{
*type |= HEARTBEAT_GAMESPY_Info;
}else if ( !_strnicmp( key, GAMESPY_KEY_Rules, sizeof( GAMESPY_KEY_Rules ) ) )
{
*type |= HEARTBEAT_GAMESPY_Rules;
}else if ( !_strnicmp( key, GAMESPY_KEY_Players, sizeof( GAMESPY_KEY_Players ) ) )
{
*type |= HEARTBEAT_GAMESPY_Players;
}else if ( !_strnicmp( key, GAMESPY_KEY_Status, sizeof( GAMESPY_KEY_Status ) ) )
{
*type |= HEARTBEAT_GAMESPY_Status;
}else if ( !_strnicmp( key, GAMESPY_KEY_Echo, sizeof( GAMESPY_KEY_Echo ) ) )
{
char *pCh;
*type |= HEARTBEAT_GAMESPY_Echo;
strncpy( GamespyEchoText, value, sizeof( GamespyEchoText ) );
pCh = strchr( GamespyEchoText, '\\' );
if ( pCh )
{
*pCh = 0;
}
}
else if ( !_strnicmp( key, GAMESPY_KEY_Secure, sizeof( GAMESPY_KEY_Secure ) ) )
{
*type |= HEARTBEAT_GAMESPY_Secure;
strncpy( GamespySecureText, value, sizeof( GamespySecureText ) );
}
}
void GamespyParsePacket( char *packet )
{
char *key, *value, *str;
int type;
type = 0;
str = packet;
do
{
key = strchr(str, '\\' );
if ( key )
{
value = strchr( ++key, '\\' );
{
if ( value )
{
ProcessGamespyKey( key, ++value, &type );
}
}
}
str = value;
}while( key && value );
if ( type )
{
ProcessGamespy( type );
}
}
DWORD WINAPI ListenThread(LPVOID p) {
struct sockaddr_in from;
int len, from_len, SelectVal;
fd_set in;
struct timeval tv;
static char tmpbuf[ 4096 ];
char *msgptr;
#ifdef UDP_PINGS
char *tempptr;
#endif
#ifdef DPTEST
char buf[ 4096 ];
#endif
uint16 status_type;
HANDLE eventHandles[2];
eventHandles[0] = DPListenThreadEvent;
eventHandles[1] = DPListenThreadKillEvent;
while (WaitForMultipleObjects(2, eventHandles, FALSE, INFINITE) == WAIT_OBJECT_0) // while just one event is TRUE
{
tv.tv_sec = 1;
tv.tv_usec = 0;
FD_ZERO (&in);
FD_SET (sockfd, &in);
SelectVal = select ( sockfd+1, &in, 0, 0, &tv );
from_len = sizeof(struct sockaddr_in);
if ( SelectVal < 0 )
{
break;
} else if ( SelectVal != 0 )
{
len = recvfrom ( sockfd, &tmpbuf[0], sizeof(tmpbuf), 0,
(struct sockaddr *) &from, &from_len );
if (len == SOCKET_ERROR && WSAGetLastError() != WSAEMSGSIZE)
break;
if ( bGameSpy )
{
if ( len )
{
GamespyParsePacket( tmpbuf );
PostGamespyServerInfo( &from );
}
continue;
}
if ( len < ( sizeof( udp_header ) - 1 ) )
continue; // message is not even big enough for header
#ifdef DPTEST
sprintf( buf, "recieving packet" );
AddMessageToQue ( buf );
#endif
if ( !memcmp( tmpbuf, udp_header, strlen( udp_header ) ) ) // if we have correct header...
{
msgptr = tmpbuf + strlen( udp_header );
#ifdef UDP_PINGS
// ping message?
if ( !_strnicmp( msgptr, UDP_MSG_Ping, strlen( UDP_MSG_Ping ) ) )
{
// replace message with 'bounced' version
memcpy( (void *)msgptr, (void *)UDP_MSG_BouncedPing, strlen( UDP_MSG_Ping ) );
// bounce back message
GrabMutex( &SendMutex, NULL );
sendto(sockfd, tmpbuf, len, 0,
(struct sockaddr*)&from, sizeof(struct sockaddr_in));
ReleaseMutex( SendMutex );
// returned ping request?
}else if ( !_strnicmp( msgptr, UDP_MSG_BouncedPing, strlen( UDP_MSG_BouncedPing ) ) )
{
DWORD senttime;
BYTE player;
msgptr += strlen( UDP_MSG_BouncedPing );
tempptr = strstr( msgptr, "player\\" );
if ( tempptr )
{
if ( sscanf( tempptr, "player\\%d\\", &player ) != 1 )
{
player = WhoIAm;
}
}
tempptr = strstr( msgptr, "time\\" );
if ( tempptr )
{
if ( sscanf( tempptr, "time\\%d\\", &senttime ) != 1 )
{
senttime = 0;
}
}
if ( ( player != WhoIAm ) && senttime )
{
EnterCriticalSection( &PlayerInfoKey );
PlayerInfo[ player ].Ping = timeGetTime() - senttime;
if ( PlayerInfo[ player ].Ping > 999999 )
PlayerInfo[ player ].Ping = 999999;
LeaveCriticalSection( &PlayerInfoKey );
#ifdef DPTEST
sprintf( buf, "ping of %d recieved for player %d ( %s )", PlayerInfo[ player ].Ping, player, Names[ player ] );
AddMessageToQue ( buf );
#endif
}
// status request?
}else
#endif
if ( !_strnicmp( msgptr, UDP_MSG_Status, strlen( UDP_MSG_Status ) ) )
{
msgptr += strlen( UDP_MSG_Status );
if( !(*msgptr) ) // added for Kali
{
ProcessForsakenInfo( 0 );
#ifdef DPTEST
sprintf( buf, "sending status msg type 0\n");
AddMessageToQue ( buf );
#endif
PostServerInfo( &from );
}else
{
if ( sscanf( msgptr, "%x", &status_type ) == 1 )
{
ProcessForsakenInfo( status_type );
#ifdef DPTEST
sprintf( buf, "sending status msg type %d\n", status_type);
AddMessageToQue ( buf );
#endif
PostServerInfo( &from );
}
}
#ifdef DPTEST
// game info?
}else if ( !_strnicmp( msgptr, UDP_MSG_Forsaken, strlen( UDP_MSG_Forsaken ) ) )
{
msgptr += strlen( UDP_MSG_Forsaken );
AddMessageToQue( "game info recieved" );
DebugPrintf( "\nrecieving: %s\n", msgptr );
}else
{
AddMessageToQue( msgptr );
}
#else
}
#endif
}
}
}
DebugPrintf("listen thread exited OK\n");
ExitThread(0);
return 0;
}