convert picker_flags_t to type safe flags

This commit is contained in:
arvidn 2017-07-23 00:55:40 -07:00 committed by Arvid Norberg
parent 78bbd298a5
commit 330904f460
6 changed files with 50 additions and 30 deletions

View File

@ -14,6 +14,7 @@
#include "libtorrent/disk_interface.hpp" // for open_file_state
#include "libtorrent/aux_/noexcept_movable.hpp"
#include "libtorrent/peer_info.hpp"
#include "libtorrent/alert_types.hpp" // for picker_flags_t
#include <vector>
using namespace boost::python;
@ -295,6 +296,7 @@ void bind_converters()
to_python_converter<lt::peer_source_flags_t, from_bitfield_flag<lt::peer_source_flags_t>>();
to_python_converter<lt::bandwidth_state_flags_t, from_bitfield_flag<lt::bandwidth_state_flags_t>>();
to_python_converter<lt::file_open_mode_t, from_bitfield_flag<lt::file_open_mode_t>>();
to_python_converter<lt::picker_flags_t, from_bitfield_flag<lt::picker_flags_t>>();
// work-around types
to_python_converter<lt::aux::noexcept_movable<lt::address>, address_to_tuple<
@ -342,4 +344,5 @@ void bind_converters()
to_bitfield_flag<lt::peer_source_flags_t>();
to_bitfield_flag<lt::bandwidth_state_flags_t>();
to_bitfield_flag<lt::file_open_mode_t>();
to_bitfield_flag<lt::picker_flags_t>();
}

View File

@ -2506,6 +2506,10 @@ namespace libtorrent {
#endif
};
// hidden
struct picker_flags_tag;
using picker_flags_t = flags::bitfield_flag<std::uint32_t, picker_flags_tag>;
// this is posted when one or more blocks are picked by the piece picker,
// assuming the verbose piece picker logging is enabled (see
// picker_log_notification).
@ -2513,7 +2517,7 @@ namespace libtorrent {
{
// internal
picker_log_alert(aux::stack_allocator& alloc, torrent_handle const& h
, tcp::endpoint const& ep, peer_id const& peer_id, std::uint32_t flags
, tcp::endpoint const& ep, peer_id const& peer_id, picker_flags_t flags
, piece_block const* blocks, int num_blocks);
TORRENT_DEFINE_ALERT(picker_log_alert, 89)
@ -2521,31 +2525,26 @@ namespace libtorrent {
static const int static_category = alert::picker_log_notification;
virtual std::string message() const override;
enum picker_flags_t
{
// the ratio of partial pieces is too high. This forces a preference
// for picking blocks from partial pieces.
partial_ratio = 0x1,
prioritize_partials = 0x2,
rarest_first_partials = 0x4,
rarest_first = 0x8,
reverse_rarest_first = 0x10,
suggested_pieces = 0x20,
prio_sequential_pieces = 0x40,
sequential_pieces = 0x80,
reverse_pieces = 0x100,
time_critical = 0x200,
random_pieces = 0x400,
prefer_contiguous = 0x800,
reverse_sequential = 0x1000,
backup1 = 0x2000,
backup2 = 0x4000,
end_game = 0x8000
};
static constexpr picker_flags_t partial_ratio{0x1};
static constexpr picker_flags_t prioritize_partials{0x2};
static constexpr picker_flags_t rarest_first_partials{0x4};
static constexpr picker_flags_t rarest_first{0x8};
static constexpr picker_flags_t reverse_rarest_first{0x10};
static constexpr picker_flags_t suggested_pieces{0x20};
static constexpr picker_flags_t prio_sequential_pieces{0x40};
static constexpr picker_flags_t sequential_pieces{0x80};
static constexpr picker_flags_t reverse_pieces{0x100};
static constexpr picker_flags_t time_critical{0x200};
static constexpr picker_flags_t random_pieces{0x400};
static constexpr picker_flags_t prefer_contiguous{0x800};
static constexpr picker_flags_t reverse_sequential{0x1000};
static constexpr picker_flags_t backup1{0x2000};
static constexpr picker_flags_t backup2{0x4000};
static constexpr picker_flags_t end_game{0x8000};
// this is a bitmask of which features were enabled for this particular
// pick. The bits are defined in the picker_flags_t enum.
std::uint32_t const picker_flags;
picker_flags_t const picker_flags;
std::vector<piece_block> blocks() const;

View File

@ -57,6 +57,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/piece_block.hpp"
#include "libtorrent/aux_/vector.hpp"
#include "libtorrent/aux_/typed_span.hpp"
#include "libtorrent/alert_types.hpp" // for picker_flags_t
namespace libtorrent {
@ -256,7 +257,7 @@ namespace libtorrent {
// this feature is used by web_peer_connection to request larger blocks
// at a time to mitigate limited pipelining and lack of keep-alive
// (i.e. higher overhead per request).
std::uint32_t pick_pieces(typed_bitfield<piece_index_t> const& pieces
picker_flags_t pick_pieces(typed_bitfield<piece_index_t> const& pieces
, std::vector<piece_block>& interesting_blocks, int num_blocks
, int prefer_contiguous_blocks, torrent_peer* peer
, int options, std::vector<piece_index_t> const& suggested_pieces

View File

@ -2133,7 +2133,7 @@ namespace {
}
picker_log_alert::picker_log_alert(aux::stack_allocator& alloc, torrent_handle const& h
, tcp::endpoint const& ep, peer_id const& peer_id, std::uint32_t flags
, tcp::endpoint const& ep, peer_id const& peer_id, picker_flags_t const flags
, piece_block const* blocks, int num_blocks)
: peer_alert(alloc, h, ep, peer_id)
, picker_flags(flags)
@ -2155,6 +2155,23 @@ namespace {
return ret;
}
constexpr picker_flags_t picker_log_alert::partial_ratio;
constexpr picker_flags_t picker_log_alert::prioritize_partials;
constexpr picker_flags_t picker_log_alert::rarest_first_partials;
constexpr picker_flags_t picker_log_alert::rarest_first;
constexpr picker_flags_t picker_log_alert::reverse_rarest_first;
constexpr picker_flags_t picker_log_alert::suggested_pieces;
constexpr picker_flags_t picker_log_alert::prio_sequential_pieces;
constexpr picker_flags_t picker_log_alert::sequential_pieces;
constexpr picker_flags_t picker_log_alert::reverse_pieces;
constexpr picker_flags_t picker_log_alert::time_critical;
constexpr picker_flags_t picker_log_alert::random_pieces;
constexpr picker_flags_t picker_log_alert::prefer_contiguous;
constexpr picker_flags_t picker_log_alert::reverse_sequential;
constexpr picker_flags_t picker_log_alert::backup1;
constexpr picker_flags_t picker_log_alert::backup2;
constexpr picker_flags_t picker_log_alert::end_game;
std::string picker_log_alert::message() const
{
static char const* const flag_names[] =
@ -2179,7 +2196,7 @@ namespace {
std::string ret = peer_alert::message();
std::uint32_t flags = picker_flags;
auto flags = static_cast<std::uint32_t>(picker_flags);
int idx = 0;
ret += " picker_log [ ";
for (; flags != 0; flags >>= 1, ++idx)

View File

@ -1833,10 +1833,10 @@ namespace {
// only one of rarest_first or sequential can be set
// the return value is a combination of picker_log_alert::picker_flags_t,
// the return value is a combination of picker_flags_t,
// indicating which path thought the picker we took to arrive at the
// returned block picks.
std::uint32_t piece_picker::pick_pieces(typed_bitfield<piece_index_t> const& pieces
picker_flags_t piece_picker::pick_pieces(typed_bitfield<piece_index_t> const& pieces
, std::vector<piece_block>& interesting_blocks, int num_blocks
, int prefer_contiguous_blocks, torrent_peer* peer
, int options, std::vector<piece_index_t> const& suggested_pieces
@ -1845,7 +1845,7 @@ namespace {
) const
{
TORRENT_ASSERT(peer == nullptr || peer->in_use);
std::uint32_t ret = 0;
picker_flags_t ret;
// prevent the number of partial pieces to grow indefinitely
// make this scale by the number of peers we have. For large

View File

@ -162,7 +162,7 @@ namespace libtorrent {
// the last argument is if we should prefer whole pieces
// for this peer. If we're downloading one piece in 20 seconds
// then use this mode.
std::uint32_t const flags = p.pick_pieces(*bits, interesting_pieces
picker_flags_t const flags = p.pick_pieces(*bits, interesting_pieces
, num_requests, prefer_contiguous_blocks, c.peer_info_struct()
, c.picker_options(), suggested, t.num_peers()
, ses.stats_counters());