From 69bd2986de2c438f289338a16693f2beb48d3f98 Mon Sep 17 00:00:00 2001 From: arvidn Date: Thu, 27 Jul 2017 01:37:32 -0700 Subject: [PATCH] convert a few more torrent_handle flags to type safe types --- bindings/python/src/converters.cpp | 6 +++ bindings/python/src/torrent_handle.cpp | 30 +++++++++------ include/libtorrent/torrent.hpp | 7 ++-- include/libtorrent/torrent_handle.hpp | 53 +++++++++++++++----------- simulation/test_swarm.cpp | 2 +- src/torrent.cpp | 10 +++-- src/torrent_handle.cpp | 10 +++-- 7 files changed, 73 insertions(+), 45 deletions(-) diff --git a/bindings/python/src/converters.cpp b/bindings/python/src/converters.cpp index 05a034991..b1bf1c592 100644 --- a/bindings/python/src/converters.cpp +++ b/bindings/python/src/converters.cpp @@ -301,6 +301,9 @@ void bind_converters() to_python_converter>(); to_python_converter>(); to_python_converter>(); + to_python_converter>(); + to_python_converter>(); + to_python_converter>(); // work-around types to_python_converter, address_to_tuple< @@ -352,4 +355,7 @@ void bind_converters() to_bitfield_flag(); to_bitfield_flag(); to_bitfield_flag(); + to_bitfield_flag(); + to_bitfield_flag(); + to_bitfield_flag(); } diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index 78b671d56..95fa36c2b 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -422,7 +422,8 @@ std::shared_ptr 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; void bind_torrent_handle() @@ -466,7 +470,7 @@ void bind_torrent_handle() ; #ifndef TORRENT_NO_DEPRECATE - enum_("deprecated_move_flags_t") + enum_("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_("add_piece_flags_t") - .value("overwrite_existing", torrent_handle::overwrite_existing) - ; - enum_("pause_flags_t") - .value("graceful_pause", torrent_handle::graceful_pause) - ; + { + scope s = class_("add_piece_flags_t"); + s.attr("overwrite_existing") = torrent_handle::overwrite_existing; + } + + { + scope s = class_("pause_flags_t"); + s.attr("graceful_pause") = torrent_handle::graceful_pause; + } { scope s = class_("save_resume_flags_t"); @@ -612,9 +619,10 @@ void bind_torrent_handle() s.attr("only_if_modified") = torrent_handle::only_if_modified; } - enum_("deadline_flags") - .value("alert_when_available", torrent_handle::alert_when_available) - ; + { + scope s = class_("deadline_flags_t"); + s.attr("alert_when_available") = torrent_handle::alert_when_available; + } { scope s = class_("status_flags_t"); diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 3f4b7ea2d..53d0f7616 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -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*) 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(); diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 6c2f93d4c..c945fe209 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -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; + + // hidden + struct pause_flags_tag; + using pause_flags_t = flags::bitfield_flag; + + // hidden + struct deadline_flags_tag; + using deadline_flags_t = flags::bitfield_flag; + + // hidden struct resume_data_flags_tag; using resume_data_flags_t = flags::bitfield_flag; @@ -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& 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. diff --git a/simulation/test_swarm.cpp b/simulation/test_swarm.cpp index 8d280c1a3..aae1ec633 100644 --- a/simulation/test_swarm.cpp +++ b/simulation/test_swarm.cpp @@ -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; } } diff --git a/src/torrent.cpp b/src/torrent.cpp index beede48a2..63f5d411a 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -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; diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index a4534b464..f029acdba 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -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(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); }