From 956bb262119a23c6b3386b83a6486a5dd5f69c28 Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Sun, 10 Dec 2006 12:02:28 +0000 Subject: [PATCH] fixed problem: protocol message was treated as audio packet --- src/channel.cpp | 119 +++++++++++++++++++++++++---------------------- src/protocol.cpp | 31 ++++++++---- src/protocol.h | 4 +- 3 files changed, 88 insertions(+), 66 deletions(-) diff --git a/src/channel.cpp b/src/channel.cpp index 608beff3..d00d9e87 100755 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -583,38 +583,59 @@ EPutDataStat CChannel::PutData ( const CVector& vecbyData, { EPutDataStat eRet = PS_GEN_ERROR; + // init flags + bool bIsProtocolPacket = false; + bool bIsAudioPacket = false; + bool bNewConnection = false; + if ( bIsEnabled ) { - bool bNewConnection = false; - bool bIsAudioPacket = false; - - // check if this is an audio packet by checking all possible lengths - for ( int i = 0; i < MAX_NET_BLOCK_SIZE_FACTOR; i++ ) + // first check if this is protocol data + // only use protocol data if channel is connected + if ( IsConnected() ) { - if ( iNumBytes == vecNetwInBufSizes[i] ) + // this seems not to be an audio block, parse the message + if ( Protocol.ParseMessage ( vecbyData, iNumBytes ) ) { - bIsAudioPacket = true; + // set status flags + eRet = PS_PROT_OK; + bIsProtocolPacket = true; - // check if we are correctly initialized - const int iNewNetwInBlSiFact = i + 1; - if ( iNewNetwInBlSiFact != iCurNetwInBlSiFact ) - { - // re-initialize to new value - SetNetwInBlSiFact ( iNewNetwInBlSiFact ); - } + // create message for protocol status + emit ProtocolStatus ( true ); } } - - // only process if packet has correct size - if ( bIsAudioPacket ) - { - Mutex.lock(); + + // only try to parse audio if it was not a protocol packet + if ( !bIsProtocolPacket ) + { + // check if this is an audio packet by checking all possible lengths + for ( int i = 0; i < MAX_NET_BLOCK_SIZE_FACTOR; i++ ) { - // decompress audio - CVector vecsDecomprAudio ( AudioCompressionIn.Decode ( vecbyData ) ); + if ( iNumBytes == vecNetwInBufSizes[i] ) + { + bIsAudioPacket = true; + + // check if we are correctly initialized + const int iNewNetwInBlSiFact = i + 1; + if ( iNewNetwInBlSiFact != iCurNetwInBlSiFact ) + { + // re-initialize to new value + SetNetwInBlSiFact ( iNewNetwInBlSiFact ); + } + } + } - // do resampling to compensate for sample rate offsets in the - // different sound cards of the clients + // only process if packet has correct size + if ( bIsAudioPacket ) + { + Mutex.lock(); + { + // decompress audio + CVector vecsDecomprAudio ( AudioCompressionIn.Decode ( vecbyData ) ); + + // do resampling to compensate for sample rate offsets in the + // different sound cards of the clients /* for (int i = 0; i < BLOCK_SIZE_SAMPLES; i++) vecdResInData[i] = (double) vecsData[i]; @@ -628,43 +649,31 @@ for ( int i = 0; i < iCurNetwInBlSiFact * MIN_BLOCK_SIZE_SAMPLES; i++ ) { vecdResOutData[i] = (double) vecsDecomprAudio[i]; } - if ( SockBuf.Put ( vecdResOutData ) ) - { - eRet = PS_AUDIO_OK; - } - else - { - eRet = PS_AUDIO_ERR; - } + if ( SockBuf.Put ( vecdResOutData ) ) + { + eRet = PS_AUDIO_OK; + } + else + { + eRet = PS_AUDIO_ERR; + } - // check if channel was not connected, this is a new connection - bNewConnection = !IsConnected(); + // check if channel was not connected, this is a new connection + bNewConnection = !IsConnected(); - // reset time-out counter - iConTimeOut = iConTimeOutStartVal; + // reset time-out counter + iConTimeOut = iConTimeOutStartVal; + } + Mutex.unlock(); } - Mutex.unlock(); - } - else - { - // only use protocol data if channel is connected - if ( IsConnected() ) + else { - // this seems not to be an audio block, parse the message - if ( Protocol.ParseMessage ( vecbyData, iNumBytes ) ) - { - eRet = PS_PROT_OK; + // the protocol parsing failed and this was no audio block, + // we treat this as protocol error (unkown packet) + eRet = PS_PROT_ERR; - // create message for protocol status - emit ProtocolStatus ( true ); - } - else - { - eRet = PS_PROT_ERR; - - // create message for protocol status - emit ProtocolStatus ( false ); - } + // create message for protocol status + emit ProtocolStatus ( false ); } } diff --git a/src/protocol.cpp b/src/protocol.cpp index b744a0ea..209b3f04 100755 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -15,10 +15,11 @@ Protocol message definition MAIN FRAME ---------- - +------------+------------+------------------+--------------+-------------+ - | 2 bytes ID | 1 byte cnt | 2 bytes length n | n bytes data | 2 bytes CRC | - +------------+------------+------------------+--------------+-------------+ + +-------------+------------+------------+------------------+--------------+-------------+ + | 2 bytes TAG | 2 bytes ID | 1 byte cnt | 2 bytes length n | n bytes data | 2 bytes CRC | + +-------------+------------+------------+------------------+--------------+-------------+ +- TAG is an all zero bit word to identify protocol messages - message ID defined by the defines PROTMESSID_x - cnt: counter which is increment for each message and wraps around at 255 - length n in bytes of the data @@ -639,6 +640,15 @@ bool CProtocol::ParseMessageFrame ( const CVector& vecIn, // decode header ----- iCurPos = 0; // start from beginning + // 2 bytes TAG + const int iTag = static_cast ( GetValFromStream ( vecIn, iCurPos, 2 ) ); + + // check if tag is correct + if ( iTag != 0 ) + { + return false; // return error code + } + /* 2 bytes ID */ iID = static_cast ( GetValFromStream ( vecIn, iCurPos, 2 ) ); @@ -715,9 +725,8 @@ void CProtocol::GenMessageFrame ( CVector& vecOut, // query length of data vector const int iDataLenByte = vecData.Size(); - // total length of message = 7 + "iDataLenByte" - // 2 byte ID + 1 byte cnt + 2 byte length + n bytes data + 2 bytes CRC - const int iTotLenByte = 7 + iDataLenByte; + // total length of message + const int iTotLenByte = MESS_LEN_WITHOUT_DATA_BYTE + iDataLenByte; // init message vector vecOut.Init( iTotLenByte ); @@ -725,15 +734,19 @@ void CProtocol::GenMessageFrame ( CVector& vecOut, // encode header ----- unsigned int iCurPos = 0; // init position pointer - /* 2 bytes ID */ + // 2 bytes TAG (all zero bits) + PutValOnStream ( vecOut, iCurPos, + static_cast ( 0 ), 2 ); + + // 2 bytes ID PutValOnStream ( vecOut, iCurPos, static_cast ( iID ), 2 ); - /* 1 byte cnt */ + // 1 byte cnt PutValOnStream ( vecOut, iCurPos, static_cast ( iCnt ), 1 ); - /* 2 bytes length */ + // 2 bytes length PutValOnStream ( vecOut, iCurPos, static_cast ( iDataLenByte ), 2 ); diff --git a/src/protocol.h b/src/protocol.h index 31e5af70..5de01fd4 100755 --- a/src/protocol.h +++ b/src/protocol.h @@ -48,8 +48,8 @@ #define PROTMESSID_CHANNEL_NAME 18 // set channel name for fader tag // lengths of message as defined in protocol.cpp file -#define MESS_HEADER_LENGTH_BYTE 5 /* ID, cnt, length */ -#define MESS_LEN_WITHOUT_DATA_BYTE ( MESS_HEADER_LENGTH_BYTE + 2 /* CRC */ ) +#define MESS_HEADER_LENGTH_BYTE 7 /* TAG (2), ID (2), cnt (1), length (2) */ +#define MESS_LEN_WITHOUT_DATA_BYTE ( MESS_HEADER_LENGTH_BYTE + 2 /* CRC (2) */ ) // time out for message re-send if no acknowledgement was received #define SEND_MESS_TIMEOUT_MS 400 // ms