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.

186 lines
7.5 KiB

  1. //====== Copyright �, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Additional shared object cache functionality for the GC
  4. //
  5. //=============================================================================
  6. #ifndef GCCLIENT_SHAREDOBJECTCACHE_H
  7. #define GCCLIENT_SHAREDOBJECTCACHE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "sharedobjectcache.h"
  12. class CMsgSOCacheSubscribed;
  13. class CMsgSOCacheSubscribed_SubscribedType;
  14. namespace GCSDK
  15. {
  16. class CGCClient;
  17. /// Enumerate different events that might trigger a callback to an ISharedObjectListener
  18. enum ESOCacheEvent
  19. {
  20. /// Dummy sentinel value
  21. eSOCacheEvent_None = 0,
  22. /// We received a our first update from the GC and are subscribed
  23. eSOCacheEvent_Subscribed = 1,
  24. /// We lost connection to GC or GC notified us that we are no longer subscribed.
  25. /// Objects stay in the cache, but we no longer receive updates
  26. eSOCacheEvent_Unsubscribed = 2,
  27. /// We received a full update from the GC on a cache for which we were already subscribed.
  28. /// This can happen if connectivity is lost, and then restored before we realized it was lost.
  29. eSOCacheEvent_Resubscribed = 3,
  30. /// We received an incremental update from the GC about specific object(s) being
  31. /// added, updated, or removed from the cache
  32. eSOCacheEvent_Incremental = 4,
  33. /// A lister was added to the cache
  34. /// @see CGCClientSharedObjectCache::AddListener
  35. eSOCacheEvent_ListenerAdded = 5,
  36. /// A lister was removed from the cache
  37. /// @see CGCClientSharedObjectCache::RemoveListener
  38. eSOCacheEvent_ListenerRemoved = 6,
  39. };
  40. //----------------------------------------------------------------------------
  41. // Purpose: Allow game components to register themselves to hear about inventory
  42. // changes when they are received from the server
  43. //----------------------------------------------------------------------------
  44. class ISharedObjectListener
  45. {
  46. public:
  47. /// Called when a new object is created in a cache we are currently subscribed to, or when we are added
  48. /// as a listener to a cache which already has objects in it
  49. ///
  50. /// eEvent will be one of:
  51. /// - eSOCacheEvent_Subscribed
  52. /// - eSOCacheEvent_Resubscribed
  53. /// - eSOCacheEvent_Incremental
  54. /// - eSOCacheEvent_ListenerAdded
  55. virtual void SOCreated( SOID_t owner, const CSharedObject *pObject, ESOCacheEvent eEvent ) = 0;
  56. /// Called when an object is updated in a cache we are currently subscribed to.
  57. ///
  58. /// eEvent will be one of:
  59. /// - eSOCacheEvent_Resubscribed
  60. /// - eSOCacheEvent_Incremental
  61. virtual void SOUpdated( SOID_t owner, const CSharedObject *pObject, ESOCacheEvent eEvent ) = 0;
  62. /// Called when an object is about to be deleted in a cache we are currently subscribed to.
  63. /// The object will have already been removed from the cache, but is still valid.
  64. ///
  65. /// eEvent will be one of:
  66. /// - eSOCacheEvent_Incremental
  67. /// - eSOCacheEvent_Resubscribed
  68. virtual void SODestroyed( SOID_t owner, const CSharedObject *pObject, ESOCacheEvent eEvent ) = 0;
  69. /// Called to notify a listener that he is subscribed to the cache.
  70. ///
  71. /// eEvent will be one of:
  72. /// - eSOCacheEvent_Subscribed
  73. /// - eSOCacheEvent_Resubscribed
  74. /// - eSOCacheEvent_ListenerAdded
  75. ///
  76. /// A listener is guaranteed that it will not receive incremental updates (SOCreated,
  77. /// SOUpdated, SODestroyed) while not subscribed. (Before the SOCacheSubscribed or
  78. /// after SOCacheUnsubscribed.) However, note that it may be possible to receive
  79. /// an SOCacheSubscribed message while already subscribed. This can happen if the
  80. /// GC loses and restores connection, or otherwise decides that a full update is
  81. /// necessary.
  82. virtual void SOCacheSubscribed( SOID_t owner, ESOCacheEvent eEvent ) = 0;
  83. /// Called to notify a listener that he is no longer subscribed to the cache.
  84. /// if he is being removed as a listener, then he will no longer receive
  85. /// updates. Otherwise, he might receive another SOCacheSubscribed
  86. /// message, followed by further update notifications.
  87. ///
  88. /// eEvent will be one of:
  89. /// - eSOCacheEvent_Unsubscribed
  90. /// - eSOCacheEvent_ListenerRemoved
  91. virtual void SOCacheUnsubscribed( SOID_t owner, ESOCacheEvent eEvent ) = 0;
  92. };
  93. //----------------------------------------------------------------------------
  94. // Purpose: The part of a shared object cache that handles all objects of a
  95. // single type.
  96. //----------------------------------------------------------------------------
  97. class CGCClientSharedObjectTypeCache : public CSharedObjectTypeCache
  98. {
  99. public:
  100. CGCClientSharedObjectTypeCache( int nTypeID );
  101. virtual ~CGCClientSharedObjectTypeCache();
  102. bool BParseCacheSubscribedMsg( const CMsgSOCacheSubscribed_SubscribedType & msg, CUtlVector<CSharedObject*> &vecCreatedObjects, CUtlVector<CSharedObject*> &vecUpdatedObjects, CUtlVector<CSharedObject*> &vecObjectsToDestroy );
  103. CSharedObject *BCreateFromMsg( const void *pvData, uint32 unSize, bool *bUpdatedExisting );
  104. bool BDestroyFromMsg( SOID_t owner, CGCClient &client, const void *pvData, uint32 unSize );
  105. bool BCreateOrUpdateFromMsg( SOID_t owner, CGCClient &client, const void *pvData, uint32 unSize );
  106. void RemoveAllObjects( CUtlVector<CSharedObject*> &vecObjects );
  107. };
  108. //----------------------------------------------------------------------------
  109. // Purpose: A cache of a bunch of shared objects of different types. This class
  110. // is shared between clients, gameservers, and the GC and is
  111. // responsible for sending messages from the GC to cause object
  112. // creation/destruction/updating on the clients/gameservers.
  113. //----------------------------------------------------------------------------
  114. class CGCClientSharedObjectCache : public CSharedObjectCache
  115. {
  116. public:
  117. explicit CGCClientSharedObjectCache( SOID_t ID );
  118. virtual ~CGCClientSharedObjectCache();
  119. /// Have we received at least one update from the GC?
  120. bool BIsInitialized() const { return m_bInitialized; }
  121. /// Are we currently subscribed to updates from the GC?
  122. bool BIsSubscribed() const { return m_bSubscribed; }
  123. void SetSubscribed( bool bSubscribed ) { m_bSubscribed = bSubscribed; }
  124. /// Who owns this cache?
  125. virtual SOID_t GetOwner() const OVERRIDE { return m_IDOwner; }
  126. const CGCClientSharedObjectTypeCache *FindTypeCache( int nClassID ) const { return static_cast<const CGCClientSharedObjectTypeCache *>(FindBaseTypeCache( nClassID )); }
  127. CGCClientSharedObjectTypeCache *FindTypeCache( int nClassID ) { return static_cast<CGCClientSharedObjectTypeCache *>(FindBaseTypeCache( nClassID )); }
  128. CGCClientSharedObjectTypeCache *CreateTypeCache( int nClassID ) { return static_cast<CGCClientSharedObjectTypeCache *>(CreateBaseTypeCache( nClassID )); }
  129. bool BParseCacheSubscribedMsg( CGCClient &owner, const CMsgSOCacheSubscribed & msg );
  130. void BuildCacheSubscribedMsg( CMsgSOCacheSubscribed &msg ) const;
  131. bool BCreateFromMsg( CGCClient &owner, int nTypeID, const void *pvData, uint32 unSize );
  132. bool BDestroyFromMsg( CGCClient &owner, int nTypeID, const void *pvData, uint32 unSize );
  133. bool BUpdateFromMsg( CGCClient &owner, int nTypeID, const void *pvData, uint32 unSize );
  134. #ifdef DBGFLAG_VALIDATE
  135. virtual void Validate( CValidator &validator, const char *pchName );
  136. #endif
  137. void NotifyCreated( ISharedObjectListener &context );
  138. private:
  139. virtual CSharedObjectTypeCache *AllocateTypeCache( int nClassID ) const OVERRIDE { return new CGCClientSharedObjectTypeCache( nClassID ); }
  140. CGCClientSharedObjectTypeCache *GetTypeCacheByIndex( int nIndex ) { return (CGCClientSharedObjectTypeCache *)CSharedObjectCache::GetTypeCacheByIndex( nIndex ); }
  141. SOID_t m_IDOwner;
  142. bool m_bInitialized;
  143. bool m_bSubscribed;
  144. };
  145. } // namespace GCSDK
  146. #endif //GCCLIENT_SHAREDOBJECTCACHE_H