mirror of
https://github.com/nillerusr/source-engine.git
synced 2026-08-14 12:46:58 +00:00
1
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
//========= Copyright Valve Corporation, All rights reserved. ============//
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
// $NoKeywords: $
|
||||
//
|
||||
//=============================================================================//
|
||||
// stdafx.cpp : source file that includes just the standard includes
|
||||
// pingpong.pch will be the pre-compiled header
|
||||
// stdafx.obj will contain the pre-compiled type information
|
||||
|
||||
#include "stdafx.h"
|
||||
|
||||
// TODO: reference any additional headers you need in STDAFX.H
|
||||
// and not in this file
|
||||
@@ -0,0 +1,29 @@
|
||||
//========= Copyright Valve Corporation, All rights reserved. ============//
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
// $NoKeywords: $
|
||||
//
|
||||
//=============================================================================//
|
||||
// stdafx.h : include file for standard system include files,
|
||||
// or project specific include files that are used frequently, but
|
||||
// are changed infrequently
|
||||
//
|
||||
|
||||
#if !defined(AFX_STDAFX_H__13A3CFA6_6BF8_45A8_A2CD_91444DCFF7C0__INCLUDED_)
|
||||
#define AFX_STDAFX_H__13A3CFA6_6BF8_45A8_A2CD_91444DCFF7C0__INCLUDED_
|
||||
|
||||
#if _MSC_VER > 1000
|
||||
#pragma once
|
||||
#endif // _MSC_VER > 1000
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
// TODO: reference additional headers your program requires here
|
||||
|
||||
//{{AFX_INSERT_LOCATION}}
|
||||
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
|
||||
|
||||
#endif // !defined(AFX_STDAFX_H__13A3CFA6_6BF8_45A8_A2CD_91444DCFF7C0__INCLUDED_)
|
||||
@@ -0,0 +1,308 @@
|
||||
//========= Copyright Valve Corporation, All rights reserved. ============//
|
||||
//
|
||||
// Purpose:
|
||||
//
|
||||
// $NoKeywords: $
|
||||
//
|
||||
//=============================================================================//
|
||||
// pingpong.cpp : Defines the entry point for the console application.
|
||||
//
|
||||
|
||||
#include "stdafx.h"
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include "tcpsocket.h"
|
||||
#include "tier0/fasttimer.h"
|
||||
#include "vmpi.h"
|
||||
#include "tcpsocket_helpers.h"
|
||||
|
||||
//#define USE_MPI
|
||||
|
||||
|
||||
#if defined( USE_MPI )
|
||||
#include "mpi/mpi.h"
|
||||
#include "vmpi.h"
|
||||
#include "tier1/bitbuf.h"
|
||||
|
||||
int myProcId = -1;
|
||||
#else
|
||||
IChannel *g_pSocket = NULL;
|
||||
int g_iPortNum = 27141;
|
||||
#endif
|
||||
|
||||
|
||||
int PrintUsage()
|
||||
{
|
||||
printf( "pingpong <-server or -client ip>\n" );
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void DoClientConnect( const char *pIP )
|
||||
{
|
||||
#if defined( USE_MPI )
|
||||
int argc = 1;
|
||||
char *testargv[1] = { "-nounc" };
|
||||
char **argv = testargv;
|
||||
if ( MPI_Init( &argc, &argv ) )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
MPI_Comm_rank( MPI_COMM_WORLD, &myProcId );
|
||||
|
||||
int nProcs;
|
||||
MPI_Comm_size( MPI_COMM_WORLD, &nProcs );
|
||||
if ( nProcs != 2 )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
#else
|
||||
// Try to connect, or listen.
|
||||
ITCPSocket *pTCPSocket = CreateTCPSocket();
|
||||
if ( !pTCPSocket->BindToAny( 0 ) )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
|
||||
CIPAddr addr;
|
||||
if ( !ConvertStringToIPAddr( pIP, &addr ) )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
|
||||
addr.port = g_iPortNum;
|
||||
printf( "Client connecting to %d.%d.%d.%d:%d\n", addr.ip[0], addr.ip[1], addr.ip[2], addr.ip[3], addr.port );
|
||||
if ( !TCPSocket_Connect( pTCPSocket, &addr, 50000 ) )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
|
||||
printf( "Client connected...\n ");
|
||||
g_pSocket = pTCPSocket;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void DoServerConnect()
|
||||
{
|
||||
#if defined( USE_MPI )
|
||||
ISocket *pSocket = CreateIPSocket();
|
||||
if ( !pSocket )
|
||||
{
|
||||
printf( "Error creating a socket.\n" );
|
||||
assert( false );
|
||||
return;
|
||||
}
|
||||
else if ( !pSocket->BindToAny( VMPI_SERVICE_PORT ) )
|
||||
{
|
||||
printf( "Error binding a socket to port %d.\n", VMPI_SERVICE_PORT );
|
||||
assert( false );
|
||||
return;
|
||||
}
|
||||
|
||||
printf( "Waiting for jobs...\n" );
|
||||
while ( 1 )
|
||||
{
|
||||
// Any incoming packets?
|
||||
char data[2048];
|
||||
CIPAddr ipFrom;
|
||||
int len = pSocket->RecvFrom( data, sizeof( data ), &ipFrom );
|
||||
if ( len > 3 )
|
||||
{
|
||||
bf_read buf( data, len );
|
||||
if ( buf.ReadByte() == VMPI_PROTOCOL_VERSION )
|
||||
{
|
||||
if ( buf.ReadByte() == VMPI_LOOKING_FOR_WORKERS )
|
||||
{
|
||||
// Read the listen port.
|
||||
int iListenPort = buf.ReadLong();
|
||||
|
||||
static char ipString[128];
|
||||
_snprintf( ipString, sizeof( ipString ), "%d.%d.%d.%d:%d", ipFrom.ip[0], ipFrom.ip[1], ipFrom.ip[2], ipFrom.ip[3], iListenPort );
|
||||
|
||||
int argc = 3;
|
||||
char *testargv[3];
|
||||
testargv[0] = "<supposedly the executable name!>";
|
||||
testargv[1] = "-mpi_worker";
|
||||
testargv[2] = ipString;
|
||||
|
||||
char **argv = testargv;
|
||||
if ( MPI_Init( &argc, &argv ) )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
MPI_Comm_rank( MPI_COMM_WORLD, &myProcId );
|
||||
|
||||
int nProcs;
|
||||
MPI_Comm_size( MPI_COMM_WORLD, &nProcs );
|
||||
if ( nProcs != 2 )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Sleep( 100 );
|
||||
}
|
||||
|
||||
pSocket->Release();
|
||||
#else
|
||||
// Try to connect, or listen.
|
||||
ITCPListenSocket *pListen = CreateTCPListenSocket( g_iPortNum );
|
||||
if ( !pListen )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
|
||||
printf( "Server listening...\n" );
|
||||
|
||||
CIPAddr addr;
|
||||
ITCPSocket *pTCPSocket = TCPSocket_ListenForOneConnection( pListen, &addr, 50000 );
|
||||
if ( !pTCPSocket )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
pListen->Release();
|
||||
|
||||
printf( "Server connected...\n ");
|
||||
g_pSocket = pTCPSocket;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void SendData( const void *pBuf, int size )
|
||||
{
|
||||
#if defined( USE_MPI )
|
||||
MPI_Send( (void*)pBuf, size, MPI_BYTE, !myProcId, 0, MPI_COMM_WORLD );
|
||||
#else
|
||||
g_pSocket->Send( pBuf, size );
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void RecvData( CUtlVector<unsigned char> &recvBuf )
|
||||
{
|
||||
#if defined( USE_MPI )
|
||||
MPI_Status stat;
|
||||
MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
|
||||
|
||||
recvBuf.SetCount( stat.count );
|
||||
MPI_Recv( recvBuf.Base(), stat.count, MPI_BYTE, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &stat);
|
||||
#else
|
||||
if ( !g_pSocket->Recv( recvBuf, 50000 ) )
|
||||
{
|
||||
g_pSocket->Release();
|
||||
g_pSocket = NULL;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
if ( argc < 2 )
|
||||
{
|
||||
return PrintUsage();
|
||||
}
|
||||
|
||||
const char *pClientOrServer = argv[1];
|
||||
const char *pIP = NULL;
|
||||
|
||||
bool bClient = false;
|
||||
if ( stricmp( pClientOrServer, "-client" ) == 0 )
|
||||
{
|
||||
if ( argc < 3 )
|
||||
{
|
||||
return PrintUsage();
|
||||
}
|
||||
|
||||
bClient = true;
|
||||
pIP = argv[2];
|
||||
}
|
||||
|
||||
CUtlVector<unsigned char> recvBuf;
|
||||
if ( bClient )
|
||||
{
|
||||
DoClientConnect( pIP );
|
||||
|
||||
// Ok, now start blasting packets of different sizes and measure how long it takes to get an ack back.
|
||||
int nIterations = 30;
|
||||
|
||||
for ( int size=350; size <= 350000; size += 512 )
|
||||
{
|
||||
CUtlVector<unsigned char> buf;
|
||||
buf.SetCount( size );
|
||||
|
||||
double flTotalRoundTripTime = 0;
|
||||
|
||||
CFastTimer throughputTimer;
|
||||
throughputTimer.Start();
|
||||
|
||||
for ( int i=0; i < nIterations; i++ )
|
||||
{
|
||||
for ( int z=0; z < size; z++ )
|
||||
buf[z] = (char)rand();
|
||||
|
||||
SendData( buf.Base(), buf.Count() );
|
||||
|
||||
CFastTimer timer;
|
||||
timer.Start();
|
||||
RecvData( recvBuf );
|
||||
timer.End();
|
||||
|
||||
|
||||
// Make sure we got the same data back.
|
||||
assert( recvBuf.Count() == buf.Count() );
|
||||
for ( z=0; z < size; z++ )
|
||||
{
|
||||
assert( recvBuf[z] == buf[z] );
|
||||
}
|
||||
|
||||
|
||||
//if ( i % 100 == 0 )
|
||||
// printf( "%05d\n", i );
|
||||
printf( "%d\n", i );
|
||||
flTotalRoundTripTime += timer.GetDuration().GetMillisecondsF();
|
||||
}
|
||||
throughputTimer.End();
|
||||
double flTotalSeconds = throughputTimer.GetDuration().GetSeconds();
|
||||
|
||||
double flAvgRoundTripTime = flTotalRoundTripTime / nIterations;
|
||||
printf( "%d: %.2f ms per roundtrip (%d bytes/sec) sec: %.2f megs: %.2f\n",
|
||||
size,
|
||||
flAvgRoundTripTime,
|
||||
(int)((size*nIterations)/flTotalSeconds),
|
||||
flTotalSeconds,
|
||||
(double)(size*nIterations) / (1024*1024) );
|
||||
}
|
||||
|
||||
// Send an 'end' message to the server.
|
||||
int val = -1;
|
||||
SendData( &val, sizeof( val ) );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Wait for a connection.
|
||||
DoServerConnect();
|
||||
|
||||
// Wait for packets and ack them.
|
||||
while ( 1 )
|
||||
{
|
||||
RecvData( recvBuf );
|
||||
if ( !g_pSocket )
|
||||
break;
|
||||
|
||||
if ( recvBuf.Count() < 4 )
|
||||
{
|
||||
assert( false );
|
||||
}
|
||||
|
||||
SendData( recvBuf.Base(), recvBuf.Count() );
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
<?xml version="1.0" encoding="Windows-1252"?>
|
||||
<VisualStudioProject
|
||||
ProjectType="Visual C++"
|
||||
Version="7.10"
|
||||
Name="pingpong"
|
||||
ProjectGUID="{5E114A75-BC7C-4E3A-895D-47C097AFF02B}"
|
||||
SccProjectName=""
|
||||
SccAuxPath=""
|
||||
SccLocalPath=""
|
||||
SccProvider="">
|
||||
<Platforms>
|
||||
<Platform
|
||||
Name="Win32"/>
|
||||
</Platforms>
|
||||
<Configurations>
|
||||
<Configuration
|
||||
Name="Release|Win32"
|
||||
OutputDirectory=".\Release"
|
||||
IntermediateDirectory=".\Release"
|
||||
ConfigurationType="1"
|
||||
UseOfMFC="0"
|
||||
ATLMinimizesCRunTimeLibraryUsage="FALSE"
|
||||
CharacterSet="2">
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="2"
|
||||
InlineFunctionExpansion="1"
|
||||
AdditionalIncludeDirectories="..\..\..\public,..\..\common,..\..\vmpi"
|
||||
PreprocessorDefinitions="NDEBUG;WIN32;_CONSOLE;PROTECTED_THINGS_DISABLE"
|
||||
StringPooling="TRUE"
|
||||
RuntimeLibrary="0"
|
||||
EnableFunctionLevelLinking="TRUE"
|
||||
PrecompiledHeaderFile=".\Release/pingpong.pch"
|
||||
AssemblerListingLocation=".\Release/"
|
||||
ObjectFile=".\Release/"
|
||||
ProgramDataBaseFileName=".\Release/"
|
||||
WarningLevel="4"
|
||||
SuppressStartupBanner="TRUE"
|
||||
DebugInformationFormat="3"
|
||||
CompileAs="0"/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
AdditionalDependencies="ws2_32.lib odbc32.lib odbccp32.lib"
|
||||
OutputFile=".\Release/pingpong.exe"
|
||||
LinkIncremental="1"
|
||||
SuppressStartupBanner="TRUE"
|
||||
IgnoreAllDefaultLibraries="FALSE"
|
||||
IgnoreDefaultLibraryNames="libcmtd.lib"
|
||||
GenerateDebugInformation="TRUE"
|
||||
ProgramDatabaseFile=".\Release/pingpong.pdb"
|
||||
SubSystem="1"
|
||||
TargetMachine="1"/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
TypeLibraryName=".\Release/pingpong.tlb"
|
||||
HeaderFileName=""/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
PreprocessorDefinitions="NDEBUG"
|
||||
Culture="1033"/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCWebDeploymentTool"/>
|
||||
<Tool
|
||||
Name="VCManagedWrapperGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
|
||||
</Configuration>
|
||||
<Configuration
|
||||
Name="Debug|Win32"
|
||||
OutputDirectory=".\Debug"
|
||||
IntermediateDirectory=".\Debug"
|
||||
ConfigurationType="1"
|
||||
UseOfMFC="0"
|
||||
ATLMinimizesCRunTimeLibraryUsage="FALSE"
|
||||
CharacterSet="2">
|
||||
<Tool
|
||||
Name="VCCLCompilerTool"
|
||||
Optimization="0"
|
||||
AdditionalIncludeDirectories="..\..\..\public,..\..\common,..\..\vmpi"
|
||||
PreprocessorDefinitions="_DEBUG;WIN32;_CONSOLE;PROTECTED_THINGS_DISABLE"
|
||||
BasicRuntimeChecks="3"
|
||||
RuntimeLibrary="1"
|
||||
PrecompiledHeaderFile=".\Debug/pingpong.pch"
|
||||
AssemblerListingLocation=".\Debug/"
|
||||
ObjectFile=".\Debug/"
|
||||
ProgramDataBaseFileName=".\Debug/"
|
||||
WarningLevel="4"
|
||||
SuppressStartupBanner="TRUE"
|
||||
DebugInformationFormat="4"
|
||||
CompileAs="0"/>
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"/>
|
||||
<Tool
|
||||
Name="VCLinkerTool"
|
||||
AdditionalDependencies="ws2_32.lib odbc32.lib odbccp32.lib"
|
||||
OutputFile=".\Debug/pingpong.exe"
|
||||
LinkIncremental="2"
|
||||
SuppressStartupBanner="TRUE"
|
||||
IgnoreDefaultLibraryNames="libcmt.lib"
|
||||
GenerateDebugInformation="TRUE"
|
||||
ProgramDatabaseFile=".\Debug/pingpong.pdb"
|
||||
SubSystem="1"
|
||||
TargetMachine="1"/>
|
||||
<Tool
|
||||
Name="VCMIDLTool"
|
||||
TypeLibraryName=".\Debug/pingpong.tlb"
|
||||
HeaderFileName=""/>
|
||||
<Tool
|
||||
Name="VCPostBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreBuildEventTool"/>
|
||||
<Tool
|
||||
Name="VCPreLinkEventTool"/>
|
||||
<Tool
|
||||
Name="VCResourceCompilerTool"
|
||||
PreprocessorDefinitions="_DEBUG"
|
||||
Culture="1033"/>
|
||||
<Tool
|
||||
Name="VCWebServiceProxyGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCXMLDataGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCWebDeploymentTool"/>
|
||||
<Tool
|
||||
Name="VCManagedWrapperGeneratorTool"/>
|
||||
<Tool
|
||||
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
|
||||
</Configuration>
|
||||
</Configurations>
|
||||
<References>
|
||||
</References>
|
||||
<Files>
|
||||
<Filter
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat">
|
||||
<File
|
||||
RelativePath="..\..\..\public\tier0\memoverride.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="pingpong.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="StdAfx.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\tcpsocket.cpp">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\tcpsocket_helpers.cpp">
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl">
|
||||
<File
|
||||
RelativePath="..\..\..\public\tier1\bitbuf.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\..\public\platform\fasttimer.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\common\ichannel.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\common\iphelpers.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\common\loopback_channel.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="StdAfx.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\common\tcpsocket.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\..\public\tier1\utlvector.h">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\common\vmpi.h">
|
||||
</File>
|
||||
</Filter>
|
||||
<Filter
|
||||
Name="Resource Files"
|
||||
Filter="ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe">
|
||||
</Filter>
|
||||
<File
|
||||
RelativePath="ReadMe.txt">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\..\lib\public\tier0.lib">
|
||||
<FileConfiguration
|
||||
Name="Release|Win32">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
Description=""
|
||||
CommandLine=""/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
Description=""
|
||||
CommandLine=""/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\..\lib\public\tier1.lib">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\..\lib\public\vmpi.lib">
|
||||
</File>
|
||||
<File
|
||||
RelativePath="..\..\..\lib\public\vstdlib.lib">
|
||||
<FileConfiguration
|
||||
Name="Release|Win32">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
Description=""
|
||||
CommandLine=""/>
|
||||
</FileConfiguration>
|
||||
<FileConfiguration
|
||||
Name="Debug|Win32">
|
||||
<Tool
|
||||
Name="VCCustomBuildTool"
|
||||
Description=""
|
||||
CommandLine=""/>
|
||||
</FileConfiguration>
|
||||
</File>
|
||||
</Files>
|
||||
<Globals>
|
||||
</Globals>
|
||||
</VisualStudioProject>
|
||||
Reference in New Issue
Block a user