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.

169 lines
3.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // hltvtest.cpp: implementation of the CHLTVTestSystem class.
  9. //
  10. //////////////////////////////////////////////////////////////////////
  11. #include "basetypes.h"
  12. #include "cmd.h"
  13. #include "convar.h"
  14. #include "hltvtest.h"
  15. #include "hltvserver.h"
  16. #include "host.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. CHLTVTestSystem *hltvtest = NULL;
  20. CHLTVTestSystem::CHLTVTestSystem(void)
  21. {
  22. }
  23. CHLTVTestSystem::~CHLTVTestSystem(void)
  24. {
  25. StopsTest();
  26. }
  27. void CHLTVTestSystem::RunFrame()
  28. {
  29. FOR_EACH_VEC( m_Servers, iServer )
  30. {
  31. m_Servers[iServer]->RunFrame();
  32. }
  33. }
  34. bool CHLTVTestSystem::StartTest(int nClients, const char *pszAddress)
  35. {
  36. Assert( m_Servers.Count() == 0 );
  37. while ( m_Servers.Count() < nClients )
  38. {
  39. extern ConVar tv_snapshotrate;
  40. CHLTVServer *pServer = new CHLTVServer(0, tv_snapshotrate.GetFloat() );
  41. m_Servers.AddToTail( pServer );
  42. pServer->Init( NET_IsDedicated() );
  43. pServer->m_ClientState.m_Socket = NET_AddExtraSocket( PORT_ANY );
  44. }
  45. for( int i=0; i<nClients; i++ )
  46. {
  47. m_Servers[i]->ConnectRelay( pszAddress );
  48. }
  49. return true;
  50. }
  51. void CHLTVTestSystem::RetryTest(int nClients)
  52. {
  53. int maxClients = MIN( nClients+1, m_Servers.Count() );
  54. for ( int i=0; i<maxClients; i++ )
  55. {
  56. CHLTVServer *pHLTV = m_Servers[i];
  57. if ( pHLTV->m_ClientState.m_Remote.Count() > 0 )
  58. {
  59. pHLTV->ConnectRelay( pHLTV->m_ClientState.m_Remote.Get( 0 ).m_szRetryAddress );
  60. }
  61. }
  62. }
  63. bool CHLTVTestSystem::StopsTest()
  64. {
  65. FOR_EACH_VEC( m_Servers, iServer )
  66. {
  67. m_Servers[iServer]->Shutdown();
  68. }
  69. m_Servers.PurgeAndDeleteElements();
  70. NET_RemoveAllExtraSockets();
  71. return true;
  72. }
  73. #ifdef _HLTVTEST
  74. CON_COMMAND( tv_test_start, "Starts the GOTV test system" )
  75. {
  76. if ( args.ArgC() < 3 )
  77. {
  78. Msg( "Usage: tv_test_start <number> <ip:port>\n" );
  79. return;
  80. }
  81. int nClients = Q_atoi( args[1] );
  82. char address[MAX_PATH];
  83. if ( args.ArgC() == 3 )
  84. {
  85. Q_strncpy( address, args[2], MAX_PATH );
  86. }
  87. else
  88. {
  89. Q_snprintf( address, MAX_PATH, "%s:%s", args[2], args[4] );
  90. }
  91. // If it's not a single player connection to "localhost", initialize networking & stop listenserver
  92. if ( StringHasPrefixCaseSensitive( address, "localhost" ) )
  93. {
  94. Msg( "GOTV test can't connect to localhost.\n" );
  95. return;
  96. }
  97. if ( !hltvtest )
  98. {
  99. // create new HLTV test system
  100. hltvtest = new CHLTVTestSystem();
  101. }
  102. else
  103. {
  104. // stop old test
  105. hltvtest->StopsTest();
  106. }
  107. // shutdown anything else
  108. Host_Disconnect( false );
  109. // start networking
  110. NET_SetMultiplayer( true );
  111. hltvtest->StartTest( nClients, address );
  112. }
  113. CON_COMMAND( tv_test_retry, "Tell test clients to reconnect" )
  114. {
  115. if ( args.ArgC() < 2 )
  116. {
  117. Msg( "Usage: tv_test_retry <number>\n" );
  118. return;
  119. }
  120. int nClients = Q_atoi( args[1] );
  121. if ( hltvtest )
  122. {
  123. hltvtest->RetryTest( nClients );
  124. }
  125. else
  126. {
  127. Msg("No test running.\n");
  128. }
  129. }
  130. CON_COMMAND( tv_test_stop, "Stops the GOTV test system" )
  131. {
  132. if ( hltvtest )
  133. {
  134. hltvtest->StopsTest();
  135. }
  136. }
  137. #endif