From 08138307dac187c925889d4d39c9107c55d2afdc Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Thu, 19 Sep 2013 23:43:21 +0300 Subject: [PATCH] Refactor code for reporting listen failure/success. --- src/qtlibtorrent/qbtsession.cpp | 45 +++++++++++++-------------------- src/qtlibtorrent/qbtsession.h | 2 -- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 0699e8400..95eb62922 100755 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -107,8 +107,7 @@ QBtSession::QBtSession() LSDEnabled(false), DHTEnabled(false), current_dht_port(0), queueingEnabled(false), m_torrentExportEnabled(false), - m_finishedTorrentExportEnabled(false), - m_randomPortEnabled(false) + m_finishedTorrentExportEnabled(false) #ifndef DISABLE_GUI , geoipDBLoaded(false), resolve_countries(false) #endif @@ -285,24 +284,15 @@ void QBtSession::setQueueingEnabled(bool enable) { void QBtSession::configureSession() { qDebug("Configuring session"); Preferences pref; + if (pref.useRandomPort()) { + pref.setSessionPort(rand() % USHRT_MAX + 1025); + } + const unsigned short old_listenPort = getListenPort(); const unsigned short new_listenPort = pref.getSessionPort(); - if (pref.useRandomPort()) { // to check if the randomPort checkbox is selected - if (!m_randomPortEnabled) { - m_randomPortEnabled = true; - const unsigned short randomPort = rand() % USHRT_MAX + 1025; - setListeningPort(randomPort); - addConsoleMessage(tr("qBittorrent is bound to port: TCP/%1", "e.g: qBittorrent is bound to port: 6881").arg(QString::number(getListenPort()))); - pref.setSessionPort(randomPort); - } - } else { - // * Ports binding - m_randomPortEnabled = false; - if (old_listenPort != new_listenPort) { - qDebug("Session port changes in program preferences: %d -> %d", old_listenPort, new_listenPort); - setListeningPort(new_listenPort); - addConsoleMessage(tr("qBittorrent is bound to port: TCP/%1", "e.g: qBittorrent is bound to port: 6881").arg(QString::number(getListenPort()))); - } + if (old_listenPort != new_listenPort) { + qDebug("Session port changes in program preferences: %d -> %d", old_listenPort, new_listenPort); + setListeningPort(new_listenPort); } // Downloads @@ -1921,6 +1911,7 @@ void QBtSession::setListeningPort(int port) { #endif const QString iface_name = pref.getNetworkInterface(); if (iface_name.isEmpty()) { + addConsoleMessage(tr("qBittorrent is trying to listen on any interface port: TCP/%1", "e.g: qBittorrent is trying to listen on any interface port: TCP/6881").arg(QString::number(port)), "blue"); #if LIBTORRENT_VERSION_NUM >= 001600 s->listen_on(ports, ec); #else @@ -1946,15 +1937,10 @@ void QBtSession::setListeningPort(int port) { if (s->listen_on(ports, entry.ip().toString().toAscii().constData())) { #endif ip = entry.ip().toString(); + addConsoleMessage(tr("qBittorrent is trying to listen on interface %1 port: TCP/%2", "e.g: qBittorrent is trying to listen on interface 192.168.0.1 port: TCP/6881").arg(ip).arg(QString::number(port)), "blue"); 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"); - } + } } // Set download rate limit @@ -2546,7 +2532,8 @@ void QBtSession::readAlerts() { } else if (listen_succeeded_alert *p = dynamic_cast(a.get())) { boost::system::error_code ec; - qDebug() << "Sucessfully listening on" << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port(); + qDebug() << "Successfully listening on" << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port(); + addConsoleMessage(tr("qBittorrent is successfully listening on interface %1 port: TCP/%2", "e.g: qBittorrent is successfully listening on interface 192.168.0.1 port: TCP/6881").arg(p->endpoint.address().to_string(ec).c_str()).arg(QString::number(p->endpoint.port())), "blue"); // Force reannounce on all torrents because some trackers blacklist some ports std::vector torrents = s->get_torrents(); @@ -2555,7 +2542,11 @@ void QBtSession::readAlerts() { for ( ; it != itend; ++it) { it->force_reannounce(); } - emit listenSucceeded(); + } + else if (listen_failed_alert *p = dynamic_cast(a.get())) { + boost::system::error_code ec; + qDebug() << "Failed listening on" << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port(); + addConsoleMessage(tr("qBittorrent failed listening on interface %1 port: TCP/%2. Reason: %3", "e.g: qBittorrent failed listening on interface 192.168.0.1 port: TCP/6881. Reason: already in use").arg(p->endpoint.address().to_string(ec).c_str()).arg(QString::number(p->endpoint.port())).arg(QString::fromStdString((p->error.message()))), "red"); } else if (torrent_checked_alert* p = dynamic_cast(a.get())) { QTorrentHandle h(p->handle); diff --git a/src/qtlibtorrent/qbtsession.h b/src/qtlibtorrent/qbtsession.h index 9bb2fbe0f..31eb06999 100755 --- a/src/qtlibtorrent/qbtsession.h +++ b/src/qtlibtorrent/qbtsession.h @@ -224,7 +224,6 @@ signals: void alternativeSpeedsModeChanged(bool alternative); void recursiveTorrentDownloadPossible(const QTorrentHandle &h); void ipFilterParsed(bool error, int ruleCount); - void listenSucceeded(); private: // Bittorrent @@ -258,7 +257,6 @@ private: bool m_torrentExportEnabled; bool m_finishedTorrentExportEnabled; bool appendqBExtension; - bool m_randomPortEnabled; QString defaultSavePath; QString defaultTempPath; // IP filtering