From c153fccee0d616baf86bfea6ebac5ef8aa14641f Mon Sep 17 00:00:00 2001 From: Joris CARRIER Date: Fri, 17 Nov 2023 15:03:03 +0100 Subject: [PATCH] allow execution of on_unknown_torrent method in the absence of active torrents --- include/libtorrent/aux_/session_impl.hpp | 1 + include/libtorrent/extensions.hpp | 4 ++++ src/session_impl.cpp | 6 ++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index b32c2c75fb0..e6723424d90 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -350,6 +350,7 @@ namespace aux { plugins_optimistic_unchoke_idx = 1, // optimistic_unchoke_feature plugins_tick_idx = 2, // tick_feature plugins_dht_request_idx = 3 // dht_request_feature + plugins_unknown_torrent_idx = 4 // unknown_torrent_feature }; template diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 708c2edc4a4..8302b9d2896 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -211,6 +211,10 @@ TORRENT_VERSION_NAMESPACE_3 // called static constexpr feature_flags_t alert_feature = 4_bit; + // include this bit if your plugin needs to have on_unknown_torrent() + // called even if there is no active torrent in the session + static constexpr feature_flags_t unknown_torrent_feature = 5_bit; + // This function is expected to return a bitmask indicating which features // this plugin implements. Some callbacks on this object may not be called // unless the corresponding feature flag is returned here. Note that diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 1e1c34e6640..3323464cb21 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -992,6 +992,8 @@ bool ssl_server_name_callback(ssl::stream_handle_type stream_handle, std::string m_ses_extensions[plugins_tick_idx].push_back(ext); if (features & plugin::dht_request_feature) m_ses_extensions[plugins_dht_request_idx].push_back(ext); + if (features & plugin::unknown_torrent_feature) + m_ses_extensions[plugins_unknown_torrent_idx].push_back(ext); if (features & plugin::alert_feature) m_alerts.add_extension(ext); session_handle h(shared_from_this()); @@ -3086,7 +3088,7 @@ namespace { // check if we have any active torrents // if we don't reject the connection - if (m_torrents.empty()) + if (m_torrents.empty() && m_ses_extensions[plugins_unknown_torrent_idx].empty()) { #ifndef TORRENT_DISABLE_LOGGING session_log("<== INCOMING CONNECTION [ rejected, there are no torrents ]"); @@ -3140,7 +3142,7 @@ namespace { // the setting to start up queued torrents when they // get an incoming connection is enabled, we cannot // perform this check. - if (!m_settings.get_bool(settings_pack::incoming_starts_queued_torrents)) + if (!m_settings.get_bool(settings_pack::incoming_starts_queued_torrents) || m_ses_extensions[plugins_unknown_torrent_idx].empty()) { bool has_active_torrent = std::any_of(m_torrents.begin(), m_torrents.end() , [](std::shared_ptr const& i)