convert block request flags to type-safe flags

This commit is contained in:
arvidn 2017-07-17 02:37:26 -07:00 committed by Arvid Norberg
parent ea8c5b32da
commit 06070ea499
4 changed files with 19 additions and 10 deletions

View File

@ -240,6 +240,9 @@ namespace aux {
http_seed
};
struct request_flags_tag;
using request_flags_t = flags::bitfield_flag<std::uint8_t, request_flags_tag>;
class TORRENT_EXTRA_EXPORT peer_connection
: public peer_connection_hot_members
, public bandwidth_socket
@ -584,10 +587,12 @@ namespace aux {
// if the block was already time-critical, it returns false.
bool make_time_critical(piece_block const& block);
static constexpr request_flags_t time_critical{1};
static constexpr request_flags_t busy{2};
// adds a block to the request queue
// returns true if successful, false otherwise
enum flags_t { req_time_critical = 1, req_busy = 2 };
bool add_request(piece_block const& b, int flags = 0);
bool add_request(piece_block const& b, request_flags_t flags = {});
// clears the request queue and sends cancels for all messages
// in the download queue

View File

@ -86,6 +86,9 @@ using namespace std::placeholders;
namespace libtorrent {
constexpr request_flags_t peer_connection::time_critical;
constexpr request_flags_t peer_connection::busy;
namespace {
// the limits of the download queue size
@ -3405,7 +3408,8 @@ namespace libtorrent {
return true;
}
bool peer_connection::add_request(piece_block const& block, int const flags)
bool peer_connection::add_request(piece_block const& block
, request_flags_t const flags)
{
TORRENT_ASSERT(is_single_thread());
INVARIANT_CHECK;
@ -3446,7 +3450,7 @@ namespace libtorrent {
return false;
}
if ((flags & req_busy) && !(flags & req_time_critical))
if ((flags & busy) && !(flags & time_critical))
{
// this block is busy (i.e. it has been requested
// from another peer already). Only allow one busy
@ -3495,8 +3499,8 @@ namespace libtorrent {
}
pending_block pb(block);
pb.busy = (flags & req_busy) ? true : false;
if (flags & req_time_critical)
pb.busy = (flags & busy) ? true : false;
if (flags & time_critical)
{
m_request_queue.insert(m_request_queue.begin() + m_queued_time_critical
, pb);

View File

@ -252,7 +252,7 @@ namespace libtorrent {
// ok, we found a piece that's not being downloaded
// by somebody else. request it from this peer
// and return
if (!c.add_request(*i, 0)) continue;
if (!c.add_request(*i, {})) continue;
TORRENT_ASSERT(p.num_peers(*i) == 1);
TORRENT_ASSERT(p.is_requested(*i));
num_requests--;
@ -298,7 +298,7 @@ namespace libtorrent {
TORRENT_ASSERT(!p.is_finished(busy_block));
TORRENT_ASSERT(p.num_peers(busy_block) > 0);
c.add_request(busy_block, peer_connection::req_busy);
c.add_request(busy_block, peer_connection::busy);
return true;
}

View File

@ -9643,8 +9643,8 @@ namespace libtorrent {
}
else
{
if (!c.add_request(b, peer_connection::req_time_critical
| (busy_mode ? peer_connection::req_busy : 0)))
if (!c.add_request(b, peer_connection::time_critical
| (busy_mode ? peer_connection::busy : request_flags_t{})))
{
#if TORRENT_DEBUG_STREAMING > 1
std::printf("failed to request block [%d, %d]\n"