ITANetAudioMessage.cpp 12.9 KB
Newer Older
Anne's avatar
Anne committed
1
#include <ITANetAudioMessage.h>
Anne's avatar
Anne committed
2
3
4
5
6
7
#include <ITAStringUtils.h>

#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaBase/VistaExceptionBase.h>

#include <cassert>
8
9
#include <iostream>
#include <iomanip>
Anne's avatar
Anne committed
10
11
12

static int S_nMessageIds = 0;

13
14
15
16
CITANetAudioMessage::CITANetAudioMessage( VistaSerializingToolset::ByteOrderSwapBehavior bSwapBuffers )
	: m_vecIncomingBuffer( 2048 )
	, m_oOutgoing( 2048 )
	, m_pConnection( NULL )
Anne's avatar
Anne committed
17
{
18
19
	m_oOutgoing.SetByteorderSwapFlag( bSwapBuffers );
	m_oIncoming.SetByteorderSwapFlag( bSwapBuffers );
Anne's avatar
Anne committed
20
21
22
23
24
	ResetMessage();
}

void CITANetAudioMessage::ResetMessage()
{
25
	if( m_oIncoming.GetTailSize() > 0 )
Anne's avatar
Anne committed
26
27
28
29
30
31
32
	{
		std::cerr << "CITANetAudioMessage::ResetMessage() called before message was fully processed!" << std::endl;
	}

	// wait till sending is complete -> this prevents us
	// from deleting the buffer while it is still being read
	// by the connection
33
34
	//if( m_pConnection )
	//	m_pConnection->WaitForSendFinish();
Anne's avatar
Anne committed
35
36
37
38

	m_nMessageId = S_nMessageIds++;

	m_oOutgoing.ClearBuffer();
39
40
41
	m_oOutgoing.WriteInt32( 0 ); // size dummy
	m_oOutgoing.WriteInt32( 0 ); // type dummy
	m_oOutgoing.WriteInt32( 0 ); // ID
Anne's avatar
Anne committed
42

43
	m_oIncoming.SetBuffer( NULL, 0 );
Anne's avatar
Anne committed
44
45
46
47
48

	m_nMessageType = CITANetAudioProtocol::NP_INVALID;
	m_nAnswerType = CITANetAudioProtocol::NP_INVALID;

	m_pConnection = NULL;
49
50
51
52

#ifdef NET_AUDIO_SHOW_TRAFFIC
	std::cout << "CITANetAudioMessage [Preparing] (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
#endif
Anne's avatar
Anne committed
53
54
}

55
void CITANetAudioMessage::SetConnection( VistaConnectionIP* pConn )
Anne's avatar
Anne committed
56
57
58
59
60
61
{
	m_pConnection = pConn;
}

void CITANetAudioMessage::WriteMessage()
{
62
	VistaType::byte* pBuffer = ( VistaType::byte* ) m_oOutgoing.GetBuffer();
Anne's avatar
Anne committed
63
64
65
	VistaType::sint32 iSwapDummy;

	// rewrite size dummy
66
67
68
69
	iSwapDummy = m_oOutgoing.GetBufferSize() - sizeof( VistaType::sint32 );
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
	memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
70

71
	pBuffer += sizeof( VistaType::sint32 );
Anne's avatar
Anne committed
72
73
74

	// rewrite type dummy
	iSwapDummy = m_nMessageType;
75
76
77
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
	memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
78

79
	pBuffer += sizeof( VistaType::sint32 );
Anne's avatar
Anne committed
80
81
82

	// rewrite messageid dummy
	iSwapDummy = m_nMessageId;
83
84
85
86
87
88
89
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
	memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );

#ifdef NET_AUDIO_SHOW_TRAFFIC
	std::cout << "CITANetAudioMessage [  Writing] " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
#endif
Anne's avatar
Anne committed
90

91
	try
Anne's avatar
Anne committed
92
93
	{
		int iRawBufferSize = m_oOutgoing.GetBufferSize();
94
95
96
97
98
99
100
101
102
		int nRet = m_pConnection->Send( m_oOutgoing.GetBuffer(), iRawBufferSize );

#ifdef NET_AUDIO_SHOW_TRAFFIC
		std::cout << "CITANetAudioMessage [  Writing] " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ") RAW BUFFER DONE" << std::endl;
#endif

		//m_pConnection->WaitForSendFinish();
		//if( nRet != m_oOutgoing.GetBufferSize() )
		//	ITA_EXCEPT1( NETWORK_ERROR, "Could not write the expected number of bytes" );
Anne's avatar
Anne committed
103
	}
104
	catch (VistaExceptionBase& ex)
Anne's avatar
Anne committed
105
	{
106
		ITA_EXCEPT1(NETWORK_ERROR, ex.GetExceptionText());
Anne's avatar
Anne committed
107
108
109
110
111
112
	}
}


void CITANetAudioMessage::ReadMessage()
{
113
114
115
	std::cout << "CITANetAudioMessage [ Reading ] Waiting for incoming data" << std::endl;
	long nIncomingBytes = m_pConnection->WaitForIncomingData( 0 );
	std::cout << "CITANetAudioMessage [ Reading ] " << nIncomingBytes << " bytes incoming" << std::endl;
116

117
118
119
120
	VistaType::sint32 nMessagePayloadSize;
	int nBytesRead = m_pConnection->ReadInt32( nMessagePayloadSize );
	assert( nBytesRead = sizeof( int ) );
	std::cout << "CITANetAudioMessage [ Reading ] Expecting " << nMessagePayloadSize << " bytes message payload" << std::endl;
Anne's avatar
Anne committed
121

122
123
	// we need at least the two protocol ints
	assert( nMessagePayloadSize >= 2 * sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
124

125
126
	if( nMessagePayloadSize > ( int ) m_vecIncomingBuffer.size() )
		m_vecIncomingBuffer.resize( nMessagePayloadSize );
Anne's avatar
Anne committed
127

128
129
	int iBytesReceivedTotal = m_pConnection->Receive( &m_vecIncomingBuffer[ 0 ], nMessagePayloadSize );
	std::cout << "CITANetAudioMessage [ Reading ] Received " << iBytesReceivedTotal << " bytes, so far." << std::endl;
Anne's avatar
Anne committed
130

131
	while( nMessagePayloadSize != iBytesReceivedTotal )
Anne's avatar
Anne committed
132
	{
133
134
135
		int iBytesReceived = m_pConnection->Receive( &m_vecIncomingBuffer[ iBytesReceivedTotal ], nMessagePayloadSize );
		iBytesReceivedTotal += iBytesReceived;
		std::cout << "CITANetAudioMessage [ Reading ] Further " << iBytesReceived << " bytes incoming" << std::endl;
Anne's avatar
Anne committed
136
137
	}

138
139
	// Transfer data into members
	m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nMessagePayloadSize, false );
Anne's avatar
Anne committed
140
141
142
	m_nMessageType = ReadInt();
	m_nMessageId = ReadInt();

143
144
145
146
#ifdef NET_AUDIO_SHOW_TRAFFIC
	std::cout << "CITANetAudioMessage [ Reading ] Finished receiving " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
#endif
}
Anne's avatar
Anne committed
147
148
149

void CITANetAudioMessage::WriteAnswer()
{
150
151
152
153
154
155
156
157

#ifdef NET_AUDIO_SHOW_TRAFFIC
	std::cout << "CITANetAudioMessage [ Answering] to " << m_nMessageType << " with " << m_nAnswerType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
#endif

	assert( m_nAnswerType != CITANetAudioProtocol::NP_INVALID );

	VistaType::byte* pBuffer = ( VistaType::byte* )m_oOutgoing.GetBuffer();
Anne's avatar
Anne committed
158
159
160
	VistaType::sint32 iSwapDummy;

	// rewrite size dummy
161
162
163
164
	iSwapDummy = m_oOutgoing.GetBufferSize() - sizeof( VistaType::sint32 );
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
	memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
165

166
	pBuffer += sizeof( VistaType::sint32 );
Anne's avatar
Anne committed
167
168
169

	// rewrite type dummy
	iSwapDummy = m_nAnswerType;
170
171
172
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
	memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
173

174
	pBuffer += sizeof( VistaType::sint32 );
Anne's avatar
Anne committed
175
176
177

	// rewrite message dummy
	iSwapDummy = m_nMessageId;
178
179
180
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
	memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
181

182
183
184
185
186
	int nRet = m_pConnection->Send( m_oOutgoing.GetBuffer(), m_oOutgoing.GetBufferSize() );
	//m_pConnection->WaitForSendFinish();
	//f( nRet != m_oOutgoing.GetBufferSize() )
	//	ITA_EXCEPT1( UNKNOWN, "Could not write the expected number of bytes" );
	
Anne's avatar
Anne committed
187
188
189
190
}

void CITANetAudioMessage::ReadAnswer()
{
191
192
193
194
195
#ifdef NET_AUDIO_SHOW_TRAFFIC
	std::cout << "CITANetAudioMessage [ Reading] yet unkown answer from message " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ") OK" << std::endl;
#endif

	//try
Anne's avatar
Anne committed
196
197
198
	{
		VistaType::sint32 nMessageSize;
		int nReturn;
199
		nReturn = m_pConnection->ReadInt32( nMessageSize );
Anne's avatar
Anne committed
200

201
202
		if( nReturn != sizeof( VistaType::sint32 ) )
			ITA_EXCEPT1( UNKNOWN, "Protokoll error, was expecting 4 bytes to read message size, but received " + std::to_string( nReturn ) );
Anne's avatar
Anne committed
203
204

		// we need at least the two protocol types
205
		assert( nMessageSize >= 2 * sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
206

207
208
		if( nMessageSize > ( int ) m_vecIncomingBuffer.size() )
			m_vecIncomingBuffer.resize( nMessageSize );
Anne's avatar
Anne committed
209
210

		// jst: hier nicht while( nReturn < nMessageSize) ReadRawBuffer??
211
212
213
		nReturn = m_pConnection->ReadRawBuffer( &m_vecIncomingBuffer[ 0 ], nMessageSize );
		if( nReturn != nMessageSize )
			ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected" );
Anne's avatar
Anne committed
214

215
		m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nReturn );
Anne's avatar
Anne committed
216
	}
217
	//catch (VistaExceptionBase& ex)
Anne's avatar
Anne committed
218
219
	{
		// Probable connection loss
220
		//ITA_EXCEPT1( UNKNOWN, ex.GetExceptionText() );
Anne's avatar
Anne committed
221
222
	}

223
224
225
226
	m_nAnswerType = ReadInt();
	int nMessageID = ReadInt();
	assert( nMessageID == m_nMessageId );
	m_nMessageId = nMessageID;
Anne's avatar
Anne committed
227
228
229
230
231
232
233
234
}


int CITANetAudioMessage::GetMessageType() const
{
	return m_nMessageType;
}

235
void CITANetAudioMessage::SetMessageType( int nType )
Anne's avatar
Anne committed
236
{
237
	assert( m_nMessageType == CITANetAudioProtocol::NP_INVALID ); // should only be set once
Anne's avatar
Anne committed
238
239
240
	m_nMessageType = nType;
}

241
void CITANetAudioMessage::SetAnswerType( int nType )
Anne's avatar
Anne committed
242
{
243
	assert( m_nAnswerType == CITANetAudioProtocol::NP_INVALID ); // should only be set once
Anne's avatar
Anne committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
	m_nAnswerType = nType;
}

int CITANetAudioMessage::GetAnswerType() const
{
	return m_nAnswerType;
}

int CITANetAudioMessage::GetIncomingMessageSize() const
{
	return m_oIncoming.GetTailSize();
}

int CITANetAudioMessage::GetOutgoingMessageSize() const
{
	return m_oOutgoing.GetBufferSize();
}

bool CITANetAudioMessage::GetOutgoingMessageHasData() const
{
264
	return ( m_oOutgoing.GetBufferSize() > 4 * sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
265
266
}

267
void CITANetAudioMessage::WriteString( const std::string& sValue )
Anne's avatar
Anne committed
268
{
269
270
	m_oOutgoing.WriteInt32( ( VistaType::sint32 )sValue.size() );
	if( !sValue.empty() ) m_oOutgoing.WriteString( sValue );
Anne's avatar
Anne committed
271
272
}

273
void CITANetAudioMessage::WriteInt( const int iValue )
Anne's avatar
Anne committed
274
{
275
	m_oOutgoing.WriteInt32( ( VistaType::sint32 )iValue );
Anne's avatar
Anne committed
276
277
}

278
void CITANetAudioMessage::WriteBool( const bool bValue )
Anne's avatar
Anne committed
279
{
280
	m_oOutgoing.WriteBool( bValue );
Anne's avatar
Anne committed
281
282
}

283
void CITANetAudioMessage::WriteFloat( const float fValue )
Anne's avatar
Anne committed
284
{
285
	m_oOutgoing.WriteFloat32( fValue );
Anne's avatar
Anne committed
286
287
}

288
void CITANetAudioMessage::WriteDouble( const double dValue )
Anne's avatar
Anne committed
289
{
290
	m_oOutgoing.WriteFloat64( dValue );
Anne's avatar
Anne committed
291
292
293
294
295
}

std::string CITANetAudioMessage::ReadString()
{
	VistaType::sint32 nSize;
296
297
	int nReturn = m_oIncoming.ReadInt32( nSize );
	assert( nReturn == sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
298
299

	// Empty string?
300
	if( nSize == 0 ) return "";
Anne's avatar
Anne committed
301
302

	std::string sValue;
303
304
	nReturn = m_oIncoming.ReadString( sValue, nSize );
	assert( nReturn == nSize );
Anne's avatar
Anne committed
305
306
307
308
309
310
	return sValue;
}

int CITANetAudioMessage::ReadInt()
{
	VistaType::sint32 nValue;
311
312
313
314
	int nReturn = m_oIncoming.ReadInt32( nValue );
	if( nReturn == -1 )
		ITA_EXCEPT1( UNKNOWN, "Could not read integer value from incoming message" );
	assert( nReturn == sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
315
316
317
318
319
320
	return nValue;
}

bool CITANetAudioMessage::ReadBool()
{
	bool bValue;
321
322
	int nReturn = m_oIncoming.ReadBool( bValue );
	assert( nReturn == sizeof( bool ) );
Anne's avatar
Anne committed
323
324
325
326
327
	return bValue;
}
float CITANetAudioMessage::ReadFloat()
{
	float fValue;
328
329
	int nReturn = m_oIncoming.ReadFloat32( fValue );
	assert( nReturn == sizeof( float ) );
Anne's avatar
Anne committed
330
331
332
333
334
	return fValue;
}
double CITANetAudioMessage::ReadDouble()
{
	double dValue;
335
336
	int nReturn = m_oIncoming.ReadFloat64( dValue );
	assert( nReturn == sizeof( double ) );
Anne's avatar
Anne committed
337
338
339
340
	return dValue;
}


341
void CITANetAudioMessage::WriteException( const ITAException& oException )
Anne's avatar
Anne committed
342
{
343
344
345
	WriteInt( oException.iErrorCode );
	WriteString( oException.sModule );
	WriteString( oException.sReason );
Anne's avatar
Anne committed
346
347
348
349
350
351
352
}

ITAException CITANetAudioMessage::ReadException()
{
	int iErrorCode = ReadInt();
	std::string sModule = ReadString();
	std::string sReason = ReadString();
353
	return ITAException( iErrorCode, sModule, sReason );
Anne's avatar
Anne committed
354
355
356
357
358
359
360
361
362
363
364
}

VistaConnectionIP* CITANetAudioMessage::GetConnection() const
{
	return m_pConnection;
}

void CITANetAudioMessage::ClearConnection() {
	m_pConnection = NULL;
}

365
void CITANetAudioMessage::WriteIntVector( const std::vector<int> viData )
Anne's avatar
Anne committed
366
{
367
368
369
370
	int iSize = ( int ) viData.size();
	WriteInt( iSize );
	for( int i = 0; i < iSize; i++ )
		WriteInt( viData[ i ] );
Anne's avatar
Anne committed
371
372
373
374
375
376
}

std::vector<int> CITANetAudioMessage::ReadIntVector()
{
	std::vector<int> viData;
	int iSize = ReadInt();
377
378
	for( int i = 0; i < iSize; i++ )
		viData.push_back( ReadInt() );
Anne's avatar
Anne committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393

	return viData;
}

CITANetAudioProtocol::StreamingParameters CITANetAudioMessage::ReadStreamingParameters()
{
	CITANetAudioProtocol::StreamingParameters oParams;

	oParams.iChannels = ReadInt();
	oParams.dSampleRate = ReadDouble();
	oParams.iBlockSize = ReadInt();

	return oParams;
}

394
void CITANetAudioMessage::WriteStreamingParameters( const CITANetAudioProtocol::StreamingParameters & oParams )
Anne's avatar
Anne committed
395
{
396
397
398
	WriteInt( oParams.iChannels );
	WriteDouble( oParams.dSampleRate );
	WriteInt( oParams.iBlockSize );
Anne's avatar
Anne committed
399
}
400
401
402
403

int CITANetAudioMessage::ReadRingBufferSize()
{
	return ReadInt();
404
405
406
}

void CITANetAudioMessage::WriteRingBufferSize( const int iBufferSize )
407
{
408
409
410
	WriteInt( iBufferSize );
}

411
412
int CITANetAudioMessage::ReadRingBufferFree()
{
413
414
415
416
417
418
419
420
421
	return ReadInt();
}

void CITANetAudioMessage::WriteRingBufferFree( const int iBufferFree )
{
	WriteInt( iBufferFree );
}

void CITANetAudioMessage::ReadSampleFrame( ITASampleFrame* pSampleFrame )
422
423
{
	int iChannels = ReadInt();
424
425
426
427
428
429
	int iSamples = ReadInt();

	if( pSampleFrame->channels() != iChannels || pSampleFrame->GetLength() != iSamples )
		pSampleFrame->init( iChannels, iSamples, false );

	for( int i = 0; i < iChannels; i++ )
430
	{
431
		for( int j = 0; j < iSamples; j++ )
432
		{
433
			( *pSampleFrame )[ i ][ j ] = ReadFloat();
434
435
		}
	}
436
437
438
}

void CITANetAudioMessage::WriteSampleFrame( ITASampleFrame* pSamples )
439
{
440
441
442
443
	WriteInt( pSamples->channels() );
	WriteInt( pSamples->GetLength() );

	for( int i = 0; i < pSamples->channels(); i++ )
444
	{
445
		for( int j = 0; j < pSamples->GetLength(); j++ )
446
		{
447
			WriteFloat( ( *pSamples )[ i ][ j ] );
448
449
		}
	}
450
451
}