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.

170 lines
5.7 KiB

  1. //====== Copyright �, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Maps message types to strings and vice versa
  4. //
  5. //=============================================================================
  6. #ifndef MESSAGELIST_H
  7. #define MESSAGELIST_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "google/protobuf/descriptor.h"
  12. #include "gcsdk/jobtime.h"
  13. namespace GCSDK
  14. {
  15. extern CGCEmitGroup g_EGMessages;
  16. extern const char *PchMsgNameFromEMsg( MsgType_t eMsg );
  17. //-----------------------------------------------------------------------------
  18. // message type flags
  19. //-----------------------------------------------------------------------------
  20. static const int MT_GC = 0x01; // this message is sent to or from a Game Coordinator (will be proxied by servers along the way)
  21. static const int MT_GC_SYSTEM = 0x02; // this message was sent to or from the steam servers as a system message. Clients can't send these
  22. //-----------------------------------------------------------------------------
  23. // Various info about each message type
  24. //-----------------------------------------------------------------------------
  25. struct MsgInfo_t
  26. {
  27. MsgType_t eMsg;
  28. int nFlags;
  29. const char *pchMsgName;
  30. struct Stats_t
  31. {
  32. Stats_t() : nSourceMask(0), nCount( 0 ), uBytes( 0 ) {}
  33. uint32 nSourceMask;
  34. uint32 nCount;
  35. uint64 uBytes;
  36. };
  37. enum EStatsType
  38. {
  39. k_EStatsTypeSent,
  40. k_EStatsTypeReceived,
  41. k_EStatsTypeMultiplexedSends,
  42. k_EStatsTypeMultiplexedSendsRaw,
  43. k_EStatsType_Count
  44. };
  45. enum EStatsGroup
  46. {
  47. k_EStatsGroupGlobal,
  48. k_EStatsGroupProfile,
  49. k_EStatsGroupWindow,
  50. k_EStatsGroup_Count
  51. };
  52. Stats_t stats[ k_EStatsGroup_Count ][ k_EStatsType_Count ];
  53. };
  54. //-----------------------------------------------------------------------------
  55. // Purpose: Using protobuf reflection, bind them into a message list
  56. //-----------------------------------------------------------------------------
  57. void MsgRegistrationFromEnumDescriptor( const ::google::protobuf::EnumDescriptor *pEnumDescriptor, int nTypeMask );
  58. //-----------------------------------------------------------------------------
  59. // manages a hashed list of messages, allowing fast tests for validity and
  60. // info lookup.
  61. //-----------------------------------------------------------------------------
  62. class CMessageList
  63. {
  64. public:
  65. CMessageList();
  66. ~CMessageList();
  67. bool BInit( );
  68. // returns false if a message isn't valid or isn't one of the types specified
  69. // or true if the message is valid. ppMsgName can be NULL.
  70. bool GetMessage( MsgType_t eMsg, const char **ppMsgName, int nTypeMask );
  71. // make stats about sending messages
  72. void TallySendMessage( MsgType_t eMsg, uint32 uMsgSize, uint32 nSourceMask = 0 );
  73. void TallyReceiveMessage( MsgType_t eMsg, uint32 uMsgSize, uint32 nSourceMask = 0);
  74. void TallyMultiplexedMessage( MsgType_t eMsg, uint32 uSent, uint32 cRecipients, uint32 uMsgSize, uint32 nSourceMask = 0 );
  75. // profiling
  76. void EnableProfiling( bool bEnableProfiling );
  77. // print out our stats
  78. void PrintStats( bool bShowAll, bool bSortByFrequency, MsgInfo_t::EStatsGroup eGroup, MsgInfo_t::EStatsType eType, uint32 nSourceMask = 0 ) const;
  79. void PrintMultiplexStats( MsgInfo_t::EStatsGroup eGroup, bool bSortByFrequency, uint32 nSourceMask = 0 ) const;
  80. // Window management - This is similar to profiling in many ways, but is separate so that the base system can monitor traffic rates without
  81. // interfering with profiles.
  82. //called to obtain the totals for the timing window
  83. const MsgInfo_t::Stats_t& GetWindowTotal( MsgInfo_t::EStatsType eType ) const;
  84. //called to reset the window timings
  85. void ResetWindow();
  86. //returns how long this window has been running in microseconds
  87. uint64 GetWindowDuration() const { return GetGroupDuration( MsgInfo_t::k_EStatsGroupWindow ); }
  88. private:
  89. void TallyMessageInternal( MsgInfo_t &msgInfo, MsgInfo_t::EStatsType eBucket, uint32 unMsgSize, uint32 nSourceMask, uint32 cMessages = 1 );
  90. void AssureBucket( int nBucket );
  91. //-----------------------------------------------------------------------------
  92. // given a particular message ID, find out what bucket it would be in,
  93. // as well as which slot in that bucket.
  94. //-----------------------------------------------------------------------------
  95. static int HashMessage( MsgType_t eMsg, int &nSlot )
  96. {
  97. // hash is everything except the lowest nibble,
  98. // because buckets are 16 entries
  99. int nBucket = eMsg / m_kcBucketSize;
  100. nSlot = eMsg % m_kcBucketSize;
  101. return nBucket;
  102. }
  103. short GetMessageIndex( MsgType_t eMsg );
  104. //given a group, this will return the time that the stats have been collected over
  105. uint64 GetGroupDuration( MsgInfo_t::EStatsGroup eGroup ) const;
  106. private:
  107. //totalled stats for the current window. It would be too costly to total these all the time
  108. MsgInfo_t::Stats_t m_WindowTotals[ MsgInfo_t::k_EStatsType_Count ];
  109. //the time that we have been collecting each of these buckets
  110. CJobTime m_sCollectTime[ MsgInfo_t::k_EStatsGroup_Count ];
  111. //are we currently actively tracking a profile?
  112. bool m_bProfiling;
  113. //the duration of the last finished profile (if it is no longer running, otherwise use the timer)
  114. uint64 m_ulProfileMicrosecs;
  115. CUtlVector< short* > m_vecMessageInfoBuckets;
  116. CUtlVector<MsgInfo_t> m_vecMsgInfo;
  117. static const int m_kcBucketSize = 16;
  118. };
  119. extern CMessageList g_theMessageList;
  120. //-----------------------------------------------------------------------------
  121. // Purpose: Returns the true if the specified message is a valid GC system message.
  122. // Input : eMsg - message type to test
  123. // ppMsgName - Optional pointer to receive message name
  124. //-----------------------------------------------------------------------------
  125. inline bool BIsValidSystemMsg( MsgType_t eMsg, const char **ppMsgName )
  126. {
  127. return g_theMessageList.GetMessage( eMsg, ppMsgName, MT_GC_SYSTEM );
  128. }
  129. } // namespace GCSDK
  130. #endif // MESSAGELIST_H