Counter Strike : Global Offensive Source Code
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.

216 lines
7.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef REPLAYSERVER_H
  7. #define REPLAYSERVER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "baseserver.h"
  12. #include "replayclient.h"
  13. #include "replaydemo.h"
  14. #include "clientframe.h"
  15. #include "networkstringtable.h"
  16. #include "replayhistorymanager.h"
  17. #include "dt_recv.h"
  18. #include <ireplay.h>
  19. #include <convar.h>
  20. #define REPLAY_BUFFER_DIRECTOR 0 // director commands
  21. #define REPLAY_BUFFER_RELIABLE 1 // reliable messages
  22. #define REPLAY_BUFFER_UNRELIABLE 2 // unreliable messages
  23. #define REPLAY_BUFFER_VOICE 3 // player voice data
  24. #define REPLAY_BUFFER_SOUNDS 4 // unreliable sounds
  25. #define REPLAY_BUFFER_TEMPENTS 5 // temporary/event entities
  26. #define REPLAY_BUFFER_MAX 6 // end marker
  27. // proxy dispatch modes
  28. #define DISPATCH_MODE_OFF 0
  29. #define DISPATCH_MODE_AUTO 1
  30. #define DISPATCH_MODE_ALWAYS 2
  31. extern ConVar replay_debug;
  32. class CReplayFrame : public CClientFrame
  33. {
  34. public:
  35. CReplayFrame();
  36. virtual ~CReplayFrame();
  37. void Reset(); // resets all data & buffers
  38. void FreeBuffers();
  39. void AllocBuffers();
  40. bool HasData();
  41. void CopyReplayData( CReplayFrame &frame );
  42. virtual bool IsMemPoolAllocated() { return false; }
  43. public:
  44. // message buffers:
  45. bf_write m_Messages[REPLAY_BUFFER_MAX];
  46. };
  47. struct CReplayFrameCacheEntry_s
  48. {
  49. CClientFrame* pFrame;
  50. int nTick;
  51. };
  52. class CReplayDeltaEntityCache
  53. {
  54. struct DeltaEntityEntry_s
  55. {
  56. DeltaEntityEntry_s *pNext;
  57. int nDeltaTick;
  58. int nBits;
  59. };
  60. public:
  61. CReplayDeltaEntityCache();
  62. ~CReplayDeltaEntityCache();
  63. void SetTick( int nTick, int nMaxEntities );
  64. unsigned char* FindDeltaBits( int nEntityIndex, int nDeltaTick, int &nBits );
  65. void AddDeltaBits( int nEntityIndex, int nDeltaTick, int nBits, bf_write *pBuffer );
  66. void Flush();
  67. protected:
  68. int m_nTick; // current tick
  69. int m_nMaxEntities; // max entities = length of cache
  70. int m_nCacheSize;
  71. DeltaEntityEntry_s* m_Cache[MAX_EDICTS]; // array of pointers to delta entries
  72. };
  73. class CGameClient;
  74. class CGameServer;
  75. class IReplayDirector;
  76. class IReplayHistoryManager;
  77. class CReplayServer : public IGameEventListener2, public CBaseServer, public CClientFrameManager, public IReplayServer
  78. {
  79. typedef CBaseServer BaseClass;
  80. public:
  81. CReplayServer();
  82. virtual ~CReplayServer();
  83. public: // CBaseServer interface:
  84. virtual bool IsMultiplayer() const OVERRIDE { return true; };
  85. virtual bool IsReplay() const OVERRIDE { return true; };
  86. virtual void Init( bool bIsDedicated ) OVERRIDE;
  87. virtual void Clear() OVERRIDE;
  88. virtual void Shutdown() OVERRIDE;
  89. virtual void FillServerInfo(CSVCMsg_ServerInfo &serverinfo) OVERRIDE;
  90. virtual void GetNetStats( float &avgIn, float &avgOut ) OVERRIDE;
  91. virtual int GetChallengeType ( const ns_address &adr ) OVERRIDE;
  92. virtual const char *GetName() const OVERRIDE;
  93. virtual const char *GetPassword() const OVERRIDE;
  94. IClient *ConnectClient ( const ns_address &adr, int protocol, int challenge, int authProtocol,
  95. const char *name, const char *password, const char *hashedCDkey, int cdKeyLen,
  96. CUtlVector< CCLCMsg_SplitPlayerConnect_t * > & splitScreenClients, bool isClientLowViolence, CrossPlayPlatform_t clientPlatform,
  97. const byte *pbEncryptionKey, int nEncryptionKeyIndex ) OVERRIDE;
  98. public: // IGameEventListener2 interface:
  99. void FireGameEvent( IGameEvent *event );
  100. int m_nDebugID;
  101. int GetEventDebugID( void );
  102. public: // IReplayServer interface:
  103. virtual IServer *GetBaseServer();
  104. virtual IReplayDirector *GetDirector();
  105. virtual int GetReplaySlot(); // return entity index-1 of Replay in game
  106. virtual float GetOnlineTime(); // seconds since broadcast started
  107. // virtual void GetLocalStats( int &proxies, int &slots, int &clients );
  108. virtual void BroadcastEvent( IGameEvent *event );
  109. public: // CBaseServer overrides:
  110. virtual void SetMaxClients( int number );
  111. virtual void UserInfoChanged( int nClientIndex );
  112. virtual void SendClientMessages ( bool bSendSnapshots );
  113. public:
  114. void StartMaster(CGameClient *client); // start Replay server as master proxy
  115. void StartDemo(const char *filename); // starts playing back a demo file
  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. void BroadcastEventLocal( IGameEvent *event, bool bReliable ); // broadcast event but not to relay proxies
  122. void BroadcastLocalChat( const char *pszChat, const char *pszGroup ); // broadcast event but not to relay proxies
  123. void BroadcastLocalTitle( CReplayClient *client = NULL ); // NULL = broadcast to all
  124. bool DispatchToRelay( CReplayClient *pClient);
  125. bf_write *GetBuffer( int nBuffer);
  126. CClientFrame *GetDeltaFrame( int nTick );
  127. inline CReplayClient* Client( int i ) { return static_cast<CReplayClient*>(m_Clients[i]); }
  128. protected:
  129. virtual bool ShouldUpdateMasterServer();
  130. private:
  131. CBaseClient *CreateNewClient( int slot );
  132. void UpdateTick();
  133. void InstallStringTables();
  134. void RestoreTick( int tick );
  135. void EntityPVSCheck( CClientFrame *pFrame );
  136. void InitClientRecvTables();
  137. void FreeClientRecvTables();
  138. void ResyncDemoClock();
  139. public:
  140. CGameClient *m_MasterClient; // if != NULL, this is the master Replay
  141. CReplayDemoRecorder m_DemoRecorder; // Replay demo object for recording and playback
  142. CGameServer *m_Server; // pointer to source server (sv.)
  143. IReplayDirector *m_Director; // Replay director exported by game.dll
  144. int m_nFirstTick; // first known server tick;
  145. int m_nLastTick; // last tick from AddFrame()
  146. CReplayFrame *m_CurrentFrame; // current delayed Replay frame
  147. int m_nViewEntity; // the current entity Replay is tracking
  148. int m_nPlayerSlot; // slot of Replay client on game server
  149. CReplayFrame m_ReplayFrame; // all incoming messages go here until Snapshot is made
  150. bool m_bSignonState; // true if connecting to server
  151. float m_flStartTime;
  152. float m_flFPS; // FPS the proxy is running;
  153. int m_nGameServerMaxClients; // max clients on game server
  154. float m_fNextSendUpdateTime; // time to send next Replay status messages
  155. RecvTable *m_pRecvTables[MAX_DATATABLES];
  156. int m_nRecvTables;
  157. Vector m_vPVSOrigin;
  158. bool m_bMasterOnlyMode;
  159. netadr_t m_RootServer; // Replay root server
  160. int m_nGlobalSlots;
  161. int m_nGlobalClients;
  162. int m_nGlobalProxies;
  163. CNetworkStringTableContainer m_NetworkStringTables;
  164. CReplayDeltaEntityCache m_DeltaCache;
  165. CUtlVector<CReplayFrameCacheEntry_s> m_FrameCache;
  166. // demoplayer stuff:
  167. CDemoFile m_DemoFile; // for demo playback
  168. int m_nStartTick;
  169. democmdinfo_t m_LastCmdInfo;
  170. bool m_bPlayingBack;
  171. bool m_bPlaybackPaused; // true if demo is paused right now
  172. float m_flPlaybackRateModifier;
  173. int m_nSkipToTick; // skip to tick ASAP, -1 = off
  174. // TODO: Index by SteamID
  175. CBitVec< 64 > m_vecClientsDownloading; // Indexed by client slot - each bit represents whether the given client is currently downloading
  176. friend class CReplayClientState;
  177. };
  178. extern CReplayServer *replay; // The global Replay server/object. NULL on xbox.
  179. #endif // REPLAYSERVER_H