convert a few more torrent_handle flags to type safe types

This commit is contained in:
arvidn 2017-07-27 01:37:32 -07:00 committed by Arvid Norberg
parent 204a029b5f
commit 69bd2986de
7 changed files with 73 additions and 45 deletions

View File

@ -301,6 +301,9 @@ void bind_converters()
to_python_converter<lt::status_flags_t, from_bitfield_flag<lt::status_flags_t>>();
to_python_converter<lt::alert_category_t, from_bitfield_flag<lt::alert_category_t>>();
to_python_converter<lt::resume_data_flags_t, from_bitfield_flag<lt::resume_data_flags_t>>();
to_python_converter<lt::add_piece_flags_t, from_bitfield_flag<lt::add_piece_flags_t>>();
to_python_converter<lt::pause_flags_t, from_bitfield_flag<lt::pause_flags_t>>();
to_python_converter<lt::deadline_flags_t, from_bitfield_flag<lt::deadline_flags_t>>();
// work-around types
to_python_converter<lt::aux::noexcept_movable<lt::address>, address_to_tuple<
@ -352,4 +355,7 @@ void bind_converters()
to_bitfield_flag<lt::status_flags_t>();
to_bitfield_flag<lt::alert_category_t>();
to_bitfield_flag<lt::resume_data_flags_t>();
to_bitfield_flag<lt::add_piece_flags_t>();
to_bitfield_flag<lt::pause_flags_t>();
to_bitfield_flag<lt::deadline_flags_t>();
}

View File

@ -422,7 +422,8 @@ std::shared_ptr<torrent_info> get_torrent_info(torrent_handle const& h)
#endif // TORRENT_NO_DEPRECAE
void add_piece(torrent_handle& th, piece_index_t piece, char const *data, int flags)
void add_piece(torrent_handle& th, piece_index_t piece, char const *data
, add_piece_flags_t const flags)
{
th.add_piece(piece, data, flags);
}
@ -430,6 +431,9 @@ void add_piece(torrent_handle& th, piece_index_t piece, char const *data, int fl
class dummy5 {};
class dummy {};
class dummy4 {};
class dummy6 {};
class dummy7 {};
class dummy8 {};
using by_value = return_value_policy<return_by_value>;
void bind_torrent_handle()
@ -466,7 +470,7 @@ void bind_torrent_handle()
;
#ifndef TORRENT_NO_DEPRECATE
enum_<deprecated_move_flags_t>("deprecated_move_flags_t")
enum_<deprecated_move_flags_t>("deprecated_move_flags_t")
.value("always_replace_files", deprecated_move_flags_t::always_replace_files)
.value("fail_if_exist", deprecated_move_flags_t::fail_if_exist)
.value("dont_replace", deprecated_move_flags_t::dont_replace)
@ -598,12 +602,15 @@ void bind_torrent_handle()
.value("piece_granularity", torrent_handle::piece_granularity)
;
enum_<torrent_handle::flags_t>("add_piece_flags_t")
.value("overwrite_existing", torrent_handle::overwrite_existing)
;
enum_<torrent_handle::pause_flags_t>("pause_flags_t")
.value("graceful_pause", torrent_handle::graceful_pause)
;
{
scope s = class_<dummy6>("add_piece_flags_t");
s.attr("overwrite_existing") = torrent_handle::overwrite_existing;
}
{
scope s = class_<dummy7>("pause_flags_t");
s.attr("graceful_pause") = torrent_handle::graceful_pause;
}
{
scope s = class_<dummy4>("save_resume_flags_t");
@ -612,9 +619,10 @@ void bind_torrent_handle()
s.attr("only_if_modified") = torrent_handle::only_if_modified;
}
enum_<torrent_handle::deadline_flags>("deadline_flags")
.value("alert_when_available", torrent_handle::alert_when_available)
;
{
scope s = class_<dummy8>("deadline_flags_t");
s.attr("alert_when_available") = torrent_handle::alert_when_available;
}
{
scope s = class_<dummy5>("status_flags_t");

View File

@ -114,7 +114,7 @@ namespace libtorrent {
// by what time we want this piece
time_point deadline;
// 1 = send alert with piece data when available
int flags;
deadline_flags_t flags;
// how many peers it's been requested from
int peers;
// the piece index
@ -411,8 +411,7 @@ namespace libtorrent {
int seed_rank(aux::session_settings const& s) const;
enum flags_t { overwrite_existing = 1 };
void add_piece(piece_index_t piece, char const* data, int flags = 0);
void add_piece(piece_index_t piece, char const* data, add_piece_flags_t flags);
void on_disk_write_complete(storage_error const& error
, peer_request const& p);
@ -569,7 +568,7 @@ namespace libtorrent {
void file_priorities(aux::vector<int, file_index_t>*) const;
void cancel_non_critical();
void set_piece_deadline(piece_index_t piece, int t, int flags);
void set_piece_deadline(piece_index_t piece, int t, deadline_flags_t flags);
void reset_piece_deadline(piece_index_t piece);
void clear_time_critical();
void update_piece_priorities();

View File

@ -85,6 +85,19 @@ namespace aux {
void TORRENT_NO_RETURN throw_invalid_handle();
#endif
// hidden
struct add_piece_flags_tag;
using add_piece_flags_t = flags::bitfield_flag<std::uint8_t, add_piece_flags_tag>;
// hidden
struct pause_flags_tag;
using pause_flags_t = flags::bitfield_flag<std::uint8_t, pause_flags_tag>;
// hidden
struct deadline_flags_tag;
using deadline_flags_t = flags::bitfield_flag<std::uint8_t, deadline_flags_tag>;
// hidden
struct resume_data_flags_tag;
using resume_data_flags_t = flags::bitfield_flag<std::uint8_t, resume_data_flags_tag>;
@ -269,8 +282,9 @@ namespace aux {
torrent_handle& operator=(torrent_handle const&) = default;
torrent_handle& operator=(torrent_handle&&) noexcept = default;
// flags for add_piece().
enum flags_t { overwrite_existing = 1 };
// instruct libtorrent to overwrite any data that may already have been
// downloaded with the data of the new piece being added.
static constexpr add_piece_flags_t overwrite_existing{1};
// This function will write ``data`` to the storage as piece ``piece``,
// as if it had been downloaded from a peer. ``data`` is expected to
@ -287,7 +301,7 @@ namespace aux {
// Since the data is written asynchronously, you may know that is passed
// or failed the hash check by waiting for piece_finished_alert or
// hash_failed_alert.
void add_piece(piece_index_t piece, char const* data, int flags = 0) const;
void add_piece(piece_index_t piece, char const* data, add_piece_flags_t flags = {}) const;
// This function starts an asynchronous read operation of the specified
// piece from this torrent. You must have completed the download of the
@ -361,8 +375,11 @@ namespace aux {
// partial_piece_info for the fields in the returned vector.
void get_download_queue(std::vector<partial_piece_info>& queue) const;
// flags for set_piece_deadline().
enum deadline_flags { alert_when_available = 1 };
// used to ask libtorrent to send an alert once the piece has been
// downloaded, by passing alert_when_available. When set, the
// read_piece_alert alert will be delivered, with the piece data, when
// it's downloaded.
static constexpr deadline_flags_t alert_when_available{1};
// This function sets or resets the deadline associated with a specific
// piece index (``index``). libtorrent will attempt to download this
@ -372,11 +389,6 @@ namespace aux {
// deadline (and flags) of a piece can be changed by calling this
// function again.
//
// The ``flags`` parameter can be used to ask libtorrent to send an alert
// once the piece has been downloaded, by passing alert_when_available.
// When set, the read_piece_alert alert will be delivered, with the piece
// data, when it's downloaded.
//
// If the piece is already downloaded when this call is made, nothing
// happens, unless the alert_when_available flag is set, in which case it
// will have the same effect as calling read_piece() for ``index``.
@ -390,7 +402,7 @@ namespace aux {
//
// ``clear_piece_deadlines()`` removes deadlines on all pieces in
// the torrent. As if reset_piece_deadline() was called on all pieces.
void set_piece_deadline(piece_index_t index, int deadline, int flags = 0) const;
void set_piece_deadline(piece_index_t index, int deadline, deadline_flags_t flags = {}) const;
void reset_piece_deadline(piece_index_t index) const;
void clear_piece_deadlines() const;
@ -552,8 +564,13 @@ namespace aux {
// torrent will become invalid.
bool is_valid() const;
// flags for torrent_session::pause()
enum pause_flags_t { graceful_pause = 1 };
// will delay the disconnect of peers that we're still downloading
// outstanding requests from. The torrent will not accept any more
// requests and will disconnect all idle peers. As soon as a peer is done
// transferring the blocks that were requested from it, it is
// disconnected. This is a graceful shut down of the torrent in the sense
// that no downloaded bytes are wasted.
static constexpr pause_flags_t graceful_pause{1};
// ``pause()``, and ``resume()`` will disconnect all peers and reconnect
// all peers respectively. When a torrent is paused, it will however
@ -565,21 +582,13 @@ namespace aux {
// To know if a torrent is paused or not, call
// ``torrent_handle::status()`` and inspect ``torrent_status::paused``.
//
// The ``flags`` argument to pause can be set to
// ``torrent_handle::graceful_pause`` which will delay the disconnect of
// peers that we're still downloading outstanding requests from. The
// torrent will not accept any more requests and will disconnect all idle
// peers. As soon as a peer is done transferring the blocks that were
// requested from it, it is disconnected. This is a graceful shut down of
// the torrent in the sense that no downloaded bytes are wasted.
//
// .. note::
// Torrents that are auto-managed may be automatically resumed again. It
// does not make sense to pause an auto-managed torrent without making it
// not auto-managed first. Torrents are auto-managed by default when added
// to the session. For more information, see queuing_.
//
void pause(int flags = 0) const;
void pause(pause_flags_t flags = {}) const;
void resume() const;
// sets and gets the torrent state flags. See torrent_flags_t.

View File

@ -270,7 +270,7 @@ void test_stop_start_download(swarm_test type, bool graceful)
{
std::printf("\nSTOP\n\n");
auto h = ses.get_torrents()[0];
h.pause(graceful ? torrent_handle::graceful_pause : 0);
h.pause(graceful ? torrent_handle::graceful_pause : pause_flags_t{});
paused_once = true;
}
}

View File

@ -1276,7 +1276,8 @@ namespace libtorrent {
// TODO: 3 there's some duplication between this function and
// peer_connection::incoming_piece(). is there a way to merge something?
void torrent::add_piece(piece_index_t const piece, char const* data, int const flags)
void torrent::add_piece(piece_index_t const piece, char const* data
, add_piece_flags_t const flags)
{
TORRENT_ASSERT(is_single_thread());
int piece_size = m_torrent_file->piece_size(piece);
@ -1290,7 +1291,7 @@ namespace libtorrent {
need_picker();
if (picker().have_piece(piece)
&& (flags & torrent::overwrite_existing) == 0)
&& !(flags & torrent_handle::overwrite_existing))
return;
peer_request p;
@ -1300,7 +1301,7 @@ namespace libtorrent {
for (int i = 0; i < blocks_in_piece; ++i, p.start += block_size())
{
if (picker().is_finished(piece_block(piece, i))
&& (flags & torrent::overwrite_existing) == 0)
&& !(flags & torrent_handle::overwrite_existing))
continue;
p.length = std::min(piece_size - p.start, int(block_size()));
@ -4610,7 +4611,8 @@ namespace libtorrent {
}
}
void torrent::set_piece_deadline(piece_index_t const piece, int const t, int const flags)
void torrent::set_piece_deadline(piece_index_t const piece, int const t
, deadline_flags_t const flags)
{
INVARIANT_CHECK;

View File

@ -61,6 +61,9 @@ namespace libtorrent {
constexpr resume_data_flags_t torrent_handle::flush_disk_cache;
constexpr resume_data_flags_t torrent_handle::save_info_dict;
constexpr resume_data_flags_t torrent_handle::only_if_modified;
constexpr add_piece_flags_t torrent_handle::overwrite_existing;
constexpr pause_flags_t torrent_handle::graceful_pause;
constexpr deadline_flags_t torrent_handle::alert_when_available;
constexpr status_flags_t torrent_handle::query_distributed_copies;
constexpr status_flags_t torrent_handle::query_accurate_download_counters;
@ -274,7 +277,7 @@ namespace libtorrent {
return sync_call_ret<bool>(false, &torrent::set_metadata, metadata);
}
void torrent_handle::pause(int flags) const
void torrent_handle::pause(pause_flags_t const flags) const
{
async_call(&torrent::pause, bool(flags & graceful_pause));
}
@ -620,7 +623,7 @@ namespace libtorrent {
async_call(&torrent::add_tracker, url);
}
void torrent_handle::add_piece(piece_index_t piece, char const* data, int flags) const
void torrent_handle::add_piece(piece_index_t piece, char const* data, add_piece_flags_t const flags) const
{
sync_call(&torrent::add_piece, piece, data, flags);
}
@ -764,7 +767,8 @@ namespace libtorrent {
sync_call(&torrent::get_download_queue, queuep);
}
void torrent_handle::set_piece_deadline(piece_index_t index, int deadline, int flags) const
void torrent_handle::set_piece_deadline(piece_index_t index, int deadline
, deadline_flags_t const flags) const
{
async_call(&torrent::set_piece_deadline, index, deadline, flags);
}