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.

193 lines
6.1 KiB

  1. //===== Copyright � 1996-2005, 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. // This is the packet payload without any header bytes (which are attached for actual sending)
  14. #define NET_MAX_PAYLOAD ( 262144 - 4) // largest message we can send in bytes
  15. #define NET_MAX_PAYLOAD_BITS 18 // 2^NET_MAX_PAYLOAD_BITS > NET_MAX_PAYLOAD
  16. // This is just the client_t->netchan.datagram buffer size (shouldn't ever need to be huge)
  17. #define NET_MAX_DATAGRAM_PAYLOAD 4000 // = maximum unreliable playload size
  18. // UDP has 28 byte headers
  19. #define UDP_HEADER_SIZE (20+8) // IP = 20, UDP = 8
  20. #define MAX_ROUTABLE_PAYLOAD 1200 // x360 requires <= 1260, but now that listen servers can support "steam" mediated sockets, steam enforces 1200 byte limit
  21. #if (MAX_ROUTABLE_PAYLOAD & 3) != 0
  22. #error Bit buffers must be a multiple of 4 bytes
  23. #endif
  24. #define MIN_ROUTABLE_PAYLOAD 16 // minimum playload size
  25. #define NETMSG_TYPE_BITS 8 // must be 2^NETMSG_TYPE_BITS > SVC_LASTMSG
  26. // This is the payload plus any header info (excluding UDP header)
  27. #define HEADER_BYTES 9 // 2*4 bytes seqnr, 1 byte flags
  28. // Pad this to next higher 16 byte boundary
  29. // This is the largest packet that can come in/out over the wire, before processing the header
  30. // bytes will be stripped by the networking channel layer
  31. #define NET_MAX_MESSAGE PAD_NUMBER( ( NET_MAX_PAYLOAD + HEADER_BYTES ), 16 )
  32. #define NET_HEADER_FLAG_SPLITPACKET -2
  33. #define NET_HEADER_FLAG_COMPRESSEDPACKET -3
  34. //-----------------------------------------------------------------------------
  35. // Forward declarations:
  36. //-----------------------------------------------------------------------------
  37. class INetworkMessageHandler;
  38. class INetworkMessage;
  39. class INetChannel;
  40. class INetworkMessageFactory;
  41. class bf_read;
  42. class bf_write;
  43. typedef struct netadr_s netadr_t;
  44. class CNetPacket;
  45. //-----------------------------------------------------------------------------
  46. // Default ports
  47. //-----------------------------------------------------------------------------
  48. enum
  49. {
  50. NETWORKSYSTEM_DEFAULT_SERVER_PORT = 27001,
  51. NETWORKSYSTEM_DEFAULT_CLIENT_PORT = 27002
  52. };
  53. //-----------------------------------------------------------------------------
  54. // This interface encompasses a one-way communication path between two
  55. //-----------------------------------------------------------------------------
  56. typedef int ConnectionHandle_t;
  57. enum ConnectionStatus_t
  58. {
  59. CONNECTION_STATE_DISCONNECTED = 0,
  60. CONNECTION_STATE_CONNECTING,
  61. CONNECTION_STATE_CONNECTION_FAILED,
  62. CONNECTION_STATE_CONNECTED,
  63. };
  64. //-----------------------------------------------------------------------------
  65. // This interface encompasses a one-way communication path between two machines
  66. //-----------------------------------------------------------------------------
  67. //
  68. // abstract_class INetChannel
  69. // {
  70. // public:
  71. // // virtual INetworkMessageHandler *GetMsgHandler( void ) const = 0;
  72. // virtual const netadr_t &GetRemoteAddress( void ) const = 0;
  73. //
  74. // // send a net message
  75. // // NOTE: There are special connect/disconnect messages?
  76. // virtual bool AddNetMsg( INetworkMessage *msg, bool bForceReliable = false ) = 0;
  77. // // virtual bool RegisterMessage( INetworkMessage *msg ) = 0;
  78. //
  79. // virtual ConnectionStatus_t GetConnectionState( ) = 0;
  80. //
  81. // /*
  82. // virtual ConnectTo( const netadr_t& to ) = 0;
  83. // virtual Disconnect() = 0;
  84. //
  85. // virtual const netadr_t& GetLocalAddress() = 0;
  86. //
  87. // virtual const netadr_t& GetRemoteAddress() = 0;
  88. // */
  89. // };
  90. //-----------------------------------------------------------------------------
  91. // Network event types + structures
  92. //-----------------------------------------------------------------------------
  93. enum NetworkEventType_t
  94. {
  95. NETWORK_EVENT_CONNECTED = 0,
  96. NETWORK_EVENT_DISCONNECTED,
  97. NETWORK_EVENT_MESSAGE_RECEIVED,
  98. };
  99. struct NetworkEvent_t
  100. {
  101. NetworkEventType_t m_nType;
  102. };
  103. struct NetworkConnectionEvent_t : public NetworkEvent_t
  104. {
  105. INetChannel *m_pChannel;
  106. };
  107. struct NetworkDisconnectionEvent_t : public NetworkEvent_t
  108. {
  109. INetChannel *m_pChannel;
  110. };
  111. struct NetworkMessageReceivedEvent_t : public NetworkEvent_t
  112. {
  113. INetChannel *m_pChannel;
  114. INetworkMessage *m_pNetworkMessage;
  115. };
  116. //-----------------------------------------------------------------------------
  117. // Main interface for low-level networking (packet sending). This is a low-level interface
  118. //-----------------------------------------------------------------------------
  119. abstract_class INetworkSystem : public IAppSystem
  120. {
  121. public:
  122. // Installs network message factories to be used with all connections
  123. virtual bool RegisterMessage( INetworkMessage *msg ) = 0;
  124. // Start, shutdown a server
  125. virtual bool StartServer( unsigned short nServerListenPort = NETWORKSYSTEM_DEFAULT_SERVER_PORT ) = 0;
  126. virtual void ShutdownServer( ) = 0;
  127. // Process server-side network messages
  128. virtual void ServerReceiveMessages() = 0;
  129. virtual void ServerSendMessages() = 0;
  130. // Start, shutdown a client
  131. virtual bool StartClient( unsigned short nClientListenPort = NETWORKSYSTEM_DEFAULT_CLIENT_PORT ) = 0;
  132. virtual void ShutdownClient( ) = 0;
  133. // Process client-side network messages
  134. virtual void ClientSendMessages() = 0;
  135. virtual void ClientReceiveMessages() = 0;
  136. // Connect, disconnect a client to a server
  137. virtual INetChannel* ConnectClientToServer( const char *pServer, int nServerListenPort = NETWORKSYSTEM_DEFAULT_SERVER_PORT ) = 0;
  138. virtual void DisconnectClientFromServer( INetChannel* pChan ) = 0;
  139. // Event queue
  140. virtual NetworkEvent_t *FirstNetworkEvent( ) = 0;
  141. virtual NetworkEvent_t *NextNetworkEvent( ) = 0;
  142. // Returns the local host name
  143. virtual const char* GetLocalHostName( void ) const = 0;
  144. virtual const char* GetLocalAddress( void ) const = 0;
  145. /*
  146. // NOTE: Server methods
  147. // NOTE: There's only 1 client INetChannel ever
  148. // There can be 0-N server INetChannels.
  149. virtual INetChannel* CreateConnection( bool bIsClientConnection ) = 0;
  150. // Add methods for setting unreliable payloads
  151. */
  152. };
  153. #endif // INETWORKSYSTEM_H