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.

162 lines
4.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef INETWORKSYSTEM_H
  7. #define INETWORKSYSTEM_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "tier0/platform.h"
  12. #include "appframework/IAppSystem.h"
  13. //-----------------------------------------------------------------------------
  14. // Forward declarations:
  15. //-----------------------------------------------------------------------------
  16. class INetworkMessageHandler;
  17. class INetworkMessage;
  18. class INetChannel;
  19. class INetworkMessageFactory;
  20. class bf_read;
  21. class bf_write;
  22. typedef struct netadr_s netadr_t;
  23. class CNetPacket;
  24. //-----------------------------------------------------------------------------
  25. // Default ports
  26. //-----------------------------------------------------------------------------
  27. enum
  28. {
  29. NETWORKSYSTEM_DEFAULT_SERVER_PORT = 27001,
  30. NETWORKSYSTEM_DEFAULT_CLIENT_PORT = 27002
  31. };
  32. //-----------------------------------------------------------------------------
  33. // This interface encompasses a one-way communication path between two
  34. //-----------------------------------------------------------------------------
  35. typedef int ConnectionHandle_t;
  36. enum ConnectionStatus_t
  37. {
  38. CONNECTION_STATE_DISCONNECTED = 0,
  39. CONNECTION_STATE_CONNECTING,
  40. CONNECTION_STATE_CONNECTION_FAILED,
  41. CONNECTION_STATE_CONNECTED,
  42. };
  43. //-----------------------------------------------------------------------------
  44. // This interface encompasses a one-way communication path between two machines
  45. //-----------------------------------------------------------------------------
  46. abstract_class INetChannel
  47. {
  48. public:
  49. // virtual INetworkMessageHandler *GetMsgHandler( void ) const = 0;
  50. virtual const netadr_t &GetRemoteAddress( void ) const = 0;
  51. // send a net message
  52. // NOTE: There are special connect/disconnect messages?
  53. virtual bool AddNetMsg( INetworkMessage *msg, bool bForceReliable = false ) = 0;
  54. // virtual bool RegisterMessage( INetworkMessage *msg ) = 0;
  55. virtual ConnectionStatus_t GetConnectionState( ) = 0;
  56. /*
  57. virtual ConnectTo( const netadr_t& to ) = 0;
  58. virtual Disconnect() = 0;
  59. virtual const netadr_t& GetLocalAddress() = 0;
  60. virtual const netadr_t& GetRemoteAddress() = 0;
  61. */
  62. };
  63. //-----------------------------------------------------------------------------
  64. // Network event types + structures
  65. //-----------------------------------------------------------------------------
  66. enum NetworkEventType_t
  67. {
  68. NETWORK_EVENT_CONNECTED = 0,
  69. NETWORK_EVENT_DISCONNECTED,
  70. NETWORK_EVENT_MESSAGE_RECEIVED,
  71. };
  72. struct NetworkEvent_t
  73. {
  74. NetworkEventType_t m_nType;
  75. };
  76. struct NetworkConnectionEvent_t : public NetworkEvent_t
  77. {
  78. INetChannel *m_pChannel;
  79. };
  80. struct NetworkDisconnectionEvent_t : public NetworkEvent_t
  81. {
  82. INetChannel *m_pChannel;
  83. };
  84. struct NetworkMessageReceivedEvent_t : public NetworkEvent_t
  85. {
  86. INetChannel *m_pChannel;
  87. INetworkMessage *m_pNetworkMessage;
  88. };
  89. //-----------------------------------------------------------------------------
  90. // Main interface for low-level networking (packet sending). This is a low-level interface
  91. //-----------------------------------------------------------------------------
  92. #define NETWORKSYSTEM_INTERFACE_VERSION "NetworkSystemVersion001"
  93. abstract_class INetworkSystem : public IAppSystem
  94. {
  95. public:
  96. // Installs network message factories to be used with all connections
  97. virtual bool RegisterMessage( INetworkMessage *msg ) = 0;
  98. // Start, shutdown a server
  99. virtual bool StartServer( unsigned short nServerListenPort = NETWORKSYSTEM_DEFAULT_SERVER_PORT ) = 0;
  100. virtual void ShutdownServer( ) = 0;
  101. // Process server-side network messages
  102. virtual void ServerReceiveMessages() = 0;
  103. virtual void ServerSendMessages() = 0;
  104. // Start, shutdown a client
  105. virtual bool StartClient( unsigned short nClientListenPort = NETWORKSYSTEM_DEFAULT_CLIENT_PORT ) = 0;
  106. virtual void ShutdownClient( ) = 0;
  107. // Process client-side network messages
  108. virtual void ClientSendMessages() = 0;
  109. virtual void ClientReceiveMessages() = 0;
  110. // Connect, disconnect a client to a server
  111. virtual INetChannel* ConnectClientToServer( const char *pServer, int nServerListenPort = NETWORKSYSTEM_DEFAULT_SERVER_PORT ) = 0;
  112. virtual void DisconnectClientFromServer( INetChannel* pChan ) = 0;
  113. // Event queue
  114. virtual NetworkEvent_t *FirstNetworkEvent( ) = 0;
  115. virtual NetworkEvent_t *NextNetworkEvent( ) = 0;
  116. // Returns the local host name
  117. virtual const char* GetLocalHostName( void ) const = 0;
  118. virtual const char* GetLocalAddress( void ) const = 0;
  119. /*
  120. // NOTE: Server methods
  121. // NOTE: There's only 1 client INetChannel ever
  122. // There can be 0-N server INetChannels.
  123. virtual INetChannel* CreateConnection( bool bIsClientConnection ) = 0;
  124. // Add methods for setting unreliable payloads
  125. */
  126. };
  127. #endif // INETWORKSYSTEM_H