1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-05 08:32:42 +02:00

Better PortFwdThread handling...separate out the thread controller from

the actual threaded worker
This commit is contained in:
Jeff Mitchell 2012-07-10 21:09:07 -04:00
parent 8dcf7d0db2
commit e9314775aa
4 changed files with 94 additions and 34 deletions

View File

@ -29,29 +29,69 @@
#include <QTimer>
PortFwdThread::PortFwdThread( unsigned int port )
: QThread()
, m_externalPort( 0 )
, m_port( port )
{
moveToThread( this );
start();
}
PortFwdThread::~PortFwdThread()
{
qDebug() << Q_FUNC_INFO << "waiting for event loop to finish...";
quit();
wait( 6000 );
}
void
PortFwdThread::run()
{
m_worker = QWeakPointer< PortFwdWorker >( new PortFwdWorker( m_port ) );
Q_ASSERT( m_worker );
connect( m_worker.data(), SIGNAL( externalAddressDetected( QHostAddress, unsigned int ) ), this, SIGNAL( externalAddressDetected( QHostAddress, unsigned int ) ) );
QTimer::singleShot( 0, m_worker.data(), SLOT( work() ) );
exec();
if ( m_worker.data()->externalPort() )
{
qDebug() << "Unregistering port fwd";
m_worker.data()->unregister();
}
if ( m_worker )
delete m_worker.data();
}
QWeakPointer< PortFwdWorker >
PortFwdThread::worker() const
{
return m_worker;
}
PortFwdWorker::PortFwdWorker( unsigned int port )
: QObject()
, m_externalPort( 0 )
, m_port( port )
{
}
PortFwdWorker::~PortFwdWorker()
{
delete m_portfwd;
}
void
PortFwdThread::work()
PortFwdWorker::unregister()
{
m_portfwd->remove( m_externalPort );
}
void
PortFwdWorker::work()
{
qsrand( QTime( 0, 0, 0 ).secsTo( QTime::currentTime() ) );
m_portfwd = new Portfwd();
@ -103,16 +143,3 @@ PortFwdThread::work()
emit externalAddressDetected( m_externalAddress, m_externalPort );
}
void
PortFwdThread::run()
{
QTimer::singleShot( 0, this, SLOT( work() ) );
exec();
if ( m_externalPort )
{
qDebug() << "Unregistering port fwd";
m_portfwd->remove( m_externalPort );
}
}

View File

@ -22,9 +22,35 @@
#include <QThread>
#include <QMutex>
#include <QHostAddress>
#include <QWeakPointer>
class Portfwd;
class PortFwdWorker : public QObject
{
Q_OBJECT
public:
explicit PortFwdWorker( unsigned int port );
~PortFwdWorker();
unsigned int externalPort() const { return m_externalPort; }
void unregister();
signals:
void externalAddressDetected( QHostAddress ha, unsigned int port );
public slots:
void work();
private:
Portfwd* m_portfwd;
QHostAddress m_externalAddress;
unsigned int m_externalPort, m_port;
};
class PortFwdThread : public QThread
{
Q_OBJECT
@ -33,18 +59,17 @@ public:
explicit PortFwdThread( unsigned int port );
~PortFwdThread();
QWeakPointer< PortFwdWorker > worker() const;
signals:
void externalAddressDetected( QHostAddress ha, unsigned int port );
private slots:
void work();
private:
protected:
void run();
Portfwd* m_portfwd;
QHostAddress m_externalAddress;
unsigned int m_externalPort, m_port;
private:
QWeakPointer< PortFwdWorker > m_worker;
unsigned int m_port;
};
#endif // PORTFWDTHREAD_H

View File

@ -60,7 +60,6 @@ Servent::Servent( QObject* parent )
, m_port( 0 )
, m_externalPort( 0 )
, m_ready( false )
, m_portfwd( 0 )
{
s_instance = this;
@ -89,7 +88,13 @@ Servent::Servent( QObject* parent )
Servent::~Servent()
{
delete m_portfwd;
if ( m_portfwd )
{
m_portfwd.data()->quit();
m_portfwd.data()->wait( 60000 );
delete m_portfwd.data();
}
}
@ -144,9 +149,11 @@ Servent::startListening( QHostAddress ha, bool upnp, int port )
}
// TODO check if we have a public/internet IP on this machine directly
tLog() << "External address mode set to upnp...";
m_portfwd = new PortFwdThread( m_port );
connect( m_portfwd, SIGNAL( externalAddressDetected( QHostAddress, unsigned int ) ),
m_portfwd = QWeakPointer< PortFwdThread >( new PortFwdThread( m_port ) );
Q_ASSERT( m_portfwd );
connect( m_portfwd.data(), SIGNAL( externalAddressDetected( QHostAddress, unsigned int ) ),
SLOT( setExternalAddress( QHostAddress, unsigned int ) ) );
m_portfwd.data()->start();
break;
}
@ -226,6 +233,7 @@ Servent::setExternalAddress( QHostAddress ha, unsigned int port )
return;
}
tLog() << "UPnP setup successful";
m_ready = true;
emit ready();
}

View File

@ -174,7 +174,7 @@ private:
QMap< QString,boost::function< QSharedPointer< QIODevice >(Tomahawk::result_ptr) > > m_iofactories;
PortFwdThread* m_portfwd;
QWeakPointer< PortFwdThread > m_portfwd;
static Servent* s_instance;
};