steam_gameserver.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //====== Copyright © 1996-2008, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #ifndef STEAM_GAMESERVER_H
  7. #define STEAM_GAMESERVER_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "steam_api.h"
  12. #include "isteamgameserver.h"
  13. #include "isteamgameserverstats.h"
  14. enum EServerMode
  15. {
  16. eServerModeInvalid = 0, // DO NOT USE
  17. eServerModeNoAuthentication = 1, // Don't authenticate user logins and don't list on the server list
  18. eServerModeAuthentication = 2, // Authenticate users, list on the server list, don't run VAC on clients that connect
  19. eServerModeAuthenticationAndSecure = 3, // Authenticate users, list on the server list and VAC protect clients
  20. };
  21. // Initialize ISteamGameServer interface object, and set server properties which may not be changed.
  22. //
  23. // After calling this function, you should set any additional server parameters, and then
  24. // call ISteamGameServer::LogOnAnonymous() or ISteamGameServer::LogOn()
  25. //
  26. // - usSteamPort is the local port used to communicate with the steam servers.
  27. // - usGamePort is the port that clients will connect to for gameplay.
  28. // - usQueryPort is the port that will manage server browser related duties and info
  29. // pings from clients. If you pass MASTERSERVERUPDATERPORT_USEGAMESOCKETSHARE for usQueryPort, then it
  30. // will use "GameSocketShare" mode, which means that the game is responsible for sending and receiving
  31. // UDP packets for the master server updater. See references to GameSocketShare in isteamgameserver.h.
  32. // - The version string is usually in the form x.x.x.x, and is used by the master server to detect when the
  33. // server is out of date. (Only servers with the latest version will be listed.)
  34. inline bool SteamGameServer_Init( uint32 unIP, uint16 usSteamPort, uint16 usGamePort, uint16 usQueryPort, EServerMode eServerMode, const char *pchVersionString );
  35. S_API void SteamGameServer_Shutdown();
  36. S_API void SteamGameServer_RunCallbacks();
  37. // Most Steam API functions allocate some amount of thread-local memory for
  38. // parameter storage. Calling SteamGameServer_ReleaseCurrentThreadMemory()
  39. // will free all API-related memory associated with the calling thread.
  40. // This memory is released automatically by SteamGameServer_RunCallbacks(),
  41. // so single-threaded servers do not need to explicitly call this function.
  42. inline void SteamGameServer_ReleaseCurrentThreadMemory();
  43. S_API bool SteamGameServer_BSecure();
  44. S_API uint64 SteamGameServer_GetSteamID();
  45. // If your application contains modules which could be built against different Steamworks SDK
  46. // versions, then you should define VERSION_SAFE_STEAM_API_INTERFACES to enforce that you cannot
  47. // use the version-less global accessors. Instead, create and use CSteamGameServerAPIContext
  48. // objects to retrieve interface pointers which are appropriate for your Steamworks SDK headers.
  49. #if !defined( VERSION_SAFE_STEAM_API_INTERFACES ) && !defined( STEAM_API_EXPORTS )
  50. inline ISteamClient *SteamGameServerClient();
  51. inline ISteamGameServer *SteamGameServer();
  52. inline ISteamUtils *SteamGameServerUtils();
  53. inline ISteamNetworking *SteamGameServerNetworking();
  54. inline ISteamGameServerStats *SteamGameServerStats();
  55. inline ISteamHTTP *SteamGameServerHTTP();
  56. inline ISteamInventory *SteamGameServerInventory();
  57. inline ISteamUGC *SteamGameServerUGC();
  58. inline ISteamApps *SteamGameServerApps();
  59. #endif
  60. class CSteamGameServerAPIContext
  61. {
  62. public:
  63. CSteamGameServerAPIContext() { Clear(); }
  64. inline void Clear();
  65. inline bool Init();
  66. ISteamClient *SteamClient() const { return m_pSteamClient; }
  67. ISteamGameServer *SteamGameServer() const { return m_pSteamGameServer; }
  68. ISteamUtils *SteamGameServerUtils() const { return m_pSteamGameServerUtils; }
  69. ISteamNetworking *SteamGameServerNetworking() const { return m_pSteamGameServerNetworking; }
  70. ISteamGameServerStats *SteamGameServerStats() const { return m_pSteamGameServerStats; }
  71. ISteamHTTP *SteamHTTP() const { return m_pSteamHTTP; }
  72. ISteamInventory *SteamInventory() const { return m_pSteamInventory; }
  73. ISteamUGC *SteamUGC() const { return m_pSteamUGC; }
  74. ISteamApps *SteamApps() const { return m_pSteamApps; }
  75. private:
  76. ISteamClient *m_pSteamClient;
  77. ISteamGameServer *m_pSteamGameServer;
  78. ISteamUtils *m_pSteamGameServerUtils;
  79. ISteamNetworking *m_pSteamGameServerNetworking;
  80. ISteamGameServerStats *m_pSteamGameServerStats;
  81. ISteamHTTP *m_pSteamHTTP;
  82. ISteamInventory *m_pSteamInventory;
  83. ISteamUGC *m_pSteamUGC;
  84. ISteamApps *m_pSteamApps;
  85. };
  86. // Older SDKs exported this global pointer, but it is no longer supported.
  87. // You should use SteamGameServerClient() or CSteamGameServerAPIContext to
  88. // safely access the ISteamClient APIs from your game server application.
  89. //S_API ISteamClient *g_pSteamClientGameServer;
  90. // SteamGameServer_InitSafe has been replaced with SteamGameServer_Init and
  91. // is no longer supported. Use SteamGameServer_Init instead.
  92. //S_API void S_CALLTYPE SteamGameServer_InitSafe();
  93. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  94. // These macros are similar to the STEAM_CALLBACK_* macros in steam_api.h, but only trigger for gameserver callbacks
  95. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  96. #define STEAM_GAMESERVER_CALLBACK( thisclass, func, /*callback_type, [deprecated] var*/... ) \
  97. _STEAM_CALLBACK_SELECT( ( __VA_ARGS__, GS, 3 ), ( this->SetGameserverFlag();, thisclass, func, __VA_ARGS__ ) )
  98. #define STEAM_GAMESERVER_CALLBACK_MANUAL( thisclass, func, callback_type, var ) \
  99. CCallbackManual< thisclass, callback_type, true > var; void func( callback_type *pParam )
  100. #define _STEAM_CALLBACK_GS( _, thisclass, func, param, var ) \
  101. CCallback< thisclass, param, true > var; void func( param *pParam )
  102. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  103. // steamclient.dll private wrapper functions
  104. //
  105. // The following functions are part of abstracting API access to the steamclient.dll, but should only be used in very specific cases
  106. //----------------------------------------------------------------------------------------------------------------------------------------------------------//
  107. S_API HSteamPipe S_CALLTYPE SteamGameServer_GetHSteamPipe();
  108. S_API HSteamUser S_CALLTYPE SteamGameServer_GetHSteamUser();
  109. S_API bool S_CALLTYPE SteamInternal_GameServer_Init( uint32 unIP, uint16 usPort, uint16 usGamePort, uint16 usQueryPort, EServerMode eServerMode, const char *pchVersionString );
  110. #if !defined( VERSION_SAFE_STEAM_API_INTERFACES ) && !defined( STEAM_API_EXPORTS )
  111. inline CSteamGameServerAPIContext& SteamGameServerInternal_ModuleContext()
  112. {
  113. // NOTE: declaring "static CSteamAPIConext" creates a large function
  114. // which queries the initialization status of the object. We know that
  115. // it is pointer-aligned and fully memset with zeros, so just alias a
  116. // static buffer of the appropriate size and call it a CSteamAPIContext.
  117. static void* ctx[ sizeof(CSteamGameServerAPIContext)/sizeof(void*) ];
  118. return *(CSteamGameServerAPIContext*)ctx;
  119. }
  120. #define _STEAMINTERNAL_ACCESSOR_BODY( AccessFunc ) \
  121. if ( !SteamGameServer_GetHSteamPipe() ) return 0; \
  122. CSteamGameServerAPIContext &ctx = SteamGameServerInternal_ModuleContext(); \
  123. if ( !ctx.AccessFunc() ) ctx.Init(); \
  124. return ctx.AccessFunc();
  125. inline ISteamClient *SteamGameServerClient() { _STEAMINTERNAL_ACCESSOR_BODY( SteamClient ) }
  126. inline ISteamGameServer *SteamGameServer() { _STEAMINTERNAL_ACCESSOR_BODY( SteamGameServer ) }
  127. inline ISteamUtils *SteamGameServerUtils() { _STEAMINTERNAL_ACCESSOR_BODY( SteamGameServerUtils ) }
  128. inline ISteamNetworking *SteamGameServerNetworking() { _STEAMINTERNAL_ACCESSOR_BODY( SteamGameServerNetworking ) }
  129. inline ISteamGameServerStats *SteamGameServerStats() { _STEAMINTERNAL_ACCESSOR_BODY( SteamGameServerStats ) }
  130. inline ISteamHTTP *SteamGameServerHTTP() { _STEAMINTERNAL_ACCESSOR_BODY( SteamHTTP ) }
  131. inline ISteamInventory *SteamGameServerInventory() { _STEAMINTERNAL_ACCESSOR_BODY( SteamInventory ) }
  132. inline ISteamUGC *SteamGameServerUGC() { _STEAMINTERNAL_ACCESSOR_BODY( SteamUGC ) }
  133. inline ISteamApps *SteamGameServerApps() { _STEAMINTERNAL_ACCESSOR_BODY( SteamApps ) }
  134. #undef _STEAMINTERNAL_ACCESSOR_BODY
  135. #endif // !defined( VERSION_SAFE_STEAM_API_INTERFACES ) && !defined( STEAM_API_EXPORTS )
  136. inline void CSteamGameServerAPIContext::Clear()
  137. {
  138. m_pSteamClient = NULL;
  139. m_pSteamGameServer = NULL;
  140. m_pSteamGameServerUtils = NULL;
  141. m_pSteamGameServerNetworking = NULL;
  142. m_pSteamGameServerStats = NULL;
  143. m_pSteamHTTP = NULL;
  144. m_pSteamInventory = NULL;
  145. m_pSteamUGC = NULL;
  146. m_pSteamApps = NULL;
  147. }
  148. // This function must be declared inline in the header so the module using steam_api.dll gets the version names they want.
  149. inline bool CSteamGameServerAPIContext::Init()
  150. {
  151. HSteamUser hSteamUser = SteamGameServer_GetHSteamUser();
  152. HSteamPipe hSteamPipe = SteamGameServer_GetHSteamPipe();
  153. if ( !hSteamPipe )
  154. return false;
  155. m_pSteamClient = (ISteamClient*) SteamInternal_CreateInterface( STEAMCLIENT_INTERFACE_VERSION );
  156. if ( !m_pSteamClient )
  157. return false;
  158. m_pSteamGameServer = m_pSteamClient->GetISteamGameServer( hSteamUser, hSteamPipe, STEAMGAMESERVER_INTERFACE_VERSION );
  159. if ( !m_pSteamGameServer )
  160. return false;
  161. m_pSteamGameServerUtils = m_pSteamClient->GetISteamUtils( hSteamPipe, STEAMUTILS_INTERFACE_VERSION );
  162. if ( !m_pSteamGameServerUtils )
  163. return false;
  164. m_pSteamGameServerNetworking = m_pSteamClient->GetISteamNetworking( hSteamUser, hSteamPipe, STEAMNETWORKING_INTERFACE_VERSION );
  165. if ( !m_pSteamGameServerNetworking )
  166. return false;
  167. m_pSteamGameServerStats = m_pSteamClient->GetISteamGameServerStats( hSteamUser, hSteamPipe, STEAMGAMESERVERSTATS_INTERFACE_VERSION );
  168. if ( !m_pSteamGameServerStats )
  169. return false;
  170. m_pSteamHTTP = m_pSteamClient->GetISteamHTTP( hSteamUser, hSteamPipe, STEAMHTTP_INTERFACE_VERSION );
  171. if ( !m_pSteamHTTP )
  172. return false;
  173. m_pSteamInventory = m_pSteamClient->GetISteamInventory( hSteamUser, hSteamPipe, STEAMINVENTORY_INTERFACE_VERSION );
  174. if ( !m_pSteamInventory )
  175. return false;
  176. m_pSteamUGC = m_pSteamClient->GetISteamUGC( hSteamUser, hSteamPipe, STEAMUGC_INTERFACE_VERSION );
  177. if ( !m_pSteamUGC )
  178. return false;
  179. m_pSteamApps = m_pSteamClient->GetISteamApps( hSteamUser, hSteamPipe, STEAMAPPS_INTERFACE_VERSION );
  180. if ( !m_pSteamApps )
  181. return false;
  182. return true;
  183. }
  184. inline bool SteamGameServer_Init( uint32 unIP, uint16 usSteamPort, uint16 usGamePort, uint16 usQueryPort, EServerMode eServerMode, const char *pchVersionString )
  185. {
  186. if ( !SteamInternal_GameServer_Init( unIP, usSteamPort, usGamePort, usQueryPort, eServerMode, pchVersionString ) )
  187. return false;
  188. return true;
  189. }
  190. inline void SteamGameServer_ReleaseCurrentThreadMemory()
  191. {
  192. SteamAPI_ReleaseCurrentThreadMemory();
  193. }
  194. #endif // STEAM_GAMESERVER_H