From 87de08cb3d41014b1ecac99c59af96cbcac88a32 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sun, 17 Dec 2017 11:19:18 +0100 Subject: [PATCH] improve type safety of plugin interface --- ChangeLog | 1 + include/libtorrent/extensions.hpp | 30 +++++++++++++++++------------- include/libtorrent/torrent.hpp | 3 ++- src/session.cpp | 6 ++++++ src/torrent.cpp | 11 +++++++---- 5 files changed, 33 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7c6c4c00a..30174134c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,4 @@ + * update plugin interface functions for improved type-safety * implemented support magnet URI extension, select specific file indices for download, BEP53 * make tracker keys multi-homed. remove set_key() function on session. diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index 0cecc4c35..00ffd007d 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/units.hpp" #include "libtorrent/flags.hpp" +#include "libtorrent/peer_info.hpp" // for peer_source_flags_t +#include "libtorrent/torrent_status.hpp" // for torrent_status::state_t // OVERVIEW // @@ -276,6 +278,9 @@ namespace libtorrent { virtual void load_state(bdecode_node const&) {} }; + struct add_peer_flags_tag; + using add_peer_flags_t = flags::bitfield_flag; + // Torrent plugins are associated with a single torrent and have a number // of functions called at certain events. Many of its functions have the // ability to change or override the default libtorrent behavior. @@ -306,8 +311,8 @@ namespace libtorrent { // check, respectively. The ``index`` is the piece index that was downloaded. // It is possible to access the list of peers that participated in sending the // piece through the ``torrent`` and the ``piece_picker``. - virtual void on_piece_pass(piece_index_t /*index*/) {} - virtual void on_piece_failed(piece_index_t /*index*/) {} + virtual void on_piece_pass(piece_index_t) {} + virtual void on_piece_failed(piece_index_t) {} // This hook is called approximately once per second. It is a way of making it // easy for plugins to do timed events, for sending messages or whatever. @@ -337,19 +342,18 @@ namespace libtorrent { // called when the torrent changes state // the state is one of torrent_status::state_t // enum members - virtual void on_state(int /*s*/) {} + virtual void on_state(torrent_status::state_t) {} // called every time policy::add_peer is called // src is a bitmask of which sources this peer // has been seen from. flags is a bitmask of: - enum flags_t { - // this is the first time we see this peer - first_time = 1, - // this peer was not added because it was - // filtered by the IP filter - filtered = 2 - }; + // this is the first time we see this peer + static constexpr add_peer_flags_t first_time = 1_bit; + + // this peer was not added because it was + // filtered by the IP filter + static constexpr add_peer_flags_t filtered = 2_bit; // called every time a new peer is added to the peer list. // This is before the peer is connected to. For ``flags``, see @@ -358,7 +362,7 @@ namespace libtorrent { // bitmask, because many sources may have told us about the same // peer. For peer source flags, see peer_info::peer_source_flags. virtual void on_add_peer(tcp::endpoint const&, - int /*src*/, int /*flags*/) {} + peer_source_flags_t, add_peer_flags_t) {} }; // peer plugins are associated with a specific peer. A peer could be @@ -381,7 +385,7 @@ namespace libtorrent { virtual void add_handshake(entry&) {} // called when the peer is being disconnected. - virtual void on_disconnect(error_code const& /*ec*/) {} + virtual void on_disconnect(error_code const&) {} // called when the peer is successfully connected. Note that // incoming connections will have been connected by the time @@ -396,7 +400,7 @@ namespace libtorrent { // Returning false 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(span /*reserved_bits*/) { return true; } + virtual bool on_handshake(span) { return true; } // called when the extension handshake from the other end is received // if this returns false, it means that this extension isn't diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index c883477f2..691125143 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -74,6 +74,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/vector.hpp" #include "libtorrent/aux_/deferred_handler.hpp" #include "libtorrent/aux_/allocating_handler.hpp" +#include "libtorrent/extensions.hpp" // for add_peer_flags_t #ifdef TORRENT_USE_OPENSSL // there is no forward declaration header for asio @@ -369,7 +370,7 @@ namespace libtorrent { void add_extension_fun(std::function(torrent_handle const&, void*)> const& ext , void* userdata); void notify_extension_add_peer(tcp::endpoint const& ip - , peer_source_flags_t src, int flags); + , peer_source_flags_t src, add_peer_flags_t flags); #endif peer_connection* find_lowest_ranking_peer() const; diff --git a/src/session.cpp b/src/session.cpp index 8be7ec24a..9602ed2a5 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/extensions.hpp" #include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_call.hpp" +#include "libtorrent/extensions.hpp" // for add_peer_flags_t namespace libtorrent { @@ -61,6 +62,11 @@ namespace aux { constexpr torrent_list_index_t session_interface::torrent_checking_auto_managed; } +#ifndef TORRENT_DISABLE_EXTENSIONS +constexpr add_peer_flags_t torrent_plugin::first_time; +constexpr add_peer_flags_t torrent_plugin::filtered; +#endif + namespace { #if defined TORRENT_ASIO_DEBUGGING diff --git a/src/torrent.cpp b/src/torrent.cpp index bf04a7279..32ec7bb7b 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -10104,7 +10104,10 @@ namespace libtorrent { { state_updated(); #ifndef TORRENT_DISABLE_EXTENSIONS - notify_extension_add_peer(adr, source, st.first_time_seen ? torrent_plugin::first_time : 0); + notify_extension_add_peer(adr, source + , st.first_time_seen + ? torrent_plugin::first_time + : add_peer_flags_t{}); #endif } else @@ -10514,18 +10517,18 @@ namespace { #ifndef TORRENT_DISABLE_EXTENSIONS for (auto& ext : m_extensions) { - ext->on_state(m_state); + ext->on_state(state()); } #endif } #ifndef TORRENT_DISABLE_EXTENSIONS void torrent::notify_extension_add_peer(tcp::endpoint const& ip - , peer_source_flags_t const src, int const flags) + , peer_source_flags_t const src, add_peer_flags_t const flags) { for (auto& ext : m_extensions) { - ext->on_add_peer(ip, static_cast(src), flags); + ext->on_add_peer(ip, src, flags); } } #endif