From 8b2b9816c9d869c2d78a41350651bcd3dec916c7 Mon Sep 17 00:00:00 2001 From: fake Date: Wed, 13 May 2020 22:26:02 -0700 Subject: [PATCH] add premiere mode --- bindings/python/src/torrent_handle.cpp | 2 ++ include/libtorrent/torrent.hpp | 10 ++++++++++ include/libtorrent/torrent_handle.hpp | 3 +++ src/bt_peer_connection.cpp | 3 ++- src/peer_connection.cpp | 8 +++++++- src/torrent.cpp | 18 ++++++++++++++++++ src/torrent_handle.cpp | 10 ++++++++++ 7 files changed, 52 insertions(+), 2 deletions(-) diff --git a/bindings/python/src/torrent_handle.cpp b/bindings/python/src/torrent_handle.cpp index 3fb4fe5dc..d951f4f69 100644 --- a/bindings/python/src/torrent_handle.cpp +++ b/bindings/python/src/torrent_handle.cpp @@ -512,6 +512,8 @@ void bind_torrent_handle() .def("piece_priority", _(piece_priority1)) .def("prioritize_pieces", &prioritize_pieces) .def("get_piece_priorities", &piece_priorities) + .def("is_piece_premiere", _(&torrent_handle::is_piece_premiere)) + .def("set_piece_premiere", _(&torrent_handle::set_piece_premiere)) .def("prioritize_files", &prioritize_files) .def("get_file_priorities", &file_priorities) .def("file_priority", &file_prioritity0) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 11e946cbf..7eecef371 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -813,6 +813,16 @@ namespace libtorrent { return m_picker->have_piece(index); } + bool premiere_mode:1; + std::unordered_map premiere_map; + + void set_piece_premiere(piece_index_t const index, bool prem); + + bool is_piece_premiere(piece_index_t const index) + { + return premiere_map[index]; + } + // returns true if we have downloaded the given piece bool has_piece_passed(piece_index_t index) const { diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index a71705f34..899d424bc 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -986,6 +986,9 @@ namespace aux { void prioritize_pieces(std::vector> const& pieces) const; std::vector get_piece_priorities() const; + void set_piece_premiere(piece_index_t index, bool prem) const; + bool is_piece_premiere(piece_index_t index) const; + #if TORRENT_ABI_VERSION == 1 TORRENT_DEPRECATED void prioritize_pieces(std::vector const& pieces) const; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 927594a89..ba46a8ed4 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -1986,7 +1986,7 @@ namespace { TORRENT_ASSERT(t->valid_metadata()); #ifndef TORRENT_DISABLE_SUPERSEEDING - if (t->super_seeding()) + if (t->is_seed() && (t->super_seeding() || t->premiere_mode)) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "BITFIELD", "not sending bitfield, super seeding"); @@ -2158,6 +2158,7 @@ namespace { // upload-only. If we do, we may be disconnected before we receive the // metadata. if (t->is_upload_only() + && !t->premiere_mode #ifndef TORRENT_DISABLE_SHARE_MODE && !t->share_mode() #endif diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 07779ab7c..3d528c932 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -635,7 +635,7 @@ namespace libtorrent { } #ifndef TORRENT_DISABLE_SUPERSEEDING - if (t->super_seeding()) + if (t->premiere_mode || t->super_seeding()) { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "ALLOWED", "skipping allowed set because of super seeding"); @@ -2331,6 +2331,12 @@ namespace libtorrent { , "piece: %d s: %x l: %x", static_cast(r.piece), r.start, r.length); #endif + if (t->is_piece_premiere(r.piece)) + { + write_reject_request(r); + return; + } + #ifndef TORRENT_DISABLE_SUPERSEEDING if (t->super_seeding() && !super_seeded_piece(r.piece)) diff --git a/src/torrent.cpp b/src/torrent.cpp index bdae6ab87..8f5a91558 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -2307,6 +2307,24 @@ bool is_downloading_state(int const st) } catch (...) { handle_exception(); } + void torrent::set_piece_premiere(piece_index_t const index, bool prem) + { + premiere_map[index] = prem; + if (prem == false) + { + for (auto c : m_connections) + { + auto p = c->self(); + p->announce_piece(index); + p->send_suggest(index); + } + } + else + { + premiere_mode = true; + } + } + void torrent::force_recheck() { INVARIANT_CHECK; diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index dfb166478..b6101b682 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -448,6 +448,16 @@ namespace libtorrent { return st; } + void torrent_handle::set_piece_premiere(piece_index_t const index, bool prem) const + { + async_call(&torrent::set_piece_premiere, index, prem); + } + + bool torrent_handle::is_piece_premiere(piece_index_t const index) const + { + return sync_call_ret(false, &torrent::is_piece_premiere, index); + } + void torrent_handle::piece_availability(std::vector& avail) const { auto availr = std::ref(static_cast&>(avail));