From bac57de5f5782ab92da4e4fd064528929c3ab263 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Fri, 28 Oct 2022 20:34:23 +0300 Subject: [PATCH] Update listening status using native session extension --- .../bittorrent/nativesessionextension.cpp | 20 +++++++++++++++++++ src/base/bittorrent/nativesessionextension.h | 16 +++++++++++++++ src/base/bittorrent/sessionimpl.cpp | 6 ++++-- src/base/bittorrent/sessionimpl.h | 2 ++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/base/bittorrent/nativesessionextension.cpp b/src/base/bittorrent/nativesessionextension.cpp index 248397cf9..6449e9e78 100644 --- a/src/base/bittorrent/nativesessionextension.cpp +++ b/src/base/bittorrent/nativesessionextension.cpp @@ -62,6 +62,17 @@ namespace } } +bool NativeSessionExtension::isSessionListening() const +{ + const QReadLocker locker {&m_lock}; + return m_isSesssionListening; +} + +void NativeSessionExtension::added(const lt::session_handle &nativeSession) +{ + m_nativeSession = nativeSession; +} + lt::feature_flags_t NativeSessionExtension::implemented_features() { return alert_feature; @@ -76,6 +87,9 @@ void NativeSessionExtension::on_alert(const lt::alert *alert) { switch (alert->type()) { + case lt::session_stats_alert::alert_type: + handleSessionStatsAlert(static_cast(alert)); + break; case lt::add_torrent_alert::alert_type: handleAddTorrentAlert(static_cast(alert)); break; @@ -86,3 +100,9 @@ void NativeSessionExtension::on_alert(const lt::alert *alert) break; } } + +void NativeSessionExtension::handleSessionStatsAlert([[maybe_unused]] const lt::session_stats_alert *alert) +{ + const QWriteLocker locker {&m_lock}; + m_isSesssionListening = m_nativeSession.is_listening(); +} diff --git a/src/base/bittorrent/nativesessionextension.h b/src/base/bittorrent/nativesessionextension.h index 7ec291f28..29b39ef44 100644 --- a/src/base/bittorrent/nativesessionextension.h +++ b/src/base/bittorrent/nativesessionextension.h @@ -29,12 +29,28 @@ #pragma once #include +#include +#include + +#include #include "extensiondata.h" class NativeSessionExtension final : public lt::plugin { +public: + bool isSessionListening() const; + +private: + void added(const lt::session_handle &nativeSession) override; lt::feature_flags_t implemented_features() override; std::shared_ptr new_torrent(const lt::torrent_handle &torrentHandle, LTClientData clientData) override; void on_alert(const lt::alert *alert) override; + + void handleSessionStatsAlert(const lt::session_stats_alert *alert); + + lt::session_handle m_nativeSession; + + mutable QReadWriteLock m_lock; + bool m_isSesssionListening = false; }; diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 10bd69daa..c67a21ff8 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -1498,7 +1498,9 @@ void SessionImpl::initializeNativeSession() if (isPeXEnabled()) m_nativeSession->add_extension(<::create_ut_pex_plugin); - m_nativeSession->add_extension(std::make_shared()); + auto nativeSessionExtension = std::make_shared(); + m_nativeSession->add_extension(nativeSessionExtension); + m_nativeSessionExtension = nativeSessionExtension.get(); } void SessionImpl::processBannedIPs(lt::ip_filter &filter) @@ -4494,7 +4496,7 @@ void SessionImpl::setTrackerFilteringEnabled(const bool enabled) bool SessionImpl::isListening() const { - return m_nativeSession->is_listening(); + return m_nativeSessionExtension->isSessionListening(); } MaxRatioAction SessionImpl::maxRatioAction() const diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index 24287517c..69a93e25c 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -65,6 +65,7 @@ class QUrl; class BandwidthScheduler; class FileSearcher; class FilterParserThread; +class NativeSessionExtension; namespace Net { @@ -549,6 +550,7 @@ namespace BitTorrent // BitTorrent lt::session *m_nativeSession = nullptr; + NativeSessionExtension *m_nativeSessionExtension = nullptr; bool m_deferredConfigureScheduled = false; bool m_IPFilteringConfigured = false;