diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 9a499191e..614e3d528 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -370,6 +370,10 @@ namespace libtorrent { // hidden virtual ~peer_plugin() {} + // This function is expected to return the name of + // the plugin. + virtual string_view type() const { return {}; } + // can add entries to the extension handshake // this is not called for web seeds virtual void add_handshake(entry&) {} diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index c0d7a09b3..54a348e02 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -303,6 +303,7 @@ namespace aux { #ifndef TORRENT_DISABLE_EXTENSIONS void add_extension(std::shared_ptr); + peer_plugin const* find_plugin(string_view type); #endif // this function is called once the torrent associated diff --git a/include/libtorrent/peer_connection_handle.hpp b/include/libtorrent/peer_connection_handle.hpp index c9a4c8085..709572668 100644 --- a/include/libtorrent/peer_connection_handle.hpp +++ b/include/libtorrent/peer_connection_handle.hpp @@ -57,6 +57,7 @@ struct TORRENT_EXPORT peer_connection_handle connection_type type() const; void add_extension(std::shared_ptr); + peer_plugin const* find_plugin(string_view type); bool is_seed() const; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 6e530df33..5e15b699c 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -518,6 +518,14 @@ namespace libtorrent { TORRENT_ASSERT(is_single_thread()); m_extensions.push_back(ext); } + + peer_plugin const* peer_connection::find_plugin(string_view type) + { + TORRENT_ASSERT(is_single_thread()); + auto p = std::find_if(m_extensions.begin(), m_extensions.end() + , [&](std::shared_ptr const& e) { return e->type() == type; }); + return p != m_extensions.end() ? p->get() : nullptr; + } #endif void peer_connection::send_allowed_set() diff --git a/src/peer_connection_handle.cpp b/src/peer_connection_handle.cpp index e5df53de1..a74f22713 100644 --- a/src/peer_connection_handle.cpp +++ b/src/peer_connection_handle.cpp @@ -57,6 +57,18 @@ void peer_connection_handle::add_extension(std::shared_ptr ext) #endif } +peer_plugin const* peer_connection_handle::find_plugin(string_view type) +{ +#ifndef TORRENT_DISABLE_EXTENSIONS + std::shared_ptr pc = native_handle(); + TORRENT_ASSERT(pc); + return pc->find_plugin(type); +#else + TORRENT_UNUSED(type); + return nullptr; +#endif +} + bool peer_connection_handle::is_seed() const { std::shared_ptr pc = native_handle();