added more mutex in the server (may fix #480)

This commit is contained in:
Volker Fischer 2020-07-31 19:21:31 +02:00
parent e5174ef461
commit df0ec35d05
3 changed files with 70 additions and 69 deletions

View file

@ -15,6 +15,8 @@
- support MIDI control faders in headless build (#483) - support MIDI control faders in headless build (#483)
- added more mutex in the server (may fix #480)
- bug fix: --showallservers ping column sort is alphabetic (#201) - bug fix: --showallservers ping column sort is alphabetic (#201)

View file

@ -599,6 +599,8 @@ void CServer::SendProtMessage ( int iChID, CVector<uint8_t> vecMessage )
void CServer::OnNewConnection ( int iChID, void CServer::OnNewConnection ( int iChID,
CHostAddress RecHostAddr ) CHostAddress RecHostAddr )
{ {
QMutexLocker locker ( &Mutex );
// inform the client about its own ID at the server (note that this // inform the client about its own ID at the server (note that this
// must be the first message to be sent for a new connection) // must be the first message to be sent for a new connection)
vecChannels[iChID].CreateClientIDMes ( iChID ); vecChannels[iChID].CreateClientIDMes ( iChID );
@ -669,6 +671,8 @@ void CServer::OnNewConnection ( int iChID,
void CServer::OnServerFull ( CHostAddress RecHostAddr ) void CServer::OnServerFull ( CHostAddress RecHostAddr )
{ {
// note: no mutex required here
// inform the calling client that no channel is free // inform the calling client that no channel is free
ConnLessProtocol.CreateCLServerFullMes ( RecHostAddr ); ConnLessProtocol.CreateCLServerFullMes ( RecHostAddr );
} }
@ -681,16 +685,6 @@ void CServer::OnSendCLProtMessage ( CHostAddress InetAddr,
Socket.SendPacket ( vecMessage, InetAddr ); Socket.SendPacket ( vecMessage, InetAddr );
} }
void CServer::OnProtcolCLMessageReceived ( int iRecID,
CVector<uint8_t> vecbyMesBodyData,
CHostAddress RecHostAddr )
{
// connection less messages are always processed
ConnLessProtocol.ParseConnectionLessMessageBody ( vecbyMesBodyData,
iRecID,
RecHostAddr );
}
void CServer::OnCLDisconnection ( CHostAddress InetAddr ) void CServer::OnCLDisconnection ( CHostAddress InetAddr )
{ {
// check if the given address is actually a client which is connected to // check if the given address is actually a client which is connected to
@ -743,7 +737,6 @@ void CServer::OnHandledSignal ( int sigNum )
#else #else
switch ( sigNum ) switch ( sigNum )
{ {
case SIGUSR1: case SIGUSR1:
RequestNewRecording(); RequestNewRecording();
break; break;
@ -1442,26 +1435,36 @@ int CServer::FindChannel ( const CHostAddress& CheckAddr )
return INVALID_CHANNEL_ID; return INVALID_CHANNEL_ID;
} }
void CServer::OnProtcolCLMessageReceived ( int iRecID,
CVector<uint8_t> vecbyMesBodyData,
CHostAddress RecHostAddr )
{
QMutexLocker locker ( &Mutex );
// connection less messages are always processed
ConnLessProtocol.ParseConnectionLessMessageBody ( vecbyMesBodyData,
iRecID,
RecHostAddr );
}
void CServer::OnProtcolMessageReceived ( int iRecCounter, void CServer::OnProtcolMessageReceived ( int iRecCounter,
int iRecID, int iRecID,
CVector<uint8_t> vecbyMesBodyData, CVector<uint8_t> vecbyMesBodyData,
CHostAddress RecHostAddr ) CHostAddress RecHostAddr )
{ {
Mutex.lock(); QMutexLocker locker ( &Mutex );
{
// find the channel with the received address
const int iCurChanID = FindChannel ( RecHostAddr );
// if the channel exists, apply the protocol message to the channel // find the channel with the received address
if ( iCurChanID != INVALID_CHANNEL_ID ) const int iCurChanID = FindChannel ( RecHostAddr );
{
vecChannels[iCurChanID].PutProtcolData ( iRecCounter, // if the channel exists, apply the protocol message to the channel
iRecID, if ( iCurChanID != INVALID_CHANNEL_ID )
vecbyMesBodyData, {
RecHostAddr ); vecChannels[iCurChanID].PutProtcolData ( iRecCounter,
} iRecID,
vecbyMesBodyData,
RecHostAddr );
} }
Mutex.unlock();
} }
bool CServer::PutAudioData ( const CVector<uint8_t>& vecbyRecBuf, bool CServer::PutAudioData ( const CVector<uint8_t>& vecbyRecBuf,
@ -1469,62 +1472,60 @@ bool CServer::PutAudioData ( const CVector<uint8_t>& vecbyRecBuf,
const CHostAddress& HostAdr, const CHostAddress& HostAdr,
int& iCurChanID ) int& iCurChanID )
{ {
QMutexLocker locker ( &Mutex );
bool bNewConnection = false; // init return value bool bNewConnection = false; // init return value
bool bChanOK = true; // init with ok, might be overwritten bool bChanOK = true; // init with ok, might be overwritten
Mutex.lock(); // Get channel ID ------------------------------------------------------
// check address
iCurChanID = FindChannel ( HostAdr );
if ( iCurChanID == INVALID_CHANNEL_ID )
{ {
// Get channel ID ------------------------------------------------------ // a new client is calling, look for free channel
// check address iCurChanID = GetFreeChan();
iCurChanID = FindChannel ( HostAdr );
if ( iCurChanID == INVALID_CHANNEL_ID ) if ( iCurChanID != INVALID_CHANNEL_ID )
{ {
// a new client is calling, look for free channel // initialize current channel by storing the calling host
iCurChanID = GetFreeChan(); // address
vecChannels[iCurChanID].SetAddress ( HostAdr );
if ( iCurChanID != INVALID_CHANNEL_ID ) // reset channel info
vecChannels[iCurChanID].ResetInfo();
// reset the channel gains of current channel, at the same
// time reset gains of this channel ID for all other channels
for ( int i = 0; i < iMaxNumChannels; i++ )
{ {
// initialize current channel by storing the calling host vecChannels[iCurChanID].SetGain ( i, 1.0 );
// address
vecChannels[iCurChanID].SetAddress ( HostAdr );
// reset channel info // other channels (we do not distinguish the case if
vecChannels[iCurChanID].ResetInfo(); // i == iCurChanID for simplicity)
vecChannels[i].SetGain ( iCurChanID, 1.0 );
// reset the channel gains of current channel, at the same
// time reset gains of this channel ID for all other channels
for ( int i = 0; i < iMaxNumChannels; i++ )
{
vecChannels[iCurChanID].SetGain ( i, 1.0 );
// other channels (we do not distinguish the case if
// i == iCurChanID for simplicity)
vecChannels[i].SetGain ( iCurChanID, 1.0 );
}
}
else
{
// no free channel available
bChanOK = false;
} }
} }
else
// Put received audio data in jitter buffer ----------------------------
if ( bChanOK )
{ {
// put packet in socket buffer // no free channel available
if ( vecChannels[iCurChanID].PutAudioData ( vecbyRecBuf, bChanOK = false;
iNumBytesRead, }
HostAdr ) == PS_NEW_CONNECTION ) }
{
// in case we have a new connection return this information
bNewConnection = true; // Put received audio data in jitter buffer ----------------------------
} if ( bChanOK )
{
// put packet in socket buffer
if ( vecChannels[iCurChanID].PutAudioData ( vecbyRecBuf,
iNumBytesRead,
HostAdr ) == PS_NEW_CONNECTION )
{
// in case we have a new connection return this information
bNewConnection = true;
} }
} }
Mutex.unlock();
// return the state if a new connection was happening // return the state if a new connection was happening
return bNewConnection; return bNewConnection;

View file

@ -458,10 +458,8 @@ public slots:
void OnCLReqVersionAndOS ( CHostAddress InetAddr ) void OnCLReqVersionAndOS ( CHostAddress InetAddr )
{ ConnLessProtocol.CreateCLVersionAndOSMes ( InetAddr ); } { ConnLessProtocol.CreateCLVersionAndOSMes ( InetAddr ); }
// the CreateChannelList() function access vecChannels which as to be mutexed
// since the normal server thread my change that at a random time
void OnCLReqConnClientsList ( CHostAddress InetAddr ) void OnCLReqConnClientsList ( CHostAddress InetAddr )
{ QMutexLocker locker ( &Mutex ); ConnLessProtocol.CreateCLConnClientsListMes ( InetAddr, CreateChannelList() ); } { ConnLessProtocol.CreateCLConnClientsListMes ( InetAddr, CreateChannelList() ); }
void OnCLRegisterServerReceived ( CHostAddress InetAddr, void OnCLRegisterServerReceived ( CHostAddress InetAddr,
CHostAddress LInetAddr, CHostAddress LInetAddr,