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.

176 lines
4.2 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef USERMESSAGES_H
  7. #define USERMESSAGES_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include <utldict.h>
  12. #include <utlvector.h>
  13. #include <bitbuf.h>
  14. #include <utldelegate.h>
  15. #include "shareddefs.h"
  16. #include "networksystem/inetworksystem.h"
  17. // eliminates a conflict with TYPE_BOOL in OSX
  18. #ifdef TYPE_BOOL
  19. #undef TYPE_BOOL
  20. #endif
  21. #include "cstrike15_usermessages.pb.h"
  22. //-----------------------------------------------------------------------------
  23. class IUserMessageBinder;
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Interface for registering and dispatching usermessages
  26. // Shred code creates same ordered list on client/server
  27. //-----------------------------------------------------------------------------
  28. class CUserMessages
  29. {
  30. public:
  31. CUserMessages();
  32. ~CUserMessages();
  33. bool DispatchUserMessage( int msg_type, int32 nPassthroughFlags, int size, const void *msg );
  34. void BindMessage( IUserMessageBinder *pMessageBinder );
  35. bool UnbindMessage( IUserMessageBinder *pMessageBinder );
  36. private:
  37. typedef CCopyableUtlVector< IUserMessageBinder * > UserMessageBinderVec_t;
  38. typedef CUtlMap< int, CCopyableUtlVectorFixed< UserMessageBinderVec_t, MAX_SPLITSCREEN_PLAYERS > > UserMessageHandlerMap_t;
  39. UserMessageHandlerMap_t m_UserMessageBinderMap;
  40. };
  41. extern CUserMessages *UserMessages();
  42. //-----------------------------------------------------------------------------
  43. class IUserMessageBinder
  44. {
  45. public:
  46. virtual ~IUserMessageBinder() = 0;
  47. virtual int GetType() const = 0;
  48. virtual ::google::protobuf::Message *Parse( int32 nPassthroughFlags, const void *msg, int size, bool &bSilentIgnore ) = 0;
  49. virtual bool Invoke( ::google::protobuf::Message const *msg ) = 0;
  50. virtual const CUtlAbstractDelegate &GetAbstractDelegate() = 0;
  51. };
  52. //-----------------------------------------------------------------------------
  53. class CUserMessageBinder
  54. {
  55. public:
  56. CUserMessageBinder()
  57. : m_pBind( NULL )
  58. {
  59. }
  60. ~CUserMessageBinder()
  61. {
  62. delete m_pBind;
  63. }
  64. template< int msgType, typename PB_OBJECT_TYPE >
  65. void Bind( CUtlDelegate< bool ( const PB_OBJECT_TYPE & obj ) > handler )
  66. {
  67. delete m_pBind;
  68. m_pBind = new BindParams< msgType, PB_OBJECT_TYPE, 0 >( handler );
  69. }
  70. template< int msgType, typename PB_OBJECT_TYPE >
  71. void BindRealtimePassthrough( CUtlDelegate< bool( const PB_OBJECT_TYPE & obj ) > handler )
  72. {
  73. delete m_pBind;
  74. m_pBind = new BindParams< msgType, PB_OBJECT_TYPE, 1 >( handler );
  75. }
  76. void Unbind()
  77. {
  78. if ( m_pBind )
  79. {
  80. delete m_pBind;
  81. m_pBind = NULL;
  82. }
  83. }
  84. bool IsBound() const
  85. {
  86. return m_pBind != NULL;
  87. }
  88. private:
  89. template< int msgType, typename PB_OBJECT_TYPE, int32 nExpectedPassthroughInReplay >
  90. struct BindParams : public IUserMessageBinder
  91. {
  92. BindParams( CUtlDelegate< bool ( PB_OBJECT_TYPE const &msg ) > handler )
  93. : m_handler( handler )
  94. {
  95. UserMessages()->BindMessage( this );
  96. }
  97. virtual ~BindParams()
  98. {
  99. UserMessages()->UnbindMessage( this );
  100. }
  101. virtual int GetType() const
  102. {
  103. return msgType;
  104. }
  105. virtual ::google::protobuf::Message *Parse( int32 nPassthroughFlags, const void *msg, int size, bool &bSilentIgnore ) OVERRIDE
  106. {
  107. if ( size < 0 || size > NET_MAX_PAYLOAD )
  108. {
  109. return NULL;
  110. }
  111. extern int CL_GetHltvReplayDelay();
  112. if ( CL_GetHltvReplayDelay() )
  113. {
  114. if ( nPassthroughFlags != nExpectedPassthroughInReplay )
  115. {
  116. // this is a wrong timeline message. Ignore it.
  117. bSilentIgnore = true;
  118. return NULL;
  119. }
  120. }
  121. ::google::protobuf::Message *pMsg = new PB_OBJECT_TYPE();
  122. if ( !pMsg->ParseFromArray( msg, size ) )
  123. {
  124. delete pMsg;
  125. return NULL;
  126. }
  127. return pMsg;
  128. }
  129. virtual bool Invoke( ::google::protobuf::Message const *msg )
  130. {
  131. if ( msg )
  132. {
  133. return m_handler( static_cast< PB_OBJECT_TYPE const & >( *msg ) );
  134. }
  135. return false;
  136. }
  137. virtual const CUtlAbstractDelegate &GetAbstractDelegate()
  138. {
  139. return m_handler.GetAbstractDelegate();
  140. }
  141. CUtlDelegate< bool ( PB_OBJECT_TYPE const &obj ) > m_handler;
  142. };
  143. IUserMessageBinder *m_pBind;
  144. };
  145. #endif // USERMESSAGES_H