Merge pull request #11182 from Chocobo1/addPeer

Move error logging of adding peers to the proper place
This commit is contained in:
Mike Tzou 2019-09-05 09:52:02 +08:00 committed by GitHub
commit ef8b37f7fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 13 deletions

View file

@ -30,7 +30,9 @@
#include <QString>
BitTorrent::PeerAddress BitTorrent::PeerAddress::parse(const QString &address)
using namespace BitTorrent;
PeerAddress PeerAddress::parse(const QString &address)
{
QVector<QStringRef> 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));
}

View file

@ -40,5 +40,6 @@ namespace BitTorrent
ushort port = 0;
static PeerAddress parse(const QString &address);
QString toString() const;
};
}

View file

@ -537,11 +537,28 @@ void TorrentHandle::removeUrlSeeds(const QVector<QUrl> &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;
}

View file

@ -28,6 +28,8 @@
#include "peerlistwidget.h"
#include <algorithm>
#include <QApplication>
#include <QClipboard>
#include <QHeaderView>
@ -242,16 +244,10 @@ void PeerListWidget::showPeerListMenu(const QPoint &)
connect(addPeerAct, &QAction::triggered, this, [this, torrent]()
{
const QVector<BitTorrent::PeerAddress> 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)