forked from SwagSoftware/Kisak-Strike
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch_searcher.cpp
794 lines (667 loc) · 22.8 KB
/
match_searcher.cpp
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
//===== Copyright � 1996-2009, Valve Corporation, All rights reserved. ======//
//
// Purpose:
//
//===========================================================================//
#include "mm_framework.h"
#include "vstdlib/random.h"
#include "fmtstr.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
static ConVar mm_session_search_num_results( "mm_session_search_num_results", "10", FCVAR_DEVELOPMENTONLY );
static ConVar mm_session_search_distance( "mm_session_search_distance", "1", FCVAR_DEVELOPMENTONLY );
ConVar mm_session_search_qos_timeout( "mm_session_search_qos_timeout", "15.0", FCVAR_RELEASE );
// static ConVar mm_session_search_ping_limit( "mm_session_search_ping_limit", "200", FCVAR_RELEASE ); -- removed, using mm_dedicated_search_maxping instead
static ConVar mm_session_search_ping_buckets( "mm_session_search_ping_buckets", "4", FCVAR_RELEASE );
CMatchSearcher::CMatchSearcher( KeyValues *pSettings ) :
m_pSettings( pSettings ), // takes ownership
m_autodelete_pSettings( m_pSettings ),
m_pSessionSearchTree( NULL ),
m_autodelete_pSessionSearchTree( m_pSessionSearchTree ),
m_pSearchPass( NULL ),
m_eState( STATE_INIT )
{
#ifdef _X360
ZeroMemory( &m_xOverlapped, sizeof( m_xOverlapped ) );
m_pQosResults = NULL;
m_pCancelOverlappedJob = NULL;
#endif
DevMsg( "Created CMatchSearcher:\n" );
KeyValuesDumpAsDevMsg( m_pSettings, 1 );
InitializeSettings();
}
CMatchSearcher::~CMatchSearcher()
{
DevMsg( "Destroying CMatchSearcher:\n" );
KeyValuesDumpAsDevMsg( m_pSettings, 1 );
// Free all retrieved game details
CUtlVector< SearchResult_t > *arrResults[] = { &m_arrSearchResults, &m_arrSearchResultsAggregate };
for ( int ia = 0; ia < ARRAYSIZE( arrResults ); ++ ia )
{
for ( int k = 0; k < arrResults[ia]->Count(); ++ k )
{
SearchResult_t &sr = arrResults[ia]->Element( k );
if ( sr.m_pGameDetails )
{
sr.m_pGameDetails->deleteThis();
sr.m_pGameDetails = NULL;
}
}
}
}
void CMatchSearcher::InitializeSettings()
{
// Initialize only the settings required to connect...
if ( KeyValues *kv = m_pSettings->FindKey( "system", true ) )
{
KeyValuesAddDefaultString( kv, "network", "LIVE" );
KeyValuesAddDefaultString( kv, "access", "public" );
}
if ( KeyValues *pMembers = m_pSettings->FindKey( "members", true ) )
{
int numMachines = pMembers->GetInt( "numMachines", -1 );
if ( numMachines == -1 )
{
numMachines = 1;
pMembers->SetInt( "numMachines", numMachines );
}
int numPlayers = pMembers->GetInt( "numPlayers", -1 );
if ( numPlayers == -1 )
{
numPlayers = 1;
#ifdef _GAMECONSOLE
numPlayers = XBX_GetNumGameUsers();
#endif
pMembers->SetInt( "numPlayers", numPlayers );
}
pMembers->SetInt( "numSlots", numPlayers );
KeyValues *pMachine = pMembers->FindKey( "machine0");
if ( !pMachine )
{
pMachine = pMembers->CreateNewKey();
pMachine->SetName( "machine0" );
XUID machineid = g_pPlayerManager->GetLocalPlayer( XBX_GetPrimaryUserId() )->GetXUID();
pMachine->SetUint64( "id", machineid );
pMachine->SetUint64( "flags", MatchSession_GetMachineFlags() );
pMachine->SetInt( "numPlayers", numPlayers );
pMachine->SetUint64( "dlcmask", g_pMatchFramework->GetMatchSystem()->GetDlcManager()->GetDataInfo()->GetUint64( "@info/installed" ) );
pMachine->SetString( "tuver", MatchSession_GetTuInstalledString() );
pMachine->SetInt( "ping", 0 );
for ( int k = 0; k < numPlayers; ++ k )
{
if ( KeyValues *pPlayer = pMachine->FindKey( CFmtStr( "player%d", k ), true ) )
{
int iController = 0;
#ifdef _GAMECONSOLE
iController = XBX_GetUserId( k );
#endif
IPlayerLocal *player = g_pPlayerManager->GetLocalPlayer( iController );
pPlayer->SetUint64( "xuid", player->GetXUID() );
pPlayer->SetString( "name", player->GetName() );
}
}
}
}
DevMsg( "CMatchSearcher::InitializeGameSettings adjusted settings:\n" );
KeyValuesDumpAsDevMsg( m_pSettings, 1 );
}
KeyValues * CMatchSearcher::GetSearchSettings()
{
return m_pSettings;
}
void CMatchSearcher::Destroy()
{
// Stop the search
if ( m_eState == STATE_SEARCHING )
{
#ifdef _X360
m_pCancelOverlappedJob = ThreadExecute( MMX360_CancelOverlapped, &m_xOverlapped ); // UpdateDormantOperations will clean the rest
MMX360_RegisterDormant( this );
return;
#endif
}
#ifdef _X360
if ( m_eState == STATE_CHECK_QOS )
{
g_pMatchExtensions->GetIXOnline()->XNetQosRelease( m_pQosResults );
m_pQosResults = NULL;
}
#endif
#if !defined( NO_STEAM )
while ( m_arrOutstandingAsyncOperation.Count() > 0 )
{
IMatchAsyncOperation *pAsyncOperation = m_arrOutstandingAsyncOperation.Tail();
m_arrOutstandingAsyncOperation.RemoveMultipleFromTail( 1 );
if ( pAsyncOperation )
pAsyncOperation->Release();
}
#endif
delete this;
}
void CMatchSearcher::OnSearchEvent( KeyValues *pNotify )
{
g_pMatchEventsSubscription->BroadcastEvent( pNotify );
}
#ifdef _X360
bool CMatchSearcher::UpdateDormantOperation()
{
if ( !m_pCancelOverlappedJob->IsFinished() )
return true; // keep running dormant
m_pCancelOverlappedJob->Release();
m_pCancelOverlappedJob = NULL;
delete this;
return false; // destroyed object, remove from dormant list
}
#endif
void CMatchSearcher::Update()
{
switch ( m_eState )
{
case STATE_INIT:
m_eState = STATE_SEARCHING;
// Session is searching
OnSearchEvent( new KeyValues(
"OnMatchSessionUpdate",
"state", "progress",
"progress", "searching"
) );
// Kick off the search
StartSearch();
break;
case STATE_SEARCHING:
// Waiting for session search to complete
#ifdef _X360
if ( XHasOverlappedIoCompleted( &m_xOverlapped ) )
Live_OnSessionSearchCompleted();
#endif
break;
#ifdef _X360
case STATE_CHECK_QOS:
// Keep checking for results or until the wait time expires
if ( Plat_FloatTime() > m_flQosTimeout ||
!m_pQosResults->cxnqosPending )
Live_OnQosCheckCompleted();
break;
#endif
#if !defined (NO_STEAM)
case STATE_WAITING_LOBBY_DATA_AND_PING:
{
bool bWaitLonger = ( ( Plat_MSTime() - m_uiQosTimeoutStartMS ) < ( mm_session_search_qos_timeout.GetFloat() * 1000.f ) );
if ( bWaitLonger )
{
bool bHasPendingLobbyData = false;
for ( int j = 0; j < m_arrSearchResults.Count(); ++ j )
{
SearchResult_t &sr = m_arrSearchResults[j];
if ( !sr.m_numPlayers )
{ // didn't even receive lobby data from Steam yet
bHasPendingLobbyData = true;
}
else if ( sr.m_svAdr.GetIPHostByteOrder() && ( sr.m_svPing <= 0 ) )
{ // received valid IP for lobby, still pinging
bHasPendingLobbyData = true;
}
}
if ( !bHasPendingLobbyData )
bWaitLonger = false;
}
if ( !bWaitLonger )
{
m_CallbackOnLobbyDataReceived.Unregister();
// Go ahead and start joining the results
AggregateSearchPassResults();
OnSearchPassDone( m_pSearchPass );
}
}
break;
#endif
}
}
#if !defined (NO_STEAM)
void CMatchSearcher::Steam_OnLobbyDataReceived( LobbyDataUpdate_t *pLobbyDataUpdate )
{
int iResultIndex = 0;
for ( ; iResultIndex < m_arrSearchResults.Count(); ++ iResultIndex )
{
if ( m_arrSearchResults[ iResultIndex ].m_uiLobbyId == pLobbyDataUpdate->m_ulSteamIDLobby )
break;
}
if ( !m_arrSearchResults.IsValidIndex( iResultIndex ) )
return;
SearchResult_t *pRes = &m_arrSearchResults[ iResultIndex ];
if ( !pLobbyDataUpdate->m_bSuccess )
{
DevMsg( "[MM] Could not get lobby data for lobby %llu (%llx)\n",
pRes->m_uiLobbyId, pRes->m_uiLobbyId );
pRes->m_numPlayers = -1; // set numPlayers to negative number to indicate a failure here
}
else
{
// Get num players
const char *numPlayers = steamapicontext->SteamMatchmaking()->GetLobbyData(
pRes->m_uiLobbyId, "members:numPlayers" );
if ( !numPlayers )
{
DevMsg( "[MM] Unable to get num players for lobby (%llx)\n",
pRes->m_uiLobbyId );
pRes->m_numPlayers = -1; // set numPlayers to negative number to indicate a failure here
}
else
{
pRes->m_numPlayers = V_atoi( numPlayers );
if ( !pRes->m_numPlayers )
pRes->m_numPlayers = -1; // set numPlayers to negative number to indicate a failure here
}
// Get the address of the server
const char* pServerAdr = steamapicontext->SteamMatchmaking()->GetLobbyData(
pRes->m_uiLobbyId, "server:adronline" );
if ( !pServerAdr )
{
// DevMsg( "[MM] Unable to get server address from lobby (%llx)\n",
// pRes->m_uiLobbyId );
}
else
{
pRes->m_svAdr.SetFromString( pServerAdr );
DevMsg ( "[MM] Lobby %d: id %llu (%llx), num Players %d, sv ip %s, pinging dist %d\n",
iResultIndex, pRes->m_uiLobbyId, pRes->m_uiLobbyId, pRes->m_numPlayers,
pRes->m_svAdr.ToString(), pRes->m_svPing );
// Ping server
IMatchAsyncOperation *pAsyncOperationPing = NULL;
g_pMatchExtensions->GetINetSupport()->ServerPing( pRes->m_svAdr, this, &pAsyncOperationPing );
m_arrOutstandingAsyncOperation.AddToTail( pAsyncOperationPing );
pRes->m_pAsyncOperationPingWeakRef = pAsyncOperationPing;
}
}
}
// Callback for server reservation check
void CMatchSearcher::OnOperationFinished( IMatchAsyncOperation *pOperation )
{
if ( !pOperation )
return;
if ( m_eState != STATE_WAITING_LOBBY_DATA_AND_PING )
return;
for ( int i = 0; i < m_arrSearchResults.Count(); ++ i )
{
if ( m_arrSearchResults[i].m_pAsyncOperationPingWeakRef != pOperation )
continue;
int result = pOperation->GetResult();
bool failed = ( pOperation->GetState() == AOS_FAILED );
SearchResult_t *pRes = &m_arrSearchResults[i];
pRes->m_svPing = ( failed ? -1 : ( result ? result : -1 ) );
if ( pRes->m_svPing < 0 )
{
DevMsg( "[MM] Failed pinging server %s for lobby#%d %llu (%llx)\n",
pRes->m_svAdr.ToString(), i, pRes->m_uiLobbyId, pRes->m_uiLobbyId );
}
else
{
DevMsg( "[MM] Successfully pinged server %s for lobby#%d %llu (%llx) = %d ms\n",
pRes->m_svAdr.ToString(), i, pRes->m_uiLobbyId, pRes->m_uiLobbyId, pRes->m_svPing );
}
m_arrSearchResults[i].m_pAsyncOperationPingWeakRef = NULL;
}
}
#endif
void CMatchSearcher::OnSearchDone()
{
m_eState = STATE_DONE;
}
void CMatchSearcher::AggregateSearchPassResults()
{
#ifndef NO_STEAM
extern ConVar mm_dedicated_search_maxping;
int nPingLimitMax = mm_dedicated_search_maxping.GetInt();
if ( nPingLimitMax <= 0 )
nPingLimitMax = 5000;
for ( int k = 0; k < mm_session_search_ping_buckets.GetInt(); ++ k )
{
int iPingLow = ( nPingLimitMax * k ) / mm_session_search_ping_buckets.GetInt();
int iPingHigh = ( nPingLimitMax * ( k + 1 ) ) / mm_session_search_ping_buckets.GetInt();
for ( int iResult = 0; iResult < m_arrSearchResults.Count(); ++ iResult )
{
SearchResult_t *pRes = &m_arrSearchResults[iResult];
if ( ( pRes->m_svPing > iPingLow ) && ( pRes->m_svPing <= iPingHigh ) )
{
m_arrSearchResultsAggregate.AddToTail( *pRes );
DevMsg ( "[MM] Search aggregated result%d / %d: id %llu (%llx), num Players %d, sv ip %s, dist %d\n",
m_arrSearchResultsAggregate.Count(), iResult, pRes->m_uiLobbyId, pRes->m_uiLobbyId, pRes->m_numPlayers,
pRes->m_svAdr.ToString(), pRes->m_svPing );
}
}
}
DevMsg ( "[MM] Search aggregated %d results\n", m_arrSearchResultsAggregate.Count() );
#endif
m_arrSearchResults.Purge();
}
void CMatchSearcher::OnSearchPassDone( KeyValues *pSearchPass )
{
// If we have enough results, then call it done
if ( m_arrSearchResultsAggregate.Count() >= mm_session_search_num_results.GetInt() )
{
OnSearchDone();
return;
}
// Evaluate if there is a nextpass condition
char const *szNextPassKeyName = "nextpass";
if ( KeyValues *pConditions = pSearchPass->FindKey( "nextpass?" ) )
{
// Inspect conditions and select which next pass will happen
}
if ( KeyValues *pNextPass = pSearchPass->FindKey( szNextPassKeyName ) )
{
StartSearchPass( pNextPass );
}
else
{
OnSearchDone();
}
}
#ifdef _X360
void CMatchSearcher::Live_OnSessionSearchCompleted()
{
DevMsg( "Received %d search results from Xbox LIVE.\n", GetXSearchResult()->dwSearchResults );
for( unsigned int i = 0; i < GetXSearchResult()->dwSearchResults; ++ i )
{
XSESSION_SEARCHRESULT const &xsr = GetXSearchResult()->pResults[i];
SearchResult_t sr = { xsr.info, NULL };
m_arrSearchResults.AddToTail( sr );
DevMsg( 2, "Result #%02d: %llx\n", i + 1, ( const uint64& ) xsr.info.sessionID );
}
if ( !m_arrSearchResults.Count() )
{
OnSearchPassDone( m_pSearchPass );
}
else
{
DevMsg( "Checking QOS with %d search results.\n", m_arrSearchResults.Count() );
Live_CheckSearchResultsQos();
}
}
void CMatchSearcher::Live_CheckSearchResultsQos()
{
m_eState = STATE_CHECK_QOS;
int nResults = m_arrSearchResults.Count();
CUtlVector< const void * > memQosData;
memQosData.SetCount( 3 * nResults );
const void ** bufQosData[3];
for ( int k = 0; k < ARRAYSIZE( bufQosData ); ++ k )
bufQosData[k] = &memQosData[ k * nResults ];
for ( int k = 0; k < m_arrSearchResults.Count(); ++ k )
{
SearchResult_t const &sr = m_arrSearchResults[k];
bufQosData[0][k] = &sr.m_info.hostAddress;
bufQosData[1][k] = &sr.m_info.sessionID;
bufQosData[2][k] = &sr.m_info.keyExchangeKey;
}
//
// Note: XNetQosLookup requires only 2 successful probes to be received from the host.
// This is much less than the recommended 8 probes because on a 10% data loss profile
// it is impossible to find the host when requiring 8 probes to be received.
m_flQosTimeout = Plat_FloatTime() + mm_session_search_qos_timeout.GetFloat();
int res = g_pMatchExtensions->GetIXOnline()->XNetQosLookup(
nResults,
reinterpret_cast< XNADDR const ** >( bufQosData[0] ),
reinterpret_cast< XNKID const ** >( bufQosData[1] ),
reinterpret_cast< XNKEY const ** >( bufQosData[2] ),
0, // number of security gateways to probe
NULL, // gateway ip addresses
NULL, // gateway service ids
2, // number of probes
0, // upstream bandwith to use (0 = default)
0, // flags - not supported
NULL, // signal event
&m_pQosResults );// results
if ( res != 0 )
{
DevWarning( "OnlineSearch::Live_CheckSearchResultsQos - XNetQosLookup failed (code = 0x%08X)!\n", res );
m_arrSearchResults.Purge();
OnSearchPassDone( m_pSearchPass );
}
}
void CMatchSearcher::Live_OnQosCheckCompleted()
{
for ( uint k = m_pQosResults->cxnqos; k --> 0; )
{
XNQOSINFO &xqi = m_pQosResults->axnqosinfo[k];
BYTE uNeedFlags = XNET_XNQOSINFO_TARGET_CONTACTED | XNET_XNQOSINFO_DATA_RECEIVED;
if ( ( ( xqi.bFlags & uNeedFlags ) != uNeedFlags) ||
( xqi.bFlags & XNET_XNQOSINFO_TARGET_DISABLED ) )
{
m_arrSearchResults.Remove( k );
continue;
}
extern ConVar mm_dedicated_search_maxping;
if ( mm_dedicated_search_maxping.GetInt() > 0 &&
xqi.wRttMedInMsecs > mm_dedicated_search_maxping.GetInt() )
{
m_arrSearchResults.Remove( k );
continue;
}
if ( xqi.cbData && xqi.pbData )
{
MM_GameDetails_QOS_t gd = { xqi.pbData, xqi.cbData, xqi.wRttMedInMsecs };
Assert( !m_arrSearchResults[k].m_pGameDetails );
m_arrSearchResults[k].m_pGameDetails = g_pMatchFramework->GetMatchNetworkMsgController()->UnpackGameDetailsFromQOS( &gd );
}
}
g_pMatchExtensions->GetIXOnline()->XNetQosRelease( m_pQosResults );
m_pQosResults = NULL;
// Go ahead and start joining the results
DevMsg( "Qos completed with %d search results.\n", m_arrSearchResults.Count() );
AggregateSearchPassResults();
OnSearchPassDone( m_pSearchPass );
}
#elif !defined( NO_STEAM )
void CMatchSearcher::Steam_OnLobbyMatchListReceived( LobbyMatchList_t *pLobbyMatchList, bool bError )
{
Msg( "[MM] Received %d search results.\n", bError ? 0 : pLobbyMatchList->m_nLobbiesMatching );
m_CallbackOnLobbyDataReceived.Register( this, &CMatchSearcher::Steam_OnLobbyDataReceived );
// Walk through search results and request lobby data
for ( int iLobby = 0; iLobby < (int) ( bError ? 0 : pLobbyMatchList->m_nLobbiesMatching ); ++ iLobby )
{
uint64 uiLobbyId = steamapicontext->SteamMatchmaking()->GetLobbyByIndex( iLobby ).ConvertToUint64();
SearchResult_t sr = { uiLobbyId };
sr.m_pGameDetails = NULL;
sr.m_svAdr.SetFromString( "0.0.0.0" );
sr.m_svPing = 0;
sr.m_numPlayers = 0;
sr.m_pAsyncOperationPingWeakRef = NULL;
m_arrSearchResults.AddToTail( sr );
steamapicontext->SteamMatchmaking()->RequestLobbyData( sr.m_uiLobbyId );
}
m_eState = STATE_WAITING_LOBBY_DATA_AND_PING; // Waiting for lobby data
m_uiQosPingLastMS = m_uiQosTimeoutStartMS = Plat_MSTime();
}
KeyValues * CMatchSearcher::SearchResult_t::GetGameDetails() const
{
if ( !m_pGameDetails )
{
m_pGameDetails = g_pMatchFramework->GetMatchNetworkMsgController()->UnpackGameDetailsFromSteamLobby( m_uiLobbyId );
}
return m_pGameDetails;
}
#endif
void CMatchSearcher::StartSearch()
{
Assert( !m_pSessionSearchTree );
m_pSessionSearchTree = g_pMMF->GetMatchTitleGameSettingsMgr()->DefineSessionSearchKeys( m_pSettings );
m_autodelete_pSessionSearchTree.Assign( m_pSessionSearchTree );
Assert( m_pSessionSearchTree );
if ( !m_pSessionSearchTree )
{
DevWarning( "OnlineSearch::StartSearch failed to build filter list!\n" );
OnSearchDone();
}
else
{
StartSearchPass( m_pSessionSearchTree );
}
}
void CMatchSearcher::StartSearchPass( KeyValues *pSearchPass )
{
KeyValues *pSearchParams = pSearchPass;
m_pSearchPass = pSearchPass;
// Make sure we have fresh buffer for search results
m_arrSearchResults.Purge();
m_eState = STATE_SEARCHING;
DevMsg( "OnlineSearch::StartSearchPass:\n" );
KeyValuesDumpAsDevMsg( pSearchParams, 1 );
#ifdef _X360
DWORD dwSearchRule = pSearchParams->GetInt( "rule" );
m_arrContexts.RemoveAll();
if ( KeyValues *pContexts = pSearchParams->FindKey( "Contexts" ) )
{
for ( KeyValues *val = pContexts->GetFirstValue(); val; val = val->GetNextValue() )
{
XUSER_CONTEXT ctx = { 0 };
ctx.dwContextId = atoi( val->GetName() );
if ( val->GetDataType() == KeyValues::TYPE_INT )
{
ctx.dwValue = val->GetInt();
m_arrContexts.AddToTail( ctx );
}
}
}
m_arrProperties.RemoveAll();
if ( KeyValues *pContexts = pSearchParams->FindKey( "Properties" ) )
{
for ( KeyValues *val = pContexts->GetFirstValue(); val; val = val->GetNextValue() )
{
XUSER_PROPERTY prop = { 0 };
prop.dwPropertyId = atoi( val->GetName() );
if ( val->GetDataType() == KeyValues::TYPE_INT )
{
prop.value.type = XUSER_DATA_TYPE_INT32;
prop.value.nData = val->GetInt();
m_arrProperties.AddToTail( prop );
}
}
}
DWORD ret = ERROR_SUCCESS;
DWORD numBytes = 0;
DWORD dwNumSlotsRequired = pSearchParams->GetInt( "numPlayers" );
//
// Issue the asynchrounous session search request
//
ret = g_pMatchExtensions->GetIXOnline()->XSessionSearchEx(
dwSearchRule, XBX_GetPrimaryUserId(), mm_session_search_num_results.GetInt(),
dwNumSlotsRequired,
m_arrProperties.Count(), m_arrContexts.Count(),
m_arrProperties.Base(), m_arrContexts.Base(),
&numBytes, NULL, NULL
);
// Log the search request to read X360 queries easier
DevMsg( "XSessionSearchEx by rule %d for slots %d\n", dwSearchRule, dwNumSlotsRequired );
for ( int k = 0; k < m_arrContexts.Count(); ++ k )
DevMsg( " CTX %u/0x%08X = 0x%X/%u\n", m_arrContexts[k].dwContextId, m_arrContexts[k].dwContextId, m_arrContexts[k].dwValue, m_arrContexts[k].dwValue );
for ( int k = 0; k < m_arrProperties.Count(); ++ k )
DevMsg( " PRP %u/0x%08X = 0x%X/%u\n", m_arrProperties[k].dwPropertyId, m_arrProperties[k].dwPropertyId, m_arrProperties[k].value.nData, m_arrProperties[k].value.nData );
DevMsg( "will use %u bytes buffer.\n", numBytes );
if ( ERROR_INSUFFICIENT_BUFFER == ret && numBytes > 0 )
{
m_bufSearchResultHeader.EnsureCapacity( numBytes );
ZeroMemory( GetXSearchResult(), numBytes );
ZeroMemory( &m_xOverlapped, sizeof( m_xOverlapped ) );
DevMsg( "Searching...\n" );
ret = g_pMatchExtensions->GetIXOnline()->XSessionSearchEx(
dwSearchRule, XBX_GetPrimaryUserId(), mm_session_search_num_results.GetInt(),
dwNumSlotsRequired,
m_arrProperties.Count(), m_arrContexts.Count(),
m_arrProperties.Base(), m_arrContexts.Base(),
&numBytes, GetXSearchResult(), &m_xOverlapped
);
if ( ret == ERROR_IO_PENDING )
return;
}
// Otherwise search failed
DevWarning( "XSessionSearchEx failed (code = 0x%08X)\n", ret );
ZeroMemory( &m_xOverlapped, sizeof( m_xOverlapped ) );
OnSearchPassDone( m_pSearchPass );
#elif !defined( NO_STEAM )
ISteamMatchmaking *mm = steamapicontext->SteamMatchmaking();
// Configure filters
DWORD dwNumSlotsRequired = pSearchParams->GetInt( "numPlayers" );
mm->AddRequestLobbyListFilterSlotsAvailable( dwNumSlotsRequired );
// Set filters
char const * arrKeys[] = { "Filter<", "Filter<=", "Filter=", "Filter<>", "Filter>=", "Filter>" };
ELobbyComparison nValueCmp[] = { k_ELobbyComparisonLessThan, k_ELobbyComparisonEqualToOrLessThan, k_ELobbyComparisonEqual, k_ELobbyComparisonNotEqual, k_ELobbyComparisonEqualToOrGreaterThan, k_ELobbyComparisonGreaterThan };
for ( int k = 0; k < ARRAYSIZE( arrKeys ); ++ k )
{
if ( KeyValues *kv = pSearchParams->FindKey( arrKeys[k] ) )
{
for ( KeyValues *val = kv->GetFirstValue(); val; val = val->GetNextValue() )
{
if ( val->GetDataType() == KeyValues::TYPE_STRING )
{
mm->AddRequestLobbyListStringFilter( val->GetName(), val->GetString(), nValueCmp[k] );
}
else if ( val->GetDataType() == KeyValues::TYPE_INT )
{
mm->AddRequestLobbyListNumericalFilter( val->GetName(), val->GetInt(), nValueCmp[k] );
}
}
}
}
// Set ordering near values
if ( KeyValues *kv = pSearchParams->FindKey( "Near" ) )
{
for ( KeyValues *val = kv->GetFirstValue(); val; val = val->GetNextValue() )
{
if ( val->GetDataType() == KeyValues::TYPE_INT )
{
mm->AddRequestLobbyListNearValueFilter( val->GetName(), val->GetInt() );
}
else
{
// TODO: need to set near value filter for strings
}
}
}
ELobbyDistanceFilter eFilter = k_ELobbyDistanceFilterDefault;
switch ( mm_session_search_distance.GetInt() )
{
case k_ELobbyDistanceFilterClose:
case k_ELobbyDistanceFilterDefault:
case k_ELobbyDistanceFilterFar:
case k_ELobbyDistanceFilterWorldwide:
eFilter = ( ELobbyDistanceFilter ) mm_session_search_distance.GetInt();
break;
}
// Set distance filter to Near
mm->AddRequestLobbyListDistanceFilter( eFilter );
// Set dependent lobby
if ( uint64 uiDependentLobbyId = pSearchParams->GetUint64( "DependentLobby", 0ull ) )
{
mm->AddRequestLobbyListCompatibleMembersFilter( uiDependentLobbyId );
}
// RequestLobbyList - will get called back at Steam_OnLobbyMatchListReceived
DevMsg( "Searching...\n" );
SteamAPICall_t hCall = mm->RequestLobbyList();
m_CallbackOnLobbyMatchListReceived.Set( hCall, this, &CMatchSearcher::Steam_OnLobbyMatchListReceived );
#endif
}
//
// Results retrieval overrides
//
bool CMatchSearcher::IsSearchFinished() const
{
return m_eState == STATE_DONE;
}
int CMatchSearcher::GetNumSearchResults() const
{
return IsSearchFinished() ? m_arrSearchResultsAggregate.Count() : 0;
}
CMatchSearcher::SearchResult_t const & CMatchSearcher::GetSearchResult( int idx ) const
{
if ( !IsSearchFinished() || !m_arrSearchResultsAggregate.IsValidIndex( idx ) )
{
Assert( false );
static SearchResult_t s_empty;
return s_empty;
}
else
{
return m_arrSearchResultsAggregate[ idx ];
}
}