Commit 856ce0b9 authored by Anne's avatar Anne
Browse files

merge fixed bodyloop

parent 5c42938a
...@@ -78,7 +78,10 @@ public: ...@@ -78,7 +78,10 @@ public:
void WriteString( const std::string& ); void WriteString( const std::string& );
void WriteIntVector( const std::vector< int > ); void WriteIntVector( const std::vector< int > );
void WriteFloatVector( const std::vector< float > ); void WriteFloatVector( const std::vector< float > );
void WriteStreamingParameters( const CITANetAudioProtocol::StreamingParameters &); void WriteStreamingParameters(const CITANetAudioProtocol::StreamingParameters &);
void WriteRingBufferSize(const int);
void WriteRingBufferFree(const int);
void WriteSampleFrame(ITASampleFrame*);
std::string ReadString(); std::string ReadString();
int ReadInt(); int ReadInt();
...@@ -88,7 +91,10 @@ public: ...@@ -88,7 +91,10 @@ public:
double ReadDouble(); double ReadDouble();
std::vector< int > ReadIntVector(); std::vector< int > ReadIntVector();
std::vector< float > ReadFloatVector(); std::vector< float > ReadFloatVector();
CITANetAudioProtocol::StreamingParameters ReadStreamingParameters(); CITANetAudioProtocol::StreamingParameters ReadStreamingParameters();
int ReadRingBufferSize();
int ReadRingBufferFree();
void ReadSampleFrame(ITASampleFrame* pSampleFrame);
private: private:
......
...@@ -74,8 +74,6 @@ public: ...@@ -74,8 +74,6 @@ public:
void SetAutomaticUpdateRate(); void SetAutomaticUpdateRate();
bool LoopBody();
protected: protected:
int Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples ); int Transmit( const ITASampleFrame& sfNewSamples, int iNumSamples );
ITADatasource* GetInputStream() const; ITADatasource* GetInputStream() const;
......
#include <ITANetAudioMessage.h> #include <ITANetAudioMessage.h>
#include <ITAStringUtils.h> #include <ITAStringUtils.h>
#include <VistaInterProcComm/Connections/VistaConnectionIP.h> #include <VistaInterProcComm/Connections/VistaConnectionIP.h>
#include <VistaBase/VistaExceptionBase.h> #include <VistaBase/VistaExceptionBase.h>
#include <cassert> #include <cassert>
static int S_nMessageIds = 0; static int S_nMessageIds = 0;
CITANetAudioMessage::CITANetAudioMessage( VistaSerializingToolset::ByteOrderSwapBehavior bSwapBuffers ) CITANetAudioMessage::CITANetAudioMessage( VistaSerializingToolset::ByteOrderSwapBehavior bSwapBuffers )
: m_vecIncomingBuffer( 2048 ) : m_vecIncomingBuffer( 2048 )
, m_oOutgoing( 2048 ) , m_oOutgoing( 2048 )
, m_pConnection( NULL ) , m_pConnection( NULL )
{ {
m_oOutgoing.SetByteorderSwapFlag( bSwapBuffers ); m_oOutgoing.SetByteorderSwapFlag( bSwapBuffers );
m_oIncoming.SetByteorderSwapFlag( bSwapBuffers ); m_oIncoming.SetByteorderSwapFlag( bSwapBuffers );
ResetMessage(); ResetMessage();
} }
void CITANetAudioMessage::ResetMessage() void CITANetAudioMessage::ResetMessage()
{ {
if( m_oIncoming.GetTailSize() > 0 ) if( m_oIncoming.GetTailSize() > 0 )
{ {
std::cerr << "CITANetAudioMessage::ResetMessage() called before message was fully processed!" << std::endl; std::cerr << "CITANetAudioMessage::ResetMessage() called before message was fully processed!" << std::endl;
} }
// wait till sending is complete -> this prevents us // wait till sending is complete -> this prevents us
// from deleting the buffer while it is still being read // from deleting the buffer while it is still being read
// by the connection // by the connection
if( m_pConnection ) if( m_pConnection )
m_pConnection->WaitForSendFinish(); m_pConnection->WaitForSendFinish();
m_nMessageId = S_nMessageIds++; m_nMessageId = S_nMessageIds++;
m_oOutgoing.ClearBuffer(); m_oOutgoing.ClearBuffer();
m_oOutgoing.WriteInt32( 0 ); // size dummy m_oOutgoing.WriteInt32( 0 ); // size dummy
m_oOutgoing.WriteInt32( 0 ); // type dummy m_oOutgoing.WriteInt32( 0 ); // type dummy
m_oOutgoing.WriteInt32( 0 ); // ID m_oOutgoing.WriteInt32( 0 ); // ID
m_oIncoming.SetBuffer( NULL, 0 ); m_oIncoming.SetBuffer( NULL, 0 );
m_nMessageType = CITANetAudioProtocol::NP_INVALID; m_nMessageType = CITANetAudioProtocol::NP_INVALID;
m_nAnswerType = CITANetAudioProtocol::NP_INVALID; m_nAnswerType = CITANetAudioProtocol::NP_INVALID;
m_pConnection = NULL; m_pConnection = NULL;
} }
void CITANetAudioMessage::SetConnection( VistaConnectionIP* pConn ) void CITANetAudioMessage::SetConnection( VistaConnectionIP* pConn )
{ {
m_pConnection = pConn; m_pConnection = pConn;
} }
void CITANetAudioMessage::WriteMessage() void CITANetAudioMessage::WriteMessage()
{ {
VistaType::byte* pBuffer = ( VistaType::byte* ) m_oOutgoing.GetBuffer(); VistaType::byte* pBuffer = ( VistaType::byte* ) m_oOutgoing.GetBuffer();
VistaType::sint32 iSwapDummy; VistaType::sint32 iSwapDummy;
// rewrite size dummy // rewrite size dummy
iSwapDummy = m_oOutgoing.GetBufferSize() - sizeof( VistaType::sint32 ); iSwapDummy = m_oOutgoing.GetBufferSize() - sizeof( VistaType::sint32 );
if( m_oOutgoing.GetByteorderSwapFlag() ) if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy ); VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) ); memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
pBuffer += sizeof( VistaType::sint32 ); pBuffer += sizeof( VistaType::sint32 );
// rewrite type dummy // rewrite type dummy
iSwapDummy = m_nMessageType; iSwapDummy = m_nMessageType;
if( m_oOutgoing.GetByteorderSwapFlag() ) if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy ); VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) ); memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
pBuffer += sizeof( VistaType::sint32 ); pBuffer += sizeof( VistaType::sint32 );
// rewrite messageid dummy // rewrite messageid dummy
iSwapDummy = m_nMessageId; iSwapDummy = m_nMessageId;
if( m_oOutgoing.GetByteorderSwapFlag() ) if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy ); VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) ); memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
try try
{ {
int iRawBufferSize = m_oOutgoing.GetBufferSize(); int iRawBufferSize = m_oOutgoing.GetBufferSize();
int nRet = m_pConnection->WriteRawBuffer( m_oOutgoing.GetBuffer(), iRawBufferSize ); int nRet = m_pConnection->WriteRawBuffer( m_oOutgoing.GetBuffer(), iRawBufferSize );
m_pConnection->WaitForSendFinish(); m_pConnection->WaitForSendFinish();
if( nRet != m_oOutgoing.GetBufferSize() ) if( nRet != m_oOutgoing.GetBufferSize() )
ITA_EXCEPT1( NETWORK_ERROR, "Could not write the expected number of bytes" ); ITA_EXCEPT1( NETWORK_ERROR, "Could not write the expected number of bytes" );
} }
catch( VistaExceptionBase& ex ) catch( VistaExceptionBase& ex )
{ {
ITA_EXCEPT1( NETWORK_ERROR, ex.GetExceptionText() ); ITA_EXCEPT1( NETWORK_ERROR, ex.GetExceptionText() );
} }
} }
void CITANetAudioMessage::ReadMessage() void CITANetAudioMessage::ReadMessage()
{ {
try try
{ {
VistaType::sint32 nMessageSize; VistaType::sint32 nMessageSize;
int nReturn = m_pConnection->ReadInt32( nMessageSize ); int nReturn = m_pConnection->ReadInt32( nMessageSize );
// we need at least the two protocol ints // we need at least the two protocol ints
assert( nMessageSize >= 3 * sizeof( VistaType::sint32 ) ); assert( nMessageSize >= 2 * sizeof( VistaType::sint32 ) );
if( nMessageSize > ( int ) m_vecIncomingBuffer.size() ) if( nMessageSize > ( int ) m_vecIncomingBuffer.size() )
m_vecIncomingBuffer.resize( nMessageSize ); m_vecIncomingBuffer.resize( nMessageSize );
nReturn = m_pConnection->ReadRawBuffer( &m_vecIncomingBuffer[ 0 ], nMessageSize ); nReturn = m_pConnection->ReadRawBuffer( &m_vecIncomingBuffer[ 0 ], nMessageSize );
if( nReturn != nMessageSize ) if( nReturn != nMessageSize )
ITA_EXCEPT1( NETWORK_ERROR, "Protokoll error, Received less bytes than expected" ); ITA_EXCEPT1( NETWORK_ERROR, "Protokoll error, Received less bytes than expected" );
m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nReturn ); m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nReturn );
// DEBUG: std::cout << "Remainign Size after Mesage Read: " << m_pConnection->PendingDataSize() << std::endl; // DEBUG: std::cout << "Remainign Size after Mesage Read: " << m_pConnection->PendingDataSize() << std::endl;
} }
catch( VistaExceptionBase& ex ) catch( VistaExceptionBase& ex )
{ {
ITA_EXCEPT1( UNKNOWN, ex.GetExceptionText() ); ITA_EXCEPT1( UNKNOWN, ex.GetExceptionText() );
} }
catch( ITAException& ex ) catch( ITAException& ex )
{ {
ex; ex;
} }
m_nMessageType = ReadInt(); m_nMessageType = ReadInt();
m_nMessageId = ReadInt(); m_nMessageId = ReadInt();
} }
void CITANetAudioMessage::WriteAnswer() void CITANetAudioMessage::WriteAnswer()
{ {
VistaType::byte* pBuffer = ( VistaType::byte* )m_oOutgoing.GetBuffer(); VistaType::byte* pBuffer = ( VistaType::byte* )m_oOutgoing.GetBuffer();
VistaType::sint32 iSwapDummy; VistaType::sint32 iSwapDummy;
// rewrite size dummy // rewrite size dummy
iSwapDummy = m_oOutgoing.GetBufferSize() - sizeof( VistaType::sint32 ); iSwapDummy = m_oOutgoing.GetBufferSize() - sizeof( VistaType::sint32 );
if( m_oOutgoing.GetByteorderSwapFlag() ) if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy ); VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) ); memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
pBuffer += sizeof( VistaType::sint32 ); pBuffer += sizeof( VistaType::sint32 );
// rewrite type dummy // rewrite type dummy
iSwapDummy = m_nAnswerType; iSwapDummy = m_nAnswerType;
if( m_oOutgoing.GetByteorderSwapFlag() ) if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy ); VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) ); memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
pBuffer += sizeof( VistaType::sint32 ); pBuffer += sizeof( VistaType::sint32 );
// rewrite message dummy // rewrite message dummy
iSwapDummy = m_nMessageId; iSwapDummy = m_nMessageId;
if( m_oOutgoing.GetByteorderSwapFlag() ) if( m_oOutgoing.GetByteorderSwapFlag() )
VistaSerializingToolset::Swap4( &iSwapDummy ); VistaSerializingToolset::Swap4( &iSwapDummy );
memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) ); memcpy( pBuffer, &iSwapDummy, sizeof( VistaType::sint32 ) );
try try
{ {
int nRet = m_pConnection->WriteRawBuffer( m_oOutgoing.GetBuffer(), m_oOutgoing.GetBufferSize() ); int nRet = m_pConnection->WriteRawBuffer( m_oOutgoing.GetBuffer(), m_oOutgoing.GetBufferSize() );
m_pConnection->WaitForSendFinish(); m_pConnection->WaitForSendFinish();
if( nRet != m_oOutgoing.GetBufferSize() ) if( nRet != m_oOutgoing.GetBufferSize() )
ITA_EXCEPT1( UNKNOWN, "Could not write the expected number of bytes" ); ITA_EXCEPT1( UNKNOWN, "Could not write the expected number of bytes" );
} }
catch( VistaExceptionBase& ex ) catch( VistaExceptionBase& ex )
{ {
ITA_EXCEPT1( UNKNOWN, ex.GetExceptionText() ); ITA_EXCEPT1( UNKNOWN, ex.GetExceptionText() );
} }
} }
void CITANetAudioMessage::ReadAnswer() void CITANetAudioMessage::ReadAnswer()
{ {
try try
{ {
VistaType::sint32 nMessageSize; VistaType::sint32 nMessageSize;
int nReturn; int nReturn;
try try
{ {
nReturn = m_pConnection->ReadInt32( nMessageSize ); nReturn = m_pConnection->ReadInt32( nMessageSize );
} }
catch( ... ) catch( ... )
{ {
nReturn = -1; // Network connection error nReturn = -1; // Network connection error
} }
if( nReturn != sizeof( VistaType::sint32 ) ) if( nReturn != sizeof( VistaType::sint32 ) )
{ {
ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected" ); ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected" );
} }
// we need at least the two protocol types // we need at least the two protocol types
assert( nMessageSize >= 2 * sizeof( VistaType::sint32 ) ); assert( nMessageSize >= 2 * sizeof( VistaType::sint32 ) );
if( nMessageSize > ( int ) m_vecIncomingBuffer.size() ) if( nMessageSize > ( int ) m_vecIncomingBuffer.size() )
m_vecIncomingBuffer.resize( nMessageSize ); m_vecIncomingBuffer.resize( nMessageSize );
// jst: hier nicht while( nReturn < nMessageSize) ReadRawBuffer?? // jst: hier nicht while( nReturn < nMessageSize) ReadRawBuffer??
nReturn = m_pConnection->ReadRawBuffer( &m_vecIncomingBuffer[ 0 ], nMessageSize ); nReturn = m_pConnection->ReadRawBuffer( &m_vecIncomingBuffer[ 0 ], nMessageSize );
if( nReturn != nMessageSize ) if( nReturn != nMessageSize )
ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected" ); ITA_EXCEPT1( UNKNOWN, "Protokoll error, Received less bytes than expected" );
m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nReturn ); m_oIncoming.SetBuffer( &m_vecIncomingBuffer[ 0 ], nReturn );
} }
catch( VistaExceptionBase& ex ) catch( VistaExceptionBase& ex )
{ {
// Probable connection loss // Probable connection loss
return; return;
ITA_EXCEPT1( UNKNOWN, ex.GetExceptionText() ); ITA_EXCEPT1( UNKNOWN, ex.GetExceptionText() );
} }
catch( ITAException& ex ) catch( ITAException& ex )
{ {
std::string sErrorText = ex.ToString(); std::string sErrorText = ex.ToString();
} }
try try
{ {
m_nAnswerType = ReadInt(); // TODO: assert weg, dafr Kontrolle falls Server crasht< m_nAnswerType = ReadInt(); // TODO: assert weg, dafr Kontrolle falls Server crasht<
int nMessageID = ReadInt(); int nMessageID = ReadInt();
assert( nMessageID == m_nMessageId ); assert( nMessageID == m_nMessageId );
m_nMessageId = nMessageID; m_nMessageId = nMessageID;
} }
catch( ITAException& ex ) catch( ITAException& ex )
{ {
std::cerr << "ITANetAudioMessage: Protocol error: " << ex << std::endl; std::cerr << "ITANetAudioMessage: Protocol error: " << ex << std::endl;
} }
} }
int CITANetAudioMessage::GetMessageType() const int CITANetAudioMessage::GetMessageType() const
{ {
return m_nMessageType; return m_nMessageType;
} }
void CITANetAudioMessage::SetMessageType( int nType ) void CITANetAudioMessage::SetMessageType( int nType )
{ {
assert( m_nMessageType == CITANetAudioProtocol::NP_INVALID ); // should only be set once assert( m_nMessageType == CITANetAudioProtocol::NP_INVALID ); // should only be set once
m_nMessageType = nType; m_nMessageType = nType;
} }
void CITANetAudioMessage::SetAnswerType( int nType ) void CITANetAudioMessage::SetAnswerType( int nType )
{ {
assert( m_nAnswerType == CITANetAudioProtocol::NP_INVALID ); // should only be set once assert( m_nAnswerType == CITANetAudioProtocol::NP_INVALID ); // should only be set once
m_nAnswerType = nType; m_nAnswerType = nType;
} }
int CITANetAudioMessage::GetAnswerType() const int CITANetAudioMessage::GetAnswerType() const
{ {
return m_nAnswerType; return m_nAnswerType;
} }
int CITANetAudioMessage::GetIncomingMessageSize() const int CITANetAudioMessage::GetIncomingMessageSize() const
{ {
return m_oIncoming.GetTailSize(); return m_oIncoming.GetTailSize();
} }
int CITANetAudioMessage::GetOutgoingMessageSize() const int CITANetAudioMessage::GetOutgoingMessageSize() const
{ {
return m_oOutgoing.GetBufferSize(); return m_oOutgoing.GetBufferSize();
} }
bool CITANetAudioMessage::GetOutgoingMessageHasData() const bool CITANetAudioMessage::GetOutgoingMessageHasData() const
{ {
return ( m_oOutgoing.GetBufferSize() > 4 * sizeof( VistaType::sint32 ) ); return ( m_oOutgoing.GetBufferSize() > 4 * sizeof( VistaType::sint32 ) );
} }
void CITANetAudioMessage::WriteString( const std::string& sValue ) void CITANetAudioMessage::WriteString( const std::string& sValue )
{ {
m_oOutgoing.WriteInt32( ( VistaType::sint32 )sValue.size() ); m_oOutgoing.WriteInt32( ( VistaType::sint32 )sValue.size() );
if( !sValue.empty() ) m_oOutgoing.WriteString( sValue ); if( !sValue.empty() ) m_oOutgoing.WriteString( sValue );
} }
void CITANetAudioMessage::WriteInt( const int iValue ) void CITANetAudioMessage::WriteInt( const int iValue )
{ {
m_oOutgoing.WriteInt32( ( VistaType::sint32 )iValue ); m_oOutgoing.WriteInt32( ( VistaType::sint32 )iValue );
} }
void CITANetAudioMessage::WriteBool( const bool bValue ) void CITANetAudioMessage::WriteBool( const bool bValue )
{ {
m_oOutgoing.WriteBool( bValue ); m_oOutgoing.WriteBool( bValue );
} }
void CITANetAudioMessage::WriteFloat( const float fValue ) void CITANetAudioMessage::WriteFloat( const float fValue )
{ {
m_oOutgoing.WriteFloat32( fValue ); m_oOutgoing.WriteFloat32( fValue );
} }
void CITANetAudioMessage::WriteDouble( const double dValue ) void CITANetAudioMessage::WriteDouble( const double dValue )
{ {
m_oOutgoing.WriteFloat64( dValue ); m_oOutgoing.WriteFloat64( dValue );
} }
std::string CITANetAudioMessage::ReadString() std::string CITANetAudioMessage::ReadString()
{ {
VistaType::sint32 nSize; VistaType::sint32 nSize;
int nReturn = m_oIncoming.ReadInt32( nSize ); int nReturn = m_oIncoming.ReadInt32( nSize );
assert( nReturn == sizeof( VistaType::sint32 ) ); assert( nReturn == sizeof( VistaType::sint32 ) );
// Empty string? // Empty string?
if( nSize == 0 ) return ""; if( nSize == 0 ) return "";
std::string sValue; std::string sValue;
nReturn = m_oIncoming.ReadString( sValue, nSize ); nReturn = m_oIncoming.ReadString( sValue, nSize );
assert( nReturn == nSize ); assert( nReturn == nSize );
return sValue; return sValue;
} }
int CITANetAudioMessage::ReadInt() int CITANetAudioMessage::ReadInt()
{ {
VistaType::sint32 nValue; VistaType::sint32 nValue;
int nReturn = m_oIncoming.ReadInt32( nValue ); int nReturn = m_oIncoming.ReadInt32( nValue );
if( nReturn == -1 ) if( nReturn == -1 )
ITA_EXCEPT1( UNKNOWN, "Could not read integer value from incoming message" ); ITA_EXCEPT1( UNKNOWN, "Could not read integer value from incoming message" );
assert( nReturn == sizeof( VistaType::sint32 ) ); assert( nReturn == sizeof( VistaType::sint32 ) );
return nValue; return nValue;