From 728e74eca7f6a22aac2c39c2837ea79fb5dd18ce Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Sun, 4 Sep 2016 12:31:02 -0400 Subject: [PATCH] refactor to avoid use of optional with piece_block_progress (#1061) --- include/libtorrent/bt_peer_connection.hpp | 2 +- include/libtorrent/http_seed_connection.hpp | 2 +- include/libtorrent/peer_connection.hpp | 8 ++--- include/libtorrent/piece_block_progress.hpp | 5 +-- include/libtorrent/web_peer_connection.hpp | 2 +- src/bt_peer_connection.cpp | 8 ++--- src/http_seed_connection.cpp | 5 ++- src/peer_connection.cpp | 26 +++++++------- src/torrent.cpp | 38 ++++++++++----------- src/web_peer_connection.cpp | 5 ++- 10 files changed, 49 insertions(+), 52 deletions(-) diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index 6a13299ee..60101e2d4 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -284,7 +284,7 @@ namespace libtorrent // block. If the peer isn't downloading // a piece for the moment, the boost::optional // will be invalid. - boost::optional downloading_piece_progress() const override; + piece_block_progress downloading_piece_progress() const override; #if !defined(TORRENT_DISABLE_ENCRYPTION) && !defined(TORRENT_DISABLE_EXTENSIONS) diff --git a/include/libtorrent/http_seed_connection.hpp b/include/libtorrent/http_seed_connection.hpp index c574fa1b4..6783971b6 100644 --- a/include/libtorrent/http_seed_connection.hpp +++ b/include/libtorrent/http_seed_connection.hpp @@ -85,7 +85,7 @@ namespace libtorrent // block. If the peer isn't downloading // a piece for the moment, the boost::optional // will be invalid. - boost::optional 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 // if it's changed referencing back into that list will fail diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 98885f536..ee35e8708 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -640,10 +640,10 @@ namespace libtorrent // returns the block currently being // downloaded. And the progress of that // block. If the peer isn't downloading - // a piece for the moment, the boost::optional - // will be invalid. - virtual boost::optional - downloading_piece_progress() const; + // a piece for the moment, implementors + // must return an object with the piece_index + // value invalid (the default constructor). + virtual piece_block_progress downloading_piece_progress() const; enum message_type_flags { message_type_request = 1 }; void send_buffer(char const* begin, int size, int flags = 0); diff --git a/include/libtorrent/piece_block_progress.hpp b/include/libtorrent/piece_block_progress.hpp index 80a0112b3..1fa890407 100644 --- a/include/libtorrent/piece_block_progress.hpp +++ b/include/libtorrent/piece_block_progress.hpp @@ -39,11 +39,13 @@ namespace libtorrent { struct piece_block_progress { + constexpr static int invalid_index = -1; + // the piece and block index // determines exactly which // part of the torrent that // is currently being downloaded - int piece_index; + int piece_index = invalid_index; int block_index; // the number of bytes we have received // of this block @@ -54,4 +56,3 @@ namespace libtorrent } #endif // TORRENT_PIECE_BLOCK_PROGRESS_HPP_INCLUDED - diff --git a/include/libtorrent/web_peer_connection.hpp b/include/libtorrent/web_peer_connection.hpp index 6aca36a40..2f600eda3 100644 --- a/include/libtorrent/web_peer_connection.hpp +++ b/include/libtorrent/web_peer_connection.hpp @@ -104,7 +104,7 @@ namespace libtorrent // block. If the peer isn't downloading // a piece for the moment, the boost::optional // will be invalid. - boost::optional downloading_piece_progress() const override; + piece_block_progress downloading_piece_progress() const override; void handle_padfile(); diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 1aefd0b5f..8c4b39d67 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -839,7 +839,7 @@ namespace libtorrent send_buffer(handshake, sizeof(handshake)); } - boost::optional bt_peer_connection::downloading_piece_progress() const + piece_block_progress bt_peer_connection::downloading_piece_progress() const { std::shared_ptr t = associated_torrent().lock(); TORRENT_ASSERT(t); @@ -849,7 +849,7 @@ namespace libtorrent if (m_state != state_t::read_packet || int(recv_buffer.size()) <= 9 || recv_buffer[0] != msg_piece) - return boost::optional(); + return piece_block_progress(); const char* ptr = recv_buffer.begin() + 1; peer_request r; @@ -859,7 +859,7 @@ namespace libtorrent // is any of the piece message header data invalid? if (!verify_piece(r)) - return boost::optional(); + return piece_block_progress(); piece_block_progress p; @@ -868,7 +868,7 @@ namespace libtorrent p.bytes_downloaded = int(recv_buffer.size()) - 9; p.full_block_bytes = r.length; - return boost::optional(p); + return p; } diff --git a/src/http_seed_connection.cpp b/src/http_seed_connection.cpp index e22d7c94d..6967fbee6 100644 --- a/src/http_seed_connection.cpp +++ b/src/http_seed_connection.cpp @@ -88,11 +88,10 @@ namespace libtorrent if (t) t->disconnect_web_seed(this); } - boost::optional - http_seed_connection::downloading_piece_progress() const + piece_block_progress http_seed_connection::downloading_piece_progress() const { if (m_requests.empty()) - return boost::optional(); + return piece_block_progress(); std::shared_ptr t = associated_torrent().lock(); TORRENT_ASSERT(t); diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index a7f59494a..1959f7708 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -4163,12 +4163,12 @@ namespace libtorrent if (!m_ignore_stats) { // report any partially received payload as redundant - boost::optional pbp = downloading_piece_progress(); - if (pbp - && pbp->bytes_downloaded > 0 - && pbp->bytes_downloaded < pbp->full_block_bytes) + piece_block_progress pbp = downloading_piece_progress(); + if (pbp.piece_index != piece_block_progress::invalid_index + && pbp.bytes_downloaded > 0 + && 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 (boost::optional 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_block_index = ret->block_index; - p.downloading_progress = ret->bytes_downloaded; - p.downloading_total = ret->full_block_bytes; + p.downloading_piece_index = ret.piece_index; + p.downloading_block_index = ret.block_index; + p.downloading_progress = ret.bytes_downloaded; + p.downloading_total = ret.full_block_bytes; } else { @@ -5582,14 +5583,13 @@ namespace libtorrent , userdata, ref); } - boost::optional - peer_connection::downloading_piece_progress() const + piece_block_progress peer_connection::downloading_piece_progress() const { #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "ERROR" , "downloading_piece_progress() dispatched to the base class!"); #endif - return boost::optional(); + return piece_block_progress(); } namespace { diff --git a/src/torrent.cpp b/src/torrent.cpp index 086c0f1b8..c1ccc9958 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -3819,14 +3819,14 @@ namespace libtorrent for (const_peer_iterator i = begin(); i != end(); ++i) { peer_connection* pc = *i; - boost::optional p - = pc->downloading_piece_progress(); - if (!p) continue; - - if (m_picker->has_piece_passed(p->piece_index)) + piece_block_progress p = pc->downloading_piece_progress(); + if (p.piece_index == piece_block_progress::invalid_index) 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)) continue; @@ -3834,16 +3834,16 @@ namespace libtorrent = downloading_piece.find(block); if (dp != downloading_piece.end()) { - if (dp->second < p->bytes_downloaded) - dp->second = p->bytes_downloaded; + if (dp->second < p.bytes_downloaded) + dp->second = p.bytes_downloaded; } 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->full_block_bytes == to_req(piece_block( - p->piece_index, p->block_index)).length); + TORRENT_ASSERT(p.bytes_downloaded <= p.full_block_bytes); + TORRENT_ASSERT(p.full_block_bytes == to_req(piece_block( + p.piece_index, p.block_index)).length); } for (std::map::iterator i = downloading_piece.begin(); i != downloading_piece.end(); ++i) @@ -6713,11 +6713,10 @@ namespace libtorrent bi.set_peer(peer->remote()); if (bi.state == block_info::requested) { - boost::optional pbp - = peer->downloading_piece_progress(); - if (pbp && pbp->piece_index == i->index && pbp->block_index == j) + auto pbp = peer->downloading_piece_progress(); + if (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); } else @@ -10684,10 +10683,9 @@ namespace libtorrent if (p && p->connection) { peer_connection* peer = static_cast(p->connection); - boost::optional pbp - = peer->downloading_piece_progress(); - if (pbp && pbp->piece_index == i->index && pbp->block_index == k) - block = pbp->bytes_downloaded; + auto pbp = peer->downloading_piece_progress(); + if (pbp.piece_index == i->index && pbp.block_index == k) + block = pbp.bytes_downloaded; TORRENT_ASSERT(block <= block_size()); } diff --git a/src/web_peer_connection.cpp b/src/web_peer_connection.cpp index a7e711ec1..16a3f2266 100644 --- a/src/web_peer_connection.cpp +++ b/src/web_peer_connection.cpp @@ -222,11 +222,10 @@ void web_peer_connection::disconnect(error_code const& ec if (t) t->disconnect_web_seed(this); } -boost::optional -web_peer_connection::downloading_piece_progress() const +piece_block_progress web_peer_connection::downloading_piece_progress() const { if (m_requests.empty()) - return boost::optional(); + return piece_block_progress(); std::shared_ptr t = associated_torrent().lock(); TORRENT_ASSERT(t);