From 396427e3b620196ec108527f6c5d6d0b63ec78fd Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 28 Nov 2009 18:23:17 +0000 Subject: [PATCH] FEATURE: Fall back to a random port if qBittorrent could not listen on the chosen port --- Changelog | 1 + src/bittorrent.cpp | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Changelog b/Changelog index 309d59606..243dfd1c3 100644 --- a/Changelog +++ b/Changelog @@ -23,6 +23,7 @@ - FEATURE: Show official documentation when pressing F1 key - FEATURE: Search engine plugins now handle HTTP protocol gzip compression - FEATURE: Enabled lazy bitfield as a counter-measure for ISP speed throttling + - FEATURE: Fall back to a random port if qBittorrent could not listen on the chosen port - FEATURE: Announce to all trackers specified for a torrent (µTorrent behavior) (libtorrent >= v0.15 only) - FEATURE: Added per-torrent super seeding mode (libtorrent >= v0.15 only) - FEATURE: Support for storing symbolic links in .torrent files (libtorrent >= v0.15 only) diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index fa670affb..e993bdd54 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include "filesystemwatcher.h" #include "bittorrent.h" @@ -1496,9 +1497,16 @@ void Bittorrent::readAlerts() { } } } - else if (dynamic_cast(a.get())) { + else if (listen_failed_alert* p = dynamic_cast(a.get())) { // Level: fatal - addConsoleMessage(tr("Couldn't listen on any of the given ports."), QString::fromUtf8("red")); + int tried_port = p->endpoint.port(); + srand(time(0)); + int fallback_port = tried_port; + do { + fallback_port = rand() % 64512 + 1024; + } while(fallback_port == tried_port); + addConsoleMessage(tr("Couldn't listen on port %1, using %2 instead.").arg(QString::number(tried_port)).arg(QString::number(fallback_port)), QString::fromUtf8("red")); + setListeningPort(fallback_port); //emit portListeningFailure(); } /*else if (torrent_paused_alert* p = dynamic_cast(a.get())) {