From 7cee486cf73a5e827d34ad0682e4731035c7f9d3 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Wed, 17 Aug 2016 14:30:24 -0400 Subject: [PATCH] extensions refactor to use span and std::shared_ptr (#1005) extensions refactor to use span. use std::shared_ptr instead of boost::shared_ptr. --- ChangeLog | 1 + include/libtorrent/add_torrent_params.hpp | 2 +- include/libtorrent/alert_manager.hpp | 4 +-- include/libtorrent/aux_/session_impl.hpp | 10 +++---- include/libtorrent/bt_peer_connection.hpp | 19 +++++++------ include/libtorrent/extensions.hpp | 28 ++++++++++--------- include/libtorrent/extensions/smart_ban.hpp | 8 ++---- include/libtorrent/extensions/ut_metadata.hpp | 8 ++---- include/libtorrent/extensions/ut_pex.hpp | 10 ++----- include/libtorrent/pe_crypto.hpp | 10 +++---- include/libtorrent/peer_connection.hpp | 6 ++-- include/libtorrent/peer_connection_handle.hpp | 6 ++-- include/libtorrent/session.hpp | 4 +-- include/libtorrent/session_handle.hpp | 4 +-- include/libtorrent/torrent.hpp | 8 +++--- include/libtorrent/torrent_handle.hpp | 2 +- src/alert_manager.cpp | 2 +- src/bt_peer_connection.cpp | 14 +++++----- src/pe_crypto.cpp | 4 +-- src/peer_connection.cpp | 7 ++--- src/peer_connection_handle.cpp | 6 ++-- src/session.cpp | 10 +++---- src/session_handle.cpp | 4 +-- src/session_impl.cpp | 15 +++++----- src/smart_ban.cpp | 9 ++---- src/torrent.cpp | 16 +++++------ src/torrent_handle.cpp | 2 +- src/ut_metadata.cpp | 23 ++++++++------- src/ut_pex.cpp | 15 +++++----- test/test_alert_manager.cpp | 9 ++---- test/test_direct_dht.cpp | 2 +- test/test_session_params.cpp | 6 ++-- test/test_torrent.cpp | 4 +-- 33 files changed, 131 insertions(+), 147 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4cfa301c9..7c9b5ac05 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * extensions API changed to use span and std::shared_ptr * plugin API changed to handle DHT requests using string_view * removed support for lt_trackers and metadata_transfer extensions (pre-dating ut_metadata) diff --git a/include/libtorrent/add_torrent_params.hpp b/include/libtorrent/add_torrent_params.hpp index 8f4b7eae5..ce2979e76 100644 --- a/include/libtorrent/add_torrent_params.hpp +++ b/include/libtorrent/add_torrent_params.hpp @@ -343,7 +343,7 @@ namespace libtorrent // to avoid race conditions. For instance it may be important to have the // plugin catch events that happen very early on after the torrent is // created. - std::vector(torrent_handle const&, void*)>> + std::vector(torrent_handle const&, void*)>> extensions; // the default tracker id to be used when announcing to trackers. By diff --git a/include/libtorrent/alert_manager.hpp b/include/libtorrent/alert_manager.hpp index f6ec105e8..39b4ec471 100644 --- a/include/libtorrent/alert_manager.hpp +++ b/include/libtorrent/alert_manager.hpp @@ -117,7 +117,7 @@ namespace libtorrent { void set_notify_function(std::function const& fun); #ifndef TORRENT_DISABLE_EXTENSIONS - void add_extension(boost::shared_ptr ext); + void add_extension(std::shared_ptr ext); #endif private: @@ -159,7 +159,7 @@ namespace libtorrent { aux::stack_allocator m_allocations[2]; #ifndef TORRENT_DISABLE_EXTENSIONS - std::list> m_ses_extensions; + std::list> m_ses_extensions; #endif }; } diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 85523ce84..92d010d2e 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -217,20 +217,20 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS using ext_function_t - = std::function(torrent_handle const&, void*)>; + = std::function(torrent_handle const&, void*)>; struct session_plugin_wrapper : plugin { explicit session_plugin_wrapper(ext_function_t const& f) : m_f(f) {} - boost::shared_ptr new_torrent(torrent_handle const& t, void* user) override + std::shared_ptr new_torrent(torrent_handle const& t, void* user) override { return m_f(t, user); } ext_function_t m_f; }; - void add_extension(std::function( + void add_extension(std::function( torrent_handle const&, void*)> ext); - void add_ses_extension(boost::shared_ptr ext); + void add_ses_extension(std::shared_ptr ext); #endif #if TORRENT_USE_ASSERTS bool has_peer(peer_connection const* p) const override; @@ -1178,7 +1178,7 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS // this is a list to allow extensions to potentially remove themselves. - std::array>, 4> m_ses_extensions; + std::array>, 4> m_ses_extensions; #endif // if this function is set, it indicates that torrents are allowed diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index d2f912b7d..930a9338a 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -45,7 +45,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/disable_warnings_push.hpp" -#include #include #include "libtorrent/aux_/disable_warnings_pop.hpp" @@ -85,6 +84,8 @@ namespace libtorrent #endif bool was_introduced_by(tcp::endpoint const& ep); + + virtual ~ut_pex_peer_store() {} }; #endif @@ -122,8 +123,8 @@ namespace libtorrent bool rc4_encrypted() const { return m_rc4_encrypted; } - void switch_send_crypto(boost::shared_ptr crypto); - void switch_recv_crypto(boost::shared_ptr crypto); + void switch_send_crypto(std::shared_ptr crypto); + void switch_recv_crypto(std::shared_ptr crypto); #endif virtual int type() const override @@ -192,10 +193,10 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS bool supports_holepunch() const { return m_holepunch_id != 0; } - void set_ut_pex(boost::shared_ptr ut_pex) - { m_ut_pex = ut_pex; } + void set_ut_pex(std::weak_ptr ut_pex) + { m_ut_pex = std::move(ut_pex); } bool was_introduced_by(tcp::endpoint const& ep) const - { return m_ut_pex && m_ut_pex->was_introduced_by(ep); } + { auto p = m_ut_pex.lock(); return p && p->was_introduced_by(ep); } #endif bool support_extensions() const { return m_supports_extensions; } @@ -419,7 +420,7 @@ private: // used during an encrypted handshake then moved // into m_enc_handler if rc4 encryption is negotiated // otherwise it is destroyed when the handshake completes - boost::shared_ptr m_rc4; + std::shared_ptr m_rc4; // if encryption is negotiated, this is used for // encryption/decryption during the entire session. @@ -457,9 +458,9 @@ private: // 0 if not supported std::uint8_t m_share_mode_id; - boost::shared_ptr m_ut_pex; + std::weak_ptr m_ut_pex; - char m_reserved_bits[8]; + std::array m_reserved_bits; #endif #if TORRENT_USE_ASSERTS diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 4d1baec2d..33c6d1ddf 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -92,19 +92,19 @@ POSSIBILITY OF SUCH DAMAGE. // // The signature of the function is:: // -// boost::shared_ptr (*)(torrent_handle const&, void*); +// std::shared_ptr (*)(torrent_handle const&, void*); // // The second argument is the userdata passed to ``session::add_torrent()`` or // ``torrent_handle::add_extension()``. // -// The function should return a ``boost::shared_ptr`` which +// The function should return a ``std::shared_ptr`` which // may or may not be 0. If it is a nullptr, the extension is simply ignored // for this torrent. If it is a valid pointer (to a class inheriting // ``torrent_plugin``), it will be associated with this torrent and callbacks // will be made on torrent events. // // For more elaborate plugins which require session wide state, you would -// implement ``plugin``, construct an object (in a ``boost::shared_ptr``) and pass +// implement ``plugin``, construct an object (in a ``std::shared_ptr``) and pass // it in to ``session::add_extension()``. // // custom alerts @@ -158,12 +158,11 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_DISABLE_EXTENSIONS #include + #include "libtorrent/config.hpp" -#include "libtorrent/sha1_hash.hpp" // for sha1_hash -#include "libtorrent/session_handle.hpp" -#include "libtorrent/peer_connection_handle.hpp" #include "libtorrent/span.hpp" #include "libtorrent/string_view.hpp" +#include "libtorrent/socket.hpp" namespace libtorrent { @@ -176,6 +175,9 @@ namespace libtorrent struct torrent_plugin; struct add_torrent_params; struct torrent_handle; + class sha1_hash; + struct session_handle; + struct peer_connection_handle; // this is the base class for a session plugin. One primary feature // is that it is notified of all torrents that are added to the session, @@ -222,11 +224,11 @@ namespace libtorrent // If the plugin returns a torrent_plugin instance, it will be added // to the new torrent. Otherwise, return an empty shared_ptr to a // torrent_plugin (the default). - virtual boost::shared_ptr new_torrent(torrent_handle const&, void*) - { return boost::shared_ptr(); } + virtual std::shared_ptr new_torrent(torrent_handle const&, void*) + { return std::shared_ptr(); } // called when plugin is added to a session - virtual void added(session_handle) {} + virtual void added(session_handle const&) {} // called when a dht request is received. // If your plugin expects this to be called, make sure to include the flag @@ -292,8 +294,8 @@ namespace libtorrent // to it, use ``weak_ptr``. // // If this function throws an exception, the connection will be closed. - virtual boost::shared_ptr new_connection(peer_connection_handle const&) - { return boost::shared_ptr(); } + virtual std::shared_ptr new_connection(peer_connection_handle const&) + { return std::shared_ptr(); } // These hooks are called when a piece passes the hash check or fails the hash // check, respectively. The ``index`` is the piece index that was downloaded. @@ -394,7 +396,7 @@ namespace libtorrent // means that the other end doesn't support this extension and will remove // it from the list of plugins. // this is not called for web seeds - virtual bool on_handshake(char const* /*reserved_bits*/) { return true; } + virtual bool on_handshake(span /*reserved_bits*/) { return true; } // called when the extension handshake from the other end is received // if this returns false, it means that this extension isn't @@ -427,7 +429,7 @@ namespace libtorrent // returns true to indicate that the piece is handled and the // rest of the logic should be ignored. virtual bool on_piece(peer_request const& /*piece*/ - , char const* /*buf*/) { return false; } + , span /*buf*/) { return false; } virtual bool on_cancel(peer_request const&) { return false; } virtual bool on_reject(peer_request const&) { return false; } diff --git a/include/libtorrent/extensions/smart_ban.hpp b/include/libtorrent/extensions/smart_ban.hpp index 0111229b4..634cb0f09 100644 --- a/include/libtorrent/extensions/smart_ban.hpp +++ b/include/libtorrent/extensions/smart_ban.hpp @@ -37,11 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" -#include "libtorrent/aux_/disable_warnings_push.hpp" - -#include - -#include "libtorrent/aux_/disable_warnings_pop.hpp" +#include namespace libtorrent { @@ -54,7 +50,7 @@ namespace libtorrent // out to have sent corrupt data. // This function can either be passed in the add_torrent_params::extensions // field, or via torrent_handle::add_extension(). - TORRENT_EXPORT boost::shared_ptr create_smart_ban_plugin(torrent_handle const&, void*); + TORRENT_EXPORT std::shared_ptr create_smart_ban_plugin(torrent_handle const&, void*); } #endif // TORRENT_DISABLE_EXTENSIONS diff --git a/include/libtorrent/extensions/ut_metadata.hpp b/include/libtorrent/extensions/ut_metadata.hpp index 923a3458b..353ddbb6f 100644 --- a/include/libtorrent/extensions/ut_metadata.hpp +++ b/include/libtorrent/extensions/ut_metadata.hpp @@ -37,11 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/config.hpp" -#include "libtorrent/aux_/disable_warnings_push.hpp" - -#include - -#include "libtorrent/aux_/disable_warnings_pop.hpp" +#include namespace libtorrent { @@ -57,7 +53,7 @@ namespace libtorrent // // This can either be passed in the add_torrent_params::extensions field, or // via torrent_handle::add_extension(). - TORRENT_EXPORT boost::shared_ptr create_ut_metadata_plugin(torrent_handle const&, void*); + TORRENT_EXPORT std::shared_ptr create_ut_metadata_plugin(torrent_handle const&, void*); } #endif // TORRENT_DISABLE_EXTENSIONS diff --git a/include/libtorrent/extensions/ut_pex.hpp b/include/libtorrent/extensions/ut_pex.hpp index cd0136efc..20988904d 100644 --- a/include/libtorrent/extensions/ut_pex.hpp +++ b/include/libtorrent/extensions/ut_pex.hpp @@ -36,14 +36,8 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_DISABLE_EXTENSIONS #include "libtorrent/config.hpp" -#include "libtorrent/socket.hpp" // for endpoint -#include "libtorrent/address.hpp" -#include "libtorrent/aux_/disable_warnings_push.hpp" - -#include - -#include "libtorrent/aux_/disable_warnings_pop.hpp" +#include namespace libtorrent { @@ -58,7 +52,7 @@ namespace libtorrent // // This can either be passed in the add_torrent_params::extensions field, or // via torrent_handle::add_extension(). - TORRENT_EXPORT boost::shared_ptr create_ut_pex_plugin(torrent_handle const&, void*); + TORRENT_EXPORT std::shared_ptr create_ut_pex_plugin(torrent_handle const&, void*); } #endif // TORRENT_DISABLE_EXTENSIONS diff --git a/include/libtorrent/pe_crypto.hpp b/include/libtorrent/pe_crypto.hpp index 32be40624..5cd4e81e8 100644 --- a/include/libtorrent/pe_crypto.hpp +++ b/include/libtorrent/pe_crypto.hpp @@ -104,10 +104,10 @@ namespace libtorrent int decrypt(crypto_receive_buffer& recv_buffer , std::size_t& bytes_transferred); - bool switch_send_crypto(boost::shared_ptr crypto + bool switch_send_crypto(std::shared_ptr crypto , int pending_encryption); - void switch_recv_crypto(boost::shared_ptr crypto + void switch_recv_crypto(std::shared_ptr crypto , crypto_receive_buffer& recv_buffer); bool is_send_plaintext() const @@ -123,14 +123,14 @@ namespace libtorrent private: struct barrier { - barrier(boost::shared_ptr plugin, int n) + barrier(std::shared_ptr plugin, int n) : enc_handler(plugin), next(n) {} - boost::shared_ptr enc_handler; + std::shared_ptr enc_handler; // number of bytes to next barrier int next; }; std::list m_send_barriers; - boost::shared_ptr m_dec_handler; + std::shared_ptr m_dec_handler; }; struct TORRENT_EXTRA_EXPORT rc4_handler : crypto_plugin diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 448258c65..414165469 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -330,7 +330,7 @@ namespace libtorrent void send_allowed_set(); #ifndef TORRENT_DISABLE_EXTENSIONS - void add_extension(boost::shared_ptr); + void add_extension(std::shared_ptr); #endif // this function is called once the torrent associated @@ -865,9 +865,9 @@ namespace libtorrent // io service io_service& m_ios; - public: + protected: #ifndef TORRENT_DISABLE_EXTENSIONS - typedef std::list> extension_list_t; + typedef std::list> extension_list_t; extension_list_t m_extensions; #endif private: diff --git a/include/libtorrent/peer_connection_handle.hpp b/include/libtorrent/peer_connection_handle.hpp index 8bd7c6452..59ec788f8 100644 --- a/include/libtorrent/peer_connection_handle.hpp +++ b/include/libtorrent/peer_connection_handle.hpp @@ -59,7 +59,7 @@ struct TORRENT_EXPORT peer_connection_handle int type() const; - void add_extension(boost::shared_ptr); + void add_extension(std::shared_ptr); bool is_seed() const; @@ -139,8 +139,8 @@ struct TORRENT_EXPORT bt_peer_connection_handle : public peer_connection_handle bool supports_encryption() const; - void switch_send_crypto(boost::shared_ptr crypto); - void switch_recv_crypto(boost::shared_ptr crypto); + void switch_send_crypto(std::shared_ptr crypto); + void switch_recv_crypto(std::shared_ptr crypto); boost::shared_ptr native_handle() const; }; diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index cf2e680dd..15432e0ed 100644 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -135,7 +135,7 @@ namespace libtorrent // This constructor helps to configure the set of initial plugins // to be added to the session before it's started. session_params(settings_pack sp - , std::vector> exts); + , std::vector> exts); session_params(session_params const&) = default; session_params(session_params&&) = default; @@ -144,7 +144,7 @@ namespace libtorrent settings_pack settings; - std::vector> extensions; + std::vector> extensions; libtorrent::dht_settings dht_settings; diff --git a/include/libtorrent/session_handle.hpp b/include/libtorrent/session_handle.hpp index a31a75935..2c85fd7ed 100644 --- a/include/libtorrent/session_handle.hpp +++ b/include/libtorrent/session_handle.hpp @@ -479,9 +479,9 @@ namespace libtorrent // // // .. _`libtorrent plugins`: libtorrent_plugins.html - void add_extension(std::function( + void add_extension(std::function( torrent_handle const&, void*)> ext); - void add_extension(boost::shared_ptr ext); + void add_extension(std::shared_ptr ext); #ifndef TORRENT_NO_DEPRECATE // GeoIP support has been removed from libtorrent internals. If you diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 4e9d50629..71b0bfc20 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -298,9 +298,9 @@ namespace libtorrent int current_stats_state() const; #ifndef TORRENT_DISABLE_EXTENSIONS - void add_extension(boost::shared_ptr); - void remove_extension(boost::shared_ptr); - void add_extension_fun(std::function(torrent_handle const&, void*)> const& ext + void add_extension(std::shared_ptr); + void remove_extension(std::shared_ptr); + void add_extension_fun(std::function(torrent_handle const&, void*)> const& ext , void* userdata); void notify_extension_add_peer(tcp::endpoint const& ip, int src, int flags); #endif @@ -1205,7 +1205,7 @@ namespace libtorrent std::list m_web_seeds; #ifndef TORRENT_DISABLE_EXTENSIONS - std::list> m_extensions; + std::list> m_extensions; #endif // used for tracker announces diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 96e7eb9f8..29fa450d8 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -511,7 +511,7 @@ namespace libtorrent // pointer. The function is expected to return a shared pointer to // a torrent_plugin instance. void add_extension( - std::function(torrent_handle const&, void*)> const& ext + std::function(torrent_handle const&, void*)> const& ext , void* userdata = 0); // ``set_metadata`` expects the *info* section of metadata. i.e. The diff --git a/src/alert_manager.cpp b/src/alert_manager.cpp index 428060bc6..8b3e8e8be 100644 --- a/src/alert_manager.cpp +++ b/src/alert_manager.cpp @@ -106,7 +106,7 @@ namespace libtorrent } #ifndef TORRENT_DISABLE_EXTENSIONS - void alert_manager::add_extension(boost::shared_ptr ext) + void alert_manager::add_extension(std::shared_ptr ext) { m_ses_extensions.push_back(ext); } diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 2e185de53..a9a09e6e4 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -91,7 +91,7 @@ namespace libtorrent // stream key (info hash of attached torrent) // secret is the DH shared secret // initializes m_enc_handler - boost::shared_ptr init_pe_rc4_handler(key_t const& secret + std::shared_ptr init_pe_rc4_handler(key_t const& secret , sha1_hash const& stream_key, bool const outgoing) { hasher h; @@ -120,7 +120,7 @@ namespace libtorrent h.update(stream_key); sha1_hash const remote_key = h.final(); - boost::shared_ptr ret = boost::make_shared(); + auto ret = std::make_shared(); ret->set_incoming_key(remote_key); ret->set_outgoing_key(local_key); @@ -192,7 +192,7 @@ namespace libtorrent m_in_constructor = false; #endif #ifndef TORRENT_DISABLE_EXTENSIONS - memset(m_reserved_bits, 0, sizeof(m_reserved_bits)); + m_reserved_bits.fill(0); #endif } @@ -209,13 +209,13 @@ namespace libtorrent bt_peer_connection::~bt_peer_connection() = default; #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) - void bt_peer_connection::switch_send_crypto(boost::shared_ptr crypto) + void bt_peer_connection::switch_send_crypto(std::shared_ptr crypto) { if (m_enc_handler.switch_send_crypto(crypto, send_buffer_size() - get_send_barrier())) set_send_barrier(send_buffer_size()); } - void bt_peer_connection::switch_recv_crypto(boost::shared_ptr crypto) + void bt_peer_connection::switch_recv_crypto(std::shared_ptr crypto) { m_enc_handler.switch_recv_crypto(crypto, m_recv_buffer); } @@ -2618,7 +2618,7 @@ namespace libtorrent // again according to peer selection. switch_send_crypto(m_rc4); write_handshake(); - switch_send_crypto(boost::shared_ptr()); + switch_send_crypto(std::shared_ptr()); // vc,crypto_select,len(pad),pad, encrypt(handshake) // 8+4+2+0+handshake_len @@ -3212,7 +3212,7 @@ namespace libtorrent #endif #ifndef TORRENT_DISABLE_EXTENSIONS - std::memcpy(m_reserved_bits, recv_buffer.begin(), 8); + std::memcpy(m_reserved_bits.data(), recv_buffer.begin(), 8); if ((recv_buffer[5] & 0x10)) m_supports_extensions = true; #endif diff --git a/src/pe_crypto.cpp b/src/pe_crypto.cpp index 5090d5b6e..c6fa0a07b 100644 --- a/src/pe_crypto.cpp +++ b/src/pe_crypto.cpp @@ -233,7 +233,7 @@ namespace libtorrent return consume; } - bool encryption_handler::switch_send_crypto(boost::shared_ptr crypto + bool encryption_handler::switch_send_crypto(std::shared_ptr crypto , int pending_encryption) { bool place_barrier = false; @@ -255,7 +255,7 @@ namespace libtorrent return place_barrier; } - void encryption_handler::switch_recv_crypto(boost::shared_ptr crypto + void encryption_handler::switch_recv_crypto(std::shared_ptr crypto , crypto_receive_buffer& recv_buffer) { m_dec_handler = crypto; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 60d6b89f8..e937a86a9 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -523,7 +523,7 @@ namespace libtorrent #endif #ifndef TORRENT_DISABLE_EXTENSIONS - void peer_connection::add_extension(boost::shared_ptr ext) + void peer_connection::add_extension(std::shared_ptr ext) { TORRENT_ASSERT(is_single_thread()); m_extensions.push_back(ext); @@ -2654,10 +2654,9 @@ namespace libtorrent update_desired_queue_size(); #ifndef TORRENT_DISABLE_EXTENSIONS - for (extension_list_t::iterator i = m_extensions.begin() - , end(m_extensions.end()); i != end; ++i) + for (auto& e : m_extensions) { - if ((*i)->on_piece(p, data.get())) + if (e->on_piece(p, {data.get(), size_t(p.length)})) { #if TORRENT_USE_ASSERTS TORRENT_ASSERT(m_received_in_piece == p.length); diff --git a/src/peer_connection_handle.cpp b/src/peer_connection_handle.cpp index a258cee35..578f575bb 100644 --- a/src/peer_connection_handle.cpp +++ b/src/peer_connection_handle.cpp @@ -48,7 +48,7 @@ int peer_connection_handle::type() const return pc->type(); } -void peer_connection_handle::add_extension(boost::shared_ptr ext) +void peer_connection_handle::add_extension(std::shared_ptr ext) { #ifndef TORRENT_DISABLE_EXTENSIONS boost::shared_ptr pc = native_handle(); @@ -299,7 +299,7 @@ bool bt_peer_connection_handle::supports_encryption() const #endif } -void bt_peer_connection_handle::switch_send_crypto(boost::shared_ptr crypto) +void bt_peer_connection_handle::switch_send_crypto(std::shared_ptr crypto) { #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) boost::shared_ptr pc = native_handle(); @@ -310,7 +310,7 @@ void bt_peer_connection_handle::switch_send_crypto(boost::shared_ptr crypto) +void bt_peer_connection_handle::switch_recv_crypto(std::shared_ptr crypto) { #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) boost::shared_ptr pc = native_handle(); diff --git a/src/session.cpp b/src/session.cpp index 4e3b71804..28d7642a2 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -325,16 +325,16 @@ namespace libtorrent namespace { - std::vector> default_plugins( + std::vector> default_plugins( bool empty = false) { #ifndef TORRENT_DISABLE_EXTENSIONS if (empty) return {}; using wrapper = session_impl::session_plugin_wrapper; return { - boost::make_shared(create_ut_pex_plugin), - boost::make_shared(create_ut_metadata_plugin), - boost::make_shared(create_smart_ban_plugin) + std::make_shared(create_ut_pex_plugin), + std::make_shared(create_ut_metadata_plugin), + std::make_shared(create_smart_ban_plugin) }; #else TORRENT_UNUSED(empty); @@ -411,7 +411,7 @@ namespace libtorrent {} session_params::session_params(settings_pack sp - , std::vector> exts) + , std::vector> exts) : settings(std::move(sp)) , extensions(std::move(exts)) #ifndef TORRENT_DISABLE_DHT diff --git a/src/session_handle.cpp b/src/session_handle.cpp index 041526028..904e26910 100644 --- a/src/session_handle.cpp +++ b/src/session_handle.cpp @@ -601,7 +601,7 @@ namespace libtorrent } #endif // TORRENT_NO_DEPRECATE - void session_handle::add_extension(std::function(torrent_handle const&, void*)> ext) + void session_handle::add_extension(std::function(torrent_handle const&, void*)> ext) { #ifndef TORRENT_DISABLE_EXTENSIONS async_call(&session_impl::add_extension, ext); @@ -610,7 +610,7 @@ namespace libtorrent #endif } - void session_handle::add_extension(boost::shared_ptr ext) + void session_handle::add_extension(std::shared_ptr ext) { #ifndef TORRENT_DISABLE_EXTENSIONS async_call(&session_impl::add_ses_extension, ext); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 87732605a..c9c2ca4dd 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -776,10 +776,10 @@ namespace aux { TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(ext); - add_ses_extension(boost::make_shared(ext)); + add_ses_extension(std::make_shared(ext)); } - void session_impl::add_ses_extension(boost::shared_ptr ext) + void session_impl::add_ses_extension(std::shared_ptr ext) { TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT_VAL(ext, ext); @@ -796,7 +796,8 @@ namespace aux { m_ses_extensions[plugins_dht_request_idx].push_back(ext); if (features & plugin::alert_feature) m_alerts.add_extension(ext); - ext->added(session_handle(this)); + session_handle h(this); + ext->added(h); } #endif // TORRENT_DISABLE_EXTENSIONS @@ -3739,11 +3740,11 @@ namespace aux { struct last_optimistic_unchoke_cmp { #ifndef TORRENT_DISABLE_EXTENSIONS - explicit last_optimistic_unchoke_cmp(std::vector>& ps) + explicit last_optimistic_unchoke_cmp(std::vector>& ps) : plugins(ps) {} - std::vector>& plugins; + std::vector>& plugins; #endif uint64_t get_ext_priority(opt_unchoke_candidate const& peer) const @@ -4627,7 +4628,7 @@ namespace aux { { for (auto& e : m_ses_extensions[plugins_all_idx]) { - boost::shared_ptr tp(e->new_torrent( + std::shared_ptr tp(e->new_torrent( torrent_ptr->get_handle(), userdata)); if (tp) torrent_ptr->add_extension(std::move(tp)); } @@ -4676,7 +4677,7 @@ namespace aux { #ifndef TORRENT_DISABLE_EXTENSIONS for (auto& ext : params.extensions) { - boost::shared_ptr tp(ext(handle, params.userdata)); + std::shared_ptr tp(ext(handle, params.userdata)); if (tp) torrent_ptr->add_extension(std::move(tp)); } diff --git a/src/smart_ban.cpp b/src/smart_ban.cpp index fa8c5ae9b..293c86201 100644 --- a/src/smart_ban.cpp +++ b/src/smart_ban.cpp @@ -74,7 +74,7 @@ namespace struct smart_ban_plugin final : torrent_plugin - , boost::enable_shared_from_this + , std::enable_shared_from_this { explicit smart_ban_plugin(torrent& t) : m_torrent(t) @@ -327,14 +327,11 @@ namespace namespace libtorrent { - - boost::shared_ptr create_smart_ban_plugin(torrent_handle const& th, void*) + std::shared_ptr create_smart_ban_plugin(torrent_handle const& th, void*) { torrent* t = th.native_handle().get(); - return boost::shared_ptr(new smart_ban_plugin(*t)); + return std::make_shared(*t); } - } #endif - diff --git a/src/torrent.cpp b/src/torrent.cpp index 2ac7a04b3..eb70058b7 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -1445,29 +1445,29 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS - void torrent::add_extension(boost::shared_ptr ext) + void torrent::add_extension(std::shared_ptr ext) { m_extensions.push_back(ext); } - void torrent::remove_extension(boost::shared_ptr ext) + void torrent::remove_extension(std::shared_ptr ext) { auto i = std::find(m_extensions.begin(), m_extensions.end(), ext); if (i == m_extensions.end()) return; m_extensions.erase(i); } - void torrent::add_extension_fun(std::function(torrent_handle const&, void*)> const& ext + void torrent::add_extension_fun(std::function(torrent_handle const&, void*)> const& ext , void* userdata) { - boost::shared_ptr tp(ext(get_handle(), userdata)); + std::shared_ptr tp(ext(get_handle(), userdata)); if (!tp) return; add_extension(std::move(tp)); for (auto p : m_connections) { - boost::shared_ptr pp(tp->new_connection(peer_connection_handle(p->self()))); + std::shared_ptr pp(tp->new_connection(peer_connection_handle(p->self()))); if (pp) p->add_extension(std::move(pp)); } @@ -6260,7 +6260,7 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS for (auto& ext : m_extensions) { - boost::shared_ptr + std::shared_ptr pp(ext->new_connection(peer_connection_handle(c->self()))); if (pp) c->add_extension(pp); } @@ -6960,7 +6960,7 @@ namespace libtorrent for (auto& ext : m_extensions) { TORRENT_TRY { - boost::shared_ptr pp(ext->new_connection( + std::shared_ptr pp(ext->new_connection( peer_connection_handle(c->self()))); if (pp) c->add_extension(pp); } TORRENT_CATCH (std::exception&) {} @@ -7254,7 +7254,7 @@ namespace libtorrent #ifndef TORRENT_DISABLE_EXTENSIONS for (auto& ext : m_extensions) { - boost::shared_ptr pp(ext->new_connection( + std::shared_ptr pp(ext->new_connection( peer_connection_handle(p->self()))); if (pp) p->add_extension(pp); } diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 90e1c57b7..f6b153e8b 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -206,7 +206,7 @@ namespace libtorrent } void torrent_handle::add_extension( - std::function(torrent_handle const&, void*)> const& ext + std::function(torrent_handle const&, void*)> const& ext , void* userdata) { #ifndef TORRENT_DISABLE_EXTENSIONS diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 11dc8e716..f649d4387 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/random.hpp" #include "libtorrent/io.hpp" #include "libtorrent/performance_counters.hpp" // for counters +#include "libtorrent/aux_/time.hpp" using namespace std::placeholders; @@ -125,7 +126,7 @@ namespace libtorrent { namespace metadata(); } - boost::shared_ptr new_connection( + std::shared_ptr new_connection( peer_connection_handle const& pc) override; int get_metadata_size() const @@ -188,7 +189,7 @@ namespace libtorrent { namespace metadata_piece(): num_requests(0), last_request(min_time()) {} int num_requests; time_point last_request; - boost::weak_ptr source; + std::weak_ptr source; bool operator<(metadata_piece const& rhs) const { return num_requests < rhs.num_requests; } }; @@ -204,7 +205,7 @@ namespace libtorrent { namespace struct ut_metadata_peer_plugin final - : peer_plugin, boost::enable_shared_from_this + : peer_plugin, std::enable_shared_from_this { friend struct ut_metadata_plugin; @@ -496,14 +497,14 @@ namespace libtorrent { namespace ut_metadata_peer_plugin& operator=(ut_metadata_peer_plugin const&); }; - boost::shared_ptr ut_metadata_plugin::new_connection( + std::shared_ptr ut_metadata_plugin::new_connection( peer_connection_handle const& pc) { if (pc.type() != peer_connection::bittorrent_connection) - return boost::shared_ptr(); + return std::shared_ptr(); bt_peer_connection* c = static_cast(pc.native_handle().get()); - return boost::shared_ptr(new ut_metadata_peer_plugin(m_torrent, *c, *this)); + return std::make_shared(m_torrent, *c, *this); } // has_metadata is false if the peer making the request has not announced @@ -624,7 +625,7 @@ namespace libtorrent { namespace for (int i = 0; i < int(m_requested_metadata.size()); ++i) { m_requested_metadata[i].num_requests = 0; - boost::shared_ptr peer + std::shared_ptr peer = m_requested_metadata[i].source.lock(); if (!peer) continue; @@ -650,15 +651,13 @@ namespace libtorrent { namespace namespace libtorrent { - - boost::shared_ptr create_ut_metadata_plugin(torrent_handle const& th, void*) + std::shared_ptr create_ut_metadata_plugin(torrent_handle const& th, void*) { torrent* t = th.native_handle().get(); // don't add this extension if the torrent is private - if (t->valid_metadata() && t->torrent_file().priv()) return boost::shared_ptr(); - return boost::shared_ptr(new ut_metadata_plugin(*t)); + if (t->valid_metadata() && t->torrent_file().priv()) return std::shared_ptr(); + return std::make_shared(*t); } - } #endif diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index 83aed37a5..6cf9b1408 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket_type.hpp" // for is_utp #include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/extensions/ut_pex.hpp" +#include "libtorrent/aux_/time.hpp" #ifndef TORRENT_DISABLE_EXTENSIONS @@ -79,7 +80,7 @@ namespace libtorrent { namespace , m_last_msg(min_time()) , m_peers_in_message(0) {} - boost::shared_ptr new_connection( + std::shared_ptr new_connection( peer_connection_handle const& pc) override; std::vector& get_ut_pex_msg() @@ -633,13 +634,13 @@ namespace libtorrent { namespace ut_pex_peer_plugin& operator=(ut_pex_peer_plugin const&); }; - boost::shared_ptr ut_pex_plugin::new_connection(peer_connection_handle const& pc) + std::shared_ptr ut_pex_plugin::new_connection(peer_connection_handle const& pc) { if (pc.type() != peer_connection::bittorrent_connection) - return boost::shared_ptr(); + return std::shared_ptr(); bt_peer_connection* c = static_cast(pc.native_handle().get()); - auto p = boost::make_shared(m_torrent, *c, *this); + auto p = std::make_shared(m_torrent, *c, *this); c->set_ut_pex(p); return p; } @@ -647,15 +648,15 @@ namespace libtorrent { namespace namespace libtorrent { - boost::shared_ptr create_ut_pex_plugin(torrent_handle const& th, void*) + std::shared_ptr create_ut_pex_plugin(torrent_handle const& th, void*) { torrent* t = th.native_handle().get(); if (t->torrent_file().priv() || (t->torrent_file().is_i2p() && !t->settings().get_bool(settings_pack::allow_i2p_mixed))) { - return boost::shared_ptr(); + return std::shared_ptr(); } - return boost::make_shared(*t); + return std::make_shared(*t); } } diff --git a/test/test_alert_manager.cpp b/test/test_alert_manager.cpp index a4c54e33a..61e5734cb 100644 --- a/test/test_alert_manager.cpp +++ b/test/test_alert_manager.cpp @@ -38,9 +38,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "setup_transfer.hpp" #include -#include -#include - #include namespace lt = libtorrent; @@ -172,9 +169,9 @@ TORRENT_TEST(extensions) memset(plugin_alerts, 0, sizeof(plugin_alerts)); alert_manager mgr(100, 0xffffffff); - mgr.add_extension(boost::make_shared(0)); - mgr.add_extension(boost::make_shared(1)); - mgr.add_extension(boost::make_shared(2)); + mgr.add_extension(std::make_shared(0)); + mgr.add_extension(std::make_shared(1)); + mgr.add_extension(std::make_shared(2)); for (int i = 0; i < 53; ++i) mgr.emplace_alert(torrent_handle()); diff --git a/test/test_direct_dht.cpp b/test/test_direct_dht.cpp index 307f04108..cfae965c6 100644 --- a/test/test_direct_dht.cpp +++ b/test/test_direct_dht.cpp @@ -101,7 +101,7 @@ TORRENT_TEST(direct_dht_request) sp.set_str(settings_pack::listen_interfaces, "127.0.0.1:45434"); lt::session requester(sp, 0); - responder.add_extension(boost::static_pointer_cast(boost::make_shared())); + responder.add_extension(std::make_shared()); // successful request diff --git a/test/test_session_params.cpp b/test/test_session_params.cpp index a8aaa0797..c8e8a6807 100644 --- a/test/test_session_params.cpp +++ b/test/test_session_params.cpp @@ -58,7 +58,7 @@ namespace struct custom_plugin : plugin { - void added(session_handle h) override + void added(session_handle const& h) override { TORRENT_UNUSED(h); g_plugin_added_invoked = true; @@ -76,7 +76,7 @@ TORRENT_TEST(default_plugins) TEST_EQUAL(int(p1.extensions.size()), 0); #endif - std::vector> exts; + std::vector> exts; session_params p2(settings_pack(), exts); TEST_EQUAL(int(p2.extensions.size()), 0); } @@ -100,7 +100,7 @@ TORRENT_TEST(add_plugin) { g_plugin_added_invoked = false; session_params params; - params.extensions.push_back(boost::make_shared()); + params.extensions.push_back(std::make_shared()); lt::session ses(params); TEST_EQUAL(g_plugin_added_invoked, true); diff --git a/test/test_torrent.cpp b/test/test_torrent.cpp index e4969e505..8acc05676 100644 --- a/test/test_torrent.cpp +++ b/test/test_torrent.cpp @@ -314,11 +314,11 @@ struct plugin_creator { explicit plugin_creator(int& c) : m_called(c) {} - boost::shared_ptr + std::shared_ptr operator()(torrent_handle const&, void*) { ++m_called; - return boost::make_shared(); + return std::make_shared(); } int& m_called;