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.

166 lines
3.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // socket_tests.cpp : Defines the entry point for the console application.
  9. //
  10. #include "stdafx.h"
  11. #include <stdlib.h>
  12. #include "iphelpers.h"
  13. #include "tcpsocket.h"
  14. #include "utlvector.h"
  15. #include "fragment_channel.h"
  16. #include "reliable_channel.h"
  17. #include "tier0/fasttimer.h"
  18. #if defined( _DEBUG )
  19. #if defined( assert )
  20. #undef assert
  21. #endif
  22. #define assert(x) if ( !x ) __asm int 3;
  23. #else
  24. #define assert(x)
  25. #endif
  26. bool CompareArrays( const CUtlVector<unsigned char> &a1, const CUtlVector<unsigned char> &a2 )
  27. {
  28. if ( a1.Count() != a2.Count() )
  29. return false;
  30. for ( int i=0; i < a1.Count(); i++ )
  31. {
  32. if ( a1[i] != a2[i] )
  33. return false;
  34. }
  35. return true;
  36. }
  37. // Test two reliable channels that are hooked up to each other.
  38. void TestChannels( IChannel *pChannel1, IChannel *pChannel2, int maxPacketSize, int nTests )
  39. {
  40. for ( int iTest=0; iTest < nTests; iTest++ )
  41. {
  42. float t = (float)rand() / VALVE_RAND_MAX;
  43. int testSize = (int)( t * (maxPacketSize-1) ) + 1;
  44. CUtlVector<unsigned char> rnd1, rnd2;
  45. rnd1.SetSize( testSize );
  46. rnd2.SetSize( testSize );
  47. for ( int i=0; i < testSize; i++ )
  48. {
  49. rnd1[i] = rand();
  50. rnd2[i] = rand();
  51. }
  52. pChannel1->Send( rnd1.Base(), testSize );
  53. pChannel2->Send( rnd2.Base(), testSize );
  54. // Now wait for up to 5 seconds for the data to come in.
  55. CUtlVector<unsigned char> tmp;
  56. tmp.SetSize( testSize );
  57. CUtlVector<unsigned char> testVec;
  58. bool bReceived;
  59. if ( !( bReceived = pChannel1->Recv( testVec, 15 ) ) || !CompareArrays( testVec, rnd2 ) )
  60. {
  61. assert( false );
  62. }
  63. if ( !( bReceived = pChannel2->Recv( testVec, 15 ) ) || !CompareArrays( testVec, rnd1 ) )
  64. {
  65. assert( false );
  66. }
  67. }
  68. }
  69. template<class T>
  70. void TestChannels( T *pSock[2] )
  71. {
  72. int iPorts[2];
  73. for ( int iPort=0; iPort < 2; iPort++ )
  74. {
  75. int nTries = 150;
  76. for ( int iTry=0; iTry < nTries; iTry++ )
  77. {
  78. iPorts[iPort] = 27111 + iTry;
  79. if ( pSock[iPort]->BindToAny( iPorts[iPort] ) )
  80. break;
  81. }
  82. }
  83. // Bind them to random ports.
  84. pSock[0]->BeginListen();
  85. pSock[1]->BeginConnect( CIPAddr( 127, 0, 0, 1, iPorts[0] ) );
  86. while ( !pSock[0]->IsConnected() || !pSock[1]->IsConnected() )
  87. {
  88. CIPAddr remoteAddr;
  89. if ( !pSock[0]->IsConnected() )
  90. pSock[0]->UpdateListen( &remoteAddr );
  91. if ( !pSock[1]->IsConnected() )
  92. pSock[1]->UpdateConnect();
  93. }
  94. // Measure ping-pong time.
  95. __int64 totalMicroseconds = 0;
  96. int nTests = 1500;
  97. for ( int i=0; i < nTests; i++ )
  98. {
  99. char buf[2116];
  100. CFastTimer timer;
  101. timer.Start();
  102. pSock[0]->Send( buf, sizeof( buf ) );
  103. CUtlVector<unsigned char> recvBuf;
  104. pSock[1]->Recv( recvBuf );
  105. timer.End();
  106. totalMicroseconds += timer.GetDuration().GetMicroseconds();
  107. }
  108. // Now, test them with the fragmentation layer.
  109. IChannel *pFrag[2] = { CreateFragmentLayer( pSock[0] ), CreateFragmentLayer( pSock[1] ) };
  110. TestChannels( pFrag[0], pFrag[1], 1024*300, 5 );
  111. TestChannels( pSock[0], pSock[1], 1024, 1000 );
  112. }
  113. int main(int argc, char* argv[])
  114. {
  115. // First, test two TCP sockets.
  116. for ( int iChannelType=0; iChannelType < 2; iChannelType++ )
  117. {
  118. DWORD startTime = GetTickCount();
  119. srand( 0 );
  120. if ( iChannelType == 0 )
  121. {
  122. ITCPSocket *pTCPSockets[2] = { CreateTCPSocket(), CreateTCPSocket() };
  123. TestChannels( pTCPSockets );
  124. }
  125. else
  126. {
  127. IReliableChannel *pReliableChannels[2] = { CreateReliableChannel(), CreateReliableChannel() };
  128. TestChannels( pReliableChannels );
  129. }
  130. float flElapsed = (float)( GetTickCount() - startTime ) / 1000.0;
  131. }
  132. return 0;
  133. }