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.

390 lines
9.7 KiB

  1. //===== Copyright � 1996-2009, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #ifndef SYS_SESSION_H
  7. #define SYS_SESSION_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. class CSysSessionBase;
  12. class CSysSessionHost;
  13. class CSysSessionClient;
  14. #include "x360_lobbyapi.h"
  15. #include "x360_netmgr.h"
  16. class CSysSessionBase
  17. #ifdef _X360
  18. : public IX360NetworkEvents
  19. #endif
  20. {
  21. friend class CSysSessionHost;
  22. friend class CSysSessionClient;
  23. protected:
  24. CSysSessionBase( KeyValues *pSettings );
  25. virtual ~CSysSessionBase();
  26. public:
  27. virtual bool Update();
  28. virtual void Destroy();
  29. virtual void Command( KeyValues *pCommand );
  30. uint64 GetReservationCookie();
  31. uint64 GetNonceCookie();
  32. uint64 GetSessionID();
  33. virtual XUID GetHostXuid( XUID xuidValidResult = 0ull ) = 0;
  34. void SetSessionActiveGameplayState( bool bActive, char const *szSecureServerAddress );
  35. void UpdateTeamProperties( KeyValues *pTeamProperties );
  36. void UpdateServerInfo( KeyValues *pServerKey );
  37. public:
  38. void ReplyLanSearch( KeyValues *msg );
  39. public:
  40. virtual void DebugPrint();
  41. void SendMessage( KeyValues *msg );
  42. protected:
  43. virtual void ReceiveMessage( KeyValues *msg, bool bValidatedLobbyMember, XUID xuidSrc );
  44. virtual void OnPlayerLeave( XUID xuid );
  45. virtual bool IsServiceSession();
  46. bool FindAndRemovePlayerFromMembers( XUID xuid );
  47. void UpdateSessionProperties( KeyValues *kv, bool bHost );
  48. virtual void OnSessionEvent( KeyValues *notify );
  49. void SendEventsNotification( KeyValues *notify );
  50. bool SendQueuedEventsNotifications();
  51. void PrintValue( KeyValues *val, char *chBuffer, int numBytesBuffer );
  52. #ifdef _X360
  53. CX360LobbyObject m_lobby;
  54. CX360NetworkMgr *m_pNetworkMgr;
  55. IX360LobbyAsyncOperation *m_pAsyncOperation;
  56. CX360LobbyMigrateHandle_t m_hLobbyMigrateCall;
  57. CX360LobbyMigrateOperation_t m_MigrateCallState;
  58. INetSupport::NetworkSocket_t GetX360NetSocket();
  59. virtual void OnAsyncOperationFinished() = 0;
  60. void ReleaseAsyncOperation();
  61. // IX360NetworkEvents
  62. virtual void OnX360NetPacket( KeyValues *msg );
  63. virtual void OnX360NetDisconnected( XUID xuidRemote );
  64. // Members management code
  65. void OnX360AllSessionMembersJoinLeave( KeyValues *kv );
  66. // Check whether host migration should be allowed
  67. // on the session
  68. virtual bool ShouldAllowX360HostMigration();
  69. virtual bool UpdateMigrationCall();
  70. #elif !defined( NO_STEAM )
  71. public:
  72. STEAM_CALLBACK_MANUAL( CSysSessionBase, Steam_OnLobbyChatMsg, LobbyChatMsg_t, m_CallbackOnLobbyChatMsg );
  73. STEAM_CALLBACK_MANUAL( CSysSessionBase, Steam_OnLobbyChatUpdate, LobbyChatUpdate_t, m_CallbackOnLobbyChatUpdate );
  74. STEAM_CALLBACK( CSysSessionBase, Steam_OnServersConnected, SteamServersConnected_t, m_CallbackOnServersConnected );
  75. STEAM_CALLBACK( CSysSessionBase, Steam_OnServersDisconnected, SteamServersDisconnected_t, m_CallbackOnServersDisconnected );
  76. STEAM_CALLBACK( CSysSessionBase, Steam_OnP2PSessionRequest, P2PSessionRequest_t, m_CallbackOnP2PSessionRequest );
  77. protected:
  78. void UnpackAndReceiveMessage( const void *pvBuffer, int numBytes, bool bValidatedLobbyMember, XUID xuidSrc );
  79. void SetupSteamRankingConfiguration();
  80. bool IsSteamRankingConfigured() const;
  81. protected:
  82. CSteamLobbyObject m_lobby;
  83. bool m_bVoiceUsingSessionP2P;
  84. char const * LobbyEnterErrorAsString( LobbyEnter_t *pLobbyEnter );
  85. void LobbySetDataFromKeyValues( char const *szPath, KeyValues *key, bool bRecurse = true );
  86. #else
  87. uint64 m_lobby;
  88. #endif
  89. protected:
  90. // Voice engine
  91. virtual void Voice_ProcessTalkers( KeyValues *pMachine, bool bAdd );
  92. virtual void Voice_CaptureAndTransmitLocalVoiceData();
  93. virtual void Voice_Playback( KeyValues *msg, XUID xuidSrc );
  94. virtual void Voice_UpdateLocalHeadsetsStatus();
  95. float m_Voice_flLastHeadsetStatusCheck;
  96. public:
  97. virtual void Voice_UpdateMutelist();
  98. protected:
  99. KeyValues *m_pSettings;
  100. XUID m_xuidMachineId;
  101. public:
  102. enum Result
  103. {
  104. RESULT_UNDEFINED,
  105. RESULT_FAIL,
  106. RESULT_SUCCESS
  107. };
  108. Result m_result;
  109. Result GetResult() { return m_result; }
  110. };
  111. class CSysSessionHost : public CSysSessionBase
  112. {
  113. public:
  114. explicit CSysSessionHost( KeyValues *pSettings );
  115. explicit CSysSessionHost( CSysSessionClient *pClient, KeyValues *pSettings );
  116. virtual ~CSysSessionHost();
  117. public:
  118. virtual bool Update();
  119. virtual void Destroy();
  120. virtual XUID GetHostXuid( XUID xuidValidResult = 0ull );
  121. void KickPlayer( KeyValues *pCommand );
  122. #ifdef _X360
  123. void GetHostSessionInfo( char chBuffer[ XSESSION_INFO_STRING_LENGTH ] );
  124. uint64 GetHostSessionId();
  125. #endif
  126. void UpdateMembersInfo();
  127. void OnUpdateSessionSettings( KeyValues *kv );
  128. void OnPlayerUpdated( KeyValues *pPlayer );
  129. void OnMachineUpdated( KeyValues *pMachine );
  130. void Migrate( KeyValues *pCommand );
  131. void ReserveTeamSession( XUID key, int numPlayers );
  132. void UnreserveTeamSession( );
  133. void SetCryptKey( uint64 ullCrypt ) { m_ullCrypt = ullCrypt; }
  134. uint64 GetCryptKey() const { return m_ullCrypt; }
  135. public:
  136. virtual void DebugPrint();
  137. protected:
  138. virtual void ReceiveMessage( KeyValues *msg, bool bValidatedLobbyMember, XUID xuidSrc ) OVERRIDE;
  139. virtual void OnPlayerLeave( XUID xuid );
  140. protected:
  141. bool Process_RequestJoinData( XUID xuidClient, KeyValues *pSettings );
  142. void Process_TeamReservation( XUID key, int teamSize );
  143. void Process_VoiceStatus( KeyValues *msg, XUID xuidSrc );
  144. void Process_VoiceMutelist( KeyValues *msg );
  145. void UpdateStateInit();
  146. void InitSessionProperties();
  147. void UpdateSessionProperties( KeyValues *kv );
  148. uint64 m_ullCrypt;
  149. XUID m_teamResKey;
  150. int m_numRemainingTeamPlayers;
  151. float m_flTeamResStartTime;
  152. #ifdef _X360
  153. virtual void OnAsyncOperationFinished();
  154. // IX360NetworkEvents
  155. virtual void OnX360NetDisconnected( XUID xuidRemote );
  156. virtual bool OnX360NetConnectionlessPacket( netpacket_t *pkt, KeyValues *msg );
  157. void DestroyAfterMigrationFinished();
  158. #elif !defined( NO_STEAM )
  159. CCallResult< CSysSessionHost, LobbyCreated_t > m_CallbackOnLobbyCreated;
  160. void Steam_OnLobbyCreated( LobbyCreated_t *p, bool bError );
  161. STEAM_CALLBACK_MANUAL( CSysSessionHost, Steam_OnLobbyEntered, LobbyEnter_t, m_CallbackOnLobbyEntered );
  162. bool GetLobbyType( KeyValues *kv, ELobbyType *peType, bool *pbJoinable );
  163. double m_dblDormantMembersCheckTime;
  164. int m_numDormantMembersDetected;
  165. #endif
  166. enum State_t
  167. {
  168. STATE_INIT,
  169. STATE_CREATING,
  170. STATE_IDLE,
  171. STATE_FAIL,
  172. STATE_MIGRATE,
  173. #ifdef _X360
  174. STATE_ALLOWING_MIGRATE,
  175. STATE_DELETE,
  176. #endif
  177. STATE_UNDEFINED
  178. };
  179. State_t m_eState;
  180. float m_flTimeOperationStarted;
  181. float m_flInitializeTimestamp;
  182. CUtlMap< XUID, double, int, CDefLess< XUID > > m_mapKickedPlayers;
  183. };
  184. class CSysSessionClient : public CSysSessionBase
  185. {
  186. public:
  187. explicit CSysSessionClient( KeyValues *pSettings );
  188. explicit CSysSessionClient( CSysSessionHost *pHost, KeyValues *pSettings );
  189. virtual ~CSysSessionClient();
  190. public:
  191. virtual bool Update();
  192. virtual void Destroy();
  193. virtual XUID GetHostXuid( XUID xuidValidResult = 0ull );
  194. #ifdef _X360
  195. char const * GetHostNetworkAddress( XSESSION_INFO &xsi );
  196. #endif
  197. void Migrate( KeyValues *pCommand );
  198. public:
  199. virtual void DebugPrint();
  200. protected:
  201. virtual void ReceiveMessage( KeyValues *msg, bool bValidatedLobbyMember, XUID xuidSrc ) OVERRIDE;
  202. virtual void OnPlayerLeave( XUID xuid );
  203. protected:
  204. void Send_RequestJoinData();
  205. void Process_ReplyJoinData_Our( KeyValues *msg );
  206. void Process_ReplyJoinData_Other( KeyValues *msg );
  207. void Process_OnPlayerUpdated( KeyValues *msg );
  208. void Process_OnMachineUpdated( KeyValues *msg );
  209. void Process_Kicked( KeyValues *msg );
  210. protected:
  211. void UpdateStateInit();
  212. void InitSessionProperties( KeyValues *pSettings );
  213. void UpdateSessionProperties( KeyValues *kv );
  214. #ifdef _X360
  215. virtual void OnAsyncOperationFinished();
  216. virtual void XP2P_Interconnect();
  217. // IX360NetworkEvents
  218. virtual void OnX360NetDisconnected( XUID xuidRemote );
  219. virtual bool OnX360NetConnectionlessPacket( netpacket_t *pkt, KeyValues *msg );
  220. XNADDR m_xnaddrLocal;
  221. #elif !defined( NO_STEAM )
  222. STEAM_CALLBACK_MANUAL( CSysSessionClient, Steam_OnLobbyEntered, LobbyEnter_t, m_CallbackOnLobbyEntered );
  223. #endif
  224. struct RequestJoinDataInfo_t
  225. {
  226. XUID m_xuidLeader; // XUID of the leader who must reply
  227. float m_fTimeSent; // Time when request was sent
  228. } m_RequestJoinDataInfo;
  229. enum State_t
  230. {
  231. STATE_INIT,
  232. #if !defined ( NO_STEAM )
  233. STATE_JOIN_LOBBY,
  234. #endif
  235. STATE_CREATING,
  236. STATE_REQUESTING_JOIN_DATA,
  237. STATE_IDLE,
  238. STATE_FAIL,
  239. STATE_MIGRATE,
  240. #ifdef _X360
  241. STATE_DELETE,
  242. #endif
  243. STATE_UNDEFINED
  244. };
  245. State_t m_eState;
  246. float m_flInitializeTimestamp;
  247. };
  248. class CSysSessionConTeamHost : public CSysSessionBase
  249. {
  250. public:
  251. explicit CSysSessionConTeamHost( KeyValues *pSettings );
  252. virtual ~CSysSessionConTeamHost();
  253. virtual bool Update();
  254. virtual void Destroy();
  255. // Once reservation is successful call this function to know which side
  256. // each player should be assigned to
  257. // In keyvalues, the convention is that team CT = 1, team T = 2
  258. bool GetPlayerSidesAssignment( int *numPlayers, uint64 playerIDs[10], int side[10] );
  259. protected:
  260. virtual void ReceiveMessage( KeyValues *msg, bool bValidatedLobbyMember, XUID xuidSrc ) OVERRIDE;
  261. void SendReservationRequest();
  262. void Succeeded();
  263. void Failed();
  264. XUID GetHostXuid( XUID xuidValidResult );
  265. enum State_t
  266. {
  267. STATE_INIT,
  268. STATE_WAITING_LOBBY_JOIN,
  269. STATE_SEND_RESERVATION_REQUEST,
  270. STATE_WAITING_RESERVATION_REQUEST,
  271. STATE_DONE,
  272. STATE_DELETE,
  273. };
  274. State_t m_eState;
  275. float m_lastRequestSendTime;
  276. public:
  277. #ifdef _X360
  278. XSESSION_INFO m_sessionInfo;
  279. virtual bool OnX360NetConnectionlessPacket( netpacket_t *pkt, KeyValues *msg );
  280. virtual void OnAsyncOperationFinished();
  281. IN_ADDR m_inaddr;
  282. #elif !defined( NO_STEAM )
  283. STEAM_CALLBACK_MANUAL( CSysSessionConTeamHost, Steam_OnLobbyEntered, LobbyEnter_t, m_CallbackOnLobbyEntered );
  284. #endif
  285. };
  286. #ifdef _X360
  287. void SysSession360_UpdatePending();
  288. #endif
  289. #endif