use mutexlocker class for nicer code

This commit is contained in:
Volker Fischer 2008-08-15 06:56:49 +00:00
parent 7445505c12
commit d2dc9f6834

View file

@ -611,6 +611,8 @@ CChannel::CChannel() : sName ( "" ),
void CChannel::SetEnable ( const bool bNEnStat ) void CChannel::SetEnable ( const bool bNEnStat )
{ {
QMutexLocker locker ( &Mutex );
// set internal parameter // set internal parameter
bIsEnabled = bNEnStat; bIsEnabled = bNEnStat;
@ -624,40 +626,36 @@ void CChannel::SetEnable ( const bool bNEnStat )
void CChannel::SetNetwInBlSiFactAndCompr ( const int iNewBlockSizeFactor, void CChannel::SetNetwInBlSiFactAndCompr ( const int iNewBlockSizeFactor,
const CAudioCompression::EAudComprType eNewAudComprType ) const CAudioCompression::EAudComprType eNewAudComprType )
{ {
Mutex.lock(); QMutexLocker locker ( &Mutex );
{
// store new value
iCurNetwInBlSiFact = iNewBlockSizeFactor;
// init audio compression unit // store new value
AudioCompressionIn.Init ( iNewBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES, iCurNetwInBlSiFact = iNewBlockSizeFactor;
eNewAudComprType );
// initial value for connection time out counter // init audio compression unit
iConTimeOutStartVal = ( CON_TIME_OUT_SEC_MAX * 1000 ) / AudioCompressionIn.Init ( iNewBlockSizeFactor * MIN_BLOCK_SIZE_SAMPLES,
( iNewBlockSizeFactor * MIN_BLOCK_DURATION_MS ); eNewAudComprType );
// socket buffer must be adjusted // initial value for connection time out counter
SetSockBufSizeIntern ( GetSockBufSize() ); iConTimeOutStartVal = ( CON_TIME_OUT_SEC_MAX * 1000 ) /
} ( iNewBlockSizeFactor * MIN_BLOCK_DURATION_MS );
Mutex.unlock();
// socket buffer must be adjusted
SetSockBufSizeIntern ( GetSockBufSize() );
} }
void CChannel::SetNetwBufSizeFactOut ( const int iNewNetwBlSiFactOut ) void CChannel::SetNetwBufSizeFactOut ( const int iNewNetwBlSiFactOut )
{ {
Mutex.lock(); QMutexLocker locker ( &Mutex );
{
// store new value
iCurNetwOutBlSiFact = iNewNetwBlSiFactOut;
// init audio compression and get audio compression block size // store new value
iAudComprSizeOut = AudioCompressionOut.Init ( iCurNetwOutBlSiFact = iNewNetwBlSiFactOut;
iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES, eAudComprTypeOut );
// init conversion buffer // init audio compression and get audio compression block size
ConvBuf.Init ( iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES ); iAudComprSizeOut = AudioCompressionOut.Init (
} iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES, eAudComprTypeOut );
Mutex.unlock();
// init conversion buffer
ConvBuf.Init ( iNewNetwBlSiFactOut * MIN_BLOCK_SIZE_SAMPLES );
} }
void CChannel::SetAudioCompressionOut ( const CAudioCompression::EAudComprType eNewAudComprTypeOut ) void CChannel::SetAudioCompressionOut ( const CAudioCompression::EAudComprType eNewAudComprTypeOut )
@ -673,12 +671,9 @@ void CChannel::SetAudioCompressionOut ( const CAudioCompression::EAudComprType e
void CChannel::SetSockBufSize ( const int iNumBlocks ) void CChannel::SetSockBufSize ( const int iNumBlocks )
{ {
// this opperation must be done with mutex QMutexLocker locker ( &Mutex ); // this opperation must be done with mutex
Mutex.lock();
{ SetSockBufSizeIntern ( iNumBlocks );
SetSockBufSizeIntern ( iNumBlocks );
}
Mutex.unlock();
} }
void CChannel::SetSockBufSizeIntern ( const int iNumBlocks ) void CChannel::SetSockBufSizeIntern ( const int iNumBlocks )
@ -697,32 +692,28 @@ void CChannel::SetSockBufSizeIntern ( const int iNumBlocks )
void CChannel::SetGain ( const int iChanID, const double dNewGain ) void CChannel::SetGain ( const int iChanID, const double dNewGain )
{ {
Mutex.lock(); QMutexLocker locker ( &Mutex );
// set value (make sure channel ID is in range)
if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) )
{ {
// set value (make sure channel ID is in range) vecdGains[iChanID] = dNewGain;
if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) )
{
vecdGains[iChanID] = dNewGain;
}
} }
Mutex.unlock();
} }
double CChannel::GetGain ( const int iChanID ) double CChannel::GetGain ( const int iChanID )
{ {
double dReturnVal = 0; QMutexLocker locker ( &Mutex );
Mutex.lock(); // get value (make sure channel ID is in range)
if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) )
{ {
// get value (make sure channel ID is in range) return vecdGains[iChanID];
if ( ( iChanID >= 0 ) && ( iChanID < MAX_NUM_CHANNELS ) ) }
{ else
dReturnVal = vecdGains[iChanID]; {
} return 0;
} }
Mutex.unlock();
return dReturnVal;
} }
void CChannel::SetName ( const QString strNewName ) void CChannel::SetName ( const QString strNewName )
@ -751,15 +742,9 @@ QString CChannel::GetName()
{ {
// make sure the string is not written at the same time when it is // make sure the string is not written at the same time when it is
// read here -> use mutex to secure access // read here -> use mutex to secure access
QString strReturn; QMutexLocker locker ( &Mutex );
Mutex.lock(); return sName;
{
strReturn = sName;
}
Mutex.unlock();
return strReturn;
} }
void CChannel::OnSendProtMessage ( CVector<uint8_t> vecMessage ) void CChannel::OnSendProtMessage ( CVector<uint8_t> vecMessage )
@ -799,24 +784,18 @@ void CChannel::OnChangeChanName ( QString strName )
bool CChannel::GetAddress(CHostAddress& RetAddr) bool CChannel::GetAddress(CHostAddress& RetAddr)
{ {
bool bReturnFlag; QMutexLocker locker ( &Mutex );
Mutex.lock(); if ( IsConnected() )
{ {
if ( IsConnected() ) RetAddr = InetAddr;
{ return true;
RetAddr = InetAddr; }
bReturnFlag = true; else
} {
else RetAddr = CHostAddress();
{ return false;
RetAddr = CHostAddress();
bReturnFlag = false;
}
} }
Mutex.unlock();
return bReturnFlag;
} }
EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData, EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData,
@ -946,59 +925,55 @@ EPutDataStat CChannel::PutData ( const CVector<unsigned char>& vecbyData,
EGetDataStat CChannel::GetData ( CVector<double>& vecdData ) EGetDataStat CChannel::GetData ( CVector<double>& vecdData )
{ {
QMutexLocker locker ( &Mutex );
// init with ok flag // init with ok flag
EGetDataStat eGetStatus = GS_BUFFER_OK; EGetDataStat eGetStatus = GS_BUFFER_OK;
Mutex.lock(); // get mutex lock if ( !SockBuf.Get ( vecdData ) )
{ {
if ( !SockBuf.Get ( vecdData ) ) // decrease time-out counter
if ( iConTimeOut > 0 )
{ {
// decrease time-out counter iConTimeOut--;
if ( iConTimeOut > 0 )
{
iConTimeOut--;
if ( iConTimeOut == 0 ) if ( iConTimeOut == 0 )
{ {
// channel is just disconnected // channel is just disconnected
eGetStatus = GS_CHAN_NOW_DISCONNECTED; eGetStatus = GS_CHAN_NOW_DISCONNECTED;
}
else
{
// channel is not yet disconnected but no data in buffer
eGetStatus = GS_BUFFER_UNDERRUN;
}
} }
else else
{ {
// channel is disconnected // channel is not yet disconnected but no data in buffer
eGetStatus = GS_CHAN_NOT_CONNECTED; eGetStatus = GS_BUFFER_UNDERRUN;
} }
} }
else
{
// channel is disconnected
eGetStatus = GS_CHAN_NOT_CONNECTED;
}
} }
Mutex.unlock(); // get mutex unlock
return eGetStatus; return eGetStatus;
} }
CVector<unsigned char> CChannel::PrepSendPacket ( const CVector<short>& vecsNPacket ) CVector<unsigned char> CChannel::PrepSendPacket ( const CVector<short>& vecsNPacket )
{ {
QMutexLocker locker ( &Mutex );
// if the block is not ready we have to initialize with zero length to // if the block is not ready we have to initialize with zero length to
// tell the following network send routine that nothing should be sent // tell the following network send routine that nothing should be sent
CVector<unsigned char> vecbySendBuf ( 0 ); CVector<unsigned char> vecbySendBuf ( 0 );
Mutex.lock(); // get mutex lock // use conversion buffer to convert sound card block size in network
// block size
if ( ConvBuf.Put ( vecsNPacket ) )
{ {
// use conversion buffer to convert sound card block size in network // a packet is ready, compress audio
// block size vecbySendBuf.Init ( iAudComprSizeOut );
if ( ConvBuf.Put ( vecsNPacket ) ) vecbySendBuf = AudioCompressionOut.Encode ( ConvBuf.Get() );
{
// a packet is ready, compress audio
vecbySendBuf.Init ( iAudComprSizeOut );
vecbySendBuf = AudioCompressionOut.Encode ( ConvBuf.Get() );
}
} }
Mutex.unlock(); // get mutex unlock
return vecbySendBuf; return vecbySendBuf;
} }