2010-12-23 00:01:24 +00:00
/*
* OpenClonk , http : //www.openclonk.org
*
2013-12-17 20:01:09 +00:00
* Copyright ( c ) 2005 - 2006 , Peter Wortmann
* Copyright ( c ) 2005 , G ü nther Brammer
2016-04-03 18:18:29 +00:00
* Copyright ( c ) 2009 - 2016 , The OpenClonk Team and contributors
2013-12-17 20:01:09 +00:00
*
* Distributed under the terms of the ISC license ; see accompanying file
* " COPYING " for details .
*
* " Clonk " is a registered trademark of Matthes Bender , used with permission .
* See accompanying file " TRADEMARK " for details .
*
* To redistribute this file separately , substitute the full license texts
* for the above references .
*/
2009-05-08 13:28:41 +00:00
2016-04-03 18:07:56 +00:00
# include "C4Include.h"
# include "network/C4NetIO.h"
2016-04-20 19:20:58 +00:00
# include "lib/C4Random.h"
2009-05-08 13:28:41 +00:00
# ifdef _WIN32
2016-04-03 18:07:56 +00:00
# include "platform/C4windowswrapper.h"
2009-05-08 13:28:41 +00:00
# include <mmsystem.h>
# else
# include <arpa/inet.h>
# endif
using namespace std ;
bool fHost ;
int iCnt = 0 , iSize = 0 ;
char DummyData [ 1024 * 1024 ] ;
# define ASYNC_CONNECT
// TCP otherwise
# define USE_UDP
class MyCBClass : public C4NetIOMan
{
2013-12-07 14:27:01 +00:00
unsigned int tTime , iPcks ;
2009-05-08 13:28:41 +00:00
public :
virtual bool OnConn ( const C4NetIO : : addr_t & addr , const C4NetIO : : addr_t & addr2 , C4NetIO * pNetIO )
{
cout < < " got connection from " < < inet_ntoa ( addr . sin_addr ) < < endl ;
2013-12-07 14:27:01 +00:00
tTime = C4TimeMilliseconds : : Now ( ) ;
iPcks = 0 ;
2009-05-08 13:28:41 +00:00
# ifdef ASYNC_CONNECT
2010-03-28 18:58:01 +00:00
if ( ! fHost )
2009-05-08 13:28:41 +00:00
{
2010-03-27 16:05:02 +00:00
DummyData [ 0 ] = 0 ;
2010-03-28 18:58:01 +00:00
if ( ! pNetIO - > Send ( C4NetIOPacket ( DummyData , iSize , true , addr ) ) )
2009-05-08 13:28:41 +00:00
cout < < " Fehler: " < < ( pNetIO - > GetError ( ) ? pNetIO - > GetError ( ) : " bla " ) < < endl ;
}
# endif
return true ;
}
virtual void OnPacket ( const class C4NetIOPacket & rPacket , C4NetIO * pNetIO )
{
2013-12-07 14:27:01 +00:00
C4TimeMilliseconds tNow = C4TimeMilliseconds : : Now ( ) ;
if ( tNow > tTime + 1000 )
2010-03-28 18:58:01 +00:00
{
2013-12-07 14:27:01 +00:00
cout < < iPcks < < " packets in " < < tNow - tTime < < " ms ( " < < iPcks * 1000 / ( tNow - tTime ) < < " per second, " < < ( iPcks ? ( tNow - tTime ) * 1000 / iPcks : - 1u ) < < " us per packet) " < < endl ;
tTime = C4TimeMilliseconds : : Now ( ) ;
iPcks = 0 ;
2010-03-27 16:05:02 +00:00
}
2010-03-28 18:58:01 +00:00
if ( ! rPacket . getStatus ( ) )
2010-03-27 16:05:02 +00:00
{
// dummys
DummyData [ 0 ] = 1 ;
C4NetIOPacket Dummy ( DummyData , iSize , true , rPacket . getAddr ( ) ) ;
2010-03-28 18:58:01 +00:00
for ( int i = 0 ; i < iCnt ; i + + )
{
if ( ! pNetIO - > Send ( Dummy ) )
cout < < " Fehler: " < < ( pNetIO - > GetError ( ) ? pNetIO - > GetError ( ) : " bla " ) < < endl ;
2010-03-27 16:05:02 +00:00
}
// pong
pNetIO - > Send ( rPacket ) ;
iPcks + + ;
}
2009-05-08 13:28:41 +00:00
// cout << "got " << rPacket.GetSize() << " bytes of data (" << int(*rPacket.GetData()) << ")" << endl;
}
virtual void OnDisconn ( const C4NetIO : : addr_t & addr , C4NetIO * pNetIO , const char * szReason )
{
cout < < " client from " < < inet_ntoa ( addr . sin_addr ) < < " disconnected ( " < < szReason < < " ) " < < endl ;
}
2010-03-28 18:58:01 +00:00
virtual void OnError ( const char * strError , C4NetIO * pNetIO )
{
2009-05-08 13:28:41 +00:00
cout < < " network error: " < < strError < < endl ;
} ;
} ;
int main ( int argc , char * argv [ ] )
{
int i ;
2010-03-28 18:58:01 +00:00
for ( i = 0 ; i < sizeof ( DummyData ) ; i + + )
2009-05-08 13:28:41 +00:00
DummyData [ i ] = ' A ' + i % 100 ;
2016-11-02 23:58:02 +00:00
FixedRandom ( time ( nullptr ) ) ;
2009-05-08 13:28:41 +00:00
# ifdef USE_UDP
C4NetIOUDP NetIO ;
# else
C4NetIOTCP NetIO ;
# endif
C4NetIO * pNetIO = & NetIO ;
MyCBClass CBClass ;
CBClass . AddIO ( pNetIO ) ;
# ifdef HAVE_WINSOCK
WSADATA wsaData ;
WSAStartup ( 0x0202 , & wsaData ) ;
# endif
C4NetIO : : addr_t addr ;
// Default
addr . sin_addr . s_addr = inet_addr ( " 127.0.0.1 " ) ;
addr . sin_port = htons ( 11111 ) ;
addr . sin_family = AF_INET ;
short iPort = 0 ;
for ( i = 1 ; i < argc ; + + i )
{
std : : string arg ( argv [ i ] ) ;
int n ;
if ( arg = = " --server " )
{
fHost = true ;
if ( ! iPort ) iPort = 11111 ;
2010-03-28 18:58:01 +00:00
}
else if ( ( n = arg . find ( " --port= " ) ) ! = - 1 )
2009-05-08 13:28:41 +00:00
{
std : : istringstream stream ( std : : string ( arg . begin ( ) + n + sizeof ( " --port= " ) , arg . end ( ) ) ) ;
stream > > iPort ;
2010-03-28 18:58:01 +00:00
}
else if ( ( n = arg . find ( " --size= " ) ) ! = - 1 )
2009-05-08 13:28:41 +00:00
{
std : : istringstream stream ( std : : string ( arg . begin ( ) + n + sizeof ( " --size= " ) , arg . end ( ) ) ) ;
stream > > iSize ;
2010-03-28 18:58:01 +00:00
}
else
2009-05-08 13:28:41 +00:00
{
if ( ! ResolveAddress ( argv [ i ] , & addr , 11111 ) ) cout < < " Fehler in ResolveAddress( " < < argv [ i ] < < " ) " < < std : : endl ;
if ( ! iPort ) iPort = 11112 ;
}
}
if ( argc = = 1 )
{
# ifndef _WIN32
cout < < " Possible usage: " < < argv [ 0 ] < < " [--server] [address[:port]] --port=port --size=size " < < std : : endl < < std : : endl ;
# endif
cout < < " Server? (j/n) " ;
char cChoice ;
do
2010-03-28 18:58:01 +00:00
cin > > cChoice ;
while ( tolower ( cChoice ) ! = ' n ' & & tolower ( cChoice ) ! = ' j ' ) ;
2009-05-08 13:28:41 +00:00
fHost = ( tolower ( cChoice ) = = ' j ' ) ;
2010-03-28 18:58:01 +00:00
if ( cChoice = = ' J ' | | cChoice = = ' N ' )
2009-05-08 13:28:41 +00:00
{
iPort = ( cChoice = = ' J ' ? 11111 : 11112 ) ;
iCnt = 0 ;
iSize = 0 ;
}
else
{
cout < < " Port? " ;
cin > > iPort ;
cout < < " Dummys? " ;
do
cin > > iCnt ;
2010-03-28 18:58:01 +00:00
while ( iCnt < 0 ) ;
2009-05-08 13:28:41 +00:00
2010-12-23 00:01:24 +00:00
cout < < " Größe? " ;
2009-05-08 13:28:41 +00:00
do
cin > > iSize ;
2010-03-28 18:58:01 +00:00
while ( iSize < 1 | | iSize > 1024 ) ;
2009-05-08 13:28:41 +00:00
}
if ( ! fHost )
{
char szAddr [ 1024 ] ;
int iDPort ;
2010-03-28 18:58:01 +00:00
if ( cChoice = = ' N ' )
2009-05-08 13:28:41 +00:00
{
strcpy ( szAddr , " 127.0.0.1 " ) ;
iDPort = 11111 ;
}
else
{
cout < < " Wohin (Adresse)? " ;
cin > > szAddr ;
cout < < " Wohin (Port)? " ;
cin > > iDPort ;
}
addr . sin_addr . s_addr = inet_addr ( szAddr ) ;
addr . sin_port = htons ( iDPort ) ;
addr . sin_family = AF_INET ;
}
}
cout < < " \n C4NetIO Init... " ;
2010-03-28 18:58:01 +00:00
if ( ! NetIO . Init ( iPort ) )
2009-05-08 13:28:41 +00:00
{
cout < < " Fehler: " < < NetIO . GetError ( ) < < endl ;
return 0 ;
}
cout < < " ok " < < endl ;
2010-03-28 18:58:01 +00:00
if ( fHost )
2009-05-08 13:28:41 +00:00
{
cout < < " Broadcast... " ;
C4NetIO : : addr_t addr ;
2010-03-28 18:58:01 +00:00
if ( ! NetIO . InitBroadcast ( & addr ) )
2009-05-08 13:28:41 +00:00
{
cout < < " Fehler: " < < NetIO . GetError ( ) < < endl ;
return 0 ;
}
cout < < " ok " < < endl ;
cout < < " Thread... " ;
2010-03-28 18:58:01 +00:00
if ( ! CBClass . Start ( ) )
2009-05-08 13:28:41 +00:00
{
cout < < " Fehler! " < < endl ;
return 0 ;
}
cout < < " ok " < < endl ;
cout < < " listening... " < < endl ;
}
else
{
cout < < " Thread... " ;
2010-03-28 18:58:01 +00:00
if ( ! CBClass . Start ( ) )
2009-05-08 13:28:41 +00:00
{
cout < < " Fehler! " < < endl ;
return 0 ;
}
cout < < " ok " < < endl ;
cout < < " verbinden mit " < < inet_ntoa ( addr . sin_addr ) < < " : " < < ntohs ( addr . sin_port ) < < " ... \n " ;
# ifdef ASYNC_CONNECT
NetIO . Connect ( addr ) ;
# else
2010-03-28 18:58:01 +00:00
if ( ! fHost )
2009-05-08 13:28:41 +00:00
{
int iBufLen = 5000 ;
char * pBuf = new char [ iBufLen ] ;
2010-03-28 18:58:01 +00:00
for ( int i = 0 ; i < 10 ; i + + )
2009-05-08 13:28:41 +00:00
{
* pBuf = i ;
2010-03-28 18:58:01 +00:00
if ( ! pNetIO - > Send ( C4NetIOPacket ( pBuf , iBufLen , true , addr ) ) )
2009-05-08 13:28:41 +00:00
cout < < " Fehler: " < < ( pNetIO - > GetError ( ) ? pNetIO - > GetError ( ) : " bla " ) < < endl ;
}
}
# endif
}
2010-03-28 18:58:01 +00:00
while ( std : : cin . get ( ) = = 10 ) ;
2009-05-08 13:28:41 +00:00
CBClass . Stop ( ) ;
NetIO . Close ( ) ;
return 1 ;
}