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.

159 lines
4.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: The main manager of the networking code in the game
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //===========================================================================//
  8. #ifndef NETWORKMANAGER_H
  9. #define NETWORKMANAGER_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "gamemanager.h"
  14. #include "networksystem/inetworksystem.h"
  15. #include "inetworkmessagelistener.h"
  16. //-----------------------------------------------------------------------------
  17. // Default ports
  18. //-----------------------------------------------------------------------------
  19. enum
  20. {
  21. LEGION_DEFAULT_SERVER_PORT = 0xFFFF,
  22. LEGION_DEFAULT_CLIENT_PORT = 0xFFFF,
  23. };
  24. //-----------------------------------------------------------------------------
  25. // Interface used to create network messages
  26. //-----------------------------------------------------------------------------
  27. abstract_class INetworkMessageFactory
  28. {
  29. public:
  30. // Creates the menu
  31. virtual INetworkMessage *CreateNetworkMessage( ) = 0;
  32. // Used to build a list during construction
  33. virtual INetworkMessageFactory *GetNextFactory( ) = 0;
  34. protected:
  35. virtual ~INetworkMessageFactory() {}
  36. };
  37. //-----------------------------------------------------------------------------
  38. // Network management system
  39. //-----------------------------------------------------------------------------
  40. class CNetworkManager : public CGameManager<>
  41. {
  42. public:
  43. // Inherited from IGameManager
  44. virtual bool Init();
  45. virtual void Update( );
  46. virtual void Shutdown();
  47. // Are we a client?/Are we a server? (we can be both)
  48. bool IsClient();
  49. bool IsServer();
  50. // If we are a client, are we connected to the server?
  51. bool IsClientConnected();
  52. // Start a server up
  53. bool StartServer( unsigned short nServerListenPort = LEGION_DEFAULT_SERVER_PORT );
  54. void ShutdownServer( );
  55. // Start a client up
  56. bool StartClient( unsigned short nClientListenPort = LEGION_DEFAULT_CLIENT_PORT );
  57. void ShutdownClient( );
  58. // Connects the client to a server
  59. bool ConnectClientToServer( const char *pServerName, unsigned short nServerListenPort = LEGION_DEFAULT_SERVER_PORT );
  60. void DisconnectClientFromServer();
  61. // Add, remove network message listeners
  62. void AddListener( NetworkMessageRoute_t route, int nGroup, int nType, INetworkMessageListener *pListener );
  63. void RemoveListener( NetworkMessageRoute_t route, int nGroup, int nType, INetworkMessageListener *hListener );
  64. // Post a network message to the server
  65. void PostClientToServerMessage( INetworkMessage *pMessage );
  66. // Broadcast a network message to all clients
  67. void BroadcastServerToClientMessage( INetworkMessage *pMessage );
  68. // Start up a game where the local player is playing
  69. bool HostGame();
  70. void StopHostingGame();
  71. // Call to register methods which can construct menus w/ particular ids
  72. // NOTE: This method is not expected to be called directly. Use the REGISTER_NETWORK_MESSAGE macro instead
  73. // It returns the previous head of the list of factories
  74. static INetworkMessageFactory* RegisterNetworkMessage( INetworkMessageFactory *pMenuFactory );
  75. private:
  76. // Process messages received by the client
  77. void ProcessClientMessages();
  78. // Process messages received by the server
  79. void ProcessServerMessages();
  80. // Notifies listeners about a network message
  81. void NotifyListeners( NetworkMessageRoute_t route, INetworkMessage *pMessage );
  82. bool m_bIsClient;
  83. bool m_bIsServer;
  84. INetChannel *m_pClientToServerConnection;
  85. CUtlVector< INetChannel* > m_ServerToClientConnection;
  86. CUtlVector< CUtlVector< CUtlVector < INetworkMessageListener* > > > m_Listeners[ NETWORK_MESSAGE_ROUTE_COUNT ];
  87. static INetworkMessageFactory* m_pFirstFactory;
  88. };
  89. //-----------------------------------------------------------------------------
  90. // Singleton accessor
  91. //-----------------------------------------------------------------------------
  92. extern CNetworkManager *g_pNetworkManager;
  93. //-----------------------------------------------------------------------------
  94. // Macro used to register menus with the menu system
  95. // For example, add the line REGISTER_NETWORK_MESSAGE( CNetworkMessageChat );
  96. // into the file defining the chat message
  97. //-----------------------------------------------------------------------------
  98. template < class T >
  99. class CNetworkMessageFactory : public INetworkMessageFactory
  100. {
  101. public:
  102. CNetworkMessageFactory( )
  103. {
  104. m_pNextFactory = CNetworkManager::RegisterNetworkMessage( this );
  105. }
  106. // Creates the network message
  107. virtual INetworkMessage *CreateNetworkMessage( )
  108. {
  109. return new T;
  110. }
  111. // Used to build a list during construction
  112. virtual INetworkMessageFactory *GetNextFactory( )
  113. {
  114. return m_pNextFactory;
  115. }
  116. private:
  117. INetworkMessage *m_pMessage;
  118. INetworkMessageFactory *m_pNextFactory;
  119. };
  120. #define REGISTER_NETWORK_MESSAGE( _className ) \
  121. static CNetworkMessageFactory< _className > __s_Factory ## _className
  122. #endif // NETWORKMANAGER_H