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.

199 lines
6.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //----------------------------------------------------------------------------------------
  4. #ifndef REPLAYSERVER_H
  5. #define REPLAYSERVER_H
  6. #ifdef _WIN32
  7. #pragma once
  8. #endif
  9. #include "baseserver.h"
  10. #include "replaydemo.h"
  11. #include "clientframe.h"
  12. #include "networkstringtable.h"
  13. #include "dt_recv.h"
  14. #include "replay/ireplayserver.h"
  15. #include <convar.h>
  16. #define REPLAY_BUFFER_DIRECTOR 0 // director commands
  17. #define REPLAY_BUFFER_RELIABLE 1 // reliable messages
  18. #define REPLAY_BUFFER_UNRELIABLE 2 // unreliable messages
  19. #define REPLAY_BUFFER_VOICE 3 // player voice data
  20. #define REPLAY_BUFFER_SOUNDS 4 // unreliable sounds
  21. #define REPLAY_BUFFER_TEMPENTS 5 // temporary/event entities
  22. #define REPLAY_BUFFER_MAX 6 // end marker
  23. // proxy dispatch modes
  24. #define DISPATCH_MODE_OFF 0
  25. #define DISPATCH_MODE_AUTO 1
  26. #define DISPATCH_MODE_ALWAYS 2
  27. class CReplayFrame : public CClientFrame
  28. {
  29. public:
  30. CReplayFrame();
  31. virtual ~CReplayFrame();
  32. void Reset(); // resets all data & buffers
  33. void FreeBuffers();
  34. void AllocBuffers();
  35. bool HasData();
  36. void CopyReplayData( CReplayFrame &frame );
  37. virtual bool IsMemPoolAllocated() { return false; }
  38. public:
  39. // message buffers:
  40. bf_write m_Messages[REPLAY_BUFFER_MAX];
  41. };
  42. struct CReplayFrameCacheEntry_s
  43. {
  44. CClientFrame* pFrame;
  45. int nTick;
  46. };
  47. class CReplayDeltaEntityCache
  48. {
  49. struct DeltaEntityEntry_s
  50. {
  51. DeltaEntityEntry_s *pNext;
  52. int nDeltaTick;
  53. int nBits;
  54. };
  55. public:
  56. CReplayDeltaEntityCache();
  57. ~CReplayDeltaEntityCache();
  58. void SetTick( int nTick, int nMaxEntities );
  59. unsigned char* FindDeltaBits( int nEntityIndex, int nDeltaTick, int &nBits );
  60. void AddDeltaBits( int nEntityIndex, int nDeltaTick, int nBits, bf_write *pBuffer );
  61. void Flush();
  62. protected:
  63. int m_nTick; // current tick
  64. int m_nMaxEntities; // max entities = length of cache
  65. int m_nCacheSize;
  66. DeltaEntityEntry_s* m_Cache[MAX_EDICTS]; // array of pointers to delta entries
  67. };
  68. class CGameClient;
  69. class CGameServer;
  70. class IReplayDirector;
  71. class IServerReplayContext;
  72. class CReplayServer : public IGameEventListener2,
  73. public CBaseServer,
  74. public CClientFrameManager,
  75. public IReplayServer
  76. {
  77. typedef CBaseServer BaseClass;
  78. public:
  79. CReplayServer();
  80. virtual ~CReplayServer();
  81. public: // CBaseServer interface:
  82. virtual bool IsMultiplayer() const { return true; };
  83. virtual bool IsReplay() const { return true; };
  84. virtual void Init( bool bIsDedicated );
  85. virtual void Clear();
  86. virtual void Shutdown();
  87. virtual void FillServerInfo(SVC_ServerInfo &serverinfo);
  88. virtual void GetNetStats( float &avgIn, float &avgOut );
  89. virtual int GetChallengeType ( netadr_t &adr );
  90. virtual const char *GetName() const;
  91. virtual const char *GetPassword() const;
  92. IClient *ConnectClient ( netadr_t &adr, int protocol, int challenge, int clientChallenge, int authProtocol,
  93. const char *name, const char *password, const char *hashedCDkey, int cdKeyLen );
  94. void ReplyChallenge(netadr_t &adr, int clientChallenge );
  95. void ReplyServerChallenge(netadr_t &adr);
  96. void RejectConnection( const netadr_t &adr, int clientChallenge, const char *s );
  97. CBaseClient *CreateFakeClient(const char *name);
  98. public: // IGameEventListener2 interface:
  99. void FireGameEvent( IGameEvent *event );
  100. int m_nDebugID;
  101. int GetEventDebugID();
  102. public: // IReplayServer interface:
  103. virtual IServer *GetBaseServer();
  104. virtual IReplayDirector *GetDirector() { return NULL; }
  105. virtual int GetReplaySlot(); // return entity index-1 of Replay in game
  106. virtual float GetOnlineTime(); // seconds since broadcast started
  107. virtual void BroadcastEvent( IGameEvent *event ) { }
  108. virtual bool IsRecording() { return m_DemoRecorder.IsRecording(); }
  109. virtual void StartRecording();
  110. virtual void StopRecording();
  111. public: // CBaseServer overrides:
  112. virtual void SetMaxClients( int number );
  113. virtual void UserInfoChanged( int nClientIndex );
  114. public:
  115. void StartMaster(CGameClient *client); // start Replay server as master proxy
  116. bool SendNetMsg( INetMessage &msg, bool bForceReliable = false );
  117. void RunFrame();
  118. void Changelevel();
  119. CClientFrame *AddNewFrame( CClientFrame * pFrame ); // add new frame, returns Replay's copy
  120. void LinkInstanceBaselines();
  121. bf_write *GetBuffer( int nBuffer);
  122. CClientFrame *GetDeltaFrame( int nTick );
  123. protected:
  124. virtual bool ShouldUpdateMasterServer();
  125. private:
  126. void UpdateTick();
  127. void InstallStringTables();
  128. void RestoreTick( int tick );
  129. void EntityPVSCheck( CClientFrame *pFrame );
  130. void InitClientRecvTables();
  131. void FreeClientRecvTables();
  132. void ResyncDemoClock();
  133. public:
  134. CGameClient *m_MasterClient; // if != NULL, this is the master Replay
  135. CReplayDemoRecorder m_DemoRecorder; // Replay demo object for recording and playback
  136. CGameServer *m_Server; // pointer to source server (sv.)
  137. int m_nFirstTick; // first known server tick;
  138. int m_nLastTick; // last tick from AddFrame()
  139. CReplayFrame *m_CurrentFrame; // current delayed Replay frame
  140. int m_nViewEntity; // the current entity Replay is tracking
  141. int m_nPlayerSlot; // slot of Replay client on game server
  142. CReplayFrame m_ReplayFrame; // all incoming messages go here until Snapshot is made
  143. bool m_bSignonState; // true if connecting to server
  144. float m_flStartTime;
  145. float m_flFPS; // FPS the proxy is running;
  146. int m_nGameServerMaxClients; // max clients on game server
  147. float m_fNextSendUpdateTime; // time to send next Replay status messages
  148. RecvTable *m_pRecvTables[MAX_DATATABLES];
  149. int m_nRecvTables;
  150. Vector m_vPVSOrigin;
  151. bool m_bMasterOnlyMode;
  152. netadr_t m_RootServer; // Replay root server
  153. int m_nGlobalSlots;
  154. int m_nGlobalClients;
  155. int m_nGlobalProxies;
  156. CNetworkStringTableContainer m_NetworkStringTables;
  157. CReplayDeltaEntityCache m_DeltaCache;
  158. CUtlVector<CReplayFrameCacheEntry_s> m_FrameCache;
  159. private:
  160. void SendPendingEvents(); // Send events to clients regarding start/stop/replays available
  161. float m_flStartRecordTime;
  162. float m_flStopRecordTime;
  163. };
  164. extern CReplayServer *replay; // The global Replay server/object. NULL on xbox.
  165. #endif // REPLAYSERVER_H