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.

122 lines
4.0 KiB

  1. //======= Copyright (c) 1996-2009, Valve Corporation, All rights reserved. ======
  2. //
  3. // Implementation of the CGameEventTransmitter class.
  4. //
  5. //
  6. //===============================================================================
  7. #include <tier0/vprof.h>
  8. #include "gameeventtransmitter.h"
  9. #include "net.h"
  10. #include "tier1/convar.h"
  11. #include "GameEventManager.h"
  12. #include "tier0/icommandline.h"
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. //////////////////////////////////////////////////////////////////////
  16. // Construction/Destruction
  17. //////////////////////////////////////////////////////////////////////
  18. static CGameEventTransmitter s_GameEventTransmitter;
  19. CGameEventTransmitter &g_GameEventTransmitter = s_GameEventTransmitter;
  20. //-----------------------------------------------------------------------------
  21. // Convar function to set the IP address and port.
  22. //-----------------------------------------------------------------------------
  23. void CC_TransmitEvents( const CCommand &args )
  24. {
  25. if ( !g_GameEventTransmitter.SetIPAndPort( args.ArgS() ) )
  26. {
  27. if ( args.ArgC() > 1 )
  28. {
  29. Msg("Invalid address or port: %s\n", args.ArgS() );
  30. }
  31. else
  32. {
  33. Msg("No address and port passed in.\n" );
  34. }
  35. }
  36. else
  37. {
  38. Msg("SUCCESS! address and port is now set to: %s\n", args.ArgS() );
  39. }
  40. }
  41. static ConCommand TransmitEvents( "TransmitEvents", CC_TransmitEvents, "Transmits Game Events to <address:port>", FCVAR_DEVELOPMENTONLY );
  42. //-----------------------------------------------------------------------------
  43. // Initializes the network address based on IP and port
  44. //-----------------------------------------------------------------------------
  45. bool CGameEventTransmitter::SetIPAndPort( const char *address )
  46. {
  47. m_Adr.SetFromString( address );
  48. return m_Adr.IsValid();
  49. }
  50. //-----------------------------------------------------------------------------
  51. // Constructor
  52. //-----------------------------------------------------------------------------
  53. CGameEventTransmitter::CGameEventTransmitter()
  54. {
  55. m_Adr.Clear();
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Destructor
  59. //-----------------------------------------------------------------------------
  60. CGameEventTransmitter::~CGameEventTransmitter()
  61. {
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Used to be able to set the IP address and port from the command line.
  65. // This will be the primary way to initialize the system, so that uses
  66. // won't have to enter the address/port during game play.
  67. //-----------------------------------------------------------------------------
  68. bool CGameEventTransmitter::Init()
  69. {
  70. const char *address = CommandLine()->ParmValue( "-transmitevents", "" );
  71. if ( address[0] )
  72. {
  73. SetIPAndPort( address );
  74. }
  75. return true;
  76. }
  77. //-----------------------------------------------------------------------------
  78. // This function actually serializes the event data and sends it out on the network
  79. // to the address and port previously provided.
  80. //-----------------------------------------------------------------------------
  81. void CGameEventTransmitter::TransmitGameEvent( IGameEvent *event )
  82. {
  83. // Don't bother doing anything if we don't have a transmit address, bad event, or a non-networked event
  84. if ( !m_Adr.IsValid() || NULL == event || ( event && event->IsLocal() ) )
  85. {
  86. return;
  87. }
  88. CSVCMsg_GameEvent_t eventData;
  89. // We send the event name instead of event ID becase the ID's can change depending on what the server sends to the clients
  90. eventData.set_event_name( event->GetName() );
  91. // create bitstream from KeyValues
  92. if ( g_GameEventManager.SerializeEvent( event, &eventData ) )
  93. {
  94. bf_write buffer;
  95. unsigned char buffer_data[MAX_EVENT_BYTES];
  96. buffer.StartWriting( buffer_data, sizeof(buffer_data) );
  97. eventData.WriteToBuffer( buffer );
  98. NET_SendPacket( NULL, NS_CLIENT, m_Adr, buffer_data, buffer.GetNumBytesWritten() );
  99. }
  100. else
  101. {
  102. DevMsg("GameEventTransmitter: failed to serialize event '%s'.\n", event->GetName() );
  103. }
  104. }