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.

308 lines
6.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // pingpong.cpp : Defines the entry point for the console application.
  9. //
  10. #include "stdafx.h"
  11. #include <assert.h>
  12. #include <stdlib.h>
  13. #include "tcpsocket.h"
  14. #include "tier0/fasttimer.h"
  15. #include "vmpi.h"
  16. #include "tcpsocket_helpers.h"
  17. //#define USE_MPI
  18. #if defined( USE_MPI )
  19. #include "mpi/mpi.h"
  20. #include "vmpi.h"
  21. #include "tier1/bitbuf.h"
  22. int myProcId = -1;
  23. #else
  24. IChannel *g_pSocket = NULL;
  25. int g_iPortNum = 27141;
  26. #endif
  27. int PrintUsage()
  28. {
  29. printf( "pingpong <-server or -client ip>\n" );
  30. return 1;
  31. }
  32. void DoClientConnect( const char *pIP )
  33. {
  34. #if defined( USE_MPI )
  35. int argc = 1;
  36. char *testargv[1] = { "-nounc" };
  37. char **argv = testargv;
  38. if ( MPI_Init( &argc, &argv ) )
  39. {
  40. assert( false );
  41. }
  42. MPI_Comm_rank( MPI_COMM_WORLD, &myProcId );
  43. int nProcs;
  44. MPI_Comm_size( MPI_COMM_WORLD, &nProcs );
  45. if ( nProcs != 2 )
  46. {
  47. assert( false );
  48. }
  49. #else
  50. // Try to connect, or listen.
  51. ITCPSocket *pTCPSocket = CreateTCPSocket();
  52. if ( !pTCPSocket->BindToAny( 0 ) )
  53. {
  54. assert( false );
  55. }
  56. CIPAddr addr;
  57. if ( !ConvertStringToIPAddr( pIP, &addr ) )
  58. {
  59. assert( false );
  60. }
  61. addr.port = g_iPortNum;
  62. printf( "Client connecting to %d.%d.%d.%d:%d\n", addr.ip[0], addr.ip[1], addr.ip[2], addr.ip[3], addr.port );
  63. if ( !TCPSocket_Connect( pTCPSocket, &addr, 50000 ) )
  64. {
  65. assert( false );
  66. }
  67. printf( "Client connected...\n ");
  68. g_pSocket = pTCPSocket;
  69. #endif
  70. }
  71. void DoServerConnect()
  72. {
  73. #if defined( USE_MPI )
  74. ISocket *pSocket = CreateIPSocket();
  75. if ( !pSocket )
  76. {
  77. printf( "Error creating a socket.\n" );
  78. assert( false );
  79. return;
  80. }
  81. else if ( !pSocket->BindToAny( VMPI_SERVICE_PORT ) )
  82. {
  83. printf( "Error binding a socket to port %d.\n", VMPI_SERVICE_PORT );
  84. assert( false );
  85. return;
  86. }
  87. printf( "Waiting for jobs...\n" );
  88. while ( 1 )
  89. {
  90. // Any incoming packets?
  91. char data[2048];
  92. CIPAddr ipFrom;
  93. int len = pSocket->RecvFrom( data, sizeof( data ), &ipFrom );
  94. if ( len > 3 )
  95. {
  96. bf_read buf( data, len );
  97. if ( buf.ReadByte() == VMPI_PROTOCOL_VERSION )
  98. {
  99. if ( buf.ReadByte() == VMPI_LOOKING_FOR_WORKERS )
  100. {
  101. // Read the listen port.
  102. int iListenPort = buf.ReadLong();
  103. static char ipString[128];
  104. _snprintf( ipString, sizeof( ipString ), "%d.%d.%d.%d:%d", ipFrom.ip[0], ipFrom.ip[1], ipFrom.ip[2], ipFrom.ip[3], iListenPort );
  105. int argc = 3;
  106. char *testargv[3];
  107. testargv[0] = "<supposedly the executable name!>";
  108. testargv[1] = "-mpi_worker";
  109. testargv[2] = ipString;
  110. char **argv = testargv;
  111. if ( MPI_Init( &argc, &argv ) )
  112. {
  113. assert( false );
  114. }
  115. MPI_Comm_rank( MPI_COMM_WORLD, &myProcId );
  116. int nProcs;
  117. MPI_Comm_size( MPI_COMM_WORLD, &nProcs );
  118. if ( nProcs != 2 )
  119. {
  120. assert( false );
  121. }
  122. break;
  123. }
  124. }
  125. }
  126. Sleep( 100 );
  127. }
  128. pSocket->Release();
  129. #else
  130. // Try to connect, or listen.
  131. ITCPListenSocket *pListen = CreateTCPListenSocket( g_iPortNum );
  132. if ( !pListen )
  133. {
  134. assert( false );
  135. }
  136. printf( "Server listening...\n" );
  137. CIPAddr addr;
  138. ITCPSocket *pTCPSocket = TCPSocket_ListenForOneConnection( pListen, &addr, 50000 );
  139. if ( !pTCPSocket )
  140. {
  141. assert( false );
  142. }
  143. pListen->Release();
  144. printf( "Server connected...\n ");
  145. g_pSocket = pTCPSocket;
  146. #endif
  147. }
  148. void SendData( const void *pBuf, int size )
  149. {
  150. #if defined( USE_MPI )
  151. MPI_Send( (void*)pBuf, size, MPI_BYTE, !myProcId, 0, MPI_COMM_WORLD );
  152. #else
  153. g_pSocket->Send( pBuf, size );
  154. #endif
  155. }
  156. void RecvData( CUtlVector<unsigned char> &recvBuf )
  157. {
  158. #if defined( USE_MPI )
  159. MPI_Status stat;
  160. MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
  161. recvBuf.SetCount( stat.count );
  162. MPI_Recv( recvBuf.Base(), stat.count, MPI_BYTE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
  163. #else
  164. if ( !g_pSocket->Recv( recvBuf, 50000 ) )
  165. {
  166. g_pSocket->Release();
  167. g_pSocket = NULL;
  168. }
  169. #endif
  170. }
  171. int main( int argc, char* argv[] )
  172. {
  173. if ( argc < 2 )
  174. {
  175. return PrintUsage();
  176. }
  177. const char *pClientOrServer = argv[1];
  178. const char *pIP = NULL;
  179. bool bClient = false;
  180. if ( stricmp( pClientOrServer, "-client" ) == 0 )
  181. {
  182. if ( argc < 3 )
  183. {
  184. return PrintUsage();
  185. }
  186. bClient = true;
  187. pIP = argv[2];
  188. }
  189. CUtlVector<unsigned char> recvBuf;
  190. if ( bClient )
  191. {
  192. DoClientConnect( pIP );
  193. // Ok, now start blasting packets of different sizes and measure how long it takes to get an ack back.
  194. int nIterations = 30;
  195. for ( int size=350; size <= 350000; size += 512 )
  196. {
  197. CUtlVector<unsigned char> buf;
  198. buf.SetCount( size );
  199. double flTotalRoundTripTime = 0;
  200. CFastTimer throughputTimer;
  201. throughputTimer.Start();
  202. for ( int i=0; i < nIterations; i++ )
  203. {
  204. for ( int z=0; z < size; z++ )
  205. buf[z] = (char)rand();
  206. SendData( buf.Base(), buf.Count() );
  207. CFastTimer timer;
  208. timer.Start();
  209. RecvData( recvBuf );
  210. timer.End();
  211. // Make sure we got the same data back.
  212. assert( recvBuf.Count() == buf.Count() );
  213. for ( z=0; z < size; z++ )
  214. {
  215. assert( recvBuf[z] == buf[z] );
  216. }
  217. //if ( i % 100 == 0 )
  218. // printf( "%05d\n", i );
  219. printf( "%d\n", i );
  220. flTotalRoundTripTime += timer.GetDuration().GetMillisecondsF();
  221. }
  222. throughputTimer.End();
  223. double flTotalSeconds = throughputTimer.GetDuration().GetSeconds();
  224. double flAvgRoundTripTime = flTotalRoundTripTime / nIterations;
  225. printf( "%d: %.2f ms per roundtrip (%d bytes/sec) sec: %.2f megs: %.2f\n",
  226. size,
  227. flAvgRoundTripTime,
  228. (int)((size*nIterations)/flTotalSeconds),
  229. flTotalSeconds,
  230. (double)(size*nIterations) / (1024*1024) );
  231. }
  232. // Send an 'end' message to the server.
  233. int val = -1;
  234. SendData( &val, sizeof( val ) );
  235. }
  236. else
  237. {
  238. // Wait for a connection.
  239. DoServerConnect();
  240. // Wait for packets and ack them.
  241. while ( 1 )
  242. {
  243. RecvData( recvBuf );
  244. if ( !g_pSocket )
  245. break;
  246. if ( recvBuf.Count() < 4 )
  247. {
  248. assert( false );
  249. }
  250. SendData( recvBuf.Base(), recvBuf.Count() );
  251. }
  252. }
  253. return 0;
  254. }