From 6c1eda8755f1e1cf1d2824a47bcf289c691b5162 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 30 Oct 2010 22:40:28 +0000 Subject: [PATCH] Code optimization Made code to listen on a particular interface more robust --- src/qtlibtorrent/qbtsession.cpp | 63 +++++++++------------------------ src/qtlibtorrent/qbtsession.h | 34 ++++++++++-------- 2 files changed, 36 insertions(+), 61 deletions(-) diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 87e34f53a..6c4a742e8 100644 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -197,14 +197,6 @@ void QBtSession::preAllocateAllFiles(bool b) { } } -ScanFoldersModel* QBtSession::getScanFoldersModel() const { - return m_scanFolders; -} - -bool QBtSession::isPexEnabled() const { - return PeXEnabled; -} - void QBtSession::processBigRatios() { if(ratio_limit < 0) return; qDebug("Process big ratios..."); @@ -243,10 +235,6 @@ void QBtSession::setDownloadLimit(QString hash, long val) { } } -bool QBtSession::isQueueingEnabled() const { - return queueingEnabled; -} - void QBtSession::setUploadLimit(QString hash, long val) { qDebug("Set upload limit rate to %ld", val); QTorrentHandle h = getTorrentHandle(hash); @@ -1358,15 +1346,6 @@ void QBtSession::setMaxUploadsPerTorrent(int max) { } } -// Return DHT state -bool QBtSession::isDHTEnabled() const{ - return DHTEnabled; -} - -bool QBtSession::isLSDEnabled() const{ - return LSDEnabled; -} - void QBtSession::enableUPnP(bool b) { if(b) { if(!UPnPEnabled) { @@ -1605,14 +1584,6 @@ void QBtSession::saveFastResumeData() { } } -QStringList QBtSession::getConsoleMessages() const { - return consoleMessages; -} - -QStringList QBtSession::getPeerBanMessages() const { - return peerBanMessages; -} - #ifdef DISABLE_GUI void QBtSession::addConsoleMessage(QString msg, QString) { #else @@ -1666,14 +1637,6 @@ void QBtSession::addTorrentsFromScanFolder(QStringList &pathList) { } } -QString QBtSession::getDefaultSavePath() const { - return defaultSavePath; -} - -bool QBtSession::useTemporaryFolder() const { - return !defaultTempPath.isEmpty(); -} - void QBtSession::setDefaultTempPath(QString temppath) { if(defaultTempPath == temppath) return; @@ -1808,15 +1771,27 @@ void QBtSession::setListeningPort(int port) { } QNetworkInterface network_iface = QNetworkInterface::interfaceFromName(iface_name); if(!network_iface.isValid()) { + qDebug("Invalid network interface: %s", qPrintable(iface_name)); + addConsoleMessage(tr("The network interface defined is invalid: %1").arg(iface_name), "red"); + addConsoleMessage(tr("Trying any other network interface available instead.")); s->listen_on(ports); return; } - QString ip = "127.0.0.1"; - if(!network_iface.addressEntries().isEmpty()) { - ip = network_iface.addressEntries().first().ip().toString(); + QString ip; + qDebug("This network interface has %d IP addresses", network_iface.addressEntries().size()); + foreach(const QNetworkAddressEntry &entry, network_iface.addressEntries()) { + qDebug("Trying to listen on IP %s (%s)", qPrintable(entry.ip().toString()), qPrintable(iface_name)); + if(s->listen_on(ports, qPrintable(entry.ip().toString()))) { + ip = entry.ip().toString(); + break; + } + } + if(s->is_listening()) { + addConsoleMessage(tr("Listening on IP address %1 on network interface %2...").arg(ip).arg(iface_name)); + } else { + qDebug("Failed to listen on any of the IP addresses"); + addConsoleMessage(tr("Failed to listen on network interface %1").arg(iface_name), "red"); } - qDebug("Listening on interface %s with ip %s", qPrintable(iface_name), qPrintable(ip)); - s->listen_on(ports, ip.toLocal8Bit().constData()); } // Set download rate limit @@ -1827,10 +1802,6 @@ void QBtSession::setDownloadRateLimit(long rate) { s->set_download_rate_limit(rate); } -session* QBtSession::getSession() const{ - return s; -} - // Set upload rate limit // -1 to disable void QBtSession::setUploadRateLimit(long rate) { diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index 4f98b953c..d6a0eb62f 100644 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -65,6 +65,10 @@ class ScanFoldersModel; class QBtSession : public QObject { Q_OBJECT +private: + QBtSession(const QBtSession& other); // Present copy + QBtSession& operator=(const QBtSession& other); + public: // Constructor / Destructor QBtSession(); @@ -72,31 +76,28 @@ public: QTorrentHandle getTorrentHandle(QString hash) const; std::vector getTorrents() const; bool isFilePreviewPossible(QString fileHash) const; - bool isDHTEnabled() const; - bool isLSDEnabled() const; float getPayloadDownloadRate() const; float getPayloadUploadRate() const; session_status getSessionStatus() const; int getListenPort() const; float getRealRatio(QString hash) const; - session* getSession() const; QHash getTrackersInfo(QString hash) const; bool hasActiveTorrents() const; bool hasDownloadingTorrents() const; - bool isQueueingEnabled() const; - int getMaximumActiveDownloads() const; - int getMaximumActiveTorrents() const; + //int getMaximumActiveDownloads() const; + //int getMaximumActiveTorrents() const; int loadTorrentPriority(QString hash); - QStringList getConsoleMessages() const; - QStringList getPeerBanMessages() const; qlonglong getETA(QString hash); - bool useTemporaryFolder() const; - QString getDefaultSavePath() const; - ScanFoldersModel* getScanFoldersModel() const; - bool isPexEnabled() const; -#if LIBTORRENT_VERSION_MINOR < 15 - void saveDHTEntry(); -#endif + inline QStringList getConsoleMessages() const { return consoleMessages; } + inline QStringList getPeerBanMessages() const { return peerBanMessages; } + inline session* getSession() const { return s; } + inline bool useTemporaryFolder() const { return !defaultTempPath.isEmpty(); } + inline QString getDefaultSavePath() const { return defaultSavePath; } + inline ScanFoldersModel* getScanFoldersModel() const { return m_scanFolders; } + inline bool isDHTEnabled() const { return DHTEnabled; } + inline bool isLSDEnabled() const { return LSDEnabled; } + inline bool isPexEnabled() const { return DHTEnabled; } + inline bool isQueueingEnabled() const { return queueingEnabled; } public slots: QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false); @@ -183,6 +184,9 @@ protected slots: void cleanUpAutoRunProcess(int); void mergeTorrents(QTorrentHandle h_ex, boost::intrusive_ptr t); void exportTorrentFile(QTorrentHandle h); +#if LIBTORRENT_VERSION_MINOR < 15 + void saveDHTEntry(); +#endif signals: void addedTorrent(QTorrentHandle& h);