VistaNetTest.cpp 3.87 KB
Newer Older
1
#include <cassert>
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
2
#include <cmath>
3 4
#include <string>

5
#include <VistaInterProcComm/Concurrency/VistaThread.h>
6 7 8
#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaInterProcComm/IPNet/VistaTCPServer.h>
#include <VistaInterProcComm/IPNet/VistaTCPSocket.h>
9
#include <VistaBase/VistaStreamUtils.h>
10 11 12

using namespace std;

13 14 15 16
static const string g_sServerName = "localhost";
static const int g_iServerPort = 12480;
static const int g_iRepetitions = 5;
static const size_t g_lDataSize = 152733239;
17 18


19
class CServer : public VistaThread
20 21 22 23 24 25
{
public:
	CServer()
	{
		m_pServer = new VistaTCPServer( g_sServerName, g_iServerPort, 1 );
		Run();
26
	};
27

28 29
	~CServer()
	{
30
		delete m_pServer;
31
	};
32

33
	void ThreadBody()
34
	{
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
35
		vstr::out() << "[ Server ] Waiting for connections" << endl;
36
		VistaTCPSocket* pSocket = m_pServer->GetNextClient();
37
		vstr::out() << "[ Server ] Connected." << endl;
38 39

		size_t nGetReceiveBufferSize = pSocket->GetReceiveBufferSize();
40
		vstr::out() << "[ Server ] " << nGetReceiveBufferSize << " receive buffer size" << endl;
41

42
		vector< char > vdIncomingData;
43

44 45
		bool bRepeat = true;
		while( bRepeat )
46
		{
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
			long nIncomingBytes = pSocket->WaitForIncomingData( 0 );
			vstr::out() << "[ Server ] " << nIncomingBytes << " incoming bytes" << endl;

			// Two-step receiving
			int iPayloadDataSize;
			int iBytesRead = pSocket->ReceiveRaw( &iPayloadDataSize, sizeof( int ) );
			assert( iBytesRead == sizeof( int ) );
			vstr::out() << "[ Server ] Expecting " << iPayloadDataSize << " bytes to come." << endl;

			if( iPayloadDataSize > vdIncomingData.size() )
				vdIncomingData.resize( iPayloadDataSize );

			int iBytesReceivedTotal = 0;
			while( iPayloadDataSize != iBytesReceivedTotal )
			{
62 63
				long nIncomingBytes = pSocket->WaitForIncomingData( 0 );
				int iBytesReceived = pSocket->ReceiveRaw( &vdIncomingData[ iBytesReceivedTotal ], nIncomingBytes );
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
				iBytesReceivedTotal += iBytesReceived;
				vstr::out() << "[ Server ] " << setw( 3 ) << std::floor( iBytesReceivedTotal / float( iPayloadDataSize ) * 100.0f ) << "% transmitted" << endl;
			}

			assert( vdIncomingData[ 0 ] == 1 );
			assert( vdIncomingData[ vdIncomingData.size() - 2 ] == -1 );

			vstr::out() << "[ Server ] Received all data and content appears valid!" << endl;

			vstr::out() << "[ Server ] Sending acknowledge flag" << endl;
			bool bTransmissionDone = true;
			pSocket->SendRaw( &bTransmissionDone, sizeof( bool ) );
			iBytesRead = pSocket->ReceiveRaw( &bRepeat, sizeof( bool ) );

			if( bRepeat )
				vstr::out() << "[ Server ] Repeating" << endl;
80
		}
81

82 83
		vstr::out() << "[ Server ] Closing." << endl;
	};
84 85 86 87 88 89 90 91 92 93

private:
	VistaTCPServer* m_pServer;
};

int main( int , char** )
{
	CServer oServer;

	VistaConnectionIP oConnection( VistaConnectionIP::CT_TCP, g_sServerName, g_iServerPort);
94
	if( !oConnection.GetIsConnected() )
95
	{
96
		vstr::out() << "[ Client ] Connection failed" << endl;
97 98
		return 255;
	}
99

100 101
	vstr::out() << "[ Client ] Connection established" << endl;
	vstr::out() << "[ Client ] Connection is buffering: " << ( oConnection.GetIsBuffering() ? "yes" : "no" ) << endl;
102

103 104 105 106
	int i = 0;
	while( i++ <= g_iRepetitions )
	{
		vstr::out() << "[ Client ] Client sending data now." << endl;
107
		vector< char > vdData( g_lDataSize + 4 );
108
		int* piDataSize = ( int* ) &vdData[ 0 ];
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
109
		*piDataSize = (unsigned int)( g_lDataSize ); // Send data size as first block
110 111 112
		vdData[ 1 * sizeof( int ) + 0 ] = 1; // First entry one (just for fun)
		vdData[ vdData.size() - 2 ] = -1; // Second last entry -1 (just for fun)
		void* pData = ( void* ) &vdData[ 0 ];
Dipl.-Ing. Jonas Stienen's avatar
style  
Dipl.-Ing. Jonas Stienen committed
113
		oConnection.Send( pData, int( vdData.size() ) );
114 115 116 117 118 119 120 121 122 123 124 125

		bool bAck;
		oConnection.ReadBool( bAck );

		if( !bAck )
			vstr::out() << "[ Client ] Received negative acknowledge flag" << endl;
		else
			vstr::out() << "[ Client ] Received positive acknowledge flag" << endl;

		bool bRepeat = ( i <= g_iRepetitions );
		oConnection.Send( &bRepeat, sizeof( bool ) );
	}
126 127

	oConnection.Close( false );
128 129 130

	return 0;
}