Team Fortress 2 Source Code as on 22/4/2020
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.

164 lines
2.9 KiB

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