diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 7fb2ece52..d3c6f8778 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -93,6 +93,7 @@ nobase_include_HEADERS = \ peer_id.hpp \ peer_info.hpp \ peer_request.hpp \ + piece_block.hpp \ piece_block_progress.hpp \ piece_picker.hpp \ platform_util.hpp \ @@ -177,6 +178,7 @@ nobase_include_HEADERS = \ aux_/ffs.hpp \ aux_/portmap.hpp \ aux_/lsd.hpp \ + aux_/has_block.hpp \ \ extensions/smart_ban.hpp \ extensions/ut_metadata.hpp \ diff --git a/include/libtorrent/alert_types.hpp b/include/libtorrent/alert_types.hpp index 955ba7e66..0a63445b9 100644 --- a/include/libtorrent/alert_types.hpp +++ b/include/libtorrent/alert_types.hpp @@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/performance_counters.hpp" #include "libtorrent/operations.hpp" // for operation_t enum #include "libtorrent/close_reason.hpp" +#include "libtorrent/piece_block.hpp" #include "libtorrent/aux_/escape_string.hpp" // for convert_from_native #include "libtorrent/aux_/disable_warnings_push.hpp" @@ -62,7 +63,6 @@ namespace libtorrent namespace aux { struct stack_allocator; } - struct piece_block; // maps an operation id (from peer_error_alert and peer_disconnected_alert) // to its name. See peer_connection for the constants diff --git a/include/libtorrent/aux_/has_block.hpp b/include/libtorrent/aux_/has_block.hpp new file mode 100644 index 000000000..983532711 --- /dev/null +++ b/include/libtorrent/aux_/has_block.hpp @@ -0,0 +1,55 @@ +/* + +Copyright (c) 2016, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_HAS_BLOCK_HPP_INCLUDED +#define TORRENT_HAS_BLOCK_HPP_INCLUDED + +#include "libtorrent/piece_block.hpp" + +namespace libtorrent { namespace aux { + struct has_block + { + has_block(has_block const&) = default; + + has_block(piece_block const& b): block(b) {} + bool operator()(pending_block const& pb) const + { return pb.block == block; } + private: + piece_block const& block; + // explicitly disallow assignment, to silence msvc warning + has_block& operator=(has_block const&); + }; + +}} + +#endif + diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 3816deca2..e590723a4 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -55,7 +55,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/session_settings.hpp" #include "libtorrent/disk_observer.hpp" #include "libtorrent/peer_connection_interface.hpp" -#include "libtorrent/piece_picker.hpp" // for piece_block #include "libtorrent/socket.hpp" // for tcp::endpoint #include "libtorrent/io_service_fwd.hpp" #include "libtorrent/receive_buffer.hpp" @@ -63,6 +62,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/aux_/time.hpp" #include "libtorrent/debug.hpp" #include "libtorrent/span.hpp" +#include "libtorrent/piece_block.hpp" #include #include @@ -73,10 +73,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include "libtorrent/aux_/disable_warnings_push.hpp" -#include -#include "libtorrent/aux_/disable_warnings_pop.hpp" - namespace libtorrent { class torrent; @@ -136,19 +132,6 @@ namespace libtorrent } }; - struct has_block - { - has_block(has_block const&) = default; - - has_block(piece_block const& b): block(b) {} - piece_block const& block; - bool operator()(pending_block const& pb) const - { return pb.block == block; } - private: - // explicitly disallow assignment, to silence msvc warning - has_block& operator=(has_block const&); - }; - // argument pack passed to peer_connection constructor struct peer_connection_args { diff --git a/include/libtorrent/piece_block.hpp b/include/libtorrent/piece_block.hpp new file mode 100644 index 000000000..19c41e5bc --- /dev/null +++ b/include/libtorrent/piece_block.hpp @@ -0,0 +1,66 @@ +/* + +Copyright (c) 2016, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_PIECE_BLOCK_HPP_INCLUDED +#define TORRENT_PIECE_BLOCK_HPP_INCLUDED + +namespace libtorrent +{ + struct TORRENT_EXTRA_EXPORT piece_block + { + static const piece_block invalid; + + piece_block() {} + piece_block(int p_index, int b_index) + : piece_index(p_index) + , block_index(b_index) + { + } + int piece_index; + int block_index; + + bool operator<(piece_block const& b) const + { + if (piece_index < b.piece_index) return true; + if (piece_index == b.piece_index) return block_index < b.block_index; + return false; + } + + bool operator==(piece_block const& b) const + { return piece_index == b.piece_index && block_index == b.block_index; } + + bool operator!=(piece_block const& b) const + { return piece_index != b.piece_index || block_index != b.block_index; } + }; +} +#endif + diff --git a/include/libtorrent/piece_picker.hpp b/include/libtorrent/piece_picker.hpp index 513356c41..241f1cf8c 100644 --- a/include/libtorrent/piece_picker.hpp +++ b/include/libtorrent/piece_picker.hpp @@ -54,6 +54,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/peer_id.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/time.hpp" +#include "libtorrent/piece_block.hpp" namespace libtorrent { @@ -64,33 +65,6 @@ namespace libtorrent struct counters; struct torrent_peer; - struct TORRENT_EXTRA_EXPORT piece_block - { - static const piece_block invalid; - - piece_block() {} - piece_block(int p_index, int b_index) - : piece_index(p_index) - , block_index(b_index) - { - } - int piece_index; - int block_index; - - bool operator<(piece_block const& b) const - { - if (piece_index < b.piece_index) return true; - if (piece_index == b.piece_index) return block_index < b.block_index; - return false; - } - - bool operator==(piece_block const& b) const - { return piece_index == b.piece_index && block_index == b.block_index; } - - bool operator!=(piece_block const& b) const - { return piece_index != b.piece_index || block_index != b.block_index; } - }; - class TORRENT_EXTRA_EXPORT piece_picker { public: diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index e5b424088..b443032a7 100644 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -65,6 +65,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/vector_utils.hpp" #include "libtorrent/linked_list.hpp" #include "libtorrent/debug.hpp" +#include "libtorrent/piece_block.hpp" #include "libtorrent/aux_/file_progress.hpp" #include "libtorrent/aux_/suggest_piece.hpp" diff --git a/src/alert.cpp b/src/alert.cpp index d0179f702..b2f4dab5b 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -43,7 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/torrent.hpp" #include "libtorrent/performance_counters.hpp" #include "libtorrent/stack_allocator.hpp" -#include "libtorrent/piece_picker.hpp" // for piece_block +#include "libtorrent/piece_block.hpp" #include "libtorrent/hex.hpp" // to_hex #include "libtorrent/aux_/escape_string.hpp" // for convert_from_native diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index fea096143..330545fc8 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -61,6 +61,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/ip_filter.hpp" #include "libtorrent/kademlia/node_id.hpp" #include "libtorrent/close_reason.hpp" +#include "libtorrent/aux_/has_block.hpp" #include "libtorrent/aux_/time.hpp" #if TORRENT_USE_ASSERTS @@ -2471,7 +2472,7 @@ namespace libtorrent return; } - piece_block b(r.piece, r.start / t->block_size()); + piece_block const b(r.piece, r.start / t->block_size()); m_receiving_block = b; bool in_req_queue = false; @@ -2694,7 +2695,7 @@ namespace libtorrent = std::find_if( m_download_queue.begin() , m_download_queue.end() - , has_block(block_finished)); + , aux::has_block(block_finished)); if (b == m_download_queue.end()) { @@ -3352,7 +3353,7 @@ namespace libtorrent { TORRENT_ASSERT(is_single_thread()); std::vector::iterator rit = std::find_if(m_request_queue.begin() - , m_request_queue.end(), has_block(block)); + , m_request_queue.end(), aux::has_block(block)); if (rit == m_request_queue.end()) return false; #if TORRENT_USE_ASSERTS std::shared_ptr t = m_torrent.lock(); @@ -3387,7 +3388,7 @@ namespace libtorrent TORRENT_ASSERT(!t->picker().is_requested(block) || (t->picker().num_peers(block) > 0)); TORRENT_ASSERT(!t->have_piece(block.piece_index)); TORRENT_ASSERT(std::find_if(m_download_queue.begin(), m_download_queue.end() - , has_block(block)) == m_download_queue.end()); + , aux::has_block(block)) == m_download_queue.end()); TORRENT_ASSERT(std::find(m_request_queue.begin(), m_request_queue.end() , block) == m_request_queue.end()); @@ -3556,11 +3557,12 @@ namespace libtorrent if (!t->picker().is_requested(block)) return; std::vector::iterator it - = std::find_if(m_download_queue.begin(), m_download_queue.end(), has_block(block)); + = std::find_if(m_download_queue.begin(), m_download_queue.end() + , aux::has_block(block)); if (it == m_download_queue.end()) { std::vector::iterator rit = std::find_if(m_request_queue.begin() - , m_request_queue.end(), has_block(block)); + , m_request_queue.end(), aux::has_block(block)); // when a multi block is received, it is cancelled // from all peers, so if this one hasn't requested diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index 2d801224d..2c329928d 100644 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -61,6 +61,7 @@ using namespace std::placeholders; namespace libtorrent { + // TODO: find a better place for this const piece_block piece_block::invalid((std::numeric_limits::max)(), (std::numeric_limits::max)()); piece_picker::piece_picker() diff --git a/src/request_blocks.cpp b/src/request_blocks.cpp index f91c2685b..0964b256b 100644 --- a/src/request_blocks.cpp +++ b/src/request_blocks.cpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/performance_counters.hpp" // for counters #include "libtorrent/request_blocks.hpp" #include "libtorrent/alert_manager.hpp" +#include "libtorrent/aux_/has_block.hpp" #include @@ -231,12 +232,12 @@ namespace libtorrent // pieces we didn't request. Those aren't marked in the // piece picker, but we still keep track of them in the // download queue - if (std::find_if(dq.begin(), dq.end(), has_block(*i)) != dq.end() - || std::find_if(rq.begin(), rq.end(), has_block(*i)) != rq.end()) + if (std::find_if(dq.begin(), dq.end(), aux::has_block(*i)) != dq.end() + || std::find_if(rq.begin(), rq.end(), aux::has_block(*i)) != rq.end()) { #if TORRENT_USE_ASSERTS std::vector::const_iterator j - = std::find_if(dq.begin(), dq.end(), has_block(*i)); + = std::find_if(dq.begin(), dq.end(), aux::has_block(*i)); if (j != dq.end()) TORRENT_ASSERT(j->timed_out || j->not_wanted); #endif #ifndef TORRENT_DISABLE_LOGGING diff --git a/src/torrent.cpp b/src/torrent.cpp index e4fa1d28a..9cfb90fc1 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -89,6 +89,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alloca.hpp" #include "libtorrent/resolve_links.hpp" #include "libtorrent/aux_/file_progress.hpp" +#include "libtorrent/aux_/has_block.hpp" #include "libtorrent/alert_manager.hpp" #include "libtorrent/disk_interface.hpp" #include "libtorrent/broadcast_socket.hpp" // for is_ip_address @@ -10013,8 +10014,8 @@ namespace libtorrent // same peer std::vector const& dq = c.download_queue(); - bool already_requested = std::find_if(dq.begin(), dq.end() - , has_block(b)) != dq.end(); + bool const already_requested = std::find_if(dq.begin(), dq.end() + , aux::has_block(b)) != dq.end(); if (already_requested) { @@ -10035,8 +10036,8 @@ namespace libtorrent std::vector const& rq = c.request_queue(); - bool already_in_queue = std::find_if(rq.begin(), rq.end() - , has_block(b)) != rq.end(); + bool const already_in_queue = std::find_if(rq.begin(), rq.end() + , aux::has_block(b)) != rq.end(); if (already_in_queue) {