improve type safety of plugin interface

This commit is contained in:
arvidn 2017-12-17 11:19:18 +01:00 committed by Arvid Norberg
parent bb945f5cf4
commit 87de08cb3d
5 changed files with 33 additions and 18 deletions

View File

@ -1,3 +1,4 @@
* update plugin interface functions for improved type-safety
* implemented support magnet URI extension, select specific file indices * implemented support magnet URI extension, select specific file indices
for download, BEP53 for download, BEP53
* make tracker keys multi-homed. remove set_key() function on session. * make tracker keys multi-homed. remove set_key() function on session.

View File

@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/units.hpp" #include "libtorrent/units.hpp"
#include "libtorrent/flags.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 // OVERVIEW
// //
@ -276,6 +278,9 @@ namespace libtorrent {
virtual void load_state(bdecode_node const&) {} virtual void load_state(bdecode_node const&) {}
}; };
struct add_peer_flags_tag;
using add_peer_flags_t = flags::bitfield_flag<std::uint8_t, add_peer_flags_tag>;
// Torrent plugins are associated with a single torrent and have a number // Torrent plugins are associated with a single torrent and have a number
// of functions called at certain events. Many of its functions have the // of functions called at certain events. Many of its functions have the
// ability to change or override the default libtorrent behavior. // 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. // 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 // It is possible to access the list of peers that participated in sending the
// piece through the ``torrent`` and the ``piece_picker``. // piece through the ``torrent`` and the ``piece_picker``.
virtual void on_piece_pass(piece_index_t /*index*/) {} virtual void on_piece_pass(piece_index_t) {}
virtual void on_piece_failed(piece_index_t /*index*/) {} virtual void on_piece_failed(piece_index_t) {}
// This hook is called approximately once per second. It is a way of making it // 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. // easy for plugins to do timed events, for sending messages or whatever.
@ -337,19 +342,18 @@ namespace libtorrent {
// called when the torrent changes state // called when the torrent changes state
// the state is one of torrent_status::state_t // the state is one of torrent_status::state_t
// enum members // enum members
virtual void on_state(int /*s*/) {} virtual void on_state(torrent_status::state_t) {}
// called every time policy::add_peer is called // called every time policy::add_peer is called
// src is a bitmask of which sources this peer // src is a bitmask of which sources this peer
// has been seen from. flags is a bitmask of: // has been seen from. flags is a bitmask of:
enum flags_t { // this is the first time we see this peer
// this is the first time we see this peer static constexpr add_peer_flags_t first_time = 1_bit;
first_time = 1,
// this peer was not added because it was // this peer was not added because it was
// filtered by the IP filter // filtered by the IP filter
filtered = 2 static constexpr add_peer_flags_t filtered = 2_bit;
};
// called every time a new peer is added to the peer list. // called every time a new peer is added to the peer list.
// This is before the peer is connected to. For ``flags``, see // 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 // bitmask, because many sources may have told us about the same
// peer. For peer source flags, see peer_info::peer_source_flags. // peer. For peer source flags, see peer_info::peer_source_flags.
virtual void on_add_peer(tcp::endpoint const&, 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 // peer plugins are associated with a specific peer. A peer could be
@ -381,7 +385,7 @@ namespace libtorrent {
virtual void add_handshake(entry&) {} virtual void add_handshake(entry&) {}
// called when the peer is being disconnected. // 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 // called when the peer is successfully connected. Note that
// incoming connections will have been connected by the time // 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 // 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 // and will remove it from the list of plugins. this is not called for web
// seeds // seeds
virtual bool on_handshake(span<char const> /*reserved_bits*/) { return true; } virtual bool on_handshake(span<char const>) { return true; }
// called when the extension handshake from the other end is received // called when the extension handshake from the other end is received
// if this returns false, it means that this extension isn't // if this returns false, it means that this extension isn't

View File

@ -74,6 +74,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/aux_/vector.hpp" #include "libtorrent/aux_/vector.hpp"
#include "libtorrent/aux_/deferred_handler.hpp" #include "libtorrent/aux_/deferred_handler.hpp"
#include "libtorrent/aux_/allocating_handler.hpp" #include "libtorrent/aux_/allocating_handler.hpp"
#include "libtorrent/extensions.hpp" // for add_peer_flags_t
#ifdef TORRENT_USE_OPENSSL #ifdef TORRENT_USE_OPENSSL
// there is no forward declaration header for asio // there is no forward declaration header for asio
@ -369,7 +370,7 @@ namespace libtorrent {
void add_extension_fun(std::function<std::shared_ptr<torrent_plugin>(torrent_handle const&, void*)> const& ext void add_extension_fun(std::function<std::shared_ptr<torrent_plugin>(torrent_handle const&, void*)> const& ext
, void* userdata); , void* userdata);
void notify_extension_add_peer(tcp::endpoint const& ip 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 #endif
peer_connection* find_lowest_ranking_peer() const; peer_connection* find_lowest_ranking_peer() const;

View File

@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/extensions.hpp" #include "libtorrent/extensions.hpp"
#include "libtorrent/aux_/session_impl.hpp" #include "libtorrent/aux_/session_impl.hpp"
#include "libtorrent/aux_/session_call.hpp" #include "libtorrent/aux_/session_call.hpp"
#include "libtorrent/extensions.hpp" // for add_peer_flags_t
namespace libtorrent { namespace libtorrent {
@ -61,6 +62,11 @@ namespace aux {
constexpr torrent_list_index_t session_interface::torrent_checking_auto_managed; 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 { namespace {
#if defined TORRENT_ASIO_DEBUGGING #if defined TORRENT_ASIO_DEBUGGING

View File

@ -10104,7 +10104,10 @@ namespace libtorrent {
{ {
state_updated(); state_updated();
#ifndef TORRENT_DISABLE_EXTENSIONS #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 #endif
} }
else else
@ -10514,18 +10517,18 @@ namespace {
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (auto& ext : m_extensions) for (auto& ext : m_extensions)
{ {
ext->on_state(m_state); ext->on_state(state());
} }
#endif #endif
} }
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
void torrent::notify_extension_add_peer(tcp::endpoint const& ip 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) for (auto& ext : m_extensions)
{ {
ext->on_add_peer(ip, static_cast<std::uint8_t>(src), flags); ext->on_add_peer(ip, src, flags);
} }
} }
#endif #endif