use mutexlocker in protocol class

This commit is contained in:
Volker Fischer 2008-08-17 19:50:28 +00:00
parent 6fb18b16df
commit fb363895ab
3 changed files with 38 additions and 44 deletions

View file

@ -184,7 +184,9 @@ void CChannelSet::CreateAndSendChatTextForAllConChannels ( const int iCurChanID,
ChanName = addrTest.toString(); ChanName = addrTest.toString();
} }
// add name of the client at the beginning of the message text // add name of the client at the beginning of the message text and use
// different colors, to get correct HTML, the "<" and ">" signs must be
// replaced by "&#60;" and "&#62;"
QString sCurColor = vstrChatColors[iCurChanID % vstrChatColors.Size()]; QString sCurColor = vstrChatColors[iCurChanID % vstrChatColors.Size()];
const QString strActualMessageText = const QString strActualMessageText =
"<font color=""" + sCurColor + """><b>&#60;" + ChanName + "<font color=""" + sCurColor + """><b>&#60;" + ChanName +
@ -213,7 +215,7 @@ int CChannelSet::GetFreeChan()
} }
} }
/* no free channel found, return invalid ID */ // no free channel found, return invalid ID
return INVALID_CHANNEL_ID; return INVALID_CHANNEL_ID;
} }
@ -221,12 +223,12 @@ int CChannelSet::CheckAddr ( const CHostAddress& Addr )
{ {
CHostAddress InetAddr; CHostAddress InetAddr;
/* Check for all possible channels if IP is already in use */ // check for all possible channels if IP is already in use
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ ) for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{ {
if ( vecChannels[i].GetAddress ( InetAddr ) ) if ( vecChannels[i].GetAddress ( InetAddr ) )
{ {
/* IP found, return channel number */ // IP found, return channel number
if ( InetAddr == Addr ) if ( InetAddr == Addr )
{ {
return i; return i;
@ -234,7 +236,7 @@ int CChannelSet::CheckAddr ( const CHostAddress& Addr )
} }
} }
/* IP not found, return invalid ID */ // IP not found, return invalid ID
return INVALID_CHANNEL_ID; return INVALID_CHANNEL_ID;
} }
@ -249,7 +251,7 @@ bool CChannelSet::PutData ( const CVector<unsigned char>& vecbyRecBuf,
{ {
bool bChanOK = true; bool bChanOK = true;
// get channel ID ------------------------------------------------------ // Get channel ID ------------------------------------------------------
// check address // check address
int iCurChanID = CheckAddr ( HostAdr ); int iCurChanID = CheckAddr ( HostAdr );
@ -294,7 +296,7 @@ bool CChannelSet::PutData ( const CVector<unsigned char>& vecbyRecBuf,
} }
// put received data in jitter buffer ---------------------------------- // Put received data in jitter buffer ----------------------------------
if ( bChanOK ) if ( bChanOK )
{ {
// put packet in socket buffer // put packet in socket buffer

View file

@ -273,12 +273,12 @@ public slots:
// CODE TAG: MAX_NUM_CHANNELS_TAG // CODE TAG: MAX_NUM_CHANNELS_TAG
// make sure we have MAX_NUM_CHANNELS connections!!! // make sure we have MAX_NUM_CHANNELS connections!!!
// send message // send message
void OnSendProtMessCh0 ( CVector<uint8_t> mess ) {emit MessReadyForSending ( 0, mess ); } void OnSendProtMessCh0 ( CVector<uint8_t> mess ) { emit MessReadyForSending ( 0, mess ); }
void OnSendProtMessCh1 ( CVector<uint8_t> mess ) {emit MessReadyForSending ( 1, mess ); } void OnSendProtMessCh1 ( CVector<uint8_t> mess ) { emit MessReadyForSending ( 1, mess ); }
void OnSendProtMessCh2 ( CVector<uint8_t> mess ) {emit MessReadyForSending ( 2, mess ); } void OnSendProtMessCh2 ( CVector<uint8_t> mess ) { emit MessReadyForSending ( 2, mess ); }
void OnSendProtMessCh3 ( CVector<uint8_t> mess ) {emit MessReadyForSending ( 3, mess ); } void OnSendProtMessCh3 ( CVector<uint8_t> mess ) { emit MessReadyForSending ( 3, mess ); }
void OnSendProtMessCh4 ( CVector<uint8_t> mess ) {emit MessReadyForSending ( 4, mess ); } void OnSendProtMessCh4 ( CVector<uint8_t> mess ) { emit MessReadyForSending ( 4, mess ); }
void OnSendProtMessCh5 ( CVector<uint8_t> mess ) {emit MessReadyForSending ( 5, mess ); } void OnSendProtMessCh5 ( CVector<uint8_t> mess ) { emit MessReadyForSending ( 5, mess ); }
void OnNewConnectionCh0() { vecChannels[0].CreateReqJitBufMes(); } void OnNewConnectionCh0() { vecChannels[0].CreateReqJitBufMes(); }
void OnNewConnectionCh1() { vecChannels[1].CreateReqJitBufMes(); } void OnNewConnectionCh1() { vecChannels[1].CreateReqJitBufMes(); }

View file

@ -146,14 +146,10 @@ void CProtocol::EnqueueMessage ( CVector<uint8_t>& vecMessage,
{ {
// check if list is empty so that we have to initiate a send process // check if list is empty so that we have to initiate a send process
bListWasEmpty = SendMessQueue.empty(); bListWasEmpty = SendMessQueue.empty();
}
Mutex.unlock();
// create send message object for the queue // create send message object for the queue
CSendMessage SendMessageObj ( vecMessage, iCnt, iID ); CSendMessage SendMessageObj ( vecMessage, iCnt, iID );
Mutex.lock();
{
// we want to have a FIFO: we add at the end and take from the beginning // we want to have a FIFO: we add at the end and take from the beginning
SendMessQueue.push_back ( SendMessageObj ); SendMessQueue.push_back ( SendMessageObj );
} }
@ -168,22 +164,20 @@ void CProtocol::EnqueueMessage ( CVector<uint8_t>& vecMessage,
void CProtocol::SendMessage() void CProtocol::SendMessage()
{ {
CVector<uint8_t> vecMessage; QMutexLocker locker ( &Mutex );
bool bSendMess = false;
Mutex.lock(); CVector<uint8_t> vecMessage;
bool bSendMess = false;
// we have to check that list is not empty, since in another thread the
// last element of the list might have been erased
if ( !SendMessQueue.empty() )
{ {
// we have to check that list is not empty, since in another thread the vecMessage.Init ( SendMessQueue.front().vecMessage.Size() );
// last element of the list might have been erased vecMessage = SendMessQueue.front().vecMessage;
if ( !SendMessQueue.empty() )
{
vecMessage.Init ( SendMessQueue.front().vecMessage.Size() );
vecMessage = SendMessQueue.front().vecMessage;
bSendMess = true; bSendMess = true;
}
} }
Mutex.unlock();
if ( bSendMess ) if ( bSendMess )
{ {
@ -206,8 +200,8 @@ void CProtocol::SendMessage()
void CProtocol::CreateAndSendMessage ( const int iID, void CProtocol::CreateAndSendMessage ( const int iID,
const CVector<uint8_t>& vecData ) const CVector<uint8_t>& vecData )
{ {
CVector<uint8_t> vecNewMessage; CVector<uint8_t> vecNewMessage;
int iCurCounter; int iCurCounter;
Mutex.lock(); Mutex.lock();
{ {
@ -228,9 +222,9 @@ void CProtocol::CreateAndSendMessage ( const int iID,
void CProtocol::CreateAndSendAcknMess ( const int& iID, const int& iCnt ) void CProtocol::CreateAndSendAcknMess ( const int& iID, const int& iCnt )
{ {
CVector<uint8_t> vecAcknMessage; CVector<uint8_t> vecAcknMessage;
CVector<uint8_t> vecData ( 2 ); // 2 bytes of data CVector<uint8_t> vecData ( 2 ); // 2 bytes of data
unsigned int iPos = 0; // init position pointer unsigned int iPos = 0; // init position pointer
// build data vector // build data vector
PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( iID ), 2 ); PutValOnStream ( vecData, iPos, static_cast<uint32_t> ( iID ), 2 );
@ -244,12 +238,10 @@ void CProtocol::CreateAndSendAcknMess ( const int& iID, const int& iCnt )
void CProtocol::DeleteSendMessQueue() void CProtocol::DeleteSendMessQueue()
{ {
Mutex.lock(); QMutexLocker locker ( &Mutex );
{
// delete complete "send message queue" // delete complete "send message queue"
SendMessQueue.clear(); SendMessQueue.clear();
}
Mutex.unlock();
} }
bool CProtocol::ParseMessage ( const CVector<unsigned char>& vecbyData, bool CProtocol::ParseMessage ( const CVector<unsigned char>& vecbyData,
@ -283,7 +275,7 @@ for ( int i = 0; i < iNumBytes; i++ ) {
// acknowledgments are not acknowledged // acknowledgments are not acknowledged
if ( iRecID != PROTMESSID_ACKN ) if ( iRecID != PROTMESSID_ACKN )
{ {
// re-send acknowledgement // resend acknowledgement
CreateAndSendAcknMess ( iRecID, iRecCounter ); CreateAndSendAcknMess ( iRecID, iRecCounter );
} }
} }
@ -382,7 +374,7 @@ for ( int i = 0; i < iNumBytes; i++ ) {
} }
} }
// save current message ID and counter to find out if message was re-sent // save current message ID and counter to find out if message was resent
iOldRecID = iRecID; iOldRecID = iRecID;
iOldRecCnt = iRecCounter; iOldRecCnt = iRecCounter;
} }