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.

134 lines
3.9 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose: INetworkMessage interface
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef INETWORKMESSAGE_H
  8. #define INETWORKMESSAGE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier0/platform.h"
  13. #include "tier0/dbg.h"
  14. //-----------------------------------------------------------------------------
  15. // Forward declarations
  16. //-----------------------------------------------------------------------------
  17. class bf_read;
  18. class bf_write;
  19. class INetMsgHandler;
  20. class INetMessage;
  21. class INetChannel;
  22. //-----------------------------------------------------------------------------
  23. // First valid group number users of the network system can use
  24. //-----------------------------------------------------------------------------
  25. enum
  26. {
  27. NETWORKSYSTEM_FIRST_GROUP = 1,
  28. };
  29. //-----------------------------------------------------------------------------
  30. // A network message
  31. //-----------------------------------------------------------------------------
  32. abstract_class INetworkMessage
  33. {
  34. public:
  35. // Use these to setup who can hear whose voice.
  36. // Pass in client indices (which are their ent indices - 1).
  37. virtual void SetNetChannel(INetChannel * netchan) = 0; // netchannel this message is from/for
  38. virtual void SetReliable( bool state ) = 0; // set to true if it's a reliable message
  39. virtual bool ReadFromBuffer( bf_read &buffer ) = 0; // returns true if parsing was OK
  40. virtual bool WriteToBuffer( bf_write &buffer ) const = 0; // returns true if writing was OK
  41. virtual bool IsReliable( void ) const = 0; // true, if message needs reliable handling
  42. virtual int GetGroup( void ) const = 0; // returns net message group of this message
  43. virtual int GetType( void ) const = 0; // returns module specific header tag eg svc_serverinfo
  44. virtual const char *GetGroupName( void ) const = 0; // returns network message group name
  45. virtual const char *GetName( void ) const = 0; // returns network message name, eg "svc_serverinfo"
  46. virtual INetChannel *GetNetChannel( void ) const = 0;
  47. virtual const char *ToString( void ) const = 0; // returns a human readable string about message content
  48. virtual void Release() = 0;
  49. protected:
  50. virtual ~INetworkMessage() {};
  51. };
  52. //-----------------------------------------------------------------------------
  53. // Helper utilities for clients to create messages
  54. //-----------------------------------------------------------------------------
  55. #define DECLARE_BASE_MESSAGE( group, msgtype, description ) \
  56. public: \
  57. virtual bool ReadFromBuffer( bf_read &buffer ); \
  58. virtual bool WriteToBuffer( bf_write &buffer ) const; \
  59. virtual const char *ToString() const { return description; } \
  60. virtual int GetGroup() const { return group; } \
  61. virtual const char *GetGroupName( void ) const { return #group; } \
  62. virtual int GetType() const { return msgtype; } \
  63. virtual const char *GetName() const { return #msgtype;}\
  64. //-----------------------------------------------------------------------------
  65. // Default empty base class for net messages
  66. //-----------------------------------------------------------------------------
  67. class CNetworkMessage : public INetworkMessage
  68. {
  69. public:
  70. CNetworkMessage()
  71. {
  72. m_bReliable = true;
  73. m_pNetChannel = NULL;
  74. }
  75. virtual void Release()
  76. {
  77. delete this;
  78. }
  79. virtual ~CNetworkMessage() {};
  80. virtual void SetReliable( bool state )
  81. {
  82. m_bReliable = state;
  83. }
  84. virtual bool IsReliable() const
  85. {
  86. return m_bReliable;
  87. }
  88. virtual void SetNetChannel(INetChannel * netchan)
  89. {
  90. m_pNetChannel = netchan;
  91. }
  92. virtual bool Process()
  93. {
  94. // no handler set
  95. Assert( 0 );
  96. return false;
  97. }
  98. INetChannel *GetNetChannel() const
  99. {
  100. return m_pNetChannel;
  101. }
  102. protected:
  103. bool m_bReliable; // true if message should be send reliable
  104. INetChannel *m_pNetChannel; // netchannel this message is from/for
  105. };
  106. #endif // INETWORKMESSAGE_H