matchmakingtypes.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //========= Copyright � 1996-2008, Valve LLC, All rights reserved. ============
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #ifndef MATCHMAKINGTYPES_H
  8. #define MATCHMAKINGTYPES_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #ifdef POSIX
  13. #ifndef _snprintf
  14. #define _snprintf snprintf
  15. #endif
  16. #endif
  17. #include <stdio.h>
  18. #include <string.h>
  19. //
  20. // Max size (in bytes of UTF-8 data, not in characters) of server fields, including null terminator.
  21. // WARNING: These cannot be changed easily, without breaking clients using old interfaces.
  22. //
  23. const int k_cbMaxGameServerGameDir = 32;
  24. const int k_cbMaxGameServerMapName = 32;
  25. const int k_cbMaxGameServerGameDescription = 64;
  26. const int k_cbMaxGameServerName = 64;
  27. const int k_cbMaxGameServerTags = 128;
  28. const int k_cbMaxGameServerGameData = 2048;
  29. /// Store key/value pair used in matchmaking queries.
  30. ///
  31. /// Actually, the name Key/Value is a bit misleading. The "key" is better
  32. /// understood as "filter operation code" and the "value" is the operand to this
  33. /// filter operation. The meaning of the operand depends upon the filter.
  34. struct MatchMakingKeyValuePair_t
  35. {
  36. MatchMakingKeyValuePair_t() { m_szKey[0] = m_szValue[0] = 0; }
  37. MatchMakingKeyValuePair_t( const char *pchKey, const char *pchValue )
  38. {
  39. strncpy( m_szKey, pchKey, sizeof(m_szKey) ); // this is a public header, use basic c library string funcs only!
  40. m_szKey[ sizeof( m_szKey ) - 1 ] = '\0';
  41. strncpy( m_szValue, pchValue, sizeof(m_szValue) );
  42. m_szValue[ sizeof( m_szValue ) - 1 ] = '\0';
  43. }
  44. char m_szKey[ 256 ];
  45. char m_szValue[ 256 ];
  46. };
  47. enum EMatchMakingServerResponse
  48. {
  49. eServerResponded = 0,
  50. eServerFailedToRespond,
  51. eNoServersListedOnMasterServer // for the Internet query type, returned in response callback if no servers of this type match
  52. };
  53. // servernetadr_t is all the addressing info the serverbrowser needs to know about a game server,
  54. // namely: its IP, its connection port, and its query port.
  55. class servernetadr_t
  56. {
  57. public:
  58. servernetadr_t() : m_usConnectionPort( 0 ), m_usQueryPort( 0 ), m_unIP( 0 ) {}
  59. void Init( unsigned int ip, uint16 usQueryPort, uint16 usConnectionPort );
  60. #ifdef NETADR_H
  61. netadr_t GetIPAndQueryPort();
  62. #endif
  63. // Access the query port.
  64. uint16 GetQueryPort() const;
  65. void SetQueryPort( uint16 usPort );
  66. // Access the connection port.
  67. uint16 GetConnectionPort() const;
  68. void SetConnectionPort( uint16 usPort );
  69. // Access the IP
  70. uint32 GetIP() const;
  71. void SetIP( uint32 );
  72. // This gets the 'a.b.c.d:port' string with the connection port (instead of the query port).
  73. const char *GetConnectionAddressString() const;
  74. const char *GetQueryAddressString() const;
  75. // Comparison operators and functions.
  76. bool operator<(const servernetadr_t &netadr) const;
  77. void operator=( const servernetadr_t &that )
  78. {
  79. m_usConnectionPort = that.m_usConnectionPort;
  80. m_usQueryPort = that.m_usQueryPort;
  81. m_unIP = that.m_unIP;
  82. }
  83. private:
  84. const char *ToString( uint32 unIP, uint16 usPort ) const;
  85. uint16 m_usConnectionPort; // (in HOST byte order)
  86. uint16 m_usQueryPort;
  87. uint32 m_unIP;
  88. };
  89. inline void servernetadr_t::Init( unsigned int ip, uint16 usQueryPort, uint16 usConnectionPort )
  90. {
  91. m_unIP = ip;
  92. m_usQueryPort = usQueryPort;
  93. m_usConnectionPort = usConnectionPort;
  94. }
  95. #ifdef NETADR_H
  96. inline netadr_t servernetadr_t::GetIPAndQueryPort()
  97. {
  98. return netadr_t( m_unIP, m_usQueryPort );
  99. }
  100. #endif
  101. inline uint16 servernetadr_t::GetQueryPort() const
  102. {
  103. return m_usQueryPort;
  104. }
  105. inline void servernetadr_t::SetQueryPort( uint16 usPort )
  106. {
  107. m_usQueryPort = usPort;
  108. }
  109. inline uint16 servernetadr_t::GetConnectionPort() const
  110. {
  111. return m_usConnectionPort;
  112. }
  113. inline void servernetadr_t::SetConnectionPort( uint16 usPort )
  114. {
  115. m_usConnectionPort = usPort;
  116. }
  117. inline uint32 servernetadr_t::GetIP() const
  118. {
  119. return m_unIP;
  120. }
  121. inline void servernetadr_t::SetIP( uint32 unIP )
  122. {
  123. m_unIP = unIP;
  124. }
  125. inline const char *servernetadr_t::ToString( uint32 unIP, uint16 usPort ) const
  126. {
  127. static char s[4][64];
  128. static int nBuf = 0;
  129. unsigned char *ipByte = (unsigned char *)&unIP;
  130. #ifdef VALVE_BIG_ENDIAN
  131. _snprintf (s[nBuf], sizeof( s[nBuf] ), "%u.%u.%u.%u:%i", (int)(ipByte[0]), (int)(ipByte[1]), (int)(ipByte[2]), (int)(ipByte[3]), usPort );
  132. #else
  133. _snprintf (s[nBuf], sizeof( s[nBuf] ), "%u.%u.%u.%u:%i", (int)(ipByte[3]), (int)(ipByte[2]), (int)(ipByte[1]), (int)(ipByte[0]), usPort );
  134. #endif
  135. const char *pchRet = s[nBuf];
  136. ++nBuf;
  137. nBuf %= ( (sizeof(s)/sizeof(s[0])) );
  138. return pchRet;
  139. }
  140. inline const char* servernetadr_t::GetConnectionAddressString() const
  141. {
  142. return ToString( m_unIP, m_usConnectionPort );
  143. }
  144. inline const char* servernetadr_t::GetQueryAddressString() const
  145. {
  146. return ToString( m_unIP, m_usQueryPort );
  147. }
  148. inline bool servernetadr_t::operator<(const servernetadr_t &netadr) const
  149. {
  150. return ( m_unIP < netadr.m_unIP ) || ( m_unIP == netadr.m_unIP && m_usQueryPort < netadr.m_usQueryPort );
  151. }
  152. //-----------------------------------------------------------------------------
  153. // Purpose: Data describing a single server
  154. //-----------------------------------------------------------------------------
  155. class gameserveritem_t
  156. {
  157. public:
  158. gameserveritem_t();
  159. const char* GetName() const;
  160. void SetName( const char *pName );
  161. public:
  162. servernetadr_t m_NetAdr; ///< IP/Query Port/Connection Port for this server
  163. int m_nPing; ///< current ping time in milliseconds
  164. bool m_bHadSuccessfulResponse; ///< server has responded successfully in the past
  165. bool m_bDoNotRefresh; ///< server is marked as not responding and should no longer be refreshed
  166. char m_szGameDir[k_cbMaxGameServerGameDir]; ///< current game directory
  167. char m_szMap[k_cbMaxGameServerMapName]; ///< current map
  168. char m_szGameDescription[k_cbMaxGameServerGameDescription]; ///< game description
  169. uint32 m_nAppID; ///< Steam App ID of this server
  170. int m_nPlayers; ///< total number of players currently on the server. INCLUDES BOTS!!
  171. int m_nMaxPlayers; ///< Maximum players that can join this server
  172. int m_nBotPlayers; ///< Number of bots (i.e simulated players) on this server
  173. bool m_bPassword; ///< true if this server needs a password to join
  174. bool m_bSecure; ///< Is this server protected by VAC
  175. uint32 m_ulTimeLastPlayed; ///< time (in unix time) when this server was last played on (for favorite/history servers)
  176. int m_nServerVersion; ///< server version as reported to Steam
  177. private:
  178. /// Game server name
  179. char m_szServerName[k_cbMaxGameServerName];
  180. // For data added after SteamMatchMaking001 add it here
  181. public:
  182. /// the tags this server exposes
  183. char m_szGameTags[k_cbMaxGameServerTags];
  184. /// steamID of the game server - invalid if it's doesn't have one (old server, or not connected to Steam)
  185. CSteamID m_steamID;
  186. };
  187. inline gameserveritem_t::gameserveritem_t()
  188. {
  189. m_szGameDir[0] = m_szMap[0] = m_szGameDescription[0] = m_szServerName[0] = 0;
  190. m_bHadSuccessfulResponse = m_bDoNotRefresh = m_bPassword = m_bSecure = false;
  191. m_nPing = m_nAppID = m_nPlayers = m_nMaxPlayers = m_nBotPlayers = m_ulTimeLastPlayed = m_nServerVersion = 0;
  192. m_szGameTags[0] = 0;
  193. }
  194. inline const char* gameserveritem_t::GetName() const
  195. {
  196. // Use the IP address as the name if nothing is set yet.
  197. if ( m_szServerName[0] == 0 )
  198. return m_NetAdr.GetConnectionAddressString();
  199. else
  200. return m_szServerName;
  201. }
  202. inline void gameserveritem_t::SetName( const char *pName )
  203. {
  204. strncpy( m_szServerName, pName, sizeof( m_szServerName ) );
  205. m_szServerName[ sizeof( m_szServerName ) - 1 ] = '\0';
  206. }
  207. #endif // MATCHMAKINGTYPES_H