From 2396ef5bd6fd0a1dc8e86a87735421175a048c2f Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 3 Sep 2019 13:58:35 +0800 Subject: [PATCH] Move error logging of adding peers to the proper place --- src/base/bittorrent/peeraddress.cpp | 15 ++++++++++++++- src/base/bittorrent/peeraddress.h | 1 + src/base/bittorrent/torrenthandle.cpp | 21 +++++++++++++++++++-- src/gui/properties/peerlistwidget.cpp | 16 ++++++---------- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/base/bittorrent/peeraddress.cpp b/src/base/bittorrent/peeraddress.cpp index eed81cebe..b7dd5c754 100644 --- a/src/base/bittorrent/peeraddress.cpp +++ b/src/base/bittorrent/peeraddress.cpp @@ -30,7 +30,9 @@ #include -BitTorrent::PeerAddress BitTorrent::PeerAddress::parse(const QString &address) +using namespace BitTorrent; + +PeerAddress PeerAddress::parse(const QString &address) { QVector ipPort; @@ -55,3 +57,14 @@ BitTorrent::PeerAddress BitTorrent::PeerAddress::parse(const QString &address) return {ip, port}; } + +QString PeerAddress::toString() const +{ + if (ip.isNull()) + return {}; + + const QString ipStr = (ip.protocol() == QAbstractSocket::IPv6Protocol) + ? ('[' + ip.toString() + ']') + : ip.toString(); + return (ipStr + ':' + QString::number(port)); +} diff --git a/src/base/bittorrent/peeraddress.h b/src/base/bittorrent/peeraddress.h index e07243d7b..4233a6e1f 100644 --- a/src/base/bittorrent/peeraddress.h +++ b/src/base/bittorrent/peeraddress.h @@ -40,5 +40,6 @@ namespace BitTorrent ushort port = 0; static PeerAddress parse(const QString &address); + QString toString() const; }; } diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index d02abd3fe..5ad0565ab 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -537,11 +537,28 @@ void TorrentHandle::removeUrlSeeds(const QVector &urlSeeds) bool TorrentHandle::connectPeer(const PeerAddress &peerAddress) { lt::error_code ec; +#if (LIBTORRENT_VERSION_NUM < 10200) const lt::address addr = lt::address::from_string(peerAddress.ip.toString().toStdString(), ec); +#else + const lt::address addr = lt::make_address(peerAddress.ip.toString().toStdString(), ec); +#endif if (ec) return false; - const boost::asio::ip::tcp::endpoint ep(addr, peerAddress.port); - m_nativeHandle.connect_peer(ep); + const lt::tcp::endpoint endpoint(addr, peerAddress.port); + try { + m_nativeHandle.connect_peer(endpoint); + } +#if (LIBTORRENT_VERSION_NUM < 10200) + catch (const boost::system::system_error &err) { +#else + catch (const lt::system_error &err) { +#endif + LogMsg(tr("Failed to add peer \"%1\" to torrent \"%2\". Reason: %3") + .arg(peerAddress.toString(), name(), QString::fromLocal8Bit(err.what())), Log::WARNING); + return false; + } + + LogMsg(tr("Peer \"%1\" is added to torrent \"%2\"").arg(peerAddress.toString(), name())); return true; } diff --git a/src/gui/properties/peerlistwidget.cpp b/src/gui/properties/peerlistwidget.cpp index 47a37d104..2b697cc70 100644 --- a/src/gui/properties/peerlistwidget.cpp +++ b/src/gui/properties/peerlistwidget.cpp @@ -28,6 +28,8 @@ #include "peerlistwidget.h" +#include + #include #include #include @@ -242,16 +244,10 @@ void PeerListWidget::showPeerListMenu(const QPoint &) connect(addPeerAct, &QAction::triggered, this, [this, torrent]() { const QVector peersList = PeersAdditionDialog::askForPeers(this); - int peerCount = 0; - for (const BitTorrent::PeerAddress &addr : peersList) { - if (torrent->connectPeer(addr)) { - ++peerCount; - LogMsg(tr("Peer \"%1\" added to \"%2\"").arg(addr.ip.toString(), torrent->name())); - } - else { - LogMsg(tr("Failed to add peer \"%1\" to \"%2\".").arg(addr.ip.toString(), torrent->name()), Log::WARNING); - } - } + const int peerCount = std::count_if(peersList.cbegin(), peersList.cend(), [torrent](const BitTorrent::PeerAddress &peer) + { + return torrent->connectPeer(peer); + }); if (peerCount < peersList.length()) QMessageBox::information(this, tr("Adding peers"), tr("Some peers cannot be added. Check the Log for details.")); else if (peerCount > 0)