refactor to avoid use of optional with piece_block_progress (#1061)

This commit is contained in:
Alden Torres 2016-09-04 12:31:02 -04:00 committed by Arvid Norberg
parent 29a4075555
commit 728e74eca7
10 changed files with 49 additions and 52 deletions

View File

@ -284,7 +284,7 @@ namespace libtorrent
// block. If the peer isn't downloading // block. If the peer isn't downloading
// a piece for the moment, the boost::optional // a piece for the moment, the boost::optional
// will be invalid. // will be invalid.
boost::optional<piece_block_progress> downloading_piece_progress() const override; piece_block_progress downloading_piece_progress() const override;
#if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS)

View File

@ -85,7 +85,7 @@ namespace libtorrent
// block. If the peer isn't downloading // block. If the peer isn't downloading
// a piece for the moment, the boost::optional // a piece for the moment, the boost::optional
// will be invalid. // will be invalid.
boost::optional<piece_block_progress> downloading_piece_progress() const override; piece_block_progress downloading_piece_progress() const override;
// this is const since it's used as a key in the web seed list in the torrent // this is const since it's used as a key in the web seed list in the torrent
// if it's changed referencing back into that list will fail // if it's changed referencing back into that list will fail

View File

@ -640,10 +640,10 @@ namespace libtorrent
// returns the block currently being // returns the block currently being
// downloaded. And the progress of that // downloaded. And the progress of that
// block. If the peer isn't downloading // block. If the peer isn't downloading
// a piece for the moment, the boost::optional // a piece for the moment, implementors
// will be invalid. // must return an object with the piece_index
virtual boost::optional<piece_block_progress> // value invalid (the default constructor).
downloading_piece_progress() const; virtual piece_block_progress downloading_piece_progress() const;
enum message_type_flags { message_type_request = 1 }; enum message_type_flags { message_type_request = 1 };
void send_buffer(char const* begin, int size, int flags = 0); void send_buffer(char const* begin, int size, int flags = 0);

View File

@ -39,11 +39,13 @@ namespace libtorrent
{ {
struct piece_block_progress struct piece_block_progress
{ {
constexpr static int invalid_index = -1;
// the piece and block index // the piece and block index
// determines exactly which // determines exactly which
// part of the torrent that // part of the torrent that
// is currently being downloaded // is currently being downloaded
int piece_index; int piece_index = invalid_index;
int block_index; int block_index;
// the number of bytes we have received // the number of bytes we have received
// of this block // of this block
@ -54,4 +56,3 @@ namespace libtorrent
} }
#endif // TORRENT_PIECE_BLOCK_PROGRESS_HPP_INCLUDED #endif // TORRENT_PIECE_BLOCK_PROGRESS_HPP_INCLUDED

View File

@ -104,7 +104,7 @@ namespace libtorrent
// block. If the peer isn't downloading // block. If the peer isn't downloading
// a piece for the moment, the boost::optional // a piece for the moment, the boost::optional
// will be invalid. // will be invalid.
boost::optional<piece_block_progress> downloading_piece_progress() const override; piece_block_progress downloading_piece_progress() const override;
void handle_padfile(); void handle_padfile();

View File

@ -839,7 +839,7 @@ namespace libtorrent
send_buffer(handshake, sizeof(handshake)); send_buffer(handshake, sizeof(handshake));
} }
boost::optional<piece_block_progress> bt_peer_connection::downloading_piece_progress() const piece_block_progress bt_peer_connection::downloading_piece_progress() const
{ {
std::shared_ptr<torrent> t = associated_torrent().lock(); std::shared_ptr<torrent> t = associated_torrent().lock();
TORRENT_ASSERT(t); TORRENT_ASSERT(t);
@ -849,7 +849,7 @@ namespace libtorrent
if (m_state != state_t::read_packet if (m_state != state_t::read_packet
|| int(recv_buffer.size()) <= 9 || int(recv_buffer.size()) <= 9
|| recv_buffer[0] != msg_piece) || recv_buffer[0] != msg_piece)
return boost::optional<piece_block_progress>(); return piece_block_progress();
const char* ptr = recv_buffer.begin() + 1; const char* ptr = recv_buffer.begin() + 1;
peer_request r; peer_request r;
@ -859,7 +859,7 @@ namespace libtorrent
// is any of the piece message header data invalid? // is any of the piece message header data invalid?
if (!verify_piece(r)) if (!verify_piece(r))
return boost::optional<piece_block_progress>(); return piece_block_progress();
piece_block_progress p; piece_block_progress p;
@ -868,7 +868,7 @@ namespace libtorrent
p.bytes_downloaded = int(recv_buffer.size()) - 9; p.bytes_downloaded = int(recv_buffer.size()) - 9;
p.full_block_bytes = r.length; p.full_block_bytes = r.length;
return boost::optional<piece_block_progress>(p); return p;
} }

View File

@ -88,11 +88,10 @@ namespace libtorrent
if (t) t->disconnect_web_seed(this); if (t) t->disconnect_web_seed(this);
} }
boost::optional<piece_block_progress> piece_block_progress http_seed_connection::downloading_piece_progress() const
http_seed_connection::downloading_piece_progress() const
{ {
if (m_requests.empty()) if (m_requests.empty())
return boost::optional<piece_block_progress>(); return piece_block_progress();
std::shared_ptr<torrent> t = associated_torrent().lock(); std::shared_ptr<torrent> t = associated_torrent().lock();
TORRENT_ASSERT(t); TORRENT_ASSERT(t);

View File

@ -4163,12 +4163,12 @@ namespace libtorrent
if (!m_ignore_stats) if (!m_ignore_stats)
{ {
// report any partially received payload as redundant // report any partially received payload as redundant
boost::optional<piece_block_progress> pbp = downloading_piece_progress(); piece_block_progress pbp = downloading_piece_progress();
if (pbp if (pbp.piece_index != piece_block_progress::invalid_index
&& pbp->bytes_downloaded > 0 && pbp.bytes_downloaded > 0
&& pbp->bytes_downloaded < pbp->full_block_bytes) && pbp.bytes_downloaded < pbp.full_block_bytes)
{ {
t->add_redundant_bytes(pbp->bytes_downloaded, waste_reason::piece_closing); t->add_redundant_bytes(pbp.bytes_downloaded, waste_reason::piece_closing);
} }
} }
@ -4325,12 +4325,13 @@ namespace libtorrent
if (i->busy) ++p.busy_requests; if (i->busy) ++p.busy_requests;
} }
if (boost::optional<piece_block_progress> ret = downloading_piece_progress()) piece_block_progress ret = downloading_piece_progress();
if (ret.piece_index != piece_block_progress::invalid_index)
{ {
p.downloading_piece_index = ret->piece_index; p.downloading_piece_index = ret.piece_index;
p.downloading_block_index = ret->block_index; p.downloading_block_index = ret.block_index;
p.downloading_progress = ret->bytes_downloaded; p.downloading_progress = ret.bytes_downloaded;
p.downloading_total = ret->full_block_bytes; p.downloading_total = ret.full_block_bytes;
} }
else else
{ {
@ -5582,14 +5583,13 @@ namespace libtorrent
, userdata, ref); , userdata, ref);
} }
boost::optional<piece_block_progress> piece_block_progress peer_connection::downloading_piece_progress() const
peer_connection::downloading_piece_progress() const
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "ERROR" peer_log(peer_log_alert::info, "ERROR"
, "downloading_piece_progress() dispatched to the base class!"); , "downloading_piece_progress() dispatched to the base class!");
#endif #endif
return boost::optional<piece_block_progress>(); return piece_block_progress();
} }
namespace { namespace {

View File

@ -3819,14 +3819,14 @@ namespace libtorrent
for (const_peer_iterator i = begin(); i != end(); ++i) for (const_peer_iterator i = begin(); i != end(); ++i)
{ {
peer_connection* pc = *i; peer_connection* pc = *i;
boost::optional<piece_block_progress> p piece_block_progress p = pc->downloading_piece_progress();
= pc->downloading_piece_progress(); if (p.piece_index == piece_block_progress::invalid_index)
if (!p) continue;
if (m_picker->has_piece_passed(p->piece_index))
continue; continue;
piece_block block(p->piece_index, p->block_index); if (m_picker->has_piece_passed(p.piece_index))
continue;
piece_block block(p.piece_index, p.block_index);
if (m_picker->is_finished(block)) if (m_picker->is_finished(block))
continue; continue;
@ -3834,16 +3834,16 @@ namespace libtorrent
= downloading_piece.find(block); = downloading_piece.find(block);
if (dp != downloading_piece.end()) if (dp != downloading_piece.end())
{ {
if (dp->second < p->bytes_downloaded) if (dp->second < p.bytes_downloaded)
dp->second = p->bytes_downloaded; dp->second = p.bytes_downloaded;
} }
else else
{ {
downloading_piece[block] = p->bytes_downloaded; downloading_piece[block] = p.bytes_downloaded;
} }
TORRENT_ASSERT(p->bytes_downloaded <= p->full_block_bytes); TORRENT_ASSERT(p.bytes_downloaded <= p.full_block_bytes);
TORRENT_ASSERT(p->full_block_bytes == to_req(piece_block( TORRENT_ASSERT(p.full_block_bytes == to_req(piece_block(
p->piece_index, p->block_index)).length); p.piece_index, p.block_index)).length);
} }
for (std::map<piece_block, int>::iterator i = downloading_piece.begin(); for (std::map<piece_block, int>::iterator i = downloading_piece.begin();
i != downloading_piece.end(); ++i) i != downloading_piece.end(); ++i)
@ -6713,11 +6713,10 @@ namespace libtorrent
bi.set_peer(peer->remote()); bi.set_peer(peer->remote());
if (bi.state == block_info::requested) if (bi.state == block_info::requested)
{ {
boost::optional<piece_block_progress> pbp auto pbp = peer->downloading_piece_progress();
= peer->downloading_piece_progress(); if (pbp.piece_index == i->index && pbp.block_index == j)
if (pbp && pbp->piece_index == i->index && pbp->block_index == j)
{ {
bi.bytes_progress = pbp->bytes_downloaded; bi.bytes_progress = pbp.bytes_downloaded;
TORRENT_ASSERT(bi.bytes_progress <= bi.block_size); TORRENT_ASSERT(bi.bytes_progress <= bi.block_size);
} }
else else
@ -10684,10 +10683,9 @@ namespace libtorrent
if (p && p->connection) if (p && p->connection)
{ {
peer_connection* peer = static_cast<peer_connection*>(p->connection); peer_connection* peer = static_cast<peer_connection*>(p->connection);
boost::optional<piece_block_progress> pbp auto pbp = peer->downloading_piece_progress();
= peer->downloading_piece_progress(); if (pbp.piece_index == i->index && pbp.block_index == k)
if (pbp && pbp->piece_index == i->index && pbp->block_index == k) block = pbp.bytes_downloaded;
block = pbp->bytes_downloaded;
TORRENT_ASSERT(block <= block_size()); TORRENT_ASSERT(block <= block_size());
} }

View File

@ -222,11 +222,10 @@ void web_peer_connection::disconnect(error_code const& ec
if (t) t->disconnect_web_seed(this); if (t) t->disconnect_web_seed(this);
} }
boost::optional<piece_block_progress> piece_block_progress web_peer_connection::downloading_piece_progress() const
web_peer_connection::downloading_piece_progress() const
{ {
if (m_requests.empty()) if (m_requests.empty())
return boost::optional<piece_block_progress>(); return piece_block_progress();
std::shared_ptr<torrent> t = associated_torrent().lock(); std::shared_ptr<torrent> t = associated_torrent().lock();
TORRENT_ASSERT(t); TORRENT_ASSERT(t);