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.

106 lines
2.6 KiB

  1. //====== Copyright (c), Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Holds the CNetPacket class
  4. //
  5. //=============================================================================
  6. #include "stdafx.h"
  7. // memdbgon must be the last include file in a .cpp file!!!
  8. #include "tier0/memdbgon.h"
  9. namespace GCSDK
  10. {
  11. //-----------------------------------------------------------------------------
  12. // Purpose: Constructor
  13. //-----------------------------------------------------------------------------
  14. CNetPacket::CNetPacket()
  15. {
  16. m_cRef = 0;
  17. m_pubData = NULL;
  18. m_cubData = 0;
  19. }
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Destructor, validates refcount
  22. //-----------------------------------------------------------------------------
  23. CNetPacket::~CNetPacket()
  24. {
  25. Assert( m_cRef == 0 );
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Inits all members
  29. // Input : hConnection - connection we're from
  30. // pubData - message data
  31. // cubData - message size
  32. // *pubNetworkBuffer - network buffer that contains the message
  33. // assumes control of this memory
  34. //-----------------------------------------------------------------------------
  35. void CNetPacket::Init( uint32 cubData, const void* pCopyData )
  36. {
  37. Assert( cubData );
  38. m_pubData = (uint8 *)g_MemPoolMsg.Alloc( cubData );
  39. m_cubData = cubData;
  40. if( pCopyData )
  41. {
  42. Q_memcpy( m_pubData, pCopyData, cubData );
  43. }
  44. AddRef();
  45. }
  46. //-----------------------------------------------------------------------------
  47. // Purpose: adds to refcount
  48. //-----------------------------------------------------------------------------
  49. void CNetPacket::AddRef()
  50. {
  51. ++m_cRef;
  52. }
  53. //-----------------------------------------------------------------------------
  54. void CNetPacket::InitAdoptBuffer( uint32 cubData, uint8* pubData )
  55. {
  56. Assert( !m_pubData );
  57. m_pubData = pubData;
  58. m_cubData = cubData;
  59. AddRef();
  60. }
  61. //-----------------------------------------------------------------------------
  62. void CNetPacket::OrphanBuffer()
  63. {
  64. m_pubData = NULL;
  65. m_cubData = 0;
  66. }
  67. //-----------------------------------------------------------------------------
  68. // Purpose: decrements refcount
  69. //-----------------------------------------------------------------------------
  70. void CNetPacket::Release()
  71. {
  72. Assert( m_cRef > 0 );
  73. if ( --m_cRef == 0 )
  74. {
  75. // delete the network buffer we're associated with, if we have one
  76. if ( m_pubData )
  77. {
  78. g_MemPoolMsg.Free( m_pubData );
  79. }
  80. // delete ourselves
  81. g_cNetPacket--;
  82. CNetPacketPool::sm_MemPoolNetPacket.Free( this );
  83. }
  84. }
  85. } // namespace GCSDK