Skip to content

Commit

Permalink
Implemented read piece callbacks in torrent handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
joriscarrier committed Nov 21, 2023
1 parent 3e1d951 commit 63ed794
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
6 changes: 3 additions & 3 deletions include/libtorrent/alert_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<read_piece_alert>
{
// internal
TORRENT_UNEXPORT read_piece_alert(aux::stack_allocator& alloc, torrent_handle const& h
, piece_index_t p, boost::shared_array<char> d, int s);
, piece_index_t p, boost::shared_array<char> d, int s, callback_t<read_piece_alert>::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<read_piece_alert>::type callback = {});

TORRENT_DEFINE_ALERT_PRIO(read_piece_alert, 5, alert_priority::critical)

Expand Down
4 changes: 3 additions & 1 deletion include/libtorrent/torrent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -485,8 +486,9 @@ namespace libtorrent {
int blocks_left;
bool fail;
error_code error;
callback_t<read_piece_alert>::type callback;
};
void read_piece(piece_index_t);
void read_piece(piece_index_t, callback_t<read_piece_alert>::type callback = {});
void on_disk_read_complete(disk_buffer_holder, storage_error const&
, peer_request const&, std::shared_ptr<read_piece_struct>);

Expand Down
6 changes: 5 additions & 1 deletion include/libtorrent/torrent_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -85,6 +87,8 @@ namespace aux {
[[noreturn]] void throw_invalid_handle();
#endif

struct read_piece_alert;

using status_flags_t = flags::bitfield_flag<std::uint32_t, struct status_flags_tag>;
using add_piece_flags_t = flags::bitfield_flag<std::uint8_t, struct add_piece_flags_tag>;
using pause_flags_t = flags::bitfield_flag<std::uint8_t, struct pause_flags_tag>;
Expand Down Expand Up @@ -309,7 +313,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<read_piece_alert>::type callback = {}) const;

// Returns true if this piece has been completely downloaded and written
// to disk, and false otherwise.
Expand Down
8 changes: 6 additions & 2 deletions src/alert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,23 @@ namespace libtorrent {

read_piece_alert::read_piece_alert(aux::stack_allocator& alloc
, torrent_handle const& h
, piece_index_t p, boost::shared_array<char> d, int s)
, piece_index_t p, boost::shared_array<char> d, int s
, callback_t<read_piece_alert>::type c)
: torrent_alert(alloc, h)
, buffer(std::move(d))
, piece(p)
, size(s)
, callback_t<read_piece_alert>(std::move(c))
{}

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<read_piece_alert>::type c)
: torrent_alert(alloc, h)
, error(e)
, piece(p)
, size(0)
, callback_t<read_piece_alert>(std::move(c))
#if TORRENT_ABI_VERSION == 1
, ec(e)
#endif
Expand Down
15 changes: 8 additions & 7 deletions src/torrent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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<read_piece_alert>::type callback)
{
error_code ec;
if (m_abort || m_deleted)
Expand All @@ -761,7 +761,7 @@ bool is_downloading_state(int const st)

if (ec)
{
m_ses.alerts().emplace_alert<read_piece_alert>(get_handle(), piece, ec);
m_ses.alerts().emplace_alert<read_piece_alert>(get_handle(), piece, ec, callback);
return;
}

Expand All @@ -776,7 +776,7 @@ bool is_downloading_state(int const st)
// this shouldn't actually happen
boost::shared_array<char> buf;
m_ses.alerts().emplace_alert<read_piece_alert>(
get_handle(), piece, buf, 0);
get_handle(), piece, buf, 0, callback);
return;
}

Expand All @@ -785,11 +785,12 @@ bool is_downloading_state(int const st)
if (!rp->piece_data)
{
m_ses.alerts().emplace_alert<read_piece_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);
Expand Down Expand Up @@ -1216,12 +1217,12 @@ bool is_downloading_state(int const st)
if (rp->fail)
{
m_ses.alerts().emplace_alert<read_piece_alert>(
get_handle(), r.piece, rp->error);
get_handle(), r.piece, rp->error, rp->callback);
}
else
{
m_ses.alerts().emplace_alert<read_piece_alert>(
get_handle(), r.piece, rp->piece_data, size);
get_handle(), r.piece, rp->piece_data, size, rp->callback);
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/torrent_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<read_piece_alert>::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
Expand Down

0 comments on commit 63ed794

Please sign in to comment.