From ddbf7de428782d0e6449f46f13b7ad417a816805 Mon Sep 17 00:00:00 2001 From: Joris CARRIER Date: Fri, 17 Nov 2023 11:05:25 +0100 Subject: [PATCH] Implemented read piece callbacks in torrent handling. --- include/libtorrent/alert_types.hpp | 6 +++--- include/libtorrent/torrent.hpp | 4 +++- include/libtorrent/torrent_handle.hpp | 4 +++- src/alert.cpp | 8 ++++++-- src/torrent.cpp | 15 ++++++++------- src/torrent_handle.cpp | 5 +++-- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index a65d5624cca..ae1fa5d1b17 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -306,13 +306,13 @@ TORRENT_VERSION_NAMESPACE_3 // number of bytes that was read. // // If the operation fails, ``error`` will indicate what went wrong. - struct TORRENT_EXPORT read_piece_alert final : torrent_alert + struct TORRENT_EXPORT read_piece_alert final : torrent_alert, callback_t { // internal TORRENT_UNEXPORT read_piece_alert(aux::stack_allocator& alloc, torrent_handle const& h - , piece_index_t p, boost::shared_array d, int s); + , piece_index_t p, boost::shared_array d, int s, callback_t::type callback = {}); TORRENT_UNEXPORT read_piece_alert(aux::stack_allocator& alloc, torrent_handle h - , piece_index_t p, error_code e); + , piece_index_t p, error_code e, callback_t::type callback = {}); TORRENT_DEFINE_ALERT_PRIO(read_piece_alert, 5, alert_priority::critical) diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index ecca52f34ea..cc523de0fd1 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -12,6 +12,7 @@ Copyright (c) 2018, d-komarov Copyright (c) 2019, ghbplayer Copyright (c) 2020, Paul-Louis Ageneau Copyright (c) 2021, AdvenT +Copyright (c) 2023, Joris Carrier All rights reserved. Redistribution and use in source and binary forms, with or without @@ -485,8 +486,9 @@ namespace libtorrent { int blocks_left; bool fail; error_code error; + callback_t::type callback; }; - void read_piece(piece_index_t); + void read_piece(piece_index_t, callback_t::type callback = {}); void on_disk_read_complete(disk_buffer_holder, storage_error const& , peer_request const&, std::shared_ptr); diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 6ffd49fb9f0..44240e85f28 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -9,6 +9,7 @@ Copyright (c) 2017, 2020, AllSeeingEyeTolledEweSew Copyright (c) 2017, Falcosc Copyright (c) 2019, Andrei Kurushin Copyright (c) 2019, ghbplayer +Copyright (c) 2023, Joris Carrier All rights reserved. Redistribution and use in source and binary forms, with or without @@ -69,6 +70,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pex_flags.hpp" #include "libtorrent/client_data.hpp" #include "libtorrent/address.hpp" // for address_v4 and address_v6 +#include "libtorrent/alert.hpp" // for callback_t namespace libtorrent { namespace aux { @@ -309,7 +311,7 @@ namespace aux { // // Note that if you read multiple pieces, the read operations are not // guaranteed to finish in the same order as you initiated them. - void read_piece(piece_index_t piece) const; + void read_piece(piece_index_t piece, callback_t::type callback = {}) const; // Returns true if this piece has been completely downloaded and written // to disk, and false otherwise. diff --git a/src/alert.cpp b/src/alert.cpp index e0853ea9b3a..ae221bb0cd0 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -200,16 +200,20 @@ namespace libtorrent { read_piece_alert::read_piece_alert(aux::stack_allocator& alloc , torrent_handle const& h - , piece_index_t p, boost::shared_array d, int s) + , piece_index_t p, boost::shared_array d, int s + , callback_t::type c) : torrent_alert(alloc, h) + , callback_t(std::move(c)) , buffer(std::move(d)) , piece(p) , size(s) {} read_piece_alert::read_piece_alert(aux::stack_allocator& alloc - , torrent_handle h, piece_index_t p, error_code e) + , torrent_handle h, piece_index_t p, error_code e + , callback_t::type c) : torrent_alert(alloc, h) + , callback_t(std::move(c)) , error(e) , piece(p) , size(0) diff --git a/src/torrent.cpp b/src/torrent.cpp index 43e2cddea18..ddaa533abac 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -18,7 +18,7 @@ Copyright (c) 2018, airium Copyright (c) 2018, d-komarov Copyright (c) 2020, Paul-Louis Ageneau Copyright (c) 2021, AdvenT -Copyright (c) 2021, Joris CARRIER +Copyright (c) 2021, 2023 Joris CARRIER Copyright (c) 2021, thrnz All rights reserved. @@ -743,7 +743,7 @@ bool is_downloading_state(int const st) m_ses.close_connection(p); } - void torrent::read_piece(piece_index_t const piece) + void torrent::read_piece(piece_index_t const piece, callback_t::type callback) { error_code ec; if (m_abort || m_deleted) @@ -761,7 +761,7 @@ bool is_downloading_state(int const st) if (ec) { - m_ses.alerts().emplace_alert(get_handle(), piece, ec); + m_ses.alerts().emplace_alert(get_handle(), piece, ec, callback); return; } @@ -776,7 +776,7 @@ bool is_downloading_state(int const st) // this shouldn't actually happen boost::shared_array buf; m_ses.alerts().emplace_alert( - get_handle(), piece, buf, 0); + get_handle(), piece, buf, 0, callback); return; } @@ -785,11 +785,12 @@ bool is_downloading_state(int const st) if (!rp->piece_data) { m_ses.alerts().emplace_alert( - get_handle(), piece, error_code(boost::system::errc::not_enough_memory, generic_category())); + get_handle(), piece, error_code(boost::system::errc::not_enough_memory, generic_category()), callback); return; } rp->blocks_left = blocks_in_piece; rp->fail = false; + rp->callback = callback; disk_job_flags_t flags{}; auto const read_mode = settings().get_int(settings_pack::disk_io_read_mode); @@ -1216,12 +1217,12 @@ bool is_downloading_state(int const st) if (rp->fail) { m_ses.alerts().emplace_alert( - get_handle(), r.piece, rp->error); + get_handle(), r.piece, rp->error, rp->callback); } else { m_ses.alerts().emplace_alert( - get_handle(), r.piece, rp->piece_data, size); + get_handle(), r.piece, rp->piece_data, size, rp->callback); } } } diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index c255e7a7282..c6936ce67ce 100644 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -8,6 +8,7 @@ Copyright (c) 2017, Falcosc Copyright (c) 2018, Steven Siloti Copyright (c) 2019, Andrei Kurushin Copyright (c) 2019, ghbplayer +Copyright (c) 2023, Joris Carrier All rights reserved. Redistribution and use in source and binary forms, with or without @@ -742,9 +743,9 @@ namespace libtorrent { async_call(&torrent::add_piece_async, piece, std::move(data), flags); } - void torrent_handle::read_piece(piece_index_t piece) const + void torrent_handle::read_piece(piece_index_t piece, callback_t::type callback) const { - async_call(&torrent::read_piece, piece); + async_call(&torrent::read_piece, piece, callback); } bool torrent_handle::have_piece(piece_index_t piece) const