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.

192 lines
6.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // net.h -- Half-Life's interface to the networking layer
  9. // For banning IP addresses (or allowing private games)
  10. #ifndef NET_H
  11. #define NET_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include "common.h"
  16. #include "bitbuf.h"
  17. #include "netadr.h"
  18. #include "proto_version.h"
  19. // Flow control bytes per second limits
  20. #define MAX_RATE (1024*1024)
  21. #define MIN_RATE 1000
  22. #define DEFAULT_RATE 80000
  23. #define SIGNON_TIME_OUT 300.0f // signon disconnect timeout
  24. #define FRAGMENT_BITS 8
  25. #define FRAGMENT_SIZE (1<<FRAGMENT_BITS)
  26. #define MAX_FILE_SIZE_BITS 26
  27. #define MAX_FILE_SIZE ((1<<MAX_FILE_SIZE_BITS)-1) // maximum transferable size is 64MB
  28. // 0 == regular, 1 == file stream
  29. #define MAX_STREAMS 2
  30. #define FRAG_NORMAL_STREAM 0
  31. #define FRAG_FILE_STREAM 1
  32. #define TCP_CONNECT_TIMEOUT 4.0f
  33. #define PORT_ANY -1
  34. #define PORT_TRY_MAX 32
  35. #define TCP_MAX_ACCEPTS 8
  36. #define LOOPBACK_SOCKETS 2
  37. #define STREAM_CMD_NONE 0 // waiting for next blob
  38. #define STREAM_CMD_AUTH 1 // first command, send back challengenr
  39. #define STREAM_CMD_DATA 2 // receiving a data blob
  40. #define STREAM_CMD_FILE 3 // receiving a file blob
  41. #define STREAM_CMD_ACKN 4 // acknowledged a recveived blob
  42. // NETWORKING INFO
  43. // This is the packet payload without any header bytes (which are attached for actual sending)
  44. #define NET_MAX_PAYLOAD 288000 // largest message we can send in bytes
  45. #define NET_MAX_PAYLOAD_V23 96000 // largest message we can send in bytes
  46. #define NET_MAX_PAYLOAD_BITS_V23 17 // 2^NET_MAX_PAYLOAD_BITS > NET_MAX_PAYLOAD
  47. // This is just the client_t->netchan.datagram buffer size (shouldn't ever need to be huge)
  48. #define NET_MAX_DATAGRAM_PAYLOAD 4000 // = maximum unreliable payload size
  49. // UDP has 28 byte headers
  50. #define UDP_HEADER_SIZE (20+8) // IP = 20, UDP = 8
  51. #define MAX_ROUTABLE_PAYLOAD 1260 // Matches x360 size
  52. #if (MAX_ROUTABLE_PAYLOAD & 3) != 0
  53. #error Bit buffers must be a multiple of 4 bytes
  54. #endif
  55. #define MIN_ROUTABLE_PAYLOAD 16 // minimum playload size
  56. #define NETMSG_TYPE_BITS 6 // must be 2^NETMSG_TYPE_BITS > SVC_LASTMSG
  57. #define NETMSG_LENGTH_BITS 11 // 256 bytes
  58. // This is the payload plus any header info (excluding UDP header)
  59. #define HEADER_BYTES 9 // 2*4 bytes seqnr, 1 byte flags
  60. // Pad this to next higher 16 byte boundary
  61. // This is the largest packet that can come in/out over the wire, before processing the header
  62. // bytes will be stripped by the networking channel layer
  63. #define NET_MAX_MESSAGE PAD_NUMBER( ( NET_MAX_PAYLOAD + HEADER_BYTES ), 16 )
  64. // Even connectionless packets require int32 value (-1) + 1 byte content
  65. #define NET_MIN_MESSAGE 5
  66. #define NET_HEADER_FLAG_SPLITPACKET -2
  67. #define NET_HEADER_FLAG_COMPRESSEDPACKET -3
  68. class INetChannel;
  69. enum
  70. {
  71. NS_CLIENT = 0, // client socket
  72. NS_SERVER, // server socket
  73. NS_HLTV,
  74. NS_MATCHMAKING,
  75. NS_SYSTEMLINK,
  76. #ifdef LINUX
  77. NS_SVLAN, // LAN udp port for Linux. See NET_OpenSockets for info.
  78. #endif
  79. MAX_SOCKETS
  80. };
  81. typedef struct netpacket_s
  82. {
  83. netadr_t from; // sender IP
  84. int source; // received source
  85. double received; // received time
  86. unsigned char *data; // pointer to raw packet data
  87. bf_read message; // easy bitbuf data access
  88. int size; // size in bytes
  89. int wiresize; // size in bytes before decompression
  90. bool stream; // was send as stream
  91. struct netpacket_s *pNext; // for internal use, should be NULL in public
  92. } netpacket_t;
  93. extern netadr_t net_local_adr;
  94. extern double net_time;
  95. class INetChannelHandler;
  96. class IConnectionlessPacketHandler;
  97. // Start up networking
  98. void NET_Init( bool bDedicated );
  99. // Shut down networking
  100. void NET_Shutdown (void);
  101. // Read any incoming packets, dispatch to known netchannels and call handler for connectionless packets
  102. void NET_ProcessSocket( int sock, IConnectionlessPacketHandler * handler );
  103. // Set a port to listen mode
  104. void NET_ListenSocket( int sock, bool listen );
  105. // Send connectionsless string over the wire
  106. void NET_OutOfBandPrintf(int sock, const netadr_t &adr, PRINTF_FORMAT_STRING const char *format, ...) FMTFUNCTION( 3, 4 );
  107. // Send a raw packet, connectionless must be provided (chan can be NULL)
  108. int NET_SendPacket ( INetChannel *chan, int sock, const netadr_t &to, const unsigned char *data, int length, bf_write *pVoicePayload = NULL, bool bUseCompression = false );
  109. // Called periodically to maybe send any queued packets (up to 4 per frame)
  110. void NET_SendQueuedPackets();
  111. // Start set current network configuration
  112. void NET_SetMutiplayer(bool multiplayer);
  113. // Set net_time
  114. void NET_SetTime( double realtime );
  115. // RunFrame must be called each system frame before reading/sending on any socket
  116. void NET_RunFrame( double realtime );
  117. // Check configuration state
  118. bool NET_IsMultiplayer( void );
  119. bool NET_IsDedicated( void );
  120. // Writes a error file with bad packet content
  121. void NET_LogBadPacket(netpacket_t * packet);
  122. // bForceNew (used for bots) tells it not to share INetChannels (bots will crash when disconnecting if they
  123. // share an INetChannel).
  124. INetChannel *NET_CreateNetChannel(int socketnumber, netadr_t *adr, const char * name, INetChannelHandler * handler, bool bForceNew=false,
  125. int nProtocolVersion=PROTOCOL_VERSION );
  126. void NET_RemoveNetChannel(INetChannel *netchan, bool bDeleteNetChan);
  127. void NET_PrintChannelStatus( INetChannel * chan );
  128. void NET_WriteStringCmd( const char * cmd, bf_write *buf );
  129. // Address conversion
  130. bool NET_StringToAdr ( const char *s, netadr_t *a);
  131. // Convert from host to network byte ordering
  132. unsigned short NET_HostToNetShort( unsigned short us_in );
  133. // and vice versa
  134. unsigned short NET_NetToHostShort( unsigned short us_in );
  135. // Find out what port is mapped to a local socket
  136. unsigned short NET_GetUDPPort(int socket);
  137. // add/remove extra sockets for testing
  138. int NET_AddExtraSocket( int port );
  139. void NET_RemoveAllExtraSockets();
  140. const char *NET_ErrorString (int code); // translate a socket error into a friendly string
  141. //============================================================================
  142. // Message data
  143. typedef struct
  144. {
  145. // Size of message sent/received
  146. int size;
  147. // Time that message was sent/received
  148. float time;
  149. } flowstats_t;
  150. // Some hackery to avoid using va() in constructor since we cache off the pointer to the string in the ConVar!!!
  151. #define NET_STRINGIZE( x ) #x
  152. #define NET_MAKESTRING( macro, val ) macro(val)
  153. #define NETSTRING( val ) NET_MAKESTRING( NET_STRINGIZE, val )
  154. #endif // !NET_H