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.

256 lines
8.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef BASECLIENTSTATE_H
  7. #define BASECLIENTSTATE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include <inetmsghandler.h>
  12. #include <protocol.h>
  13. #include <client_class.h>
  14. #include <cdll_int.h>
  15. #include <netadr.h>
  16. #include "common.h"
  17. #include "clockdriftmgr.h"
  18. #include "convar.h"
  19. #include "cl_bounded_cvars.h"
  20. // Only send this many requests before timing out.
  21. #define CL_CONNECTION_RETRIES 4
  22. // Mininum time gap (in seconds) before a subsequent connection request is sent.
  23. #define CL_MIN_RESEND_TIME 1.5f
  24. // Max time. The cvar cl_resend is bounded by these.
  25. #define CL_MAX_RESEND_TIME 20.0f
  26. // In release, send commands at least this many times per second
  27. #define MIN_CMD_RATE 10.0f
  28. #define MAX_CMD_RATE 100.0f
  29. extern ConVar cl_name;
  30. // This represents a server's
  31. class C_ServerClassInfo
  32. {
  33. public:
  34. C_ServerClassInfo();
  35. ~C_ServerClassInfo();
  36. public:
  37. ClientClass *m_pClientClass;
  38. char *m_ClassName;
  39. char *m_DatatableName;
  40. // This is an index into the network string table (cl.GetInstanceBaselineTable()).
  41. int m_InstanceBaselineIndex; // INVALID_STRING_INDEX if not initialized yet.
  42. };
  43. #define EndGameAssertMsg( assertion, msg ) \
  44. if ( !(assertion) )\
  45. Host_EndGame msg
  46. class CNetworkStringTableContainer;
  47. class PackedEntity;
  48. class INetworkStringTable;
  49. class CEntityReadInfo;
  50. abstract_class CBaseClientState : public INetChannelHandler, public IConnectionlessPacketHandler, public IServerMessageHandler
  51. {
  52. public:
  53. CBaseClientState();
  54. virtual ~CBaseClientState();
  55. public: // IConnectionlessPacketHandler interface:
  56. virtual bool ProcessConnectionlessPacket(struct netpacket_s *packet);
  57. public: // INetMsgHandler interface:
  58. virtual void ConnectionStart(INetChannel *chan);
  59. virtual void ConnectionClosing( const char *reason );
  60. virtual void ConnectionCrashed(const char *reason);
  61. virtual void PacketStart(int incoming_sequence, int outgoing_acknowledged) {};
  62. virtual void PacketEnd( void ) {};
  63. virtual void FileReceived( const char *fileName, unsigned int transferID );
  64. virtual void FileRequested( const char *fileName, unsigned int transferID );
  65. virtual void FileDenied( const char *fileName, unsigned int transferID );
  66. virtual void FileSent( const char *fileName, unsigned int transferID );
  67. public: // IServerMessageHandlers
  68. PROCESS_NET_MESSAGE( Tick );
  69. PROCESS_NET_MESSAGE( StringCmd );
  70. PROCESS_NET_MESSAGE( SetConVar );
  71. PROCESS_NET_MESSAGE( SignonState );
  72. PROCESS_SVC_MESSAGE( Print );
  73. PROCESS_SVC_MESSAGE( ServerInfo );
  74. PROCESS_SVC_MESSAGE( SendTable );
  75. PROCESS_SVC_MESSAGE( ClassInfo );
  76. PROCESS_SVC_MESSAGE( SetPause );
  77. PROCESS_SVC_MESSAGE( CreateStringTable );
  78. PROCESS_SVC_MESSAGE( UpdateStringTable );
  79. PROCESS_SVC_MESSAGE( SetView );
  80. PROCESS_SVC_MESSAGE( PacketEntities );
  81. PROCESS_SVC_MESSAGE( Menu );
  82. PROCESS_SVC_MESSAGE( GameEventList );
  83. PROCESS_SVC_MESSAGE( GetCvarValue );
  84. PROCESS_SVC_MESSAGE( CmdKeyValues );
  85. PROCESS_SVC_MESSAGE( SetPauseTimed );
  86. // Returns dem file protocol version, or, if not playing a demo, just returns PROTOCOL_VERSION
  87. virtual int GetDemoProtocolVersion() const;
  88. public:
  89. inline bool IsActive( void ) const { return m_nSignonState == SIGNONSTATE_FULL; };
  90. inline bool IsConnected( void ) const { return m_nSignonState >= SIGNONSTATE_CONNECTED; };
  91. virtual void Clear( void );
  92. virtual void FullConnect( netadr_t &adr ); // a connection was established
  93. virtual void Connect(const char* adr, const char *pszSourceTag); // start a connection challenge
  94. virtual bool SetSignonState ( int state, int count );
  95. virtual void Disconnect( const char *pszReason, bool bShowMainMenu );
  96. virtual void SendConnectPacket (int challengeNr, int authProtocol, uint64 unGSSteamID, bool bGSSecure );
  97. virtual const char *GetCDKeyHash() { return "123"; }
  98. virtual void RunFrame ( void );
  99. virtual void CheckForResend ( void );
  100. virtual void InstallStringTableCallback( char const *tableName ) { }
  101. virtual bool HookClientStringTable( char const *tableName ) { return false; }
  102. virtual bool LinkClasses( void );
  103. virtual int GetConnectionRetryNumber() const { return CL_CONNECTION_RETRIES; }
  104. virtual const char *GetClientName() { return cl_name.GetString(); }
  105. static ClientClass* FindClientClass(const char *pClassName);
  106. CClockDriftMgr& GetClockDriftMgr();
  107. int GetClientTickCount() const; // Get the client tick count.
  108. void SetClientTickCount( int tick );
  109. int GetServerTickCount() const;
  110. void SetServerTickCount( int tick );
  111. void SetClientAndServerTickCount( int tick );
  112. INetworkStringTable *GetStringTable( const char * name ) const;
  113. PackedEntity *GetEntityBaseline( int iBaseline, int nEntityIndex );
  114. void SetEntityBaseline(int iBaseline, ClientClass *pClientClass, int index, char *packedData, int length);
  115. void CopyEntityBaseline( int iFrom, int iTo );
  116. void FreeEntityBaselines();
  117. bool GetClassBaseline( int iClass, void const **pData, int *pDatalen );
  118. ClientClass *GetClientClass( int i );
  119. void ForceFullUpdate( void );
  120. void SendStringCmd(const char * command);
  121. void ReadPacketEntities( CEntityReadInfo &u );
  122. virtual void ReadEnterPVS( CEntityReadInfo &u ) = 0;
  123. virtual void ReadLeavePVS( CEntityReadInfo &u ) = 0;
  124. virtual void ReadDeltaEnt( CEntityReadInfo &u ) = 0;
  125. virtual void ReadPreserveEnt( CEntityReadInfo &u ) = 0;
  126. virtual void ReadDeletions( CEntityReadInfo &u ) = 0;
  127. bool IsClientConnectionViaMatchMaking( void );
  128. static bool ConnectMethodAllowsRedirects( void );
  129. private:
  130. bool PrepareSteamConnectResponse( uint64 unGSSteamID, bool bGSSecure, const netadr_t &adr, bf_write &msg );
  131. public:
  132. // Connection to server.
  133. int m_Socket; // network socket
  134. INetChannel *m_NetChannel; // Our sequenced channel to the remote server.
  135. unsigned int m_nChallengeNr; // connection challenge number
  136. double m_flConnectTime; // If gap of connect_time to net_time > 3000, then resend connect packet
  137. int m_nRetryNumber; // number of retry connection attemps
  138. char m_szRetryAddress[ MAX_OSPATH ];
  139. CUtlString m_sRetrySourceTag; // string that describes why we decided to connect to this server (empty for command line, "serverbrowser", "quickplay", etc)
  140. int m_retryChallenge; // challenge we sent to the server
  141. int m_nSignonState; // see SIGNONSTATE_* definitions
  142. double m_flNextCmdTime; // When can we send the next command packet?
  143. int m_nServerCount; // server identification for prespawns, must match the svs.spawncount which
  144. // is incremented on server spawning. This supercedes svs.spawn_issued, in that
  145. // we can now spend a fair amount of time sitting connected to the server
  146. // but downloading models, sounds, etc. So much time that it is possible that the
  147. // server might change levels again and, if so, we need to know that.
  148. uint64 m_ulGameServerSteamID; // Steam ID of the game server we are trying to connect to, or are connected to. Zero if unknown
  149. int m_nCurrentSequence; // this is the sequence number of the current incoming packet
  150. CClockDriftMgr m_ClockDriftMgr;
  151. int m_nDeltaTick; // last valid received snapshot (server) tick
  152. bool m_bPaused; // send over by server
  153. float m_flPausedExpireTime;
  154. int m_nViewEntity; // cl_entitites[cl.viewentity] == player point of view
  155. int m_nPlayerSlot; // own player entity index-1. skips world. Add 1 to get cl_entitites index;
  156. char m_szLevelFileName[ 128 ]; // for display on solo scoreboard
  157. char m_szLevelBaseName[ 128 ]; // removes maps/ and .bsp extension
  158. int m_nMaxClients; // max clients on server
  159. PackedEntity *m_pEntityBaselines[2][MAX_EDICTS]; // storing entity baselines
  160. // This stuff manages the receiving of data tables and instantiating of client versions
  161. // of server-side classes.
  162. C_ServerClassInfo *m_pServerClasses;
  163. int m_nServerClasses;
  164. int m_nServerClassBits;
  165. char m_szEncrytionKey[STEAM_KEYSIZE];
  166. unsigned int m_iEncryptionKeySize;
  167. CNetworkStringTableContainer *m_StringTableContainer;
  168. bool m_bRestrictServerCommands; // If true, then the server is only allowed to execute commands marked with FCVAR_SERVER_CAN_EXECUTE on the client.
  169. bool m_bRestrictClientCommands; // If true, then IVEngineClient::ClientCmd is only allowed to execute commands marked with FCVAR_CLIENTCMD_CAN_EXECUTE on the client.
  170. };
  171. inline CClockDriftMgr& CBaseClientState::GetClockDriftMgr()
  172. {
  173. return m_ClockDriftMgr;
  174. }
  175. inline void CBaseClientState::SetClientTickCount( int tick )
  176. {
  177. m_ClockDriftMgr.m_nClientTick = tick;
  178. }
  179. inline int CBaseClientState::GetClientTickCount() const
  180. {
  181. return m_ClockDriftMgr.m_nClientTick;
  182. }
  183. inline int CBaseClientState::GetServerTickCount() const
  184. {
  185. return m_ClockDriftMgr.m_nServerTick;
  186. }
  187. inline void CBaseClientState::SetServerTickCount( int tick )
  188. {
  189. m_ClockDriftMgr.m_nServerTick = tick;
  190. }
  191. inline void CBaseClientState::SetClientAndServerTickCount( int tick )
  192. {
  193. m_ClockDriftMgr.m_nServerTick = m_ClockDriftMgr.m_nClientTick = tick;
  194. }
  195. #endif // BASECLIENTSTATE_H