sys_lobby_backend.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __SYS_LOBBY_BACKEND_H__
  21. #define __SYS_LOBBY_BACKEND_H__
  22. extern idCVar net_verboseResource;
  23. #define NET_VERBOSERESOURCE_PRINT if ( net_verboseResource.GetBool() ) idLib::Printf
  24. extern idCVar net_verbose;
  25. #define NET_VERBOSE_PRINT if ( net_verbose.GetBool() ) idLib::Printf
  26. class lobbyAddress_t {
  27. public:
  28. lobbyAddress_t();
  29. void InitFromNetadr( const netadr_t & netadr );
  30. void InitFromIPandPort( const char * ip, int port );
  31. const char * ToString() const;
  32. bool UsingRelay() const;
  33. bool Compare( const lobbyAddress_t & addr, bool ignoreSessionCheck = false ) const;
  34. void WriteToMsg( idBitMsg & msg ) const;
  35. void ReadFromMsg( idBitMsg & msg );
  36. // IP address
  37. netadr_t netAddr;
  38. };
  39. struct lobbyConnectInfo_t {
  40. public:
  41. void WriteToMsg( idBitMsg & msg ) const {
  42. msg.WriteNetadr( netAddr );
  43. }
  44. void ReadFromMsg( idBitMsg & msg ) {
  45. msg.ReadNetadr( &netAddr );
  46. }
  47. lobbyConnectInfo_t() : netAddr() { }
  48. netadr_t netAddr;
  49. };
  50. class idNetSessionPort {
  51. public:
  52. idNetSessionPort();
  53. bool InitPort( int portNumber, bool useBackend );
  54. bool ReadRawPacket( lobbyAddress_t & from, void * data, int & size, int maxSize );
  55. void SendRawPacket( const lobbyAddress_t & to, const void * data, int size );
  56. bool IsOpen();
  57. void Close();
  58. private:
  59. float forcePacketDropCurr; // Used with net_forceDrop and net_forceDropCorrelation
  60. float forcePacketDropPrev;
  61. idUDP UDP;
  62. };
  63. struct lobbyUser_t {
  64. static const int INVALID_PING = 9999;
  65. // gamertags can be up to 16 4-byte characters + \0
  66. static const int MAX_GAMERTAG = 64 + 1;
  67. lobbyUser_t() {
  68. isBot = false;
  69. peerIndex = -1;
  70. disconnecting = false;
  71. level = 1;
  72. pingMs = INVALID_PING;
  73. teamNumber = 0;
  74. arbitrationAcked = false;
  75. partyToken = 0;
  76. selectedSkin = 0;
  77. weaponAutoSwitch = true;
  78. weaponAutoReload = true;
  79. migrationGameData = -1;
  80. }
  81. // Common variables
  82. bool isBot; // true if lobbyUser is a bot.
  83. int peerIndex; // peer number on host
  84. lobbyUserID_t lobbyUserID; // Locally generated to be unique, and internally keeps the local user handle
  85. char gamertag[MAX_GAMERTAG];
  86. int pingMs; // round trip time in milliseconds
  87. bool disconnecting; // true if we've sent a msg to disconnect this user from the session
  88. int level;
  89. int teamNumber;
  90. uint32 partyToken; // set by the server when people join as a party
  91. int selectedSkin;
  92. bool weaponAutoSwitch;
  93. bool weaponAutoReload;
  94. bool arbitrationAcked; // if the user is verified for arbitration
  95. lobbyAddress_t address;
  96. int migrationGameData; // index into the local migration gamedata array that is associated with this user. -1=no migration game data available
  97. // Platform variables
  98. bool IsDisconnected() const { return lobbyUserID.IsValid() ? false : true; }
  99. void WriteToMsg( idBitMsg & msg ) {
  100. address.WriteToMsg( msg );
  101. lobbyUserID.WriteToMsg( msg );
  102. msg.WriteLong( peerIndex );
  103. msg.WriteShort( pingMs );
  104. msg.WriteLong( partyToken );
  105. msg.WriteString( gamertag, MAX_GAMERTAG, false );
  106. WriteClientMutableData( msg );
  107. }
  108. void ReadFromMsg( idBitMsg & msg ) {
  109. address.ReadFromMsg( msg );
  110. lobbyUserID.ReadFromMsg( msg );
  111. peerIndex = msg.ReadLong();
  112. pingMs = msg.ReadShort();
  113. partyToken = msg.ReadLong();
  114. msg.ReadString( gamertag, MAX_GAMERTAG );
  115. ReadClientMutableData( msg );
  116. }
  117. bool UpdateClientMutableData( const idLocalUser * localUser );
  118. void WriteClientMutableData( idBitMsg & msg ) {
  119. msg.WriteBits( selectedSkin, 4 );
  120. msg.WriteBits( teamNumber, 2 ); // We need two bits since we use team value of 2 for spectating
  121. msg.WriteBool( weaponAutoSwitch );
  122. msg.WriteBool( weaponAutoReload );
  123. release_assert( msg.GetWriteBit() == 0 );
  124. }
  125. void ReadClientMutableData( idBitMsg & msg ) {
  126. selectedSkin = msg.ReadBits( 4 );
  127. teamNumber = msg.ReadBits( 2 ); // We need two bits since we use team value of 2 for spectating
  128. weaponAutoSwitch = msg.ReadBool();
  129. weaponAutoReload = msg.ReadBool();
  130. }
  131. };
  132. /*
  133. ================================================
  134. idLobbyBackend
  135. This class interfaces with the various back ends for the different platforms
  136. ================================================
  137. */
  138. class idLobbyBackend {
  139. public:
  140. enum lobbyBackendState_t {
  141. STATE_INVALID = 0,
  142. STATE_READY = 1,
  143. STATE_CREATING = 2, // In the process of creating the lobby as a host
  144. STATE_SEARCHING = 3, // In the process of searching for a lobby to join
  145. STATE_OBTAINING_ADDRESS = 4, // In the process of obtaining the address of the lobby owner
  146. STATE_ARBITRATING = 5, // Arbitrating
  147. STATE_SHUTTING_DOWN = 6, // In the process of shutting down
  148. STATE_SHUTDOWN = 7, // Was a host or peer at one point, now ready to be deleted
  149. STATE_FAILED = 8, // Failure occurred
  150. NUM_STATES
  151. };
  152. static const char * GetStateString( lobbyBackendState_t state_ ) {
  153. static const char * stateToString[NUM_STATES] = {
  154. "STATE_INVALID",
  155. "STATE_READY",
  156. "STATE_CREATING",
  157. "STATE_SEARCHING",
  158. "STATE_OBTAINING_ADDRESS",
  159. "STATE_ARBITRATING",
  160. "STATE_SHUTTING_DOWN",
  161. "STATE_SHUTDOWN",
  162. "STATE_FAILED"
  163. };
  164. return stateToString[ state_ ];
  165. }
  166. enum lobbyBackendType_t {
  167. TYPE_PARTY = 0,
  168. TYPE_GAME = 1,
  169. TYPE_GAME_STATE = 2,
  170. TYPE_INVALID = 0xff,
  171. };
  172. idLobbyBackend() : type( TYPE_INVALID ), isHost( false ), isLocal( false ) {}
  173. idLobbyBackend( lobbyBackendType_t lobbyType ) : type( lobbyType ), isHost( false ), isLocal( false ) {}
  174. virtual void StartHosting( const idMatchParameters & p, float skillLevel, lobbyBackendType_t type ) = 0;
  175. virtual void StartFinding( const idMatchParameters & p, int numPartyUsers, float skillLevel ) = 0;
  176. virtual void JoinFromConnectInfo( const lobbyConnectInfo_t & connectInfo ) = 0;
  177. virtual void GetSearchResults( idList< lobbyConnectInfo_t > & searchResults ) = 0;
  178. virtual lobbyConnectInfo_t GetConnectInfo() = 0;
  179. virtual void FillMsgWithPostConnectInfo( idBitMsg & msg ) = 0; // Passed itno PostConnectFromMsg
  180. virtual void PostConnectFromMsg( idBitMsg & msg ) = 0; // Uses results from FillMsgWithPostConnectInfo
  181. virtual bool IsOwnerOfConnectInfo( const lobbyConnectInfo_t & connectInfo ) const { return false; }
  182. virtual void Shutdown() = 0;
  183. virtual void GetOwnerAddress( lobbyAddress_t & outAddr ) = 0;
  184. virtual bool IsHost() { return isHost; }
  185. virtual void SetIsJoinable( bool joinable ) {}
  186. virtual void Pump() = 0;
  187. virtual void UpdateMatchParms( const idMatchParameters & p ) = 0;
  188. virtual void UpdateLobbySkill( float lobbySkill ) = 0;
  189. virtual void SetInGame( bool value ) {}
  190. virtual lobbyBackendState_t GetState() = 0;
  191. virtual bool IsLocal() const { return isLocal; }
  192. virtual bool IsOnline() const { return !isLocal; }
  193. virtual bool StartArbitration() { return false; }
  194. virtual void Arbitrate() {}
  195. virtual void VerifyArbitration() {}
  196. virtual bool UserArbitrated( lobbyUser_t * user ) { return false; }
  197. virtual void RegisterUser( lobbyUser_t * user, bool isLocal ) {}
  198. virtual void UnregisterUser( lobbyUser_t * user, bool isLocal ) {}
  199. virtual void StartSession() {}
  200. virtual void EndSession() {}
  201. virtual bool IsSessionStarted() { return false; }
  202. virtual void FlushStats() {}
  203. virtual void BecomeHost( int numInvites ) {} // Become the host of this lobby
  204. virtual void RegisterAddress( lobbyAddress_t & address ) {} // Called after becoming a new host, to register old addresses to send invites to
  205. virtual void FinishBecomeHost() {}
  206. void SetLobbyType( lobbyBackendType_t lobbyType ) { type = lobbyType; }
  207. lobbyBackendType_t GetLobbyType() const { return type; }
  208. const char * GetLobbyTypeString() const { return ( GetLobbyType() == TYPE_PARTY ) ? "Party" : "Game"; }
  209. bool IsRanked() { return MatchTypeIsRanked( parms.matchFlags ); }
  210. bool IsPrivate() { return MatchTypeIsPrivate( parms.matchFlags ); }
  211. protected:
  212. lobbyBackendType_t type;
  213. idMatchParameters parms;
  214. bool isLocal; // True if this lobby is restricted to local play only (won't need and can't connect to online lobbies)
  215. bool isHost; // True if we created this lobby
  216. };
  217. class idLobbyToSessionCB {
  218. public:
  219. virtual class idLobbyBackend * GetLobbyBackend( idLobbyBackend::lobbyBackendType_t type ) const = 0;
  220. virtual bool CanJoinLocalHost() const = 0;
  221. // Ugh, hate having to ifdef these, but we're doing some fairly platform specific callbacks
  222. };
  223. #endif // __SYS_LOBBY_BACKEND_H__