From 9c94f548681d48c459b7a6a3dfdf7c96ead97d50 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 18 Jun 2008 12:34:39 +0000 Subject: [PATCH] fixes for torrents with smaller pieces than 16kB --- src/torrent.cpp | 8 ++++++-- test/setup_transfer.cpp | 10 +++++----- test/setup_transfer.hpp | 4 ++-- test/test_swarm.cpp | 3 ++- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/torrent.cpp b/src/torrent.cpp index 3303860dd..8650ea42f 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -179,7 +179,7 @@ namespace libtorrent , m_max_uploads((std::numeric_limits::max)()) , m_num_uploads(0) , m_max_connections((std::numeric_limits::max)()) - , m_block_size(block_size) + , m_block_size((std::min)(block_size, tf->piece_length())) , m_complete(-1) , m_incomplete(-1) , m_deficit_counter(0) @@ -405,13 +405,15 @@ namespace libtorrent TORRENT_ASSERT(m_torrent_file->num_files() > 0); TORRENT_ASSERT(m_torrent_file->total_size() >= 0); + m_block_size = (std::min)(m_block_size, m_torrent_file->piece_length()); + // the shared_from_this() will create an intentional // cycle of ownership, se the hpp file for description. m_owning_storage = new piece_manager(shared_from_this(), m_torrent_file , m_save_path, m_ses.m_files, m_ses.m_disk_thread, m_storage_constructor , m_storage_mode); m_storage = m_owning_storage.get(); - m_picker->init(m_torrent_file->piece_length() / m_block_size + m_picker->init((std::max)(m_torrent_file->piece_length() / m_block_size, 1) , int((m_torrent_file->total_size()+m_block_size-1)/m_block_size)); std::vector const& url_seeds = m_torrent_file->url_seeds(); @@ -3350,6 +3352,8 @@ namespace libtorrent TORRENT_ASSERT(total_done == m_torrent_file->total_size()); else TORRENT_ASSERT(total_done != m_torrent_file->total_size() || !m_files_checked); + + TORRENT_ASSERT(m_block_size <= m_torrent_file->piece_length()); } else { diff --git a/test/setup_transfer.cpp b/test/setup_transfer.cpp index 6334b448a..1e43b634c 100644 --- a/test/setup_transfer.cpp +++ b/test/setup_transfer.cpp @@ -164,7 +164,7 @@ boost::intrusive_ptr clone_ptr(boost::intrusive_ptr const& ptr) return boost::intrusive_ptr(new T(*ptr)); } -boost::intrusive_ptr create_torrent(std::ostream* file) +boost::intrusive_ptr create_torrent(std::ostream* file, int piece_size) { char const* tracker_url = "http://non-existent-name.com/announce"; @@ -173,10 +173,10 @@ boost::intrusive_ptr create_torrent(std::ostream* file) file_storage fs; int total_size = 2 * 1024 * 1024; fs.add_file(path("temporary"), total_size); - libtorrent::create_torrent t(fs, 16 * 1024); + libtorrent::create_torrent t(fs, piece_size); t.add_tracker(tracker_url); - std::vector piece(16 * 1024); + std::vector piece(piece_size); for (int i = 0; i < int(piece.size()); ++i) piece[i] = (i % 26) + 'A'; @@ -204,7 +204,7 @@ boost::intrusive_ptr create_torrent(std::ostream* file) boost::tuple setup_transfer(session* ses1, session* ses2, session* ses3 , bool clear_files, bool use_metadata_transfer, bool connect_peers - , std::string suffix) + , std::string suffix, int piece_size) { using namespace boost::filesystem; @@ -218,7 +218,7 @@ setup_transfer(session* ses1, session* ses2, session* ses3 create_directory("./tmp1" + suffix); std::ofstream file(("./tmp1" + suffix + "/temporary").c_str()); - boost::intrusive_ptr t = ::create_torrent(&file); + boost::intrusive_ptr t = ::create_torrent(&file, piece_size); file.close(); if (clear_files) { diff --git a/test/setup_transfer.hpp b/test/setup_transfer.hpp index 063e6c271..e75d2fd75 100644 --- a/test/setup_transfer.hpp +++ b/test/setup_transfer.hpp @@ -10,13 +10,13 @@ void print_alerts(libtorrent::session& ses, char const* name , bool allow_no_torrents = false); void test_sleep(int millisec); -boost::intrusive_ptr create_torrent(std::ostream* file = 0); +boost::intrusive_ptr create_torrent(std::ostream* file = 0, int piece_size = 16 * 1024); boost::tuple setup_transfer(libtorrent::session* ses1, libtorrent::session* ses2 , libtorrent::session* ses3, bool clear_files, bool use_metadata_transfer = true - , bool connect = true, std::string suffix = ""); + , bool connect = true, std::string suffix = "", int piece_size = 16 * 1024); void start_web_server(int port, bool ssl = false); void stop_web_server(int port); diff --git a/test/test_swarm.cpp b/test/test_swarm.cpp index 7d96ba1e4..64b4a4b90 100644 --- a/test/test_swarm.cpp +++ b/test/test_swarm.cpp @@ -54,7 +54,8 @@ void test_swarm() torrent_handle tor2; torrent_handle tor3; - boost::tie(tor1, tor2, tor3) = setup_transfer(&ses1, &ses2, &ses3, true, false, true, "_swarm"); + // test using piece sizes smaller than 16kB + boost::tie(tor1, tor2, tor3) = setup_transfer(&ses1, &ses2, &ses3, true, false, true, "_swarm", 8 * 1024); float sum_dl_rate2 = 0.f; float sum_dl_rate3 = 0.f;