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.

273 lines
7.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. // Should move to common/networksystem
  7. #ifndef NETCHANNEL_H
  8. #define NETCHANNEL_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "tier1/bitbuf.h"
  13. #include "tier1/netadr.h"
  14. #include "sm_Protocol.h"
  15. #include "tier1/utlvector.h"
  16. #include "networksystem/inetworksystem.h"
  17. #include "tier1/mempool.h"
  18. //-----------------------------------------------------------------------------
  19. // Forward declarations
  20. //-----------------------------------------------------------------------------
  21. class CUDPSocket;
  22. class CUtlBuffer;
  23. class CNetPacket;
  24. class CNetChannel;
  25. class INetChannel;
  26. // 0 == regular, 1 == file stream
  27. enum
  28. {
  29. FRAG_NORMAL_STREAM = 0,
  30. FRAG_FILE_STREAM,
  31. MAX_STREAMS
  32. };
  33. #define NET_MAX_DATAGRAM_PAYLOAD 1400
  34. #define NET_MAX_PAYLOAD_BITS 11 // 2^NET_MAX_PALYLOAD_BITS > NET_MAX_PAYLOAD
  35. #define DEFAULT_RATE 10000
  36. #define SIGNON_TIME_OUT 120.0f
  37. #define CONNECTION_PROBLEM_TIME 15.0f
  38. #define MAX_RATE 50000
  39. #define MIN_RATE 100
  40. #define FRAGMENT_BITS 8
  41. #define FRAGMENT_SIZE (1<<FRAGMENT_BITS)
  42. #define MAX_FILE_SIZE_BITS 26
  43. #define MAX_FILE_SIZE ((1<<MAX_FILE_SIZE_BITS)-1) // maximum transferable size is 64MB
  44. #define NET_MAX_PAYLOAD 4000
  45. #define NET_MAX_MESSAGE 4096
  46. #define MIN_ROUTEABLE_PACKET 16
  47. #define MAX_ROUTEABLE_PACKET 1400 // Ethernet 1518 - ( CRC + IP + UDP header)
  48. #define UDP_HEADER_SIZE 28
  49. // each channel packet has 1 byte of FLAG bits
  50. #define PACKET_FLAG_RELIABLE (1<<0) // packet contains subchannel stream data
  51. #define PACKET_FLAG_CHOKED (1<<1) // packet was choked by sender
  52. // shared commands used by all streams, handled by stream layer, TODO
  53. abstract_class INetworkMessageHandler
  54. {
  55. public:
  56. virtual void OnConnectionClosing( INetChannel *channel, char const *reason ) = 0;
  57. virtual void OnConnectionStarted( INetChannel *channel ) = 0;
  58. virtual void OnPacketStarted( int inseq, int outseq ) = 0;
  59. virtual void OnPacketFinished() = 0;
  60. protected:
  61. virtual ~INetworkMessageHandler() {}
  62. };
  63. class INetChannelHandler
  64. {
  65. public:
  66. virtual ~INetChannelHandler( void ) {};
  67. virtual void ConnectionStart(INetChannel *chan) = 0; // called first time network channel is established
  68. virtual void ConnectionClosing(const char *reason) = 0; // network channel is being closed by remote site
  69. virtual void ConnectionCrashed(const char *reason) = 0; // network error occured
  70. virtual void PacketStart(int incoming_sequence, int outgoing_acknowledged) = 0; // called each time a new packet arrived
  71. virtual void PacketEnd( void ) = 0; // all messages has been parsed
  72. virtual void FileRequested(const char *fileName, unsigned int transferID) = 0; // other side request a file for download
  73. virtual void FileReceived(const char *fileName, unsigned int transferID) = 0; // we received a file
  74. virtual void FileDenied(const char *fileName, unsigned int transferID) = 0; // a file request was denied by other side
  75. };
  76. // server to client
  77. class CNetPacket
  78. {
  79. DECLARE_FIXEDSIZE_ALLOCATOR( CNetPacket );
  80. public:
  81. CNetPacket();
  82. ~CNetPacket();
  83. void AddRef();
  84. void Release();
  85. public:
  86. netadr_t m_From; // sender IP
  87. CUDPSocket *m_pSource; // received source
  88. float m_flReceivedTime; // received time
  89. unsigned char *m_pData; // pointer to raw packet data
  90. bf_read m_Message; // easy bitbuf data access
  91. int m_nSizeInBytes; // size in bytes
  92. private:
  93. int m_nRefCount;// Reference count
  94. };
  95. abstract_class IConnectionlessPacketHandler
  96. {
  97. public:
  98. virtual bool ProcessConnectionlessPacket( CNetPacket *packet ) = 0; // process a connectionless packet
  99. protected:
  100. virtual ~IConnectionlessPacketHandler( void ) {};
  101. };
  102. abstract_class ILookupChannel
  103. {
  104. public:
  105. virtual INetChannel *FindNetChannel( const netadr_t& from ) = 0;
  106. };
  107. // FIXME: Make an INetChannel
  108. class CNetChannel : public INetChannel
  109. {
  110. public:
  111. explicit CNetChannel();
  112. ~CNetChannel();
  113. // Methods of INetChannel
  114. virtual ConnectionStatus_t GetConnectionState( );
  115. virtual const netadr_t &GetRemoteAddress( void ) const;
  116. void Setup( bool serverSide, const netadr_t *remote_address, CUDPSocket *sendSocket, char const *name, INetworkMessageHandler *handler );
  117. void Reset();
  118. void Clear();
  119. void Shutdown( const char *reason );
  120. CUDPSocket *GetSocket();
  121. void SetDataRate( float rate );
  122. void SetTimeout( float seconds );
  123. bool StartProcessingPacket( CNetPacket *packet );
  124. bool ProcessPacket( CNetPacket *packet );
  125. void EndProcessingPacket( CNetPacket *packet );
  126. bool CanSendPacket( void ) const;
  127. void SetChoked( void ); // choke a packet
  128. bool HasPendingReliableData( void );
  129. // Queues data for sending:
  130. // send a net message
  131. bool AddNetMsg( INetworkMessage *msg, bool bForceReliable = false );
  132. // send a chunk of data
  133. bool AddData( bf_write &msg, bool bReliable = true );
  134. // Puts data onto the wire:
  135. int SendDatagram( bf_write *data ); // Adds data to unreliable payload and then calls transmits the data
  136. bool Transmit( bool onlyReliable = false ); // send data from buffers (calls SendDataGram( NULL ) )
  137. bool IsOverflowed( void ) const;
  138. bool IsTimedOut( void ) const;
  139. bool IsTimingOut() const;
  140. // Info:
  141. const char *GetName( void ) const;
  142. const char *GetAddress( void ) const;
  143. float GetTimeConnected( void ) const;
  144. float GetTimeSinceLastReceived( void ) const;
  145. int GetDataRate( void ) const;
  146. float GetLatency( int flow ) const;
  147. float GetAvgLatency( int flow ) const;
  148. float GetAvgLoss( int flow ) const;
  149. float GetAvgData( int flow ) const;
  150. float GetAvgChoke( int flow ) const;
  151. float GetAvgPackets( int flow ) const;
  152. int GetTotalData( int flow ) const;
  153. void SetConnectionState( ConnectionStatus_t state );
  154. private:
  155. int ProcessPacketHeader( bf_read &buf );
  156. bool ProcessControlMessage( int cmd, bf_read &buf );
  157. bool ProcessMessages( bf_read &buf );
  158. ConnectionStatus_t m_ConnectionState;
  159. // last send outgoing sequence number
  160. int m_nOutSequenceNr;
  161. // last received incoming sequnec number
  162. int m_nInSequenceNr;
  163. // last received acknowledge outgoing sequnce number
  164. int m_nOutSequenceNrAck;
  165. // state of outgoing reliable data (0/1) flip flop used for loss detection
  166. int m_nOutReliableState;
  167. // state of incoming reliable data
  168. int m_nInReliableState;
  169. int m_nChokedPackets; //number of choked packets
  170. int m_PacketDrop;
  171. // Reliable data buffer, send wich each packet (or put in waiting list)
  172. bf_write m_StreamReliable;
  173. byte m_ReliableDataBuffer[8 * 1024]; // In SP, we don't need much reliable buffer, so save the memory (this is mostly for xbox).
  174. CUtlVector<byte> m_ReliableDataBufferMP;
  175. // unreliable message buffer, cleared wich each packet
  176. bf_write m_StreamUnreliable;
  177. byte m_UnreliableDataBuffer[NET_MAX_DATAGRAM_PAYLOAD];
  178. // don't use any vars below this (only in net_ws.cpp)
  179. CUDPSocket *m_pSocket; // NS_SERVER or NS_CLIENT index, depending on channel.
  180. int m_StreamSocket; // TCP socket handle
  181. unsigned int m_MaxReliablePayloadSize; // max size of reliable payload in a single packet
  182. // Address this channel is talking to.
  183. netadr_t remote_address;
  184. // For timeouts. Time last message was received.
  185. float last_received;
  186. // Time when channel was connected.
  187. float connect_time;
  188. // Bandwidth choke
  189. // Bytes per second
  190. int m_Rate;
  191. // If realtime > cleartime, free to send next packet
  192. float m_fClearTime;
  193. float m_Timeout; // in seconds
  194. char m_Name[32]; // channel name
  195. // packet history
  196. // netflow_t m_DataFlow[ MAX_FLOWS ];
  197. INetworkMessageHandler *m_MessageHandler; // who registers and processes messages
  198. };
  199. #endif // NETCHANNEL_H