Browse Source

Merge pull request #11182 from Chocobo1/addPeer

Move error logging of adding peers to the proper place
adaptive-webui-19844
Mike Tzou 5 years ago committed by GitHub
parent
commit
ef8b37f7fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      src/base/bittorrent/peeraddress.cpp
  2. 1
      src/base/bittorrent/peeraddress.h
  3. 21
      src/base/bittorrent/torrenthandle.cpp
  4. 16
      src/gui/properties/peerlistwidget.cpp

15
src/base/bittorrent/peeraddress.cpp

@ -30,7 +30,9 @@ @@ -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) @@ -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));
}

1
src/base/bittorrent/peeraddress.h

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

21
src/base/bittorrent/torrenthandle.cpp

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

16
src/gui/properties/peerlistwidget.cpp

@ -28,6 +28,8 @@ @@ -28,6 +28,8 @@
#include "peerlistwidget.h"
#include <algorithm>
#include <QApplication>
#include <QClipboard>
#include <QHeaderView>
@ -242,16 +244,10 @@ void PeerListWidget::showPeerListMenu(const QPoint &) @@ -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)

Loading…
Cancel
Save