1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-05 13:47:26 +02:00

Less races in Servent::controlConnections

This commit is contained in:
Uwe L. Korn
2014-01-30 23:26:24 +00:00
parent 19434a492c
commit f67ce789f2
2 changed files with 34 additions and 11 deletions

View File

@@ -365,27 +365,35 @@ Servent::deleteLazyOffer( const QString& key )
void void
Servent::registerControlConnection( ControlConnection* conn ) Servent::registerControlConnection( ControlConnection* conn )
{ {
Q_D( Servent );
Q_ASSERT( conn ); Q_ASSERT( conn );
QMutexLocker locker( &d->controlconnectionsMutex );
tLog( LOGVERBOSE ) << Q_FUNC_INFO << conn->name(); tLog( LOGVERBOSE ) << Q_FUNC_INFO << conn->name();
d_func()->controlconnections << conn; d->controlconnections << conn;
d_func()->connectedNodes << conn->id(); d->connectedNodes << conn->id();
} }
void void
Servent::unregisterControlConnection( ControlConnection* conn ) Servent::unregisterControlConnection( ControlConnection* conn )
{ {
Q_D( Servent );
Q_ASSERT( conn ); Q_ASSERT( conn );
QMutexLocker locker( &d->controlconnectionsMutex );
tLog( LOGVERBOSE ) << Q_FUNC_INFO << conn->name(); tLog( LOGVERBOSE ) << Q_FUNC_INFO << conn->name();
d_func()->connectedNodes.removeAll( conn->id() ); d->connectedNodes.removeAll( conn->id() );
d_func()->controlconnections.removeAll( conn ); d->controlconnections.removeAll( conn );
} }
ControlConnection* ControlConnection*
Servent::lookupControlConnection( const SipInfo& sipInfo ) Servent::lookupControlConnection( const SipInfo& sipInfo )
{ {
Q_D( Servent );
QMutexLocker locker( &d->controlconnectionsMutex );
foreach ( ControlConnection* c, d_func()->controlconnections ) foreach ( ControlConnection* c, d_func()->controlconnections )
{ {
tLog() << sipInfo.port() << c->peerPort() << sipInfo.host() << c->peerIpAddress().toString(); tLog() << sipInfo.port() << c->peerPort() << sipInfo.host() << c->peerIpAddress().toString();
@@ -400,6 +408,9 @@ Servent::lookupControlConnection( const SipInfo& sipInfo )
ControlConnection* ControlConnection*
Servent::lookupControlConnection( const QString& nodeid ) Servent::lookupControlConnection( const QString& nodeid )
{ {
Q_D( Servent );
QMutexLocker locker( &d->controlconnectionsMutex );
foreach ( ControlConnection* c, d_func()->controlconnections ) foreach ( ControlConnection* c, d_func()->controlconnections )
{ {
if ( c->id() == nodeid ) if ( c->id() == nodeid )
@@ -604,6 +615,7 @@ Servent::incomingConnection( int sd )
void void
Servent::readyRead() Servent::readyRead()
{ {
Q_D( Servent );
Q_ASSERT( this->thread() == QThread::currentThread() ); Q_ASSERT( this->thread() == QThread::currentThread() );
QPointer< QTcpSocketExtra > sock = (QTcpSocketExtra*)sender(); QPointer< QTcpSocketExtra > sock = (QTcpSocketExtra*)sender();
tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Starting to read from new incoming connection from: " << sock->peerAddress().toString(); tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Starting to read from new incoming connection from: " << sock->peerAddress().toString();
@@ -654,6 +666,7 @@ Servent::readyRead()
tDebug( LOGVERBOSE ) << "Incoming connection details:" << m; tDebug( LOGVERBOSE ) << "Incoming connection details:" << m;
if ( !nodeid.isEmpty() ) // only control connections send nodeid if ( !nodeid.isEmpty() ) // only control connections send nodeid
{ {
QMutexLocker locker( &d->controlconnectionsMutex );
bool dupe = false; bool dupe = false;
if ( d_func()->connectedNodes.contains( nodeid ) ) if ( d_func()->connectedNodes.contains( nodeid ) )
{ {
@@ -710,6 +723,8 @@ Servent::readyRead()
} }
} }
{
QMutexLocker locker( &d->controlconnectionsMutex );
foreach ( ControlConnection* con, d_func()->controlconnections ) foreach ( ControlConnection* con, d_func()->controlconnections )
{ {
Q_ASSERT( con ); Q_ASSERT( con );
@@ -721,6 +736,7 @@ Servent::readyRead()
break; break;
} }
} }
}
// they connected to us and want something we are offering // they connected to us and want something we are offering
if ( conntype == "accept-offer" || conntype == "push-offer" ) if ( conntype == "accept-offer" || conntype == "push-offer" )
@@ -1097,6 +1113,8 @@ equalByIPv6Address( QHostAddress a1, QHostAddress a2 )
Connection* Connection*
Servent::claimOffer( ControlConnection* cc, const QString &nodeid, const QString &key, const QHostAddress peer ) Servent::claimOffer( ControlConnection* cc, const QString &nodeid, const QString &key, const QHostAddress peer )
{ {
Q_D( Servent );
// magic key for stream connections: // magic key for stream connections:
if ( key.startsWith( "FILE_REQUEST_KEY:" ) ) if ( key.startsWith( "FILE_REQUEST_KEY:" ) )
{ {
@@ -1105,6 +1123,7 @@ Servent::claimOffer( ControlConnection* cc, const QString &nodeid, const QString
{ {
bool authed = false; bool authed = false;
tDebug() << Q_FUNC_INFO << "Checking for ControlConnection with IP" << peer; tDebug() << Q_FUNC_INFO << "Checking for ControlConnection with IP" << peer;
QMutexLocker locker( &d->controlconnectionsMutex );
foreach ( ControlConnection* cc, d_func()->controlconnections ) foreach ( ControlConnection* cc, d_func()->controlconnections )
{ {
tDebug() << Q_FUNC_INFO << "Probing:" << cc->name(); tDebug() << Q_FUNC_INFO << "Probing:" << cc->name();
@@ -1315,6 +1334,9 @@ Servent::isIPWhitelisted( QHostAddress ip )
bool bool
Servent::connectedToSession( const QString& session ) Servent::connectedToSession( const QString& session )
{ {
Q_D( Servent );
QMutexLocker locker( &d->controlconnectionsMutex );
foreach ( ControlConnection* cc, d_func()->controlconnections ) foreach ( ControlConnection* cc, d_func()->controlconnections )
{ {
Q_ASSERT( cc ); Q_ASSERT( cc );

View File

@@ -57,6 +57,7 @@ private:
/** /**
* canonical list of authed peers * canonical list of authed peers
*/ */
QMutex controlconnectionsMutex;
QList< ControlConnection* > controlconnections; QList< ControlConnection* > controlconnections;
/** /**