From 3ed0745cb197cc5fb740fe9e9cff01ddb43dee15 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 8 Dec 2003 01:37:30 +0000 Subject: [PATCH] *** empty log message *** --- examples/client_test.cpp | 25 ++++++++++++++++------- include/libtorrent/peer_connection.hpp | 2 ++ src/peer_connection.cpp | 28 ++++++++++++++++++-------- src/session.cpp | 2 +- src/storage.cpp | 6 ++++-- src/torrent.cpp | 4 ++-- 6 files changed, 47 insertions(+), 20 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index d3455be01..025d9f611 100755 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -133,18 +133,29 @@ void clear() #endif +std::string to_string(float v) +{ + std::stringstream s; + s.precision(4); + s.flags(std::ios_base::right); + s.width(5); + s.fill(' '); + s << v; + return s.str(); +} + std::string add_suffix(float val) { - const char* prefix[] = {"B", "kB", "MB", "GB", "TB"}; + const char* prefix[] = {"B ", "kB", "MB", "GB", "TB"}; const int num_prefix = sizeof(prefix) / sizeof(const char*); int i; for (i = 0; i < num_prefix; ++i) { if (val < 1024.f) - return boost::lexical_cast(val) + prefix[i]; + return to_string(val) + prefix[i]; val /= 1024.f; } - return boost::lexical_cast(val) + prefix[i]; + return to_string(val) + prefix[i]; } int main(int argc, char* argv[]) @@ -252,14 +263,14 @@ int main(int argc, char* argv[]) boost::posix_time::time_duration t = s.next_announce; // std::cout << "next announce: " << boost::posix_time::to_simple_string(t) << "\n"; std::cout << "next announce: " << t.hours() << ":" << t.minutes() << ":" << t.seconds() << "\n"; -/* + for (std::vector::iterator i = peers.begin(); i != peers.end(); ++i) { std::cout << "d: " << add_suffix(i->down_speed) << "/s (" << add_suffix(i->total_download) << ") u: " << add_suffix(i->up_speed) << "/s (" << add_suffix(i->total_upload) - << ") ratio: " << static_cast(i->total_upload+1) / static_cast(i->total_download+1) + << ") diff: " << add_suffix(i->total_download - i->total_upload) << " flags: " << static_cast((i->flags & peer_info::interesting)?"I":"_") << static_cast((i->flags & peer_info::choked)?"C":"_") @@ -267,8 +278,8 @@ int main(int argc, char* argv[]) << static_cast((i->flags & peer_info::remote_choked)?"c":"_") << "\n"; } -*/ +/* i->get_download_queue(queue); for (std::vector::iterator i = queue.begin(); i != queue.end(); @@ -283,7 +294,7 @@ int main(int argc, char* argv[]) } std::cout << "\n"; } - +*/ std::cout << "___________________________________\n"; } diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 7980ffbd8..89bfa9973 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -342,6 +342,7 @@ namespace libtorrent // this peer given the current download rate // and the current share ratio with this peer. // this limit will maintain a 1:1 share ratio. + // -1 means no limit int m_send_quota_limit; // for every valid piece we receive where this @@ -369,6 +370,7 @@ namespace libtorrent return; } + assert(m_send_quota_left > 0 || m_send_quota_left == -1); assert(has_data()); if (!m_added_to_selector) { diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 7deb30214..a0ead07fa 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -762,15 +762,27 @@ void libtorrent::peer_connection::second_tick() // both peers uses this technique! It could be // enough to just have a constant positive bias // of the send_quota_limit - int bias = (static_cast(m_statistics.total_download()) - - static_cast(m_statistics.total_upload())) / 1024; + int diff = static_cast(m_statistics.total_download()) + - static_cast(m_statistics.total_upload()); - // the maximum send_quota given our download rate from this peer - int m_send_quota_limit = m_statistics.download_rate() + bias; - if (m_send_quota_limit < 500) m_send_quota_limit = 500; - - // TODO: temporary - m_send_quota_limit = 1024*1024; + if (diff > m_torrent->torrent_file().piece_length()) + { + // if we have downloaded more than one piece more + // than we have uploaded, have an unlimited + // upload rate + m_send_quota = -1; + } + else + { + // if we have downloaded too much, response with an + // upload rate of 10 kB/s more than we dowlload + // if we have uploaded too much, send with a rate of + // 10 kB/s less than we receive + int bias = (diff > 0 ? 10 : -10) * 1024; + // the maximum send_quota given our download rate from this peer + m_send_quota_limit = m_statistics.download_rate() + bias; + if (m_send_quota_limit < 500) m_send_quota_limit = 500; + } } // -------------------------- diff --git a/src/session.cpp b/src/session.cpp index 11ab062f5..d5b19c6e5 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -93,7 +93,7 @@ namespace return; } - // TODO: IMPLEMENT! + // TODO: upload limit support is currently broken assert(false); diff --git a/src/storage.cpp b/src/storage.cpp index f19718b27..17a208adc 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -1427,7 +1427,8 @@ namespace libtorrent { in.read(buf + buf_pos, read_bytes); - assert(read_bytes == in.gcount()); + int actual_read = in.gcount(); + assert(read_bytes == actual_read); left_to_read -= read_bytes; buf_pos += read_bytes; @@ -1590,7 +1591,7 @@ namespace libtorrent { std::vector piece_data(m_info.piece_length()); std::size_t piece_offset = 0; - std::size_t current_piece = 0; + int current_piece = 0; std::size_t bytes_to_read = piece_size; std::size_t bytes_current_read = 0; std::size_t seek_into_next = 0; @@ -1679,6 +1680,7 @@ namespace libtorrent { { m_unallocated_slots.push_back(current_piece); ++current_piece; + assert(current_piece <= m_info.num_pieces()); } seek_into_next = pos - file_end; diff --git a/src/torrent.cpp b/src/torrent.cpp index b24ba7f5b..f8af30876 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -376,12 +376,12 @@ namespace libtorrent const entry::dictionary_type& msg = e.dict(); i = msg.find("interval"); - if (i == msg.end()) throw std::runtime_error("invalid response from tracker"); + if (i == msg.end()) throw std::runtime_error("invalid response from tracker (no interval)"); m_duration = i->second.integer(); i = msg.find("peers"); - if (i == msg.end()) throw std::runtime_error("invalid response from tracker"); + if (i == msg.end()) throw std::runtime_error("invalid response from tracker (no peers)"); peer_list.clear();