summaryrefslogtreecommitdiff
path: root/public/gcsdk/gcbase.h
blob: fbefbc0d40d0c083b84be9a9ff2fe0c9f8a6b87f (plain) (blame)
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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#ifndef GCBASE_H
#define GCBASE_H
#ifdef _WIN32
#pragma once
#endif

#include "gamecoordinator/igamecoordinator.h"
#include "gamecoordinator/igamecoordinatorhost.h"
#include "tier1/utlallocation.h"
#include "gcmsg.h"
#include "jobmgr.h"
#include "tier1/thash.h"
#include "tier1/UtlSortVector.h"
#include "http.h"
#include "language.h"
#include "accountdetails.h"
#include "gcsession.h"

class CGCMsgGetSystemStatsResponse;

extern EUniverse GetUniverse();

const int16 k_nLockTypeLobby             =   3;
const int16 k_nLockTypeParty             =   2;
const int16	k_nLockTypeIndividual        =   1;
const int16	k_nLockTypeGameServer        =   0;
const int16 k_nLockTypeGroupIDGeneration =  -1;
 // For one-off locks of specific resources. The specific game controls the subtype ordering.
const int16	k_nLockTypeGameMisc          = -10;

namespace GCSDK
{
class CGCSession;
class CGCUserSession;
class CGCGSSession;
class CGCSharedObjectCache;
class CSharedObject;
class CAccountDetails;
class CScopedSteamIDLock;

struct PackageLicense_t
{
	uint32 m_unPackageID;
	RTime32 m_rtimeCreated;
};

#define SERVER_KEY_HASH 0x5a4f4944u

class CGCBase : public IGameCoordinator
{
public:
	CGCBase( );
	virtual ~CGCBase();

	// Hooks to extend the base behaviors of the IGameCoordinator interface
	virtual bool OnInit() { return true; }
	virtual bool OnMainLoopOncePerFrame( CLimitTimer &limitTimer ) { return false; }
	virtual bool OnMainLoopUntilFrameCompletion( CLimitTimer &limitTimer ) { return false; }
	virtual void OnUninit() {}
	// returns true if this function handled the message
	virtual bool OnMessageFromClient( const CSteamID & senderID, uint32 unMsgType, void *pubData, uint32 cubData ) { return false; }
	virtual void OnValidate( CValidator &validator, const char *pchName ) {}
	virtual const char *LocalizeToken( const char *pchToken, ELanguage eLanguage, bool bReturnTokenIfNotFound = true ) { return bReturnTokenIfNotFound ? pchToken : NULL; }

	virtual void YldOnAccountPhoneVerificationChange( CSteamID steamID ) {}
	virtual void YldOnAccountTwoFactorChange( CSteamID steamID ) {}

	/// The main loop calls Run() on game servers and user sessions until a certain amount of time
	/// is expired.  Thus the user list is iterated, but the iteration is amortized over multiple frames.
	/// this function is called when a sweep of the user sessions ends and a new one is about to begin.
	virtual void FinishedMainLoopUserSweep();

	// Life cycle management functions

	// Called to do any yielding initialization work before reporting as fully operational
	virtual bool BYieldingFinishStartup() = 0;

	// Called to do any yielding work immediately after becoming full operational
	virtual bool BYieldingPostStartup() = 0;

	// Call to report that we're fully operational
	void SetStartupComplete( bool bSuccess );

	// Called to do any yielding work before reporting as ready to shutdown
	virtual void YieldingGracefulShutdown() = 0;


	virtual CGCUserSession *CreateUserSession( const CSteamID & steamID, CGCSharedObjectCache *pSOCache ) const;
	virtual CGCGSSession *CreateGSSession( const CSteamID & steamID, CGCSharedObjectCache *pSOCache, uint32 unServerAddr, uint16 usServerPort ) const;
	virtual void YieldingSessionStartPlaying( CGCUserSession *pSession ) {}
	virtual void YieldingSessionStopPlaying( CGCUserSession *pSession ) {}
	virtual void YieldingSessionStartServer( CGCGSSession *pSession ) {}
	virtual void YieldingSessionStopServer( CGCGSSession *pSession ) {}
	virtual void YieldingSOCacheLoaded( CGCSharedObjectCache *pSOCache );
	virtual void UpdateGSSessionAddress( CGCGSSession *pSession, uint32 unServerAddr, uint16 usServerPort );

	virtual void YieldingPreTestSetup()	{}

	// cache management
	CGCSharedObjectCache *YieldingGetLockedSOCache( const CSteamID &steamID, const char *pszFilename, int nLineNum );
	CGCSharedObjectCache *YieldingFindOrLoadSOCache( const CSteamID &steamID );
	CGCSharedObjectCache *FindSOCache( const CSteamID & steamID );					// non-yielding, but may return NULL if the cache exists but is not loaded
	bool UnloadUnusedCaches( uint32 unMaxCacheCount, CLimitTimer *pLimitTimer = NULL );
	void YieldingReloadCache( CGCSharedObjectCache *pSOCache );
	virtual CGCSharedObjectCache *CreateSOCache( const CSteamID &steamID );
	void FlushInventoryCache( AccountID_t unAccountID );

	CGCUserSession *YieldingGetLockedUserSession( const CSteamID & steamID, const char *pszFilename, int nLineNum );
	CGCUserSession *FindUserSession( const CSteamID & steamID ) const;
	bool BUserSessionPending( const CSteamID & steamID ) const;

	CGCGSSession *YieldingGetLockedGSSession( const CSteamID & steamID, const char *pszFilename, int nLineNum );
	CGCGSSession *YieldingFindOrCreateGSSession( const CSteamID & steamID, uint32 unServerAddr, uint16 usServerPort, const uint8 *pubVarData, uint32 cubVarData );
	CGCGSSession *FindGSSession( const CSteamID & steamID ) const;

	/// Lookup the user or GS session, depending on whether the supplied
	/// Steam ID is an individual or gameserver ID
	CGCSession *FindUserOrGSSession( const CSteamID & steamID ) const;

	CGCSession *YieldingRequestSession( const CSteamID & steamID );
	bool BYieldingIsOnline( const CSteamID & steamID );
	bool BSendWebApiRegistration();

	// Will return true, indicating the request was sent, a response was received, and the response had a valid
	// status code, or false, indicating that there was a transport-layer problem -- the request timed out, or
	// a response came back with an invalid status code, etc. All "false" return values are meant to indicate
	// temporary errors.
	bool BYieldingSendHTTPRequest( const CHTTPRequest *pRequest, CHTTPResponse *pResponse );

	// Possible return values:
	//		k_EResultOK					-- everything went fine and pkvResponse has been populated.
	//		k_EResultTimeout			-- there was a temporary/transport-layer problem (see "BYieldingSendHTTPRequest").
	//									   These are temporary errors, or programming errors on our side.
	//		k_EResultRemoteCallFailed	-- we didn't timeout but we got a bad, unparseable, or otherwise invalid response
	//									   back and so we don't have data we can use.
	//		k_EResultFail				-- something has gone catastrophically wrong and no forward progress can be
	//									   expected to be made. May or may not ever be returned.
	EResult YieldingSendHTTPRequestKV( const CHTTPRequest *pRequest, KeyValues *pkvResponse );

	int GetSOCacheCount() const;
	bool IsSOCached( const CSharedObject *pObj, uint32 nTypeID ) const;

	int GetUserSessionCount() const;
	int GetGSSessionCount() const;

	void SetIsShuttingDown();
	bool GetIsShuttingDown() const { return m_bIsShuttingDown; }

	// Iterate over sessions
	// WARNING: Don't yield between GetFirst*/GetNext*.  Use caution when using
	// these any time you might have tens of thousands of sessions to iterate through.
	CGCUserSession **GetFirstUserSession()								{ return m_hashUserSessions.PvRecordFirst(); }
	CGCUserSession **GetNextUserSession( CGCUserSession **pUserSession )		{ return m_hashUserSessions.PvRecordNext( pUserSession ); }
	CGCGSSession **GetFirstGSSession()								{ return m_hashGSSessions.PvRecordFirst(); }
	CGCGSSession **GetNextGSSession( CGCGSSession **pGSSession )		{ return m_hashGSSessions.PvRecordNext( pGSSession ); }

	AppId_t GetAppID() const { return m_unAppID; }
	bool BIsStartupComplete() const { return m_bStartupComplete; }
	CJobMgr &GetJobMgr() { return m_JobMgr; }

	CSteamID YieldingGuessSteamIDFromInput( const char *pchInput );
	bool BYieldingRecordSupportAction( const CSteamID & actorID, const CSteamID & targetID, const char *pchData, const char *pchNote );
	void PostAlert( EAlertType eAlertType, bool bIsCritical, const char *pchAlertText, const CUtlVector< CUtlString > *pvecExtendedInfo = NULL, bool bAlsoSpew = true );
	const CAccountDetails *YieldingGetAccountDetails( const CSteamID & steamID, bool bForceReload = false );
	const char *YieldingGetPersonaName( const CSteamID & steamID, const char *pchUnknownName = NULL );
	void PreloadPersonaName( const CSteamID & steamID );
	void ClearCachedPersonaName( const CSteamID & steamID );
	bool BYieldingGetAccountLicenses( const CSteamID & steamID, CUtlVector< PackageLicense_t > & vecPackages );
	bool BYieldingLookupAccount( EAccountFindType eFindType, const char *pchInput, CUtlVector< CSteamID > *prSteamIDs );
	bool BYieldingAddFreeLicense( const CSteamID & steamID, uint32 unPackageID, uint32 unIPPublic = 0, const char *pchStoreCountryCode = NULL );
	int  YieldingGrantGuestPass( const CSteamID & steamID, uint32 unPackageID, uint32 unPassesToGrant = 1, int32 nDaysToExpiration = -1 );

	bool BSendGCMsgToClient( const CSteamID & steamIDTarget, const CGCMsgBase& msg );
	bool BSendGCMsgToClient( const CSteamID & steamIDTarget, const CProtoBufMsgBase& msg );

	//sends a message to the system (GCH or GC.exe)
	bool BSendSystemMessage( const CGCMsgBase& msg, uint32 *pcubSent = NULL );
	bool BSendSystemMessage( const CProtoBufMsgBase& msg, uint32 *pcubSent = NULL );
	bool BSendSystemMessage( const ::google::protobuf::Message &msgOut, MsgType_t eSendMsg );

	//utilities that will send a message and then wait for the specified reply. This will return false if no reply is sent in the default timeout, or the message or status 
	//don't match what is expected. This can only be called from within a job
	bool BYldSendMessageAndGetReply( const CSteamID &steamIDTarget, CProtoBufMsgBase &msgOut, CProtoBufMsgBase *pMsgIn, MsgType_t eMsg );
	//bool BYldSendGCMessageAndGetReply( int32 nGCDirIndex, CProtoBufMsgBase &msgOut, CProtoBufMsgBase *pMsgIn, MsgType_t eMsg );
	bool BYldSendSystemMessageAndGetReply( CGCMsgBase &msgOut, CGCMsgBase *pMsgIn, MsgType_t eMsg );
	bool BYldSendSystemMessageAndGetReply( CProtoBufMsgBase &msgOut, CProtoBufMsgBase *pMsgIn, MsgType_t eMsg );
	bool BYldSendSystemMessageAndGetReply( const ::google::protobuf::Message &msgSend, MsgType_t eSendMsg, ::google::protobuf::Message *pMsgResponse, MsgType_t eRespondMsg );

	bool BReplyToMessage( CGCMsgBase &msgOut, const CGCMsgBase &msgIn );
	bool BReplyToMessage( CProtoBufMsgBase &msgOut, const CProtoBufMsgBase &msgIn );

	//sending messages to clients or as responses with pre-serialized bodies so that the work to generate the message and body doesn't have to be done multiple times
	bool BSendGCMsgToClientWithPreSerializedBody( const CSteamID & steamIDTarget, MsgType_t eMsgType, const CMsgProtoBufHeader& hdr, const byte *pubBody, uint32 cubBody ) const;
	bool BSendGCMsgToSystemWithPreSerializedBody( MsgType_t eMsgType, const CMsgProtoBufHeader& hdr, const byte *pubBody, uint32 cubBody ) const;
	bool BReplyToMessageWithPreSerializedBody( MsgType_t eMsgType, const CProtoBufMsgBase &msgIn, const byte *pubBody, uint32 cubBody ) const;

	const char *GetPath() const { return m_sPath; }
	virtual const char *GetSteamAPIKey();

	void QueueStartPlaying( const CSteamID & steamID, const CSteamID & gsSteamID, uint32 unServerAddr, uint16 usServerPort, const uint8 *pubVarData, uint32 cubVarData );
	void YieldingExecuteNextStartPlaying();
	bool BIsInLogonSurge() const { return m_nLogonSurgeFramesRemaining > 0; }

	/// Are we too busy to process any "low service level guarantee" WebAPI jobs?
	bool BShouldThrottleLowServiceLevelWebAPIJobs() const;

	/// Removes the entry in the start playing queue for the specified
	/// Steam ID, if one exists.  Returns true if it was found
	/// and removed, false if no entry existed
	bool BRemoveStartPlayingQueueEntry( const CSteamID & steamID );

	void YieldingStopPlaying( const CSteamID & steamID );
	void YieldingStopGameserver( const CSteamID & steamID );
	bool BIsSOCacheBeingLoaded( const CSteamID & steamID ) const { return m_rbtreeSOCachesBeingLoaded.Find( steamID ) != m_rbtreeSOCachesBeingLoaded.InvalidIndex(); }

	bool BYieldingLockSteamID( const CSteamID &steamID, const char *pszFilename, int nLineNum );
	bool BYieldingLockSteamIDPair( const CSteamID &steamIDA, const CSteamID &steamIDB, const char *pszFilename, int nLineNum );
	bool BLockSteamIDImmediate( const CSteamID &steamID );
	void UnlockSteamID( const CSteamID &steamID );
	bool IsSteamIDLocked( const CSteamID &steamID );
	bool IsSteamIDLockedByJob( const CSteamID &steamID, const CJob *pJob ) const;
	bool IsSteamIDLockedByCurJob( const CSteamID &steamID ) const;
	bool IsSteamIDUnlockedOrLockedByCurJob( const CSteamID &steamID );
	CJob *PJobHoldingLock( const CSteamID &steamID );

	const CLock *FindSteamIDLock( const CSteamID &steamID );
	void DumpSteamIDLocks( bool bFull, int nMax = 10 );
	void DumpJobs( const char *pszJobName, int nMax, int nPrintLocksMax ) const;
	void DumpJob( JobID_t jobID, int nPrintLocksMax ) const;
	virtual void Dump() const;
	void VerifySOCacheLRU();

	void SetProfilingEnabled( bool bEnabled );
	void SetDumpVprofImbalances( bool bEnabled );
	bool GetVprofImbalances();

	bool YieldingWritebackDirtyCaches( uint32 unSecondToDelayWrite );
	void AddCacheToWritebackQueue( CGCSharedObjectCache *pSOCache );

	bool BYieldingRetrieveCacheVersion( CGCSharedObjectCache *pSOCache );
	void AddCacheToVersionChangedList( CGCSharedObjectCache *pSOCache );

	const char *GetCDNURL() const;

	struct GCMemcachedBuffer_t
	{
		const void *m_pubData;
		int m_cubData;
	};

	struct GCMemcachedGetResult_t
	{
		bool m_bKeyFound;					// true if the key was found
		CUtlAllocation m_bufValue;			// the value of the key
	};
	
	// Memcached access
	bool BMemcachedSet( const char *pKey, const ::google::protobuf::Message &protoBufObj );
	bool BMemcachedDelete( const char *pKey );
	bool BYieldingMemcachedGet( const char *pKey, ::google::protobuf::Message &protoBufObj );
	bool BMemcachedSet( const CUtlString &strKey, const CUtlBuffer &buf );
	bool BMemcachedSet( const CUtlVector<CUtlString> &vecKeys, const CUtlVector<GCMemcachedBuffer_t> &vecValues );
	bool BMemcachedDelete( const CUtlString &strKey );
	bool BMemcachedDelete( const CUtlVector<CUtlString> &vecKeys );
	bool BYieldingMemcachedGet( const CUtlString &strKey, GCMemcachedGetResult_t &result );
	bool BYieldingMemcachedGet( const CUtlVector<CUtlString> &vecKeys, CUtlVector<GCMemcachedGetResult_t> &vecResults );

	// IP location
	bool BYieldingGetIPLocations( CUtlVector<uint32> &vecIPs, CUtlVector<CIPLocationInfo> &infos );

	/// Check if any of the sessions don't have geolocation info, then fetch
	/// the info we can.  The supplied SteamID's may refer to individuals or
	/// game servers
	bool BYieldingUpdateGeoLocation( CUtlVector<CSteamID> const &requestedVecSteamIds );

	// Stats
	virtual void SystemStats_Update( CGCMsgGetSystemStatsResponse &msgStats );

	//called to determine the amount of uptime this GC has had
	uint32 GetGCUpTime() const;
	RTime32 GetGCInitTime() const		{ return m_nInitTime; }

protected:
	virtual bool BYieldingLoadSOCache( CGCSharedObjectCache *pSOCache );
	void RemoveSOCache( const CSteamID & steamID );

	void AddCacheToLRU( CGCSharedObjectCache * pSOCache );
	void RemoveCacheFromLRU( CGCSharedObjectCache * pSOCache );

	void SetStartupComplete() { m_bStartupComplete = true; }
private:

	static void AssertCallbackFunc( const char *pchFile, int nLine, const char *pchMessage );
	void UpdateSOCacheVersions();

	//
	// Create and initialize player / gameserver sessions.  This should be called only from two places:
	// - YieldingExecuteNextStartPlaying()
	// - YieldingRequestSession(), which can be called ANY TIME we ask for a locked session but don't have one
	//
	void YieldingStartPlaying( const CSteamID & steamID, const CSteamID & gsSteamID, uint32 unServerAddr, uint16 usServerPort, CUtlBuffer *pVarData );
	void YieldingStartGameserver( const CSteamID & steamID, uint32 unServerAddr, uint16 usServerPort, const uint8 *pubVarData, uint32 cubVarData );

	void YieldingExecuteStartPlayingQueueEntryByIndex( int idxStartPlayingQueue );

	// Base behaviors of the IGameCoordinator interface. These are not overridable.
	// They each call the On* version below, which is overridable by subclasses
	virtual bool BInit( AppId_t unAppID, const char *pchAppPath, IGameCoordinatorHost *pHost );
	virtual bool BMainLoopOncePerFrame( uint64 ulLimitMicroseconds );
	virtual bool BMainLoopUntilFrameCompletion( uint64 ulLimitMicroseconds );
	virtual void Shutdown();
	virtual void Uninit();
	virtual void MessageFromClient( const CSteamID & senderID, uint32 unMsgType, void *pubData, uint32 cubData );
	virtual void Validate( CValidator &validator, const char *pchName );
	virtual void SQLResults( GID_t gidContextID );

	static void SetUserSessionDetails( CGCUserSession *pUserSession, KeyValues *pkvDetails );

	// Remembers the console spew func we overrode so we can restore it at uninit
	SpewOutputFunc_t m_OutputFuncPrev;

	// profiling
	bool m_bStartProfiling;
	bool m_bStopProfiling;
	bool m_bDumpVprofImbalances;

	//the time that the GC started so that we can compute uptime
	RTime32	m_nInitTime;
	RTime32 m_nStartupCompleteTime;

	// local job handling
	CJobMgr m_JobMgr;
	CGCWGJobMgr m_wgJobMgr;

	// session tracking
	CTHash<CGCUserSession *, uint64> m_hashUserSessions;
	CTHash<CGCGSSession *, uint64> m_hashGSSessions;
	CUtlHashMapLarge< uint64, CGCGSSession* > m_mapGSSessionsByNetAddress;

	// Shared object caches
	CUtlHashMapLarge<CSteamID, CGCSharedObjectCache *> m_mapSOCache;
	CUtlVector< CGCSharedObjectCache * >m_vecCacheWritebacks;
	CUtlLinkedList< CSteamID, uint32> m_listCachesToUnload;
	CUtlRBTree<CSteamID, int > m_rbtreeSOCachesBeingLoaded;
	CUtlRBTree<CSteamID, int > m_rbtreeSOCachesWithDirtyVersions;
	CUtlRBTree< AccountID_t, int32, CDefLess< AccountID_t > > m_rbFlushInventoryCacheAccounts;
	JobID_t m_jobidFlushInventoryCacheAccounts;
	uint32 m_numFlushInventoryCacheAccountsLastScheduled;

	// steamID locks
	CTHash<CLock, CSteamID>	m_hashSteamIDLocks;

	// Account Details
	CAccountDetailsManager	m_AccountDetailsManager;

	// State
	AppId_t					m_unAppID;
	CUtlString				m_sPath;
	IGameCoordinatorHost	*m_pHost;
	bool					m_bStartupComplete;
	bool					m_bIsShuttingDown;

	struct StartPlayingWork_t 
	{
		CSteamID m_steamID;
		CSteamID m_gsSteamID;
		uint32 m_unServerAddr;
		uint16 m_usServerPort;
		CUtlBuffer *m_pVarData;
	};
	CUtlLinkedList< StartPlayingWork_t, int > m_llStartPlaying;
	CUtlMap< CSteamID, int, int > m_mapStartPlayingQueueIndexBySteamID;

	/// Number of active jobs
	int m_nStartPlayingJobCount;

	// URL to use for our app's CDN'd images
	mutable CUtlString m_sCDNURL;

	/// Number of active jobs currently inside YieldingRequestSession
	int m_nRequestSessionJobsActive;

	/// Number of frames to wait before checking to
	/// see if we're done with logon surge.  This will
	/// be nonzero while in logon surge, and zero
	/// if we're not in logon surge.
	int m_nLogonSurgeFramesRemaining;

	//rate limiting system 
	CSteamIDRateLimit	m_MsgRateLimit;

	//a map of the HTTP error messages so we can batch them up as they occur as they tend to be very spammy
	void ReportHTTPError( const char* pszError, CGCEmitGroup::EMsgLevel eLevel );
	void DumpHTTPErrors();
	struct SHTTPError
	{
		CUtlString				m_sStr;
		uint32					m_nCount;
		CGCEmitGroup::EMsgLevel	m_eSeverity;
	};
	CUtlHashMapLarge< const char*, SHTTPError*, CaseSensitiveStrEquals, MurmurHash3ConstCharPtr >	m_HTTPErrors;
	CScheduledFunction< CGCBase >																	m_DumpHTTPErrorsSchedule;
};

extern CGCBase *GGCBase();

// ----------------------------------------------------------------------------
// BEGIN BLOCK OF PRE-TF-GC-SPLIT SCAFFOLDING
// ----------------------------------------------------------------------------

// Alright, here's why this mess is here: we (TF) are doing some work that depends
// on work that Dota has done, things like the SQL message queue, etc. We want to
// update our code so that it does the right thing and uses their functionaity so
// that they can then integrate our changes.
//
// TF is way out of date, though, and so rather than make that work, some of which
// has significant time pressure, dependent on a massive weeks-long integrate that
// we aren't sure is the right thing to do anyway, we're going to bring over their
// code as-is and then stub in implementations.
//
// This horrific template mess basically says "are we compiling in an environment
// with a spit GC?" (specifically, does CGCBase::GetGCType() exist?). If so, call
// into real implementations for things like GetGCGType(), etc. If not, feed back
// sane default values (ie., we're a master GC and there's only one of us). Doing
// this means that Dota can integrate our code and we can integrate Dota code and
// no callsites have to change. If/when TF *does* split our GC, things will silently
// update their implementation and then we can delete the scaffolding whenever.
template < class tBaseGCType >
struct IsSplitGC
{
	typedef char t_Yes[1];
	typedef char t_No[2];

	template < typename T, T >
	struct InternalTypeCheck;                     \
	
	template < typename T >
	static t_Yes& Test( InternalTypeCheck< uint32(), &T::GetGCType> * );

	template < typename T >
	static t_No& Test( ... );

	enum { kValue = sizeof( Test<tBaseGCType>( NULL ) ) == sizeof( t_Yes ) };
};

template < bool tTest, typename T > struct EnableIf { typedef T Type; };
template < typename T > struct EnableIf< false, T > { };

#define SplitVsSingleGCImpl( returntype_, functionandparams_, splitgcimpl_, singlegcimpl_ ) \
	template < class tBaseGCType = CGCBase > typename EnableIf<IsSplitGC<tBaseGCType>::kValue, returntype_>::Type functionandparams_	{ return splitgcimpl_; } \
	template < class tBaseGCType = CGCBase > typename EnableIf<!IsSplitGC<tBaseGCType>::kValue, returntype_>::Type functionandparams_	{ return singlegcimpl_; }

SplitVsSingleGCImpl( uint32, GGetGCType(), GGCBase()->GetGCType(), 0 );
SplitVsSingleGCImpl( uint32, GGetGCCountForType( uint32 unGCType ), GDirectory()->GetGCCountForType( unGCType ), 1 );
SplitVsSingleGCImpl( uint32, GGetGCInstance(), GGCBase()->GetDirInfo()->GetInstance(), 0 );

#undef SplitVsSingleGCImpl

// ----------------------------------------------------------------------------
// END BLOCK OF TEMPORARY PRE-INTEGRATE SCAFFOLDING
// ----------------------------------------------------------------------------

EResult YieldingSendWebAPIRequest( CSteamAPIRequest &request, KeyValues *pKVResponse, CUtlString &errMsg, bool b200MeansSuccess );

/// Scope object that remembers if a Steam ID is locked, and automatically unlocks it upon destruction
class CScopedSteamIDLock
{
public:

	/// Create object without assigning SteamID
	CScopedSteamIDLock() : m_steamID(), m_bLockedSuccesfully(false), m_iSavedLockCount(-1) {}

	/// Construct object to lock a particular steam ID
	CScopedSteamIDLock( const CSteamID& steamID ) : m_steamID( steamID ), m_bLockedSuccesfully(false), m_iSavedLockCount(-1) {}

	/// Destructor performs an unlock, if we are holing a lock.  That's pretty much the whole purpose of this class.
	~CScopedSteamIDLock() { Unlock(); }

	/// Return true if we're locked
	bool IsLocked() const { return m_bLockedSuccesfully; }

	/// Lock the currently assigned Steam ID.  (Set by constructor)
	bool BYieldingPerformLock( const char *pszFilename, int nLineNumber )
	{
		Assert( !m_bLockedSuccesfully );
		Unlock();

		Assert( m_steamID.IsValid() );

		m_bLockedSuccesfully = GGCBase()->BYieldingLockSteamID( m_steamID, pszFilename, nLineNumber );
		if ( m_bLockedSuccesfully )
		{
			const CLock *pLock = GGCBase()->FindSteamIDLock( m_steamID );
			if ( pLock )
			{
				m_iSavedLockCount = pLock->GetReferenceCount();
			}
			else
			{
				Assert( pLock );
			}
		}
		return m_bLockedSuccesfully;
	}

	/// Lock the specified Steam ID.
	bool BYieldingPerformLock( const CSteamID &steamID, const char *pszFilename, int nLineNumber )
	{

		// Should not already be locked, but unlock just in case
		Assert( !m_bLockedSuccesfully );
		Unlock();

		// Attempt lock
		m_steamID = steamID;
		m_bLockedSuccesfully = GGCBase()->BYieldingLockSteamID( m_steamID, pszFilename, nLineNumber );
		if ( m_bLockedSuccesfully )
		{
			const CLock *pLock = GGCBase()->FindSteamIDLock( m_steamID );
			if ( pLock )
			{
				m_iSavedLockCount = pLock->GetReferenceCount();
			}
			else
			{
				Assert( pLock );
			}
		}

		// Report status
		return m_bLockedSuccesfully;
	}

	/// Mark us as already being locked.  This is used when you already have a lock, and just want to use
	/// the scoped class to do the unlock when you're done
	void MarkLocked( const CSteamID &steamID )
	{

		// Should not already be locked, but unlock just in case
		Assert( !m_bLockedSuccesfully );
		Unlock();

		// Remember state
		m_steamID = steamID;
		m_bLockedSuccesfully = true;
		const CLock *pLock = GGCBase()->FindSteamIDLock( m_steamID );
		if ( pLock )
		{
			m_iSavedLockCount = pLock->GetReferenceCount();
		}
		else
		{
			Assert( pLock );
		}
	}

	/// Stop tracking that we're locking this object. This does *not* unlock anything -- Unlock does
	/// that. This is for "we hit the end of our scope but now want to do something else with the
	/// lock (ie., pass back to a caller)". This is like un-smarting a smart pointer.
	void Release()
	{
		if ( m_bLockedSuccesfully )
		{
			VerifyPreUnlock();
			m_bLockedSuccesfully = false;
			m_iSavedLockCount = -1;
		}
	}

	/// Unlock the lock, if we are holding it.  Usually this is not called directly,
	/// we let the destructor do it.
	void Unlock()
	{
		if ( m_bLockedSuccesfully )
		{
			VerifyPreUnlock();
			GGCBase()->UnlockSteamID( m_steamID );
			m_bLockedSuccesfully = false;
			m_iSavedLockCount = -1;
		}
	}

private:
	void VerifyPreUnlock() const
	{
		Assert( m_bLockedSuccesfully );

		const CLock *pLock = GGCBase()->FindSteamIDLock( m_steamID );
		if ( pLock )
		{
			AssertMsg1( m_iSavedLockCount == pLock->GetReferenceCount(), "Lock imbalance on %s detected by scope lock", pLock->GetName() );
		}
		else
		{
			Assert( pLock );
		}
	}

private:
	CSteamID m_steamID;
	bool m_bLockedSuccesfully;
	int m_iSavedLockCount;

};

/// Scope object that remembers if a specific lock is taken, and releases it
class CScopedGenericLock
{
public:

	/// Create object for specific lock SteamID
	CScopedGenericLock( CLock &lock ) : m_pLock( &lock ), m_bLockedSuccesfully(false), m_iSavedLockCount(-1) {}

	/// Destructor performs an unlock, if we are holing a lock.  That's pretty much the whole purpose of this class.
	~CScopedGenericLock() { Unlock(); }

	/// Return true if we're locked
	bool IsLocked() const { return m_bLockedSuccesfully; }

	/// Lock the current lock.  (Set by constructor)
	bool BYieldingPerformLock( const char *pszFilename, int nLineNumber )
	{
		Assert( !m_bLockedSuccesfully );
		Unlock();

		Assert( m_pLock );

		m_bLockedSuccesfully = GJobCur()._BYieldingAcquireLock( m_pLock, pszFilename, nLineNumber );
		if ( m_bLockedSuccesfully )
		{
			m_iSavedLockCount = m_pLock->GetReferenceCount();
		}
		return m_bLockedSuccesfully;
	}

	/// Stop tracking that we're locking this object. This does *not* unlock anything -- Unlock does
	/// that. This is for "we hit the end of our scope but now want to do something else with the
	/// lock (ie., pass back to a caller)". This is like un-smarting a smart pointer.
	void Release()
	{
		if ( m_bLockedSuccesfully )
		{
			VerifyPreUnlock();
			m_bLockedSuccesfully = false;
			m_iSavedLockCount = -1;
		}
	}

	/// Unlock the lock, if we are holding it.  Usually this is not called directly,
	/// we let the destructor do it.
	void Unlock()
	{
		if ( m_bLockedSuccesfully )
		{
			VerifyPreUnlock();
			if ( m_pLock->GetJobLocking() != &GJobCur() )
			{
				AssertMsg( false, "CScopedGenericLock::Unlock called when job %s doesn't own the lock", GJobCur().GetName() );
				return;
			}

			GJobCur().ReleaseLock( m_pLock );
			m_bLockedSuccesfully = false;
			m_iSavedLockCount = -1;
		}
	}

private:
	void VerifyPreUnlock() const
	{
		Assert( m_bLockedSuccesfully );

		AssertMsg1( m_iSavedLockCount == m_pLock->GetReferenceCount(), "Lock imbalance on %s detected by scope lock", m_pLock->GetName() );
	}

private:
	CLock *m_pLock;
	bool m_bLockedSuccesfully;
	int m_iSavedLockCount;

};

class CTrustedHelper_AssertOnNonPublicUniverseFailures
{
public:
	void operator()( const bool bExpResult, const char *pszExp ) const
	{
		if ( GetUniverse() != k_EUniversePublic )
		{
			AssertMsg1( bExpResult, "%s", pszExp );
		}
	}
};

class CVerifyIfTrustedHelper
{
public:
	template < typename tTrustedCriteriaImpl >
	CVerifyIfTrustedHelper( const tTrustedCriteriaImpl& CriteriaImpl, const bool bExpResult, const char *pszExp )
	{
		CommonConstructor( CriteriaImpl, bExpResult, pszExp );
	}

	// Helper constructor so we can do VerifyIfTrusted( pSomePointer ) without getting a type-
	// conversion-to-bool warning.
	template < typename tTrustedCriteriaImpl >
	CVerifyIfTrustedHelper( const tTrustedCriteriaImpl& CriteriaImpl, const void *pointer, const char *pszExp )
	{
		CommonConstructor( CriteriaImpl, pointer != NULL, pszExp );
	}

	bool GetResult() const { return m_bExpResult; }

private:
	template < typename tTrustedCriteriaImpl >
	void CommonConstructor( const tTrustedCriteriaImpl& CriteriaImpl, const bool bExpResult, const char *pszExp )
	{
		m_bExpResult = bExpResult;

		CriteriaImpl( bExpResult, pszExp );
	}

	bool m_bExpResult;
};

// Examples of things that we don't want to assert on, even during testing:
//		- we got a message from someone who didn't have a session. This could be a Steam
//		  problem, or we could be backlogged processing messages, or we could have an offline
//		  trade get processed in the background.
//		- someone sent up a message involving an item that they don't own. (Same reasons as
//		  a missing session.)
//
// Examples of things that we do want to assert on internally, but not when running public:
//		- our messages match contents fulfill criteria that the client specifies. ie., if we're
//		  passing up a "victim" and a "killer" Steam ID, they won't be identical.
//		- our schema data is set up correctly. ie., necessary fields ("kill eater localization
//		  string") that we expect to fail a schema parse init for if they're absent are present.
//		- our messages are coming from places that make sense. ie., a game server isn't sending
//		  messages we expect to get from a client, or we aren't getting a message from a client
//		  that we expect only to come from elsewhere in GC code.
#define VerifyIfTrusted( exp_ ) \
	CVerifyIfTrustedHelper( CTrustedHelper_AssertOnNonPublicUniverseFailures(), (exp_), #exp_ ).GetResult()

// !KLUDGE! Shim to make it easier to merge over stuff from DOTA
class CGCInterface
{
public:
	CSteamID ConstructSteamIDForClient( AccountID_t unAccountID ) const;
};
extern CGCInterface *GGCInterface();

} // namespace GCSDK

struct CRatelimitedSpewController
{
public:
	CRatelimitedSpewController() : m_rtCooldownStart( 0 ), m_rtLastSpew( 0 ), m_numSpewed( 0 ), m_numSkipped( 0 ) {}
	~CRatelimitedSpewController() {}

	enum ERate_t
	{
		k_ERate_Skip = -1,		// skip printing anything, rate limit too high
		k_ERate_Print = 0,		// print the full message
		// default is to print how many messages total as well
	};

	int RegisterSpew();

private:
	RTime32 m_rtCooldownStart;
	RTime32 m_rtLastSpew;
	int m_numSpewed;
	int m_numSkipped;
};

#define EmitErrorRatelimited( SPEW_GROUP_ID, fmtstring, ... ) \
			static CRatelimitedSpewController s_spewcontroller; \
			int nspewcontroller = s_spewcontroller.RegisterSpew(); \
			switch ( nspewcontroller ) \
			{ \
			case CRatelimitedSpewController::k_ERate_Skip: break; \
			default: \
				EmitError( SPEW_GROUP_ID, nspewcontroller \
					? fmtstring "(... and %d more skipped)\n" \
					: fmtstring \
					, __VA_ARGS__ \
					, nspewcontroller \
					); \
				break; \
			} \


#define EmitInfoRatelimited( SPEW_GROUP_ID, ConsoleLevel, LogLevel, fmtstring, ... ) \
			static CRatelimitedSpewController s_spewcontroller; \
			int nspewcontroller = s_spewcontroller.RegisterSpew(); \
			switch ( nspewcontroller ) \
			{ \
			case CRatelimitedSpewController::k_ERate_Skip: break; \
			default: \
				EmitInfo( SPEW_GROUP_ID, ConsoleLevel, LogLevel, nspewcontroller \
					? fmtstring "(... and %d more skipped)\n" \
					: fmtstring \
					, __VA_ARGS__ \
					, nspewcontroller \
					); \
				break; \
			} \


#define EG_WARNING_RATELIMITED( SPEW_GROUP_ID, fmtstring, ... ) \
			static CRatelimitedSpewController s_spewcontroller; \
			int nspewcontroller = s_spewcontroller.RegisterSpew(); \
			switch ( nspewcontroller ) \
			{ \
			case CRatelimitedSpewController::k_ERate_Skip: break; \
			default: \
				EG_WARNING( SPEW_GROUP_ID, nspewcontroller \
					? fmtstring "(... and %d more skipped)\n" \
					: fmtstring \
					, __VA_ARGS__ \
					, nspewcontroller \
					); \
				break; \
			} \



#endif // GCBASE_H