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.

262 lines
8.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef HLTVSERVER_H
  7. #define HLTVSERVER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "baseserver.h"
  12. #include "hltvclient.h"
  13. #include "hltvdemo.h"
  14. #include "hltvclientstate.h"
  15. #include "clientframe.h"
  16. #include "networkstringtable.h"
  17. #include <ihltv.h>
  18. #include <convar.h>
  19. #define HLTV_BUFFER_DIRECTOR 0 // director commands
  20. #define HLTV_BUFFER_RELIABLE 1 // reliable messages
  21. #define HLTV_BUFFER_UNRELIABLE 2 // unreliable messages
  22. #define HLTV_BUFFER_VOICE 3 // player voice data
  23. #define HLTV_BUFFER_SOUNDS 4 // unreliable sounds
  24. #define HLTV_BUFFER_TEMPENTS 5 // temporary/event entities
  25. #define HLTV_BUFFER_MAX 6 // end marker
  26. // proxy dispatch modes
  27. #define DISPATCH_MODE_OFF 0
  28. #define DISPATCH_MODE_AUTO 1
  29. #define DISPATCH_MODE_ALWAYS 2
  30. extern ConVar tv_debug;
  31. class CHLTVFrame : public CClientFrame
  32. {
  33. public:
  34. CHLTVFrame();
  35. virtual ~CHLTVFrame();
  36. void Reset(); // resets all data & buffers
  37. void FreeBuffers();
  38. void AllocBuffers();
  39. bool HasData();
  40. void CopyHLTVData( CHLTVFrame &frame );
  41. virtual bool IsMemPoolAllocated() { return false; }
  42. public:
  43. // message buffers:
  44. bf_write m_Messages[HLTV_BUFFER_MAX];
  45. };
  46. struct CFrameCacheEntry_s
  47. {
  48. CClientFrame* pFrame;
  49. int nTick;
  50. };
  51. class CDeltaEntityCache
  52. {
  53. struct DeltaEntityEntry_s
  54. {
  55. DeltaEntityEntry_s *pNext;
  56. int nDeltaTick;
  57. int nBits;
  58. };
  59. public:
  60. CDeltaEntityCache();
  61. ~CDeltaEntityCache();
  62. void SetTick( int nTick, int nMaxEntities );
  63. unsigned char* FindDeltaBits( int nEntityIndex, int nDeltaTick, int &nBits );
  64. void AddDeltaBits( int nEntityIndex, int nDeltaTick, int nBits, bf_write *pBuffer );
  65. void Flush();
  66. protected:
  67. int m_nTick; // current tick
  68. int m_nMaxEntities; // max entities = length of cache
  69. int m_nCacheSize;
  70. DeltaEntityEntry_s* m_Cache[MAX_EDICTS]; // array of pointers to delta entries
  71. };
  72. class CGameClient;
  73. class CGameServer;
  74. class IHLTVDirector;
  75. class CHLTVServer : public IGameEventListener2, public CBaseServer, public CClientFrameManager, public IHLTVServer, public IDemoPlayer
  76. {
  77. friend class CHLTVClientState;
  78. public:
  79. CHLTVServer();
  80. virtual ~CHLTVServer();
  81. public: // CBaseServer interface:
  82. bool ProcessConnectionlessPacket( netpacket_t * packet );
  83. void Init (bool bIsDedicated);
  84. void Shutdown( void );
  85. void Clear( void );
  86. bool IsHLTV( void ) const { return true; };
  87. bool IsMultiplayer( void ) const { return true; };
  88. void FillServerInfo(SVC_ServerInfo &serverinfo);
  89. void GetNetStats( float &avgIn, float &avgOut );
  90. int GetChallengeType ( netadr_t &adr );
  91. const char *GetName( void ) const;
  92. const char *GetPassword() const;
  93. IClient *ConnectClient ( netadr_t &adr, int protocol, int challenge, int clientChallenge, int authProtocol,
  94. const char *name, const char *password, const char *hashedCDkey, int cdKeyLen );
  95. public:
  96. void FireGameEvent(IGameEvent *event);
  97. public: // IHLTVServer interface:
  98. IServer *GetBaseServer( void );
  99. IHLTVDirector *GetDirector( void );
  100. int GetHLTVSlot( void ); // return entity index-1 of HLTV in game
  101. float GetOnlineTime( void ); // seconds since broadcast started
  102. void GetLocalStats( int &proxies, int &slots, int &clients );
  103. void GetGlobalStats( int &proxies, int &slots, int &clients );
  104. void GetRelayStats( int &proxies, int &slots, int &clients );
  105. bool IsMasterProxy( void ); // true, if this is the HLTV master proxy
  106. bool IsTVRelay(); // true if we're running a relay (i.e. this is the opposite of IsMasterProxy()).
  107. bool IsDemoPlayback( void ); // true if this is a HLTV demo
  108. const netadr_t *GetRelayAddress( void ); // returns relay address
  109. void BroadcastEvent(IGameEvent *event);
  110. public: // IDemoPlayer interface
  111. CDemoFile *GetDemoFile();
  112. int GetPlaybackStartTick( void );
  113. int GetPlaybackTick( void );
  114. int GetTotalTicks( void );
  115. bool StartPlayback( const char *filename, bool bAsTimeDemo );
  116. bool IsPlayingBack( void ); // true if demo loaded and playing back
  117. bool IsPlaybackPaused( void ); // true if playback paused
  118. bool IsPlayingTimeDemo( void ) { return false; } // true if playing back in timedemo mode
  119. bool IsSkipping( void ) { return false; }; // true, if demo player skiiping trough packets
  120. bool CanSkipBackwards( void ) { return true; } // true if demoplayer can skip backwards
  121. void SetPlaybackTimeScale( float timescale ); // sets playback timescale
  122. float GetPlaybackTimeScale( void ); // get playback timescale
  123. void PausePlayback( float seconds ) {}
  124. void SkipToTick( int tick, bool bRelative, bool bPause ) {}
  125. void SetEndTick( int tick) {}
  126. void ResumePlayback( void ) {}
  127. void StopPlayback( void ) {}
  128. void InterpolateViewpoint() {}
  129. netpacket_t *ReadPacket( void ) { return NULL; }
  130. void ResetDemoInterpolation( void ) {}
  131. int GetProtocolVersion();
  132. bool ShouldLoopDemos() { return true; }
  133. void OnLastDemoInLoopPlayed() {}
  134. bool IsLoading( void ) { return false; } // true if demo is currently loading
  135. public:
  136. void StartMaster(CGameClient *client); // start HLTV server as master proxy
  137. void ConnectRelay(const char *address); // connect to other HLTV proxy
  138. void StartDemo(const char *filename); // starts playing back a demo file
  139. void StartRelay( void ); // start HLTV server as relay proxy
  140. bool SendNetMsg( INetMessage &msg, bool bForceReliable = false );
  141. void RunFrame();
  142. void SetMaxClients( int number );
  143. void Changelevel( void );
  144. void UserInfoChanged( int nClientIndex );
  145. void SendClientMessages ( bool bSendSnapshots );
  146. CClientFrame *AddNewFrame( CClientFrame * pFrame ); // add new frame, returns HLTV's copy
  147. void SignonComplete( void );
  148. void LinkInstanceBaselines( void );
  149. void BroadcastEventLocal( IGameEvent *event, bool bReliable ); // broadcast event but not to relay proxies
  150. void BroadcastLocalChat( const char *pszChat, const char *pszGroup ); // broadcast event but not to relay proxies
  151. void BroadcastLocalTitle( CHLTVClient *client = NULL ); // NULL = broadcast to all
  152. bool DispatchToRelay( CHLTVClient *pClient);
  153. bf_write *GetBuffer( int nBuffer);
  154. CClientFrame *GetDeltaFrame( int nTick );
  155. inline CHLTVClient* Client( int i ) { return static_cast<CHLTVClient*>(m_Clients[i]); }
  156. protected:
  157. virtual bool ShouldUpdateMasterServer();
  158. private:
  159. CBaseClient *CreateNewClient( int slot );
  160. void UpdateTick( void );
  161. void UpdateStats( void );
  162. void InstallStringTables( void );
  163. void RestoreTick( int tick );
  164. void EntityPVSCheck( CClientFrame *pFrame );
  165. void InitClientRecvTables();
  166. void FreeClientRecvTables();
  167. void ReadCompleteDemoFile();
  168. void ResyncDemoClock();
  169. #ifndef NO_STEAM
  170. void ReplyInfo( const netadr_t &adr );
  171. #endif
  172. // Vector GetOriginFromPackedEntity(PackedEntity* pe);
  173. public:
  174. CGameClient *m_MasterClient; // if != NULL, this is the master HLTV
  175. CHLTVClientState m_ClientState;
  176. CHLTVDemoRecorder m_DemoRecorder; // HLTV demo object for recording and playback
  177. CGameServer *m_Server; // pointer to source server (sv.)
  178. IHLTVDirector *m_Director; // HTLV director exported by game.dll
  179. int m_nFirstTick; // first known server tick;
  180. int m_nLastTick; // last tick from AddFrame()
  181. CHLTVFrame *m_CurrentFrame; // current delayed HLTV frame
  182. int m_nViewEntity; // the current entity HLTV is tracking
  183. int m_nPlayerSlot; // slot of HLTV client on game server
  184. CHLTVFrame m_HLTVFrame; // all incoming messages go here until Snapshot is made
  185. bool m_bSignonState; // true if connecting to server
  186. float m_flStartTime;
  187. float m_flFPS; // FPS the proxy is running;
  188. int m_nGameServerMaxClients; // max clients on game server
  189. float m_fNextSendUpdateTime; // time to send next HLTV status messages
  190. RecvTable *m_pRecvTables[MAX_DATATABLES];
  191. int m_nRecvTables;
  192. Vector m_vPVSOrigin;
  193. bool m_bMasterOnlyMode;
  194. netadr_t m_RootServer; // HLTV root server
  195. int m_nGlobalSlots;
  196. int m_nGlobalClients;
  197. int m_nGlobalProxies;
  198. CNetworkStringTableContainer m_NetworkStringTables;
  199. CDeltaEntityCache m_DeltaCache;
  200. CUtlVector<CFrameCacheEntry_s> m_FrameCache;
  201. // demoplayer stuff:
  202. CDemoFile m_DemoFile; // for demo playback
  203. int m_nStartTick;
  204. democmdinfo_t m_LastCmdInfo;
  205. bool m_bPlayingBack;
  206. bool m_bPlaybackPaused; // true if demo is paused right now
  207. float m_flPlaybackRateModifier;
  208. int m_nSkipToTick; // skip to tick ASAP, -1 = off
  209. };
  210. extern CHLTVServer *hltv; // The global HLTV server/object. NULL on xbox.
  211. #endif // HLTVSERVER_H