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

#include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaBase/VistaExceptionBase.h>
6
#include <VistaBase/VistaStreamUtils.h>
7
8
9

#include <cstring>
#include <algorithm>
Anne's avatar
Anne committed
10
#include <cassert>
11
12
#include <iostream>
#include <iomanip>
Anne's avatar
Anne committed
13
14
15

static int S_nMessageIds = 0;

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

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

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

	m_nMessageId = S_nMessageIds++;

	m_oOutgoing.ClearBuffer();
40
41
42
	m_oOutgoing.WriteInt32( 0 ); // Payload size
	m_oOutgoing.WriteInt32( 0 ); // Message Type
	m_oOutgoing.WriteInt32( 0 ); // Identifier
Anne's avatar
Anne committed
43

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

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

	m_pConnection = NULL;
50

Anne's avatar
Anne committed
51
#if NET_AUDIO_SHOW_TRAFFIC
52
	vstr::out() << "CITANetAudioMessage [Preparing] (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
53
#endif
Anne's avatar
Anne committed
54
55
}

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

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

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

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

	// rewrite type dummy
	iSwapDummy = m_nMessageType;
76
77
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
78
	std::memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
79

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

	// rewrite messageid dummy
	iSwapDummy = m_nMessageId;
84
85
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
86
	std::memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
87

Anne's avatar
Anne committed
88
#if NET_AUDIO_SHOW_TRAFFIC
89
	vstr::out() << "CITANetAudioMessage [  Writing] " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
90
#endif
Anne's avatar
Anne committed
91

92
	try
Anne's avatar
Anne committed
93
	{
94
		// It appears safe to send even very big data payload, so we will send at once
Anne's avatar
Anne committed
95
		int iRawBufferSize = m_oOutgoing.GetBufferSize();
96
97
		int nRet = m_pConnection->Send( m_oOutgoing.GetBuffer(), iRawBufferSize );

Anne's avatar
Anne committed
98
#if NET_AUDIO_SHOW_TRAFFIC
99
		vstr::out() << "CITANetAudioMessage [  Writing] " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ") RAW BUFFER DONE" << std::endl;
100
101
#endif

102
103
104
		m_pConnection->WaitForSendFinish();
		if( nRet != m_oOutgoing.GetBufferSize() )
			VISTA_THROW( "ITANetAudioMessage: could not send all data from output buffer via network connection", 255 );
Anne's avatar
Anne committed
105
	}
106
	catch (VistaExceptionBase& ex)
Anne's avatar
Anne committed
107
	{
108
		ITA_EXCEPT1( NETWORK_ERROR, ex.GetExceptionText() );
Anne's avatar
Anne committed
109
110
111
112
113
114
	}
}


void CITANetAudioMessage::ReadMessage()
{
Anne's avatar
Anne committed
115
#if NET_AUDIO_SHOW_TRAFFIC
116
	vstr::out() << "CITANetAudioMessage [ Reading ] Waiting for incoming data" << std::endl;
Anne's avatar
Anne committed
117
#endif
118
	long nIncomingBytes = m_pConnection->WaitForIncomingData( 0 );
119
	assert( nIncomingBytes >= 4 ); // we need at least the size of message
Anne's avatar
Anne committed
120
#if NET_AUDIO_SHOW_TRAFFIC
121
	vstr::out() << "CITANetAudioMessage [ Reading ] " << nIncomingBytes << " bytes incoming" << std::endl;
Anne's avatar
Anne committed
122
#endif
123

124
125
	VistaType::sint32 nMessagePayloadSize;
	int nBytesRead = m_pConnection->ReadInt32( nMessagePayloadSize );
126
	assert( nBytesRead == sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
127
#if NET_AUDIO_SHOW_TRAFFIC
128
	vstr::out() << "CITANetAudioMessage [ Reading ] Expecting " << nMessagePayloadSize << " bytes message payload" << std::endl;
Anne's avatar
Anne committed
129
#endif
130
131
	// we need at least the two protocol ints
	assert( nMessagePayloadSize >= 2 * sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
132

133
134
	if( nMessagePayloadSize > ( int ) m_vecIncomingBuffer.size() )
		m_vecIncomingBuffer.resize( nMessagePayloadSize );
135
136
	
	// Receive all incoming data (potentially splitted)
137
	int iBytesReceivedTotal = 0;
138
	while( nMessagePayloadSize != iBytesReceivedTotal )
Anne's avatar
Anne committed
139
	{
Anne's avatar
Anne committed
140
141
		int iIncommingBytes = m_pConnection->WaitForIncomingData( 0 );
		int iBytesReceived = m_pConnection->Receive( &m_vecIncomingBuffer[ iBytesReceivedTotal ], iIncommingBytes );
142
		iBytesReceivedTotal += iBytesReceived;
143
#if NET_AUDIO_SHOW_TRAFFIC
144
		vstr::out() << "[ CITANetAudioMessage ] " << std::setw( 3 ) << std::floor( iBytesReceivedTotal / float( nMessagePayloadSize ) * 100.0f ) << "% transmitted" << std::endl;
Anne's avatar
Anne committed
145
#endif
Anne's avatar
Anne committed
146
147
	}

148
149
	// Transfer data into members
	m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nMessagePayloadSize, false );
Anne's avatar
Anne committed
150
151
152
	m_nMessageType = ReadInt();
	m_nMessageId = ReadInt();

Anne's avatar
Anne committed
153
#if NET_AUDIO_SHOW_TRAFFIC
154
	vstr::out() << "CITANetAudioMessage [ Reading ] Finished receiving " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
155
156
#endif
}
Anne's avatar
Anne committed
157
158
159

void CITANetAudioMessage::WriteAnswer()
{
160

Anne's avatar
Anne committed
161
#if NET_AUDIO_SHOW_TRAFFIC
162
	vstr::out() << "CITANetAudioMessage [ Answering] to " << m_nMessageType << " with " << m_nAnswerType << " (id=" << std::setw( 4 ) << m_nMessageId << ")" << std::endl;
163
164
165
166
167
#endif

	assert( m_nAnswerType != CITANetAudioProtocol::NP_INVALID );

	VistaType::byte* pBuffer = ( VistaType::byte* )m_oOutgoing.GetBuffer();
Anne's avatar
Anne committed
168
169
170
	VistaType::sint32 iSwapDummy;

	// rewrite size dummy
171
172
173
174
	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
175

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

	// rewrite type dummy
	iSwapDummy = m_nAnswerType;
180
181
182
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
	memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
183

184
	pBuffer += sizeof( VistaType::sint32 );
Anne's avatar
Anne committed
185
186
187

	// rewrite message dummy
	iSwapDummy = m_nMessageId;
188
189
190
	if( m_oOutgoing.GetByteorderSwapFlag() )
		VistaSerializingToolset::Swap4( &iSwapDummy );
	memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
191

192
193
	int nRet = m_pConnection->Send( m_oOutgoing.GetBuffer(), m_oOutgoing.GetBufferSize() );
	
194
195
196
	m_pConnection->WaitForSendFinish();
	if( nRet != m_oOutgoing.GetBufferSize() )
		ITA_EXCEPT1( UNKNOWN, "Could not write the expected number of bytes" );
Anne's avatar
Anne committed
197
198
199
200
}

void CITANetAudioMessage::ReadAnswer()
{
Anne's avatar
Anne committed
201
202

#if NET_AUDIO_SHOW_TRAFFIC
203
	vstr::out() << "CITANetAudioMessage [ Reading] yet unkown answer from initial message type " << m_nMessageType << " (id=" << std::setw( 4 ) << m_nMessageId << ") OK" << std::endl;
204
205
#endif

206
	VistaType::sint32 nMessagePayloadSize;
207
	int nReturn;
208
	nReturn = m_pConnection->ReadInt32( nMessagePayloadSize );
209
	assert( nReturn == sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
210
#if NET_AUDIO_SHOW_TRAFFIC
211
	vstr::out() << "CITANetAudioMessage [ Reading] Answer type " << nReturn << " (id=" << std::setw( 4 ) << m_nMessageId << ") OK" << std::endl;
Anne's avatar
Anne committed
212
#endif
Anne's avatar
Anne committed
213

214
	// We need at least the message type and message id in payload
215
	assert( nMessagePayloadSize >= 2 * sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
216

217
218
	if( nMessagePayloadSize > ( int ) m_vecIncomingBuffer.size() )
		m_vecIncomingBuffer.resize( nMessagePayloadSize );
219
220
221

	// @todo: read over while( received < total ) loop!!!

222
223
224
225
226
227
	int iBytesReceivedTotal = 0;
	while( nMessagePayloadSize != iBytesReceivedTotal )
	{
		int iIncommingBytes = m_pConnection->WaitForIncomingData( 0 );
		int iBytesReceived = m_pConnection->Receive( &m_vecIncomingBuffer[ iBytesReceivedTotal ], iIncommingBytes );
		iBytesReceivedTotal += iBytesReceived;
228
#if NET_AUDIO_SHOW_TRAFFIC
229
		vstr::out() << "[ CITANetAudioMessage ] " << std::setw( 3 ) << std::floor( iBytesReceivedTotal / float( nMessagePayloadSize ) * 100.0f ) << "% of answer transmitted" << std::endl;
Anne's avatar
Anne committed
230
#endif
231
232
233
234
235
236
	}

	if( iBytesReceivedTotal != nMessagePayloadSize )
		ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected when trying to receive answer" );

	// Swap data to deserialization buffer
237
	m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nMessagePayloadSize, false );
Anne's avatar
Anne committed
238

239
	// Take out the two protocol variables type and message id from deserialization buffer
240
241
242
	m_nAnswerType = ReadInt();
	int nMessageID = ReadInt();
	assert( nMessageID == m_nMessageId );
Anne's avatar
Anne committed
243
244
245
246
247
248
249
}

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

250
void CITANetAudioMessage::SetMessageType( int nType )
Anne's avatar
Anne committed
251
{
252
	assert( m_nMessageType == CITANetAudioProtocol::NP_INVALID ); // should only be set once
Anne's avatar
Anne committed
253
254
255
	m_nMessageType = nType;
}

256
void CITANetAudioMessage::SetAnswerType( int nType )
Anne's avatar
Anne committed
257
{
258
	assert( m_nAnswerType == CITANetAudioProtocol::NP_INVALID ); // should only be set once
Anne's avatar
Anne committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	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
{
279
	return ( m_oOutgoing.GetBufferSize() > 4 * sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
280
281
}

282
void CITANetAudioMessage::WriteString( const std::string& sValue )
Anne's avatar
Anne committed
283
{
284
285
	m_oOutgoing.WriteInt32( ( VistaType::sint32 )sValue.size() );
	if( !sValue.empty() ) m_oOutgoing.WriteString( sValue );
Anne's avatar
Anne committed
286
287
}

288
void CITANetAudioMessage::WriteInt( const int iValue )
Anne's avatar
Anne committed
289
{
290
	m_oOutgoing.WriteInt32( ( VistaType::sint32 )iValue );
Anne's avatar
Anne committed
291
292
}

293
void CITANetAudioMessage::WriteBool( const bool bValue )
Anne's avatar
Anne committed
294
{
295
	m_oOutgoing.WriteBool( bValue );
Anne's avatar
Anne committed
296
297
}

298
void CITANetAudioMessage::WriteFloat( const float fValue )
Anne's avatar
Anne committed
299
{
300
	m_oOutgoing.WriteFloat32( fValue );
Anne's avatar
Anne committed
301
302
}

303
void CITANetAudioMessage::WriteDouble( const double dValue )
Anne's avatar
Anne committed
304
{
305
	m_oOutgoing.WriteFloat64( dValue );
Anne's avatar
Anne committed
306
307
308
309
310
}

std::string CITANetAudioMessage::ReadString()
{
	VistaType::sint32 nSize;
311
312
	int nReturn = m_oIncoming.ReadInt32( nSize );
	assert( nReturn == sizeof( VistaType::sint32 ) );
Anne's avatar
Anne committed
313
314

	// Empty string?
315
	if( nSize == 0 ) return "";
Anne's avatar
Anne committed
316
317

	std::string sValue;
318
319
	nReturn = m_oIncoming.ReadString( sValue, nSize );
	assert( nReturn == nSize );
Anne's avatar
Anne committed
320
321
322
323
324
325
	return sValue;
}

int CITANetAudioMessage::ReadInt()
{
	VistaType::sint32 nValue;
326
327
328
329
	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
330
331
332
333
334
335
	return nValue;
}

bool CITANetAudioMessage::ReadBool()
{
	bool bValue;
336
337
	int nReturn = m_oIncoming.ReadBool( bValue );
	assert( nReturn == sizeof( bool ) );
Anne's avatar
Anne committed
338
339
340
341
342
	return bValue;
}
float CITANetAudioMessage::ReadFloat()
{
	float fValue;
343
344
	int nReturn = m_oIncoming.ReadFloat32( fValue );
	assert( nReturn == sizeof( float ) );
Anne's avatar
Anne committed
345
346
347
348
349
	return fValue;
}
double CITANetAudioMessage::ReadDouble()
{
	double dValue;
350
351
	int nReturn = m_oIncoming.ReadFloat64( dValue );
	assert( nReturn == sizeof( double ) );
Anne's avatar
Anne committed
352
353
354
355
	return dValue;
}


356
void CITANetAudioMessage::WriteException( const ITAException& oException )
Anne's avatar
Anne committed
357
{
358
359
360
	WriteInt( oException.iErrorCode );
	WriteString( oException.sModule );
	WriteString( oException.sReason );
Anne's avatar
Anne committed
361
362
363
364
365
366
367
}

ITAException CITANetAudioMessage::ReadException()
{
	int iErrorCode = ReadInt();
	std::string sModule = ReadString();
	std::string sReason = ReadString();
368
	return ITAException( iErrorCode, sModule, sReason );
Anne's avatar
Anne committed
369
370
371
372
373
374
375
376
377
378
379
}

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

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

380
void CITANetAudioMessage::WriteIntVector( const std::vector<int> viData )
Anne's avatar
Anne committed
381
{
382
383
384
385
	int iSize = ( int ) viData.size();
	WriteInt( iSize );
	for( int i = 0; i < iSize; i++ )
		WriteInt( viData[ i ] );
Anne's avatar
Anne committed
386
387
388
389
390
391
}

std::vector<int> CITANetAudioMessage::ReadIntVector()
{
	std::vector<int> viData;
	int iSize = ReadInt();
392
393
	for( int i = 0; i < iSize; i++ )
		viData.push_back( ReadInt() );
Anne's avatar
Anne committed
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408

	return viData;
}

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

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

	return oParams;
}

409
void CITANetAudioMessage::WriteStreamingParameters( const CITANetAudioProtocol::StreamingParameters & oParams )
Anne's avatar
Anne committed
410
{
411
412
413
	WriteInt( oParams.iChannels );
	WriteDouble( oParams.dSampleRate );
	WriteInt( oParams.iBlockSize );
Anne's avatar
Anne committed
414
}
415
416
417
418

int CITANetAudioMessage::ReadRingBufferSize()
{
	return ReadInt();
419
420
421
}

void CITANetAudioMessage::WriteRingBufferSize( const int iBufferSize )
422
{
423
424
425
	WriteInt( iBufferSize );
}

426
427
int CITANetAudioMessage::ReadRingBufferFree()
{
428
429
430
431
432
433
434
435
436
	return ReadInt();
}

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

void CITANetAudioMessage::ReadSampleFrame( ITASampleFrame* pSampleFrame )
437
438
{
	int iChannels = ReadInt();
439
440
441
442
443
444
	int iSamples = ReadInt();

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

	for( int i = 0; i < iChannels; i++ )
445
	{
446
		for( int j = 0; j < iSamples; j++ )
447
		{
448
			( *pSampleFrame )[ i ][ j ] = ReadFloat();
449
450
		}
	}
451
452
453
}

void CITANetAudioMessage::WriteSampleFrame( ITASampleFrame* pSamples )
454
{
455
456
457
458
	WriteInt( pSamples->channels() );
	WriteInt( pSamples->GetLength() );

	for( int i = 0; i < pSamples->channels(); i++ )
459
	{
460
		for( int j = 0; j < pSamples->GetLength(); j++ )
461
		{
462
			WriteFloat( ( *pSamples )[ i ][ j ] );
463
464
		}
	}
465
466
}