diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index a65d5624cca..9615eb7cbfd 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -13,6 +13,7 @@ Copyright (c) 2019, ghbplayer Copyright (c) 2020, AllSeeingEyeTolledEweSew Copyright (c) 2020, Fonic Copyright (c) 2020, Viktor Elofsson +Copyright (c) 2023, Joris Carrier All rights reserved. Redistribution and use in source and binary forms, with or without @@ -1891,11 +1892,16 @@ TORRENT_VERSION_NAMESPACE_3 struct TORRENT_EXPORT cache_flushed_alert final : torrent_alert { // internal - TORRENT_UNEXPORT cache_flushed_alert(aux::stack_allocator& alloc, torrent_handle const& h); + TORRENT_UNEXPORT cache_flushed_alert(aux::stack_allocator& alloc, + torrent_handle const& h, client_data_t userdata = {}); TORRENT_DEFINE_ALERT_PRIO(cache_flushed_alert, 58, alert_priority::high) static constexpr alert_category_t static_category = alert_category::storage; + + // '`userdata`` as set in torrent_handle::flush_cache on call. + // This can be used to associate the call with related data. + client_data_t userdata; }; #if TORRENT_ABI_VERSION == 1 diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index ecca52f34ea..d8f4d9f50c4 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 @@ -563,7 +564,7 @@ namespace libtorrent { bool has_error() const { return !!m_error; } error_code error() const { return m_error; } - void flush_cache(); + void flush_cache(client_data_t userdata = {}); void pause(pause_flags_t flags = {}); void resume(); @@ -1307,7 +1308,7 @@ namespace libtorrent { void on_file_renamed(std::string const& filename , file_index_t file_idx , storage_error const& error); - void on_cache_flushed(bool manually_triggered); + void on_cache_flushed(bool manually_triggered, client_data_t userdata = {}); // this is used when a torrent is being removed.It synchronizes with the // disk thread diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 6ffd49fb9f0..ad6f2ef0f9f 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 @@ -652,13 +653,15 @@ namespace aux { // Instructs libtorrent to flush all the disk caches for this torrent and // close all file handles. This is done asynchronously and you will be - // notified that it's complete through cache_flushed_alert. + // notified that it's complete through cache_flushed_alert + // with custom data (client_data_t) to be associated with this operation. // // Note that by the time you get the alert, libtorrent may have cached // more data for the torrent, but you are guaranteed that whatever cached // data libtorrent had by the time you called - // ``torrent_handle::flush_cache()`` has been written to disk. - void flush_cache() const; + // ``torrent_handle::flush_cache(client_data_t)`` with the provided client_data_t + // has been written to disk. + void flush_cache(client_data_t userdata = {}) const; // ``force_recheck`` puts the torrent back in a state where it assumes to // have no resume data. All peers will be disconnected and the torrent diff --git a/src/alert.cpp b/src/alert.cpp index e0853ea9b3a..c18a0af97b2 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -1561,8 +1561,9 @@ namespace { #endif // TORRENT_ABI_VERSION cache_flushed_alert::cache_flushed_alert(aux::stack_allocator& alloc - , torrent_handle const& h) - : torrent_alert(alloc, h) {} + , torrent_handle const& h, client_data_t u) + : torrent_alert(alloc, h) + , userdata(u) {} #if TORRENT_ABI_VERSION == 1 anonymous_mode_alert::anonymous_mode_alert(aux::stack_allocator& alloc diff --git a/src/torrent.cpp b/src/torrent.cpp index 43e2cddea18..ed10c0939c2 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. @@ -8397,7 +8397,7 @@ namespace { { // we need to keep the object alive during this operation m_ses.disk_thread().async_release_files(m_storage - , std::bind(&torrent::on_cache_flushed, shared_from_this(), false)); + , std::bind(&torrent::on_cache_flushed, shared_from_this(), false, client_data_t())); m_ses.deferred_submit_jobs(); } @@ -9462,7 +9462,7 @@ namespace { && !m_session_paused; } - void torrent::flush_cache() + void torrent::flush_cache(client_data_t userdata) { TORRENT_ASSERT(is_single_thread()); @@ -9473,18 +9473,18 @@ namespace { return; } m_ses.disk_thread().async_release_files(m_storage - , std::bind(&torrent::on_cache_flushed, shared_from_this(), true)); + , std::bind(&torrent::on_cache_flushed, shared_from_this(), true, userdata)); m_ses.deferred_submit_jobs(); } - void torrent::on_cache_flushed(bool const manually_triggered) try + void torrent::on_cache_flushed(bool const manually_triggered, client_data_t userdata) try { TORRENT_ASSERT(is_single_thread()); if (m_ses.is_aborted()) return; if (manually_triggered || alerts().should_post()) - alerts().emplace_alert(get_handle()); + alerts().emplace_alert(get_handle(), userdata); } catch (...) { handle_exception(); } diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index c255e7a7282..ac9e464e4e2 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 @@ -356,9 +357,9 @@ namespace libtorrent { { async_call(&torrent::set_sequential_download, sd); } #endif - void torrent_handle::flush_cache() const + void torrent_handle::flush_cache(client_data_t userdata) const { - async_call(&torrent::flush_cache); + async_call(&torrent::flush_cache, userdata); } void torrent_handle::set_ssl_certificate(