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.

296 lines
7.0 KiB

  1. //====== Copyright � 1996-2009, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <sys/stat.h>
  11. #include "net_support.h"
  12. #ifndef DEDICATED
  13. #include "client.h"
  14. #endif
  15. #include "server.h"
  16. #include "host_cmd.h"
  17. #include "net.h"
  18. #include "proto_oob.h"
  19. #include "steamdatagram/isteamdatagramclient.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. CNetSupportImpl::CNetSupportImpl()
  23. {
  24. ;
  25. }
  26. CNetSupportImpl::~CNetSupportImpl()
  27. {
  28. ;
  29. }
  30. static CNetSupportImpl g_NetSupport;
  31. EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CNetSupportImpl, INetSupport,
  32. INETSUPPORT_VERSION_STRING, g_NetSupport );
  33. //
  34. // IAppSystem implementation
  35. //
  36. static CreateInterfaceFn s_pfnDelegateFactory;
  37. static void * InternalFactory( const char *pName, int *pReturnCode )
  38. {
  39. if ( pReturnCode )
  40. {
  41. *pReturnCode = IFACE_OK;
  42. }
  43. // Try to get interface via delegate
  44. if ( void *pInterface = s_pfnDelegateFactory ? s_pfnDelegateFactory( pName, pReturnCode ) : NULL )
  45. {
  46. return pInterface;
  47. }
  48. // Try to get internal interface
  49. if ( void *pInterface = Sys_GetFactoryThis()( pName, pReturnCode ) )
  50. {
  51. return pInterface;
  52. }
  53. // Failed
  54. if ( pReturnCode )
  55. {
  56. *pReturnCode = IFACE_FAILED;
  57. }
  58. return NULL;
  59. }
  60. bool CNetSupportImpl::Connect( CreateInterfaceFn factory )
  61. {
  62. Assert( !s_pfnDelegateFactory );
  63. s_pfnDelegateFactory = factory;
  64. CreateInterfaceFn ourFactory = InternalFactory;
  65. ConnectTier1Libraries( &ourFactory, 1 );
  66. ConnectTier2Libraries( &ourFactory, 1 );
  67. s_pfnDelegateFactory = NULL;
  68. return true;
  69. }
  70. void CNetSupportImpl::Disconnect()
  71. {
  72. DisconnectTier2Libraries();
  73. DisconnectTier1Libraries();
  74. }
  75. void * CNetSupportImpl::QueryInterface( const char *pInterfaceName )
  76. {
  77. if ( !Q_stricmp( pInterfaceName, INETSUPPORT_VERSION_STRING ) )
  78. return static_cast< INetSupport * >( this );
  79. return NULL;
  80. }
  81. InitReturnVal_t CNetSupportImpl::Init()
  82. {
  83. return INIT_OK;
  84. }
  85. void CNetSupportImpl::Shutdown()
  86. {
  87. return;
  88. }
  89. //
  90. // Net support implementation
  91. //
  92. int CNetSupportImpl::GetEngineBuildNumber()
  93. {
  94. return GetHostVersion();
  95. }
  96. void CNetSupportImpl::GetServerInfo( ServerInfo_t *pServerInfo )
  97. {
  98. if ( !pServerInfo )
  99. return;
  100. memset( pServerInfo, 0, sizeof( *pServerInfo ) );
  101. extern netadr_t net_local_adr;
  102. extern ConVar sv_steamgroup_exclusive;
  103. pServerInfo->m_netAdr = net_local_adr;
  104. static bool s_bHideHostIP = !!CommandLine()->FindParm( "-hidehostip" );
  105. if ( !NET_GetPublicAdr( pServerInfo->m_netAdrOnline ) )
  106. {
  107. if ( s_bHideHostIP )
  108. {
  109. static netadr_t s_netAdrLocalhost( "127.0.0.1" );
  110. pServerInfo->m_netAdr.SetIP( s_netAdrLocalhost.GetIPHostByteOrder() );
  111. }
  112. pServerInfo->m_netAdrOnline = pServerInfo->m_netAdr;
  113. }
  114. else if ( s_bHideHostIP )
  115. {
  116. pServerInfo->m_netAdr = pServerInfo->m_netAdrOnline;
  117. }
  118. pServerInfo->m_nPort = sv.GetUDPPort();
  119. pServerInfo->m_netAdr.SetPort( pServerInfo->m_nPort );
  120. pServerInfo->m_netAdrOnline.SetPort( pServerInfo->m_nPort );
  121. pServerInfo->m_bActive = sv.IsActive() || sv.IsLoading();
  122. pServerInfo->m_bDedicated = sv.IsDedicated();
  123. pServerInfo->m_bLobbyExclusive = sv.IsExclusiveToLobbyConnections();
  124. pServerInfo->m_bGroupExclusive = sv_steamgroup_exclusive.GetBool() || sv.GetPassword();
  125. pServerInfo->m_bInMainMenuBkgnd = sv.IsLevelMainMenuBackground();
  126. pServerInfo->m_szServerName = sv.GetName();
  127. pServerInfo->m_szMapName = sv.GetMapName();
  128. pServerInfo->m_szMapGroupName = sv.GetMapGroupName();
  129. pServerInfo->m_numMaxHumanPlayers = sv.GetMaxHumanPlayers();
  130. pServerInfo->m_numHumanPlayers = sv.GetNumHumanPlayers();
  131. }
  132. void CNetSupportImpl::GetClientInfo( ClientInfo_t *pClientInfo )
  133. {
  134. #ifndef DEDICATED
  135. if ( !pClientInfo )
  136. return;
  137. memset( pClientInfo, 0, sizeof( *pClientInfo ) );
  138. #ifndef DEDICATED
  139. CClientState &rcs = GetBaseLocalClient();
  140. pClientInfo->m_nSignonState = rcs.m_nSignonState;
  141. pClientInfo->m_nSocket = rcs.m_Socket;
  142. pClientInfo->m_pNetChannel = rcs.m_NetChannel;
  143. #endif
  144. // Compute number of human players on the server
  145. pClientInfo->m_numHumanPlayers = 0;
  146. if ( rcs.IsConnected() && rcs.m_pUserInfoTable )
  147. {
  148. for ( int i = 0; i < rcs.m_nMaxClients; i++ )
  149. {
  150. extern IVEngineClient *engineClient;
  151. player_info_t pi;
  152. if ( engineClient->GetPlayerInfo( i + 1, &pi ) &&
  153. !pi.fakeplayer &&
  154. !pi.ishltv )
  155. {
  156. ++ pClientInfo->m_numHumanPlayers; // This is an actual human player
  157. }
  158. }
  159. }
  160. #endif
  161. }
  162. void CNetSupportImpl::UpdateClientReservation( uint64 uiReservation, uint64 uiMachineIdHost )
  163. {
  164. #ifndef DEDICATED
  165. GetBaseLocalClient().SetServerReservationCookie( uiReservation );
  166. GetBaseLocalClient().m_ListenServerSteamID = uiMachineIdHost;
  167. #endif
  168. }
  169. void CNetSupportImpl::UpdateServerReservation( uint64 uiReservation )
  170. {
  171. sv.SetReservationCookie( uiReservation, "netsupport" );
  172. }
  173. void CNetSupportImpl::ReserveServer(
  174. const ns_address &netAdrPublic, const ns_address &netAdrPrivate, uint64 nServerReservationCookie,
  175. KeyValues *pKVGameSettings,
  176. IMatchAsyncOperationCallback *pCallback, IMatchAsyncOperation **ppAsyncOperation )
  177. {
  178. #ifndef DEDICATED
  179. GetBaseLocalClient().ReserveServer( netAdrPublic, netAdrPrivate,
  180. nServerReservationCookie, pKVGameSettings,
  181. pCallback, ppAsyncOperation );
  182. #else
  183. Assert( 0 );
  184. #endif
  185. }
  186. bool CNetSupportImpl::CheckServerReservation(
  187. const ns_address &netAdrPublic, uint64 nServerReservationCookie, uint32 uiReservationStage,
  188. IMatchAsyncOperationCallback *pCallback, IMatchAsyncOperation **ppAsyncOperation )
  189. {
  190. #ifndef DEDICATED
  191. return GetBaseLocalClient().CheckServerReservation( netAdrPublic,
  192. nServerReservationCookie, uiReservationStage, pCallback, ppAsyncOperation );
  193. #else
  194. Assert( 0 );
  195. return false;
  196. #endif
  197. }
  198. bool CNetSupportImpl::ServerPing( const ns_address &netAdrPublic,
  199. IMatchAsyncOperationCallback *pCallback, IMatchAsyncOperation **ppAsyncOperation )
  200. {
  201. #ifndef DEDICATED
  202. return GetBaseLocalClient().ServerPing( netAdrPublic, pCallback, ppAsyncOperation );
  203. #else
  204. Assert( 0 );
  205. return false;
  206. #endif
  207. }
  208. // When client event is fired
  209. void CNetSupportImpl::OnMatchEvent( KeyValues *pEvent )
  210. {
  211. #ifndef DEDICATED
  212. GetBaseLocalClient().OnEvent( pEvent );
  213. #endif
  214. }
  215. void CNetSupportImpl::ProcessSocket( int sock, IConnectionlessPacketHandler * pHandler )
  216. {
  217. NET_ProcessSocket( sock, pHandler );
  218. }
  219. // Send a network packet
  220. int CNetSupportImpl::SendPacket (
  221. INetChannel *chan, int sock, const netadr_t &to,
  222. const void *data, int length,
  223. bf_write *pVoicePayload /*= NULL*/,
  224. bool bUseCompression /*= false*/ )
  225. {
  226. netadr_t inetAddr( to );
  227. if ( !inetAddr.GetPort() && inetAddr.GetType() == NA_BROADCAST )
  228. {
  229. inetAddr.SetPort(
  230. #ifdef _X360
  231. NET_GetUDPPort( NS_X360_SYSTEMLINK )
  232. #else
  233. PORT_SERVER
  234. #endif
  235. );
  236. }
  237. return NET_SendPacket( chan, sock, inetAddr, ( const unsigned char * ) data, length, pVoicePayload, bUseCompression );
  238. }
  239. ISteamNetworkingUtils *CNetSupportImpl::GetSteamNetworkingUtils()
  240. {
  241. #ifdef DEDICATED
  242. Assert( false ); // why are we asking?
  243. return nullptr;
  244. #else
  245. return ::SteamNetworkingUtils();
  246. #endif
  247. }