From a318040eaa82f0e615e3bc329658150a39f394df Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 28 Apr 2016 10:56:58 +0300 Subject: [PATCH] Use new alert dispathing API --- src/base/bittorrent/session.cpp | 41 ++++++++++++++++++++++++--------- src/base/bittorrent/session.h | 8 +++++-- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index cac2663ec..42bcc6e8a 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -48,8 +48,6 @@ #include #include -#include - #include #include #include @@ -227,7 +225,17 @@ Session::Session(QObject *parent) logger->addMessage(tr("Peer ID: ") + Utils::String::fromStdString(fingerprint.to_string())); - m_nativeSession->set_alert_dispatch(boost::bind(&Session::dispatchAlerts, this, _1)); +#if LIBTORRENT_VERSION_NUM < 10100 + m_nativeSession->set_alert_dispatch([this](std::auto_ptr alertPtr) + { + dispatchAlerts(alertPtr); + }); +#else + m_nativeSession->set_alert_notify([this]() + { + QMetaObject::invokeMethod(this, "readAlerts", Qt::QueuedConnection); + }); +#endif // Enabling plugins //m_nativeSession->add_extension(&libt::create_metadata_plugin); @@ -1638,7 +1646,7 @@ void Session::saveResumeData() generateResumeData(true); while (m_numResumeData > 0) { - QVector alerts; + std::vector alerts; getPendingAlerts(alerts, 30 * 1000); if (alerts.empty()) { std::cerr << " aborting with " << m_numResumeData @@ -1647,7 +1655,7 @@ void Session::saveResumeData() break; } - foreach (libt::alert *const a, alerts) { + for (const auto a: alerts) { switch (a->type()) { case libt::save_resume_data_failed_alert::alert_type: case libt::save_resume_data_alert::alert_type: @@ -1656,8 +1664,9 @@ void Session::saveResumeData() torrent->handleAlert(a); break; } - +#if LIBTORRENT_VERSION_NUM < 10100 delete a; +#endif } } } @@ -2282,41 +2291,51 @@ void Session::handleIPFilterError() emit ipFilterParsed(true, 0); } +#if LIBTORRENT_VERSION_NUM < 10100 void Session::dispatchAlerts(std::auto_ptr alertPtr) { QMutexLocker lock(&m_alertsMutex); - bool wasEmpty = m_alerts.isEmpty(); + bool wasEmpty = m_alerts.empty(); - m_alerts.append(alertPtr.release()); + m_alerts.push_back(alertPtr.release()); if (wasEmpty) { m_alertsWaitCondition.wakeAll(); QMetaObject::invokeMethod(this, "readAlerts", Qt::QueuedConnection); } } +#endif -void Session::getPendingAlerts(QVector &out, ulong time) +void Session::getPendingAlerts(std::vector &out, ulong time) { Q_ASSERT(out.empty()); +#if LIBTORRENT_VERSION_NUM < 10100 QMutexLocker lock(&m_alertsMutex); if (m_alerts.empty()) m_alertsWaitCondition.wait(&m_alertsMutex, time); m_alerts.swap(out); +#else + if (time > 0) + m_nativeSession->wait_for_alert(libt::milliseconds(time)); + m_nativeSession->pop_alerts(&out); +#endif } // Read alerts sent by the BitTorrent session void Session::readAlerts() { - QVector alerts; + std::vector alerts; getPendingAlerts(alerts); - foreach (libt::alert *const a, alerts) { + for (const auto a: alerts) { handleAlert(a); +#if LIBTORRENT_VERSION_NUM < 10100 delete a; +#endif } } diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index f55d1a310..d5d79f018 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -388,8 +388,10 @@ namespace BitTorrent void saveResumeData(); +#if LIBTORRENT_VERSION_NUM < 10100 void dispatchAlerts(std::auto_ptr alertPtr); - void getPendingAlerts(QVector &out, ulong time = 0); +#endif + void getPendingAlerts(std::vector &out, ulong time = 0); SettingsStorage *m_settings; @@ -437,9 +439,11 @@ namespace BitTorrent TorrentStatusReport m_torrentStatusReport; QStringMap m_categories; +#if LIBTORRENT_VERSION_NUM < 10100 QMutex m_alertsMutex; QWaitCondition m_alertsWaitCondition; - QVector m_alerts; + std::vector m_alerts; +#endif QNetworkConfigurationManager m_networkManager;