From f108e67dcc499af473ae48fced71ae40bed5b628 Mon Sep 17 00:00:00 2001 From: Pawel Polewicz Date: Mon, 15 Jun 2015 00:06:56 +0200 Subject: [PATCH] Call setListeningPort() when state of network interface changes (closes qbittorrent/qBittorrent#475, closes qbittorrent/qBittorrent#3072) --- src/core/bittorrent/session.cpp | 28 +++++++++++++++++++++++++--- src/core/bittorrent/session.h | 9 ++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/core/bittorrent/session.cpp b/src/core/bittorrent/session.cpp index f7457313e..f442ec2e3 100644 --- a/src/core/bittorrent/session.cpp +++ b/src/core/bittorrent/session.cpp @@ -202,6 +202,12 @@ Session::Session(QObject *parent) configure(); connect(pref, SIGNAL(changed()), SLOT(configure())); + // Network configuration monitor + connect(&m_networkManager, SIGNAL(onlineStateChanged(bool)), SLOT(networkOnlineStateChanged(bool))); + connect(&m_networkManager, SIGNAL(configurationAdded(const QNetworkConfiguration&)), SLOT(networkConfigurationChange(const QNetworkConfiguration&))); + connect(&m_networkManager, SIGNAL(configurationRemoved(const QNetworkConfiguration&)), SLOT(networkConfigurationChange(const QNetworkConfiguration&))); + connect(&m_networkManager, SIGNAL(configurationChanged(const QNetworkConfiguration&)), SLOT(networkConfigurationChange(const QNetworkConfiguration&))); + m_resumeDataTimer->start(); // initialize PortForwarder instance @@ -483,7 +489,7 @@ void Session::configure() const unsigned short newListenPort = pref->getSessionPort(); if (oldListenPort != newListenPort) { qDebug("Session port changes in program preferences: %d -> %d", oldListenPort, newListenPort); - setListeningPort(newListenPort); + setListeningPort(); } // * Save path @@ -1458,12 +1464,28 @@ void Session::setAppendExtension(bool append) } } +void Session::networkOnlineStateChanged(const bool online) +{ + Logger::instance()->addMessage(tr("System network status changed to %1", "e.g: System network status changed to ONLINE").arg(online ? tr("ONLINE") : tr("OFFLINE")), Log::INFO); +} + +void Session::networkConfigurationChange(const QNetworkConfiguration& cfg) +{ + const QString configuredInterfaceName = Preferences::instance()->getNetworkInterface(); + const QString changedInterface = cfg.name(); + if (configuredInterfaceName.isEmpty() || configuredInterfaceName == changedInterface) { + Logger::instance()->addMessage(tr("Network configuration of %1 has changed, refreshing session binding", "e.g: Network configuration of tun0 has changed, refreshing session binding").arg(changedInterface), Log::INFO); + setListeningPort(); + } +} + // Set the ports range in which is chosen the port // the BitTorrent session will listen to -void Session::setListeningPort(int port) +void Session::setListeningPort() { - qDebug() << Q_FUNC_INFO << port; Preferences* const pref = Preferences::instance(); + const unsigned short port = pref->getSessionPort(); + qDebug() << Q_FUNC_INFO << port; Logger* const logger = Logger::instance(); std::pair ports(port, port); diff --git a/src/core/bittorrent/session.h b/src/core/bittorrent/session.h index 78420e74e..ab96af405 100644 --- a/src/core/bittorrent/session.h +++ b/src/core/bittorrent/session.h @@ -36,6 +36,7 @@ #include #include #include +#include #include "core/tristatebool.h" #include "core/types.h" @@ -227,6 +228,10 @@ namespace BitTorrent void handleRedirectedToMagnet(const QString &url, const QString &magnetUri); void switchToAlternativeMode(bool alternative); + // Session reconfiguration triggers + void networkOnlineStateChanged(const bool online); + void networkConfigurationChange(const QNetworkConfiguration&); + private: explicit Session(QObject *parent = 0); ~Session(); @@ -242,7 +247,7 @@ namespace BitTorrent void setProxySettings(libtorrent::proxy_settings proxySettings); void adjustLimits(); void adjustLimits(libtorrent::session_settings &sessionSettings); - void setListeningPort(int port); + void setListeningPort(); void setDefaultSavePath(const QString &path); void setDefaultTempPath(const QString &path = QString()); void preAllocateAllFiles(bool b); @@ -356,6 +361,8 @@ namespace BitTorrent QWaitCondition m_alertsWaitCondition; QVector m_alerts; + QNetworkConfigurationManager m_networkManager; + static Session *m_instance; }; }