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.

172 lines
5.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef NETWORKSYSTEM_H
  7. #define NETWORKSYSTEM_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "networksystem/inetworksystem.h"
  12. #include "tier1/utlvector.h"
  13. #include "tier1/bitbuf.h"
  14. #include "sm_protocol.h"
  15. #include "networksystem/inetworkmessage.h"
  16. #include "tier1/netadr.h"
  17. #include "tier1/utlstring.h"
  18. #include "tier2/tier2.h"
  19. //-----------------------------------------------------------------------------
  20. // Forward declarations
  21. //-----------------------------------------------------------------------------
  22. class CNetworkServer;
  23. class CNetworkClient;
  24. class IConnectionlessPacketHandler;
  25. class CNetChannel;
  26. enum SystemNetworkMessageType_t;
  27. //-----------------------------------------------------------------------------
  28. // Global interfaces
  29. //-----------------------------------------------------------------------------
  30. class CNetworkSystem;
  31. extern CNetworkSystem *g_pNetworkSystemImp;
  32. //-----------------------------------------------------------------------------
  33. // Implementation of the network system
  34. //-----------------------------------------------------------------------------
  35. class CNetworkSystem : public CTier2AppSystem< INetworkSystem >
  36. {
  37. typedef CTier2AppSystem< INetworkSystem > BaseClass;
  38. public:
  39. // Constructor, destructor
  40. CNetworkSystem();
  41. virtual ~CNetworkSystem();
  42. // Inherited from IAppSystem
  43. virtual bool Connect( CreateInterfaceFn factory );
  44. virtual InitReturnVal_t Init();
  45. virtual void Shutdown();
  46. // Inherited from INetworkSystem
  47. virtual bool RegisterMessage( INetworkMessage *msg );
  48. virtual bool StartServer( unsigned short nServerListenPort );
  49. virtual void ShutdownServer( );
  50. virtual void ServerReceiveMessages();
  51. virtual void ServerSendMessages();
  52. virtual bool StartClient( unsigned short nClientListenPort );
  53. virtual void ShutdownClient( );
  54. virtual void ClientSendMessages();
  55. virtual void ClientReceiveMessages();
  56. virtual INetChannel* ConnectClientToServer( const char *pServer, int nServerListenPort );
  57. virtual void DisconnectClientFromServer( INetChannel* pChan );
  58. virtual NetworkEvent_t *FirstNetworkEvent( );
  59. virtual NetworkEvent_t *NextNetworkEvent( );
  60. virtual const char* GetLocalHostName( void ) const;
  61. virtual const char* GetLocalAddress( void ) const;
  62. // Methods internal for use in networksystem library
  63. // Method to allow systems to add network events received
  64. NetworkEvent_t* CreateNetworkEvent( int nSizeInBytes );
  65. template< class T > T* CreateNetworkEvent();
  66. bool IsNetworkEventCreated();
  67. // Finds a network message given a particular message type
  68. INetworkMessage* FindNetworkMessage( int group, int type );
  69. // Returns the number of bits to encode the type + group with
  70. int GetTypeBitCount() const;
  71. int GetGroupBitCount() const;
  72. // Returns the current time
  73. float GetTime( void );
  74. // Converts a string to a socket address
  75. bool StringToSockaddr( const char *s, struct sockaddr *sadr );
  76. // Queues up a network packet
  77. void EnqueueConnectionlessNetworkPacket( CNetPacket *pPacket, IConnectionlessPacketHandler *pHandler );
  78. void EnqueueNetworkPacket( CNetPacket *pPacket, CNetChannel *pNetChannel );
  79. private:
  80. struct PacketInfo_t
  81. {
  82. CNetPacket *m_pPacket;
  83. IConnectionlessPacketHandler *m_pHandler;
  84. CNetChannel *m_pNetChannel;
  85. };
  86. // Network event iteration helpers
  87. bool StartProcessingNewPacket();
  88. bool AdvanceProcessingNetworkPacket( );
  89. void CleanupNetworkMessages( );
  90. bool m_bWinsockInitialized : 1;
  91. bool m_bNetworkEventCreated : 1;
  92. bool m_bInMidPacket : 1;
  93. int m_nTypeBits;
  94. int m_nGroupBits;
  95. netadr_t m_LocalAddress;
  96. CUtlString m_LocalAddressString;
  97. CUtlString m_LocalHostName;
  98. CNetworkServer *m_pServer;
  99. CNetworkClient *m_pClient;
  100. unsigned char m_EventMessageBuffer[256];
  101. CUtlVector<PacketInfo_t> m_PacketQueue;
  102. int m_nProcessingPacket;
  103. CUtlVector<INetworkMessage*> m_NetworkMessages;
  104. };
  105. //-----------------------------------------------------------------------------
  106. // Inline methods
  107. //-----------------------------------------------------------------------------
  108. template< class T >
  109. T* CNetworkSystem::CreateNetworkEvent()
  110. {
  111. // Increase the size of m_EventMessageBuffer if this assertion fails
  112. COMPILE_TIME_ASSERT( sizeof(T) <= sizeof( m_EventMessageBuffer ) );
  113. return (T*)CreateNetworkEvent( sizeof(T) );
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Returns the number of bits to encode the type + group with
  117. //-----------------------------------------------------------------------------
  118. inline int CNetworkSystem::GetTypeBitCount() const
  119. {
  120. return m_nTypeBits;
  121. }
  122. inline int CNetworkSystem::GetGroupBitCount() const
  123. {
  124. return m_nGroupBits;
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Writes a system network message
  128. //-----------------------------------------------------------------------------
  129. inline void WriteSystemNetworkMessage( bf_write &msg, SystemNetworkMessageType_t type )
  130. {
  131. msg.WriteUBitLong( net_group_networksystem, g_pNetworkSystemImp->GetGroupBitCount() );
  132. msg.WriteUBitLong( type, g_pNetworkSystemImp->GetTypeBitCount() );
  133. }
  134. inline void WriteNetworkMessage( bf_write &msg, INetworkMessage *pNetworkMessage )
  135. {
  136. msg.WriteUBitLong( pNetworkMessage->GetGroup(), g_pNetworkSystemImp->GetGroupBitCount() );
  137. msg.WriteUBitLong( pNetworkMessage->GetType(), g_pNetworkSystemImp->GetTypeBitCount() );
  138. }
  139. #endif // NETWORKSYSTEM_H