From c93ada1e7bf50c3122e3d16b79e4ccfdc2cb67df Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Wed, 20 Jan 2016 09:57:02 +0300 Subject: [PATCH] Prepare for libtorrent v1.1 support --- src/base/bittorrent/session.cpp | 41 +++++++++++--------- src/base/bittorrent/session.h | 6 ++- src/base/bittorrent/torrentcreatorthread.cpp | 2 +- src/base/bittorrent/torrenthandle.cpp | 10 ++--- src/base/bittorrent/torrentinfo.cpp | 12 +++--- src/base/bittorrent/torrentinfo.h | 11 ++++-- 6 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 73fbf321a..fdeea71c5 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -47,6 +47,8 @@ using namespace BitTorrent; #include #include +#include + #include #include #include @@ -951,10 +953,8 @@ bool Session::addTorrent(QString source, const AddTorrentParams ¶ms) adjustLimits(); // use common 2nd step of torrent addition - libt::add_torrent_alert *alert = new libt::add_torrent_alert(handle, libt::add_torrent_params(), libt::error_code()); m_addingTorrents.insert(hash, addDataFromParams(params)); - handleAddTorrentAlert(alert); - delete alert; + createTorrentHandle(handle); return true; } @@ -2060,26 +2060,18 @@ void Session::dispatchTorrentAlert(libt::alert *a) torrent->handleAlert(a); } -void Session::handleAddTorrentAlert(libtorrent::add_torrent_alert *p) +void Session::createTorrentHandle(const libt::torrent_handle &nativeHandle) { - Logger *const logger = Logger::instance(); - if (p->error) { - qDebug("/!\\ Error: Failed to add torrent!"); - QString msg = Utils::String::fromStdString(p->message()); - logger->addMessage(tr("Couldn't add torrent. Reason: %1").arg(msg), Log::WARNING); - emit addTorrentFailed(msg); - return; - } - // Magnet added for preload its metadata - if (!m_addingTorrents.contains(p->handle.info_hash())) return; + if (!m_addingTorrents.contains(nativeHandle.info_hash())) return; - AddTorrentData data = m_addingTorrents.take(p->handle.info_hash()); + AddTorrentData data = m_addingTorrents.take(nativeHandle.info_hash()); - TorrentHandle *const torrent = new TorrentHandle(this, p->handle, data); + TorrentHandle *const torrent = new TorrentHandle(this, nativeHandle, data); m_torrents.insert(torrent->hash(), torrent); Preferences *const pref = Preferences::instance(); + Logger *const logger = Logger::instance(); bool fromMagnetUri = !torrent->hasMetadata(); @@ -2133,7 +2125,20 @@ void Session::handleAddTorrentAlert(libtorrent::add_torrent_alert *p) emit torrentAdded(torrent); } -void Session::handleTorrentRemovedAlert(libtorrent::torrent_removed_alert *p) +void Session::handleAddTorrentAlert(libt::add_torrent_alert *p) +{ + if (p->error) { + qDebug("/!\\ Error: Failed to add torrent!"); + QString msg = Utils::String::fromStdString(p->message()); + Logger::instance()->addMessage(tr("Couldn't add torrent. Reason: %1").arg(msg), Log::WARNING); + emit addTorrentFailed(msg); + } + else { + createTorrentHandle(p->handle); + } +} + +void Session::handleTorrentRemovedAlert(libt::torrent_removed_alert *p) { if (m_loadedMetadata.contains(p->info_hash)) emit metadataLoaded(m_loadedMetadata.take(p->info_hash)); @@ -2271,7 +2276,7 @@ void Session::handleListenFailedAlert(libt::listen_failed_alert *p) qDebug() << "Failed listening on " << proto << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port(); Logger::instance()->addMessage( tr("qBittorrent failed listening on interface %1 port: %2/%3. Reason: %4.", - "e.g: qBittorrent failed listening on interface 192.168.0.1 port: TCP/6881. Reason: already in use") + "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(proto).arg(QString::number(p->endpoint.port())) .arg(QString::fromLocal8Bit(p->error.message().c_str())), Log::CRITICAL); } diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index ee945512b..d7610d525 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -45,13 +45,15 @@ namespace libtorrent { class session; + struct torrent_handle; class entry; struct add_torrent_params; struct pe_settings; - struct proxy_settings; struct session_settings; struct session_status; + struct proxy_settings; + class alert; struct torrent_alert; struct state_update_alert; @@ -313,6 +315,8 @@ namespace BitTorrent void handleListenFailedAlert(libtorrent::listen_failed_alert *p); void handleExternalIPAlert(libtorrent::external_ip_alert *p); + void createTorrentHandle(const libtorrent::torrent_handle &nativeHandle); + void saveResumeData(); bool writeResumeDataFile(TorrentHandle *const torrent, const libtorrent::entry &data); diff --git a/src/base/bittorrent/torrentcreatorthread.cpp b/src/base/bittorrent/torrentcreatorthread.cpp index ea82c44e1..2669c5af1 100644 --- a/src/base/bittorrent/torrentcreatorthread.cpp +++ b/src/base/bittorrent/torrentcreatorthread.cpp @@ -55,7 +55,7 @@ using namespace BitTorrent; // name starts with a . bool fileFilter(const std::string &f) { - return (libt::filename(f)[0] != '.'); + return !Utils::Fs::fileName(Utils::String::fromStdString(f)).startsWith('.'); } TorrentCreatorThread::TorrentCreatorThread(QObject *parent) diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index 708ac482e..8565d327d 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -40,6 +40,7 @@ #include #include #include + #include #ifdef Q_OS_WIN @@ -433,7 +434,7 @@ bool TorrentHandle::connectPeer(const PeerAddress &peerAddress) libt::address addr = libt::address::from_string(Utils::String::toStdString(peerAddress.ip.toString()), ec); if (ec) return false; - libt::asio::ip::tcp::endpoint ep(addr, peerAddress.port); + boost::asio::ip::tcp::endpoint ep(addr, peerAddress.port); SAFE_CALL_BOOL(connect_peer, ep); } @@ -847,7 +848,7 @@ qulonglong TorrentHandle::eta() const QVector TorrentHandle::filesProgress() const { - std::vector fp; + std::vector fp; QVector result; SAFE_CALL(file_progress, fp, libt::torrent_handle::piece_granularity); @@ -1022,9 +1023,9 @@ qreal TorrentHandle::maxRatio(bool *usesGlobalRatio) const qreal TorrentHandle::realRatio() const { - libt::size_type upload = m_nativeStatus.all_time_upload; + boost::int64_t upload = m_nativeStatus.all_time_upload; // special case for a seeder who lost its stats, also assume nobody will import a 99% done torrent - libt::size_type download = (m_nativeStatus.all_time_download < m_nativeStatus.total_done * 0.01) ? m_nativeStatus.total_done : m_nativeStatus.all_time_download; + boost::int64_t download = (m_nativeStatus.all_time_download < m_nativeStatus.total_done * 0.01) ? m_nativeStatus.total_done : m_nativeStatus.all_time_download; if (download == 0) return (upload == 0) ? 0.0 : MAX_RATIO; @@ -1689,7 +1690,6 @@ libtorrent::torrent_handle TorrentHandle::nativeHandle() const void TorrentHandle::updateTorrentInfo() { if (!hasMetadata()) return; - m_torrentInfo = TorrentInfo(m_nativeStatus.torrent_file); } diff --git a/src/base/bittorrent/torrentinfo.cpp b/src/base/bittorrent/torrentinfo.cpp index d7da2f517..ffe2ad90c 100644 --- a/src/base/bittorrent/torrentinfo.cpp +++ b/src/base/bittorrent/torrentinfo.cpp @@ -43,8 +43,8 @@ namespace libt = libtorrent; using namespace BitTorrent; -TorrentInfo::TorrentInfo(boost::intrusive_ptr nativeInfo) - : m_nativeInfo(const_cast(nativeInfo.get())) +TorrentInfo::TorrentInfo(NativeConstPtr nativeInfo) + : m_nativeInfo(nativeInfo) { } @@ -63,7 +63,7 @@ TorrentInfo TorrentInfo::loadFromFile(const QString &path, QString &error) { error.clear(); libt::error_code ec; - TorrentInfo info(new libt::torrent_info(Utils::String::toStdString(Utils::Fs::toNativePath(path)), ec)); + TorrentInfo info(NativePtr(new libt::torrent_info(Utils::String::toStdString(Utils::Fs::toNativePath(path)), ec))); if (ec) { error = QString::fromUtf8(ec.message().c_str()); qDebug("Cannot load .torrent file: %s", qPrintable(error)); @@ -214,10 +214,10 @@ QByteArray TorrentInfo::metadata() const void TorrentInfo::renameFile(uint index, const QString &newPath) { if (!isValid()) return; - m_nativeInfo->rename_file(index, Utils::String::toStdString(newPath)); + nativeInfo()->rename_file(index, Utils::String::toStdString(newPath)); } -boost::intrusive_ptr TorrentInfo::nativeInfo() const +TorrentInfo::NativePtr TorrentInfo::nativeInfo() const { - return m_nativeInfo; + return *reinterpret_cast(&m_nativeInfo); } diff --git a/src/base/bittorrent/torrentinfo.h b/src/base/bittorrent/torrentinfo.h index 392fa8b46..c9acc2cc9 100644 --- a/src/base/bittorrent/torrentinfo.h +++ b/src/base/bittorrent/torrentinfo.h @@ -30,6 +30,7 @@ #define BITTORRENT_TORRENTINFO_H #include + #include class QString; @@ -47,7 +48,10 @@ namespace BitTorrent class TorrentInfo { public: - explicit TorrentInfo(boost::intrusive_ptr nativeInfo = boost::intrusive_ptr()); + typedef boost::intrusive_ptr NativeConstPtr; + typedef boost::intrusive_ptr NativePtr; + + explicit TorrentInfo(NativeConstPtr nativeInfo = NativeConstPtr()); TorrentInfo(const TorrentInfo &other); static TorrentInfo loadFromFile(const QString &path, QString &error); @@ -77,10 +81,11 @@ namespace BitTorrent QByteArray metadata() const; void renameFile(uint index, const QString &newPath); - boost::intrusive_ptr nativeInfo() const; + + NativePtr nativeInfo() const; private: - boost::intrusive_ptr m_nativeInfo; + NativeConstPtr m_nativeInfo; }; }