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.

470 lines
17 KiB

  1. //====== Copyright � 1996-2004, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef MSGPROTOBUF_H
  7. #define MSGPROTOBUF_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "msgbase.h"
  12. #include "gcmsg.h"
  13. #include "tier0/tslist.h"
  14. // eliminates a conflict with TYPE_BOOL in OSX
  15. #ifdef TYPE_BOOL
  16. #undef TYPE_BOOL
  17. #endif
  18. #pragma warning(push)
  19. #pragma warning( disable:4512 )
  20. #include <tier0/valve_minmax_off.h>
  21. #include "steammessages.pb.h"
  22. #include <tier0/valve_minmax_on.h>
  23. #pragma warning(pop)
  24. namespace GCSDK
  25. {
  26. //-----------------------------------------------------------------------------
  27. // CProtoBufNetPacket
  28. // Thin wrapper around raw CNetPacket which implements our IMsgNetPacket interface.
  29. //-----------------------------------------------------------------------------
  30. class CProtoBufNetPacket : public IMsgNetPacket
  31. {
  32. #ifdef GC
  33. DECLARE_CLASS_MEMPOOL( CProtoBufNetPacket );
  34. #endif
  35. public:
  36. CProtoBufNetPacket( CNetPacket *pNetPacket, GCProtoBufMsgSrc eReplyType, const CSteamID steamID, uint32 nGCDirIndex, MsgType_t msgType );
  37. EMsgFormatType GetEMsgFormatType() const { return k_EMsgFormatTypeProtocolBuffer; }
  38. CNetPacket *GetCNetPacket() const { return m_pNetPacket; }
  39. uint8 *PubData() const { return m_pNetPacket->PubData(); }
  40. uint CubData() const { return m_pNetPacket->CubData(); }
  41. bool IsValid() const { return m_bIsValid; }
  42. ProtoBufMsgHeader_t &GetFixedHeader() const { return *( (ProtoBufMsgHeader_t *)PubData() ); }
  43. CMsgProtoBufHeader *GetProtoHeader() const { return m_pHeader; }
  44. MsgType_t GetEMsg() const { return m_msgType; }
  45. JobID_t GetSourceJobID() const { return m_pHeader->job_id_source(); }
  46. JobID_t GetTargetJobID() const { return m_pHeader->job_id_target(); }
  47. void SetTargetJobID( JobID_t ulJobID ) { m_pHeader->set_job_id_target( ulJobID ); }
  48. CSteamID GetSteamID() const { return m_steamID; }
  49. void SetSteamID( CSteamID steamID ) { m_steamID = steamID; }
  50. AppId_t GetSourceAppID() const { return m_pHeader->source_app_id(); };
  51. void SetSourceAppID( AppId_t appId ) { m_pHeader->set_source_app_id( appId ); }
  52. virtual bool BHasTargetJobName() const { return m_pHeader->has_target_job_name(); }
  53. virtual const char *GetTargetJobName() const { return m_pHeader->target_job_name().c_str(); }
  54. //called to obtain access to the body portion of the net packet associated with this message. Will return NULL if not in a valid state
  55. bool GetMsgBody( const uint8*& pubData, uint32& cubData ) const;
  56. private:
  57. virtual ~CProtoBufNetPacket();
  58. CNetPacket *m_pNetPacket;
  59. CMsgProtoBufHeader *m_pHeader;
  60. CSteamID m_steamID;
  61. MsgType_t m_msgType;
  62. bool m_bIsValid;
  63. };
  64. //-----------------------------------------------------------------------------
  65. // CProtoBufMsgBase - Base class for templated protobuf msgs. As much code is
  66. // in this class as possible to reduce template copy overhead
  67. //-----------------------------------------------------------------------------
  68. class CProtoBufMsgBase
  69. {
  70. public:
  71. // allows any kind of destination to be the target of an AsyncSend
  72. class IProtoBufSendHandler
  73. {
  74. public:
  75. virtual bool BAsyncSend( MsgType_t eMsg, const uint8 *pubMsgBytes, uint32 cubSize ) = 0;
  76. };
  77. // Receive constructor. We expect InitFromPacket will be called later
  78. CProtoBufMsgBase();
  79. // Send constructor. InitFromPacket must not be called later
  80. CProtoBufMsgBase( MsgType_t eMsgType );
  81. virtual ~CProtoBufMsgBase();
  82. bool InitFromPacket( IMsgNetPacket * pNetPacket );
  83. bool BAsyncSend( IProtoBufSendHandler & pSender ) const;
  84. bool BAsyncSendWithPreSerializedBody( IProtoBufSendHandler & pSender, const byte *pubBody, uint32 cubBody ) const;
  85. //free standing version to send a protobuff given a header and pre-serialized body. Primarily used for efficient message routing
  86. static bool BAsyncSendWithPreSerializedBody( IProtoBufSendHandler& sender, MsgType_t eMsgType, const CMsgProtoBufHeader& hdr, const byte* pubBody, uint32 cubBody );
  87. //similar to the above, but sends a protobuf object that will be serialized into the buffer
  88. static bool BAsyncSendProto( IProtoBufSendHandler& sender, MsgType_t eMsgType, const CMsgProtoBufHeader& hdr, const ::google::protobuf::Message& proto );
  89. CMsgProtoBufHeader &Hdr() { return *m_pProtoBufHdr; }
  90. const CMsgProtoBufHeader &Hdr() const { return *m_pProtoBufHdr; }
  91. const CMsgProtoBufHeader &ConstHdr() const { return *m_pProtoBufHdr; }
  92. MsgType_t GetEMsg() const { return m_eMsg & (~k_EMsgProtoBufFlag); }
  93. CSteamID GetClientSteamID() const { return CSteamID( static_cast<uint64>( m_pProtoBufHdr->client_steam_id() ) ); }
  94. JobID_t GetJobIDTarget() const { return m_pProtoBufHdr->job_id_target(); }
  95. JobID_t GetJobIDSource() const { return m_pProtoBufHdr->job_id_source(); }
  96. AppId_t GetSourceAppID() const { return m_pProtoBufHdr->source_app_id(); }
  97. bool BIsExpectingReply() const { return GetJobIDSource() != k_GIDNil; }
  98. void SetJobIDSource( JobID_t jobId ) { m_pProtoBufHdr->set_job_id_source( jobId ); }
  99. void SetJobIDTarget( JobID_t jobId ) { m_pProtoBufHdr->set_job_id_target( jobId ); }
  100. void SetSourceAppID( AppId_t appId ) { m_pProtoBufHdr->set_source_app_id( appId ); }
  101. void ExpectingReply( JobID_t jobId ) { SetJobIDSource( jobId ); }
  102. EResult GetEResult() const { return (EResult)ConstHdr().eresult(); }
  103. void SetEResult( EResult eResult ) { Hdr().set_eresult( eResult ); }
  104. const char *GetErrorMessage() const { return ConstHdr().error_message().c_str(); }
  105. void SetErrorMessage( const char *pchErrorMessage ) { Hdr().set_error_message( pchErrorMessage ); }
  106. void AppendErrorMessage( const char *pchErrorMessage ) { Hdr().mutable_error_message()->append( pchErrorMessage ); }
  107. // Must be implemented by subclasses. Returns the body. The templated subclasses have their
  108. // own body accessor that returns the body as the specific type
  109. virtual ::google::protobuf::Message *GetGenericBody() const = 0;
  110. protected:
  111. // Mutex to use when registering a new pool type
  112. static CThreadMutex s_PoolRegMutex;
  113. private:
  114. //utility function that handles allocating a memory pool big enough for the provided header and specified body
  115. //size and writing the header into the pool. This will return a pointer to the memory, as well as the header size.
  116. static uint8* AllocateMessageMemory( MsgType_t eMsgType, const CMsgProtoBufHeader& hdr, uint32 cubBodySize, uint32* pCubTotalSizeOut );
  117. //called to free the memory returned by allocate message memory
  118. static void FreeMessageMemory( uint8* pMemory );
  119. // Pointer to an external net packet if we have one. If we have one then we will
  120. // not have allocated m_pProtoBufHdr ourselves
  121. CProtoBufNetPacket *m_pNetPacket;
  122. // Protobuf objects for extended pb based header
  123. CMsgProtoBufHeader *m_pProtoBufHdr;
  124. // Our message type
  125. MsgType_t m_eMsg;
  126. // Private and unimplemented. Implement these if you want to be able to copy
  127. // these objects.
  128. CProtoBufMsgBase( const CProtoBufMsgBase& );
  129. CProtoBufMsgBase& operator=( const CProtoBufMsgBase& );
  130. };
  131. //-----------------------------------------------------------------------------
  132. // CProtoBufMsgMemoryPoolBase - Interface to allocation pools for each protobufmsg type
  133. //-----------------------------------------------------------------------------
  134. class CProtoBufMsgMemoryPoolBase
  135. {
  136. public:
  137. CProtoBufMsgMemoryPoolBase( uint32 unTargetLow, uint32 unTargetHigh );
  138. virtual ~CProtoBufMsgMemoryPoolBase();
  139. // Memory interface
  140. ::google::protobuf::Message *Alloc();
  141. void Free( ::google::protobuf::Message *pMsg );
  142. // Stats
  143. uint32 GetEstimatedSize();
  144. uint32 GetAllocated() { return m_unAllocated; }
  145. uint32 GetFree() { return m_pTSQueueFreeObjects->Count(); }
  146. uint32 GetAllocHitCount() { return m_unAllocHitCounter; }
  147. uint32 GetAllocMissCount() { return m_unAllocMissCounter; }
  148. // To be overriden by the templated class
  149. virtual CUtlString GetName() = 0;
  150. protected:
  151. // The actual memory management. Must be overriden by the templated class
  152. virtual google::protobuf::Message *InternalAlloc() = 0;
  153. virtual void InternalFree( google::protobuf::Message *pMsg ) = 0;
  154. // Called by the derived destructor to deallocate the outstanding messages
  155. bool PopItem( google::protobuf::Message **ppMsg );
  156. private:
  157. CTSQueue<google::protobuf::Message *> *m_pTSQueueFreeObjects;
  158. // These counters are important to get correct, so interlocked in case of allocating on threads
  159. CInterlockedInt m_unAllocHitCounter;
  160. CInterlockedInt m_unAllocMissCounter;
  161. CInterlockedInt m_unAllocated;
  162. // Only set at construction, so not needed to be thread safe
  163. uint32 m_unTargetCountLow;
  164. uint32 m_unTargetCountHigh;
  165. };
  166. } // namespace GCDSK
  167. // The rest of the file needs memdbgon because the code in the templates do actual allocation
  168. #include "tier0/memdbgon.h"
  169. namespace GCSDK
  170. {
  171. //-----------------------------------------------------------------------------
  172. // CProtoBufMsgMemoryPool - Implementation for allocation pools for protobufmsgs.
  173. // We create one of these per protobuf msg type, created on first construction of
  174. // an object of that type.
  175. //-----------------------------------------------------------------------------
  176. template< typename PB_OBJECT_TYPE >
  177. class CProtoBufMsgMemoryPool : public CProtoBufMsgMemoryPoolBase
  178. {
  179. public:
  180. CProtoBufMsgMemoryPool()
  181. : CProtoBufMsgMemoryPoolBase( PB_OBJECT_TYPE::descriptor()->options().GetExtension( msgpool_soft_limit ),
  182. PB_OBJECT_TYPE::descriptor()->options().GetExtension( msgpool_hard_limit ) ) {}
  183. virtual ~CProtoBufMsgMemoryPool()
  184. {
  185. google::protobuf::Message *pObject = NULL;
  186. while ( PopItem( &pObject ) )
  187. {
  188. InternalFree( pObject );
  189. }
  190. }
  191. virtual CUtlString GetName() OVERRIDE
  192. {
  193. return PB_OBJECT_TYPE::default_instance().GetTypeName().c_str();
  194. }
  195. private:
  196. virtual ::google::protobuf::Message *InternalAlloc()
  197. {
  198. PB_OBJECT_TYPE *pObject = (PB_OBJECT_TYPE *)malloc( sizeof( PB_OBJECT_TYPE ) );
  199. Construct( pObject );
  200. return pObject;
  201. }
  202. virtual void InternalFree( google::protobuf::Message *pMsg )
  203. {
  204. if ( NULL == pMsg )
  205. {
  206. Assert( NULL != pMsg );
  207. return;
  208. }
  209. PB_OBJECT_TYPE *pObject = (PB_OBJECT_TYPE *)pMsg;
  210. Destruct( pObject );
  211. free( pObject );
  212. }
  213. };
  214. //-----------------------------------------------------------------------------
  215. // CProtoBufMsgMemoryPoolMgr - Manages all the message pools for protobufmsgs.
  216. // Should have one global singleton instance of this which tracks all the pools
  217. // for individual message types.
  218. //-----------------------------------------------------------------------------
  219. class CProtoBufMsgMemoryPoolMgr
  220. {
  221. public:
  222. CProtoBufMsgMemoryPoolMgr();
  223. ~CProtoBufMsgMemoryPoolMgr();
  224. void RegisterPool( CProtoBufMsgMemoryPoolBase *pPool );
  225. void DumpPoolInfo();
  226. CMsgProtoBufHeader *AllocProtoBufHdr() { return (CMsgProtoBufHeader *)m_PoolHeaders.Alloc(); }
  227. void FreeProtoBufHdr( CMsgProtoBufHeader *pObject ) { m_PoolHeaders.Free( pObject ); }
  228. private:
  229. CProtoBufMsgMemoryPool<CMsgProtoBufHeader> m_PoolHeaders;
  230. CUtlVector< CProtoBufMsgMemoryPoolBase * > m_vecMsgPools;
  231. };
  232. extern CProtoBufMsgMemoryPoolMgr *GProtoBufMsgMemoryPoolMgr();
  233. //-----------------------------------------------------------------------------
  234. // CProtoBufPtrMsg
  235. // Similar to a CProtoBufMsg, but the constructor simply takes in a pointer which is a
  236. // pointer to the protobuf object that is being wrapped by a message. This memory is managed
  237. // by the caller, this object does nothing to free the memory
  238. //-----------------------------------------------------------------------------
  239. class CProtoBufPtrMsg : public CProtoBufMsgBase
  240. {
  241. public:
  242. CProtoBufPtrMsg( google::protobuf::Message *pProto ) : m_pProtoBufBody( pProto ) {}
  243. private:
  244. virtual google::protobuf::Message *GetGenericBody() const OVERRIDE { return m_pProtoBufBody; }
  245. // Protobuf object for the message body
  246. google::protobuf::Message *m_pProtoBufBody;
  247. // Private and unimplemented. Implement these if you want to be able to copy
  248. // these objects.
  249. CProtoBufPtrMsg( const CProtoBufPtrMsg& );
  250. CProtoBufPtrMsg& operator=( const CProtoBufPtrMsg& );
  251. };
  252. //-----------------------------------------------------------------------------
  253. // CProtoBufMsg
  254. // New style steam inter-server message class based on Google Protocol Buffers
  255. // Handles a message with a header of type MsgHdr_t, a body of type T, and optional variable length data
  256. //-----------------------------------------------------------------------------
  257. template< typename PB_OBJECT_TYPE >
  258. class CProtoBufMsg : public CProtoBufMsgBase
  259. {
  260. private:
  261. static bool s_bRegisteredWithMemoryPoolMgr;
  262. static CProtoBufMsgMemoryPool< PB_OBJECT_TYPE > *s_pMemoryPool;
  263. public:
  264. // Used to alloc a protobuf of this type from the pool. Can be used by functions
  265. // working with protobufs that aren't messages to take advantage of pooling
  266. static PB_OBJECT_TYPE *AllocProto()
  267. {
  268. // If we haven't done registration do so now
  269. // Called on construction of each object of this type, but only does work
  270. // once to setup memory pools for the class type.
  271. if ( !s_bRegisteredWithMemoryPoolMgr )
  272. {
  273. // Get the lock and make sure we still haven't
  274. s_PoolRegMutex.Lock();
  275. if ( !s_bRegisteredWithMemoryPoolMgr )
  276. {
  277. s_pMemoryPool = new CProtoBufMsgMemoryPool< PB_OBJECT_TYPE >();
  278. GProtoBufMsgMemoryPoolMgr()->RegisterPool( s_pMemoryPool );
  279. s_bRegisteredWithMemoryPoolMgr = true;
  280. }
  281. s_PoolRegMutex.Unlock();
  282. }
  283. return static_cast<PB_OBJECT_TYPE *>( s_pMemoryPool->Alloc() );
  284. }
  285. // Frees a protobuf allocated with AllocProto()
  286. static void FreeProto( PB_OBJECT_TYPE *pbObj )
  287. {
  288. s_pMemoryPool->Free( pbObj );
  289. }
  290. // Constructor for an empty message
  291. CProtoBufMsg( MsgType_t eMsg )
  292. : CProtoBufMsgBase( eMsg )
  293. , m_pProtoBufBody( NULL )
  294. {
  295. VPROF_BUDGET( "CProtoBufMsg::CProtoBufMsg( MsgType_t )", VPROF_BUDGETGROUP_OTHER_NETWORKING );
  296. m_pProtoBufBody = AllocProto();
  297. }
  298. // Constructor for an empty message responding to a client
  299. CProtoBufMsg( MsgType_t eMsg, CSteamID steamIDClient, int32 nSessionIDClient )
  300. : CProtoBufMsgBase( eMsg )
  301. , m_pProtoBufBody( NULL )
  302. {
  303. VPROF_BUDGET( "CProtoBufMsg::CProtoBufMsg( MsgType_t, CSteamID, int32 )", VPROF_BUDGETGROUP_OTHER_NETWORKING );
  304. m_pProtoBufBody = AllocProto();
  305. Hdr()->set_client_steam_id( steamIDClient.ConvertToUint64() );
  306. Hdr()->set_client_session_id( nSessionIDClient );
  307. }
  308. // Constructor from an incoming netpacket
  309. CProtoBufMsg( IMsgNetPacket *pNetPacket )
  310. : CProtoBufMsgBase()
  311. , m_pProtoBufBody( NULL )
  312. {
  313. m_pProtoBufBody = AllocProto();
  314. InitFromPacket( pNetPacket );
  315. }
  316. // constructor for use in catching replies or any other place where you have nothing to stuff in
  317. // the message at construct time
  318. CProtoBufMsg()
  319. : CProtoBufMsgBase()
  320. , m_pProtoBufBody( NULL )
  321. {
  322. m_pProtoBufBody = AllocProto();
  323. }
  324. // Constructor for replying to another protobuf message
  325. CProtoBufMsg( MsgType_t eMsg, const CProtoBufMsgBase & msgReplyingTo )
  326. : CProtoBufMsgBase( eMsg )
  327. , m_pProtoBufBody( NULL )
  328. {
  329. VPROF_BUDGET( "CProtoBufMsg::CProtoBufMsg( EMsg, CProtoBufMsgMemoryPoolBase )", VPROF_BUDGETGROUP_OTHER_NETWORKING );
  330. m_pProtoBufBody = AllocProto();
  331. // set up the actual reply
  332. SetJobIDTarget( msgReplyingTo.GetJobIDSource() );
  333. }
  334. // Destructor
  335. virtual ~CProtoBufMsg()
  336. {
  337. if ( m_pProtoBufBody )
  338. {
  339. FreeProto( m_pProtoBufBody );
  340. m_pProtoBufBody = NULL;
  341. }
  342. }
  343. // Accessors
  344. PB_OBJECT_TYPE &Body() { return *m_pProtoBufBody; }
  345. const PB_OBJECT_TYPE &Body() const { return *m_pProtoBufBody; }
  346. private:
  347. virtual google::protobuf::Message *GetGenericBody() const OVERRIDE { return m_pProtoBufBody; }
  348. // Protobuf object for the message body
  349. PB_OBJECT_TYPE *m_pProtoBufBody;
  350. // Private and unimplemented. Implement these if you want to be able to copy
  351. // these objects.
  352. CProtoBufMsg( const CProtoBufMsg& );
  353. CProtoBufMsg& operator=( const CProtoBufMsg& );
  354. };
  355. // Statics
  356. template< typename PB_OBJECT_TYPE > bool CProtoBufMsg< PB_OBJECT_TYPE>::s_bRegisteredWithMemoryPoolMgr = false;
  357. template< typename PB_OBJECT_TYPE > CProtoBufMsgMemoryPool< PB_OBJECT_TYPE > *CProtoBufMsg< PB_OBJECT_TYPE>::s_pMemoryPool = NULL;
  358. //-----------------------------------------------------------------------------
  359. // Purpose: Wrapper class to handle alloc/free using the pool allocators
  360. //-----------------------------------------------------------------------------
  361. template <class TMsg>
  362. class CProtoBufPoolObj
  363. {
  364. private:
  365. TMsg *m_pMsg;
  366. private: // Disallow copying/assignment
  367. CProtoBufPoolObj( CProtoBufPoolObj const &x );
  368. CProtoBufPoolObj & operator = ( CProtoBufPoolObj const &x );
  369. public:
  370. CProtoBufPoolObj() { m_pMsg = CProtoBufMsg<TMsg>::AllocProto(); }
  371. ~CProtoBufPoolObj() { CProtoBufMsg<TMsg>::FreeProto( m_pMsg ); }
  372. operator TMsg & () { return *m_pMsg; }
  373. };
  374. } // namespace GCSDK
  375. // memdbgon is only supposed to be on in cpp files, turn it off in case the next thing includes has a conflict with it
  376. #include "tier0/memdbgoff.h"
  377. #endif // MSGPROTOBUF_H