Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
7.1 KiB

  1. //====== Copyright (c), Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Holds the CGCClient class
  4. //
  5. //=============================================================================
  6. #ifndef GCCLIENT_H
  7. #define GCCLIENT_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "steam/steam_api.h"
  12. #include "jobmgr.h"
  13. #include "sharedobject.h"
  14. class ISteamGameCoordinator;
  15. struct GCMessageAvailable_t;
  16. class CTestEvent;
  17. namespace GCSDK
  18. {
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Interface for communicating with the GC
  21. //-----------------------------------------------------------------------------
  22. class CGCClient
  23. {
  24. public:
  25. CGCClient( bool bGameserver = false );
  26. virtual ~CGCClient( );
  27. /// Call once at program startup
  28. bool BInit( uint32 unVersion, ISteamClient *pSteamClient, HSteamUser hSteamUser, HSteamPipe hSteamPipe );
  29. /// Cleanup
  30. void Uninit( );
  31. /// Call this to perform periodic service
  32. bool BMainLoop( uint64 ulLimitMicroseconds, uint64 ulFrameTimeMicroseconds = 0 );
  33. /// Set current session need state value that is sent in the HELLO message to
  34. /// determine login priority. At this generic level we don't know what the
  35. /// game-specific client states mean, and which states imply a need for a
  36. /// GC session, so you need to tell us that, too. This decides whether we are
  37. /// aggressive at sending HELLO messages to try to establish the connection
  38. /// or not.
  39. void SetSessionNeed( uint32 nSessionNeed, bool bWantSession );
  40. /// Launcher value. Sent in the HELLO message
  41. void SetLauncherType( uint32 nLauncherType ) { m_nLauncherType = nLauncherType; }
  42. /// Steam datagram port, for servers. Sent in the HELLO message
  43. void SetServerSteamdatagramPort( uint16 usPort ) { m_usSteamdatagramPort = usPort; }
  44. CJobMgr &GetJobMgr() { return m_JobMgr; }
  45. /// Send a message to the GC.
  46. bool BSendMessage( uint32 unMsgType, const uint8 *pubData, uint32 cubData );
  47. bool BSendMessage( const CGCMsgBase& msg );
  48. bool BSendMessage( const CProtoBufMsgBase& msg );
  49. /// Locate a given shared object from the cache
  50. CSharedObject *FindSharedObject( SOID_t ID, const CSharedObject & soIndex );
  51. /// Find a shared object cache for the specified user. Optionally, the cache will be
  52. /// created if it doesn't not currently exist.
  53. CGCClientSharedObjectCache *FindSOCache( SOID_t ID, bool bCreateIfMissing = true );
  54. /// Find a set of shared object caches for a specific type of SOID
  55. /// returns true if any were found
  56. typedef CUtlVectorFixedGrowable< CGCClientSharedObjectCache *, 1 > ClientSOCacheVec_t;
  57. bool FindSOCacheByType( uint32 type, ClientSOCacheVec_t &cacheList );
  58. /// Adds a listener to the shared object cache for the specified Steam ID.
  59. ///
  60. /// @see CGCClientSharedObjectCache::AddListener
  61. bool AddSOCacheListener( ISharedObjectListener *pListener );
  62. /// Removes a listener for the shared object cache for the specified Steam ID.
  63. /// Returns true if we were listening and were successfully removed, false
  64. /// otherwise
  65. ///
  66. /// @see CGCClientSharedObjectCache::RemoveListener
  67. bool RemoveSOCacheListener( ISharedObjectListener *pListener );
  68. void DispatchSOCreated( SOID_t owner, const CSharedObject *pObject, ESOCacheEvent eEvent );
  69. void DispatchSOUpdated( SOID_t owner, const CSharedObject *pObject, ESOCacheEvent eEvent );
  70. void DispatchSODestroyed( SOID_t owner, const CSharedObject *pObject, ESOCacheEvent eEvent );
  71. void DispatchSOCacheSubscribed( SOID_t owner, ESOCacheEvent eEvent );
  72. void DispatchSOCacheUnsubscribed( SOID_t owner, ESOCacheEvent eEvent );
  73. typedef CUtlVector< ISharedObjectListener * > SharedObjectListensersVec_t;
  74. const SharedObjectListensersVec_t & GetListeners() const { return m_vecListeners; }
  75. void OnGCMessageAvailable( GCMessageAvailable_t *pCallback );
  76. ISteamGameCoordinator *GetSteamGameCoordinator() { return m_pSteamGameCoordinator; }
  77. virtual void Test_AddEvent( CTestEvent *pEvent ) {}
  78. virtual void Test_CacheSubscribed( SOID_t ID ) {}
  79. void NotifySOCacheUnsubscribed( SOID_t ID );
  80. void NotifyResubscribedUpToDate( SOID_t ID );
  81. /// Send a HELLO message to the GC now.
  82. void SendHello();
  83. // Called when we receive a welcome message, to sync up our SO caches with the
  84. // what the GC is telling us we have.
  85. void ProcessSOCacheSubscribedMsg( const CMsgSOCacheSubscribed &msg );
  86. /// Simulate inability to connect to DOTA's GC.
  87. /// (But allow us to connect to Steam.)
  88. bool GetSimulateGCConnectionFailure() const { return m_bSimulateGCConnectionFailure; }
  89. void SetSimulateGCConnectionFailure( bool bForcedFailure );
  90. /// Called when any messages times out. When this happens, it's usually
  91. /// safe to assume that the connection has been interrupted, and we should
  92. /// renegotiate.
  93. void MessageReplyTimedOut( uint32 nExpectedMsg, uint nTimeoutSecs );
  94. //
  95. // Logon queue stats.
  96. //
  97. // These return negative if quantities are not known.
  98. // All of these numbers can only be valid if we're in the
  99. // GCConnectionStatus_NO_SESSION_IN_LOGON_QUEUE state. However,
  100. // just because we're in that state does NOT mean that they will be
  101. // available!
  102. int GetLogonQueuePosition() const { return m_nLogonQueuePosition; }
  103. int GetLogonQueueSize() const { return m_nLogonQueueSize; }
  104. int GetLogonQueueEstimatedSecondsRemaining() const;
  105. int GetLogonQueueApproxWaitSeconds() const;
  106. protected:
  107. ISteamUser *m_pSteamUser;
  108. ISteamGameServer *m_pSteamGameserver;
  109. ISteamGameCoordinator *m_pSteamGameCoordinator;
  110. CUtlMemory<uint8> m_memMsg;
  111. // local job handling
  112. CJobMgr m_JobMgr;
  113. // Shared object caches
  114. typedef CUtlMap<SOID_t, CGCClientSharedObjectCache *> MapSOCache_t;
  115. MapSOCache_t m_mapSOCache;
  116. SharedObjectListensersVec_t m_vecListeners;
  117. uint64 m_timeLastSendHello;
  118. uint64 m_timeReceivedConnectionStatus;
  119. uint64 m_timeLoggedOn;
  120. uint32 m_unVersion;
  121. const bool m_bGameserver;
  122. bool m_bSimulateGCConnectionFailure;
  123. uint32 m_nSessionNeed;
  124. uint32 m_nLastSessionNeed; // last session need state sent / received from the GC
  125. bool m_bWantSession;
  126. uint32 m_nLauncherType;
  127. uint16 m_usSteamdatagramPort;
  128. int m_nLogonQueuePosition;
  129. int m_nLogonQueueSize;
  130. uint64 m_timeLogonQueueApproxTimeEnteredQueue;
  131. uint64 m_timeLogonQueueEstimatedTimeExitQueue;
  132. void ClearLogonQueueStats();
  133. void UpdateLogonState();
  134. void ThinkConnection();
  135. void DispatchPacket( IMsgNetPacket *pMsgNetPacket );
  136. // Steam callback for getting notified about messages available. Not part of the class
  137. // in Steam builds because we use the TestClientManager instead of steam_api.dll in Steam
  138. #ifndef STEAM
  139. CCallback< CGCClient, GCMessageAvailable_t, false > m_callbackGCMessageAvailable;
  140. STEAM_CALLBACK( CGCClient, OnSteamServersDisconnected, SteamServersDisconnected_t, m_CallbackSteamServersDisconnected );
  141. STEAM_CALLBACK( CGCClient, OnSteamServerConnectFailure, SteamServerConnectFailure_t, m_CallbackSteamServerConnectFailure );
  142. STEAM_CALLBACK( CGCClient, OnSteamServersConnected, SteamServersConnected_t, m_CallbackSteamServersConnected );
  143. #endif
  144. };
  145. //utility to make defining client jobs more straight forward
  146. #define GC_REG_CLIENT_JOB( JobClass, Msg ) \
  147. GC_REG_JOB( GCSDK::CGCClient, JobClass, #JobClass, Msg )
  148. } // namespace GCSDK
  149. #endif // GCCLIENT_H