Team Fortress 2 Source Code as on 22/4/2020
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.

233 lines
7.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This file defines all of the Game Coordinator messages for the
  4. // current IS-embedded implementation of the GC
  5. //
  6. //=============================================================================
  7. #ifndef GCMSG_H
  8. #define GCMSG_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "msgbase.h"
  13. #include "messagelist.h"
  14. #pragma pack( push, 1 )
  15. namespace GCSDK
  16. {
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Header for messages from a client or gameserver to or from the GC
  19. //-----------------------------------------------------------------------------
  20. struct GCMsgHdr_t
  21. {
  22. MsgType_t m_eMsg; // The message type
  23. uint64 m_ulSteamID; // User's SteamID
  24. CUtlString GetHeaderDescription();
  25. const char *PchMsgName( ) const { return PchMsgNameFromEMsg( m_eMsg ); }
  26. };
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Header for messages from a client or gameserver to or from the GC
  29. // That contains source and destination jobs for the purpose of
  30. // replying messages.
  31. //-----------------------------------------------------------------------------
  32. struct GCMsgHdrEx_t
  33. {
  34. MsgType_t m_eMsg; // The message type
  35. uint64 m_ulSteamID; // User's SteamID
  36. uint16 m_nHdrVersion;
  37. JobID_t m_JobIDTarget;
  38. JobID_t m_JobIDSource;
  39. CUtlString GetHeaderDescription();
  40. const char *PchMsgName( ) const { return PchMsgNameFromEMsg( m_eMsg ); }
  41. };
  42. #pragma pack( push, 1 )
  43. struct ProtoBufMsgHeader_t
  44. {
  45. int32 m_EMsgFlagged; // High bit should be set to indicate this message header type is in use. The rest of the bits indicate message type.
  46. uint32 m_cubProtoBufExtHdr; // Size of the extended header which is a serialized protobuf object. Indicates where it ends and the serialized body protobuf begins.
  47. ProtoBufMsgHeader_t() : m_EMsgFlagged( 0 ), m_cubProtoBufExtHdr( 0 ) {}
  48. ProtoBufMsgHeader_t( MsgType_t eMsg, uint32 cubProtoBufExtHdr ) : m_EMsgFlagged( eMsg | k_EMsgProtoBufFlag ), m_cubProtoBufExtHdr( cubProtoBufExtHdr ) {}
  49. const char *PchMsgName() const { return PchMsgNameFromEMsg( GetEMsg() ); }
  50. MsgType_t GetEMsg() const { return (MsgType_t)(m_EMsgFlagged & (~k_EMsgProtoBufFlag) ); }
  51. };
  52. #pragma pack(pop)
  53. //-----------------------------------------------------------------------------
  54. // CStructNetPacket
  55. // Thin wrapper around raw CNetPacket which implements our IMsgNetPacket interface.
  56. //-----------------------------------------------------------------------------
  57. class CStructNetPacket : public IMsgNetPacket
  58. {
  59. #ifdef GC
  60. DECLARE_CLASS_MEMPOOL( CStructNetPacket );
  61. #endif
  62. public:
  63. CStructNetPacket( CNetPacket *pNetPacket )
  64. {
  65. m_pHeader = (GCMsgHdrEx_t*)pNetPacket->PubData();
  66. m_pNetPacket = pNetPacket;
  67. m_pNetPacket->AddRef();
  68. }
  69. EMsgFormatType GetEMsgFormatType() const { return k_EMsgFormatTypeStruct; }
  70. CNetPacket *GetCNetPacket() const { return m_pNetPacket; }
  71. uint8 *PubData() const { return m_pNetPacket->PubData(); }
  72. uint CubData() const { return m_pNetPacket->CubData(); }
  73. MsgType_t GetEMsg() const { return (MsgType_t)m_pHeader->m_eMsg; }
  74. JobID_t GetSourceJobID() const { return m_pHeader->m_JobIDSource; }
  75. JobID_t GetTargetJobID() const { return m_pHeader->m_JobIDTarget; }
  76. void SetTargetJobID( JobID_t ulJobID ) { m_pHeader->m_JobIDTarget = ulJobID; }
  77. CSteamID GetSteamID() const { return CSteamID( m_pHeader->m_ulSteamID ); }
  78. void SetSteamID( CSteamID steamID ) { m_pHeader->m_ulSteamID = steamID.ConvertToUint64(); }
  79. AppId_t GetSourceAppID() const { return k_uAppIdInvalid; }
  80. void SetSourceAppID( AppId_t appId ) {}
  81. // Routing to a job name is not permitted with the old packet format
  82. virtual bool BHasTargetJobName() const { return false; }
  83. virtual const char *GetTargetJobName() const { return NULL; }
  84. private:
  85. virtual ~CStructNetPacket()
  86. {
  87. m_pNetPacket->Release();
  88. }
  89. CNetPacket *m_pNetPacket;
  90. GCMsgHdrEx_t *m_pHeader;
  91. };
  92. #define GCMSG_EX_HEADER_SIZE ( sizeof( GCSDK::GCMsgHdrEx_t ) - sizeof( GCSDK::GCMsgHdr_t ) )
  93. static const uint16 k_nHdrVersion = 0x1;
  94. //-----------------------------------------------------------------------------
  95. // Purpose: Header for messages from one GC to another
  96. //-----------------------------------------------------------------------------
  97. struct GCMsgInterHdr_t
  98. {
  99. MsgType_t m_eMsg; // The message type
  100. uint32 m_unSourceAppId; // App ID of the source GC
  101. uint16 m_nHdrVersion;
  102. JobID_t m_JobIDTarget;
  103. JobID_t m_JobIDSource;
  104. CUtlString GetHeaderDescription();
  105. const char *PchMsgName( ) const { return PchMsgNameFromEMsg( m_eMsg ); }
  106. };
  107. #pragma pack( pop )
  108. //-----------------------------------------------------------------------------
  109. // CGCMsgBase
  110. // Message class for messages between the GC and a GS or client
  111. // Handles a message with a header of type GCMsgHdrEx_t, a payload of type MSG_BODY_TYPE, and optional variable length data
  112. //-----------------------------------------------------------------------------
  113. typedef CMsgBase_t<GCMsgHdrEx_t> CGCMsgBase;
  114. //-----------------------------------------------------------------------------
  115. //
  116. //-----------------------------------------------------------------------------
  117. template <typename MSG_BODY_TYPE>
  118. class CGCMsg : public CGCMsgBase
  119. {
  120. public:
  121. // Client send constructor
  122. CGCMsg( MsgType_t eMsg, uint32 cubReserve = 64 ) :
  123. CGCMsgBase( sizeof( MSG_BODY_TYPE ), cubReserve )
  124. {
  125. // Fill out the message header
  126. Hdr().m_eMsg = eMsg;
  127. Hdr().m_nHdrVersion = k_nHdrVersion;
  128. Hdr().m_JobIDSource = k_GIDNil;
  129. Hdr().m_JobIDTarget = k_GIDNil;
  130. }
  131. // reply constructor
  132. CGCMsg( MsgType_t eMsg, const CGCMsgBase &msg, uint32 cubReserve = 64 ) :
  133. CGCMsgBase( sizeof( MSG_BODY_TYPE ), cubReserve )
  134. {
  135. // Fill out the message header
  136. Hdr().m_eMsg = eMsg;
  137. Hdr().m_ulSteamID = msg.Hdr().m_ulSteamID;
  138. Hdr().m_nHdrVersion = k_nHdrVersion;
  139. Hdr().m_JobIDSource = k_GIDNil;
  140. Hdr().m_JobIDTarget = msg.Hdr().m_JobIDSource;
  141. }
  142. CGCMsg( uint8 *pubPkt, uint32 cubPkt ) :
  143. CGCMsgBase( sizeof( GCMsgHdrEx_t ), sizeof( MSG_BODY_TYPE ), pubPkt, cubPkt )
  144. {
  145. }
  146. // Receive constructor
  147. // Use this constructor when creating a message from a received network packet
  148. CGCMsg( CIMsgNetPacketAutoRelease &refNetPacket )
  149. :CGCMsgBase( sizeof( GCMsgHdrEx_t ), sizeof( MSG_BODY_TYPE ), refNetPacket->PubData(), refNetPacket->CubData() )
  150. {
  151. }
  152. // Receive constructor
  153. // Use this constructor when creating a message from a received network packet
  154. CGCMsg( IMsgNetPacket *pNetPacket )
  155. :CGCMsgBase( sizeof( GCMsgHdrEx_t ), sizeof( MSG_BODY_TYPE ), pNetPacket->PubData(), pNetPacket->CubData() )
  156. {
  157. }
  158. // Receive constructor
  159. // Use this constructor when creating a message from a received network packet
  160. CGCMsg( CNetPacket *pNetPacket )
  161. :CGCMsgBase( sizeof( GCMsgHdrEx_t ), sizeof( MSG_BODY_TYPE ), pNetPacket->PubData(), pNetPacket->CubData() )
  162. {
  163. }
  164. // empty constructor
  165. CGCMsg() :
  166. CGCMsgBase( sizeof( GCMsgHdrEx_t ), sizeof( MSG_BODY_TYPE ), NULL, 0 )
  167. {
  168. }
  169. ~CGCMsg()
  170. {
  171. }
  172. // Accessors
  173. MSG_BODY_TYPE &Body() { return * ( MSG_BODY_TYPE * ) ( m_pubPkt + m_cubMsgHdr ); }
  174. const MSG_BODY_TYPE &Body() const { return * ( MSG_BODY_TYPE * ) ( m_pubPkt + m_cubMsgHdr ); }
  175. GCMsgHdrEx_t * PGCMsgHdr() { return ( ( GCMsgHdrEx_t * ) m_pubPkt ); }
  176. MsgType_t GetEMsg() const { return Hdr().m_eMsg; }
  177. // Called to set the JobID that will be expecting
  178. // a reply to this message.
  179. void ExpectingReply( JobID_t jobIDSource )
  180. {
  181. Hdr().m_JobIDSource = jobIDSource;
  182. }
  183. bool BIsExpectingReply() { return Hdr().m_JobIDSource != k_GIDNil; }
  184. };
  185. } // namespace GCSDK
  186. #endif // GCMSG_H