From e78aa7dcc378c33cfbcb8d50f94ed6e53ed8cc2a Mon Sep 17 00:00:00 2001 From: Marcos Pinto Date: Tue, 3 Jun 2008 20:24:42 +0000 Subject: [PATCH] lt sync 2374 --- libtorrent/include/libtorrent/torrent.hpp | 1 + .../libtorrent/web_peer_connection.hpp | 3 ++ libtorrent/src/http_tracker_connection.cpp | 14 +++++--- libtorrent/src/peer_connection.cpp | 3 ++ libtorrent/src/web_peer_connection.cpp | 34 ++++++++++++++----- 5 files changed, 43 insertions(+), 12 deletions(-) diff --git a/libtorrent/include/libtorrent/torrent.hpp b/libtorrent/include/libtorrent/torrent.hpp index 842cc7cb7..bdef2918a 100755 --- a/libtorrent/include/libtorrent/torrent.hpp +++ b/libtorrent/include/libtorrent/torrent.hpp @@ -170,6 +170,7 @@ namespace libtorrent unfinished_pieces); stat statistics() const { return m_stat; } + void add_stats(stat const& s) { m_stat += s; } size_type bytes_left() const; boost::tuples::tuple bytes_done() const; size_type quantized_bytes_done() const; diff --git a/libtorrent/include/libtorrent/web_peer_connection.hpp b/libtorrent/include/libtorrent/web_peer_connection.hpp index 742d823f0..10161370f 100755 --- a/libtorrent/include/libtorrent/web_peer_connection.hpp +++ b/libtorrent/include/libtorrent/web_peer_connection.hpp @@ -174,6 +174,9 @@ namespace libtorrent // response. used to know where in the buffer the // next response starts int m_received_body; + + // position in the current range response + int m_range_pos; }; } diff --git a/libtorrent/src/http_tracker_connection.cpp b/libtorrent/src/http_tracker_connection.cpp index 9fa870ed7..5c14d818a 100755 --- a/libtorrent/src/http_tracker_connection.cpp +++ b/libtorrent/src/http_tracker_connection.cpp @@ -121,9 +121,10 @@ namespace libtorrent { TORRENT_ASSERT(recv_buffer.left() >= m_recv_buffer.left()); boost::tuple ret(0, 0); + int start_pos = m_recv_buffer.left(); // early exit if there's nothing new in the receive buffer - if (recv_buffer.left() == m_recv_buffer.left()) return ret; + if (start_pos == recv_buffer.left()) return ret; m_recv_buffer = recv_buffer; char const* pos = recv_buffer.begin + m_recv_pos; @@ -132,7 +133,11 @@ namespace libtorrent TORRENT_ASSERT(!m_finished); char const* newline = std::find(pos, recv_buffer.end, '\n'); // if we don't have a full line yet, wait. - if (newline == recv_buffer.end) return ret; + if (newline == recv_buffer.end) + { + boost::get<1>(ret) += m_recv_buffer.left() - start_pos; + return ret; + } if (newline == pos) throw std::runtime_error("unexpected newline in HTTP response"); @@ -144,7 +149,7 @@ namespace libtorrent ++newline; int incoming = (int)std::distance(pos, newline); m_recv_pos += incoming; - boost::get<1>(ret) += incoming; + boost::get<1>(ret) += newline - (m_recv_buffer.begin + start_pos); pos = newline; line >> m_protocol; @@ -162,6 +167,7 @@ namespace libtorrent m_status_code = 0; } m_state = read_header; + start_pos = pos - recv_buffer.begin; } if (m_state == read_header) @@ -179,7 +185,6 @@ namespace libtorrent line.assign(pos, line_end); ++newline; m_recv_pos += newline - pos; - boost::get<1>(ret) += newline - pos; pos = newline; std::string::size_type separator = line.find(':'); @@ -234,6 +239,7 @@ namespace libtorrent TORRENT_ASSERT(m_recv_pos <= (int)recv_buffer.left()); newline = std::find(pos, recv_buffer.end, '\n'); } + boost::get<1>(ret) += newline - (m_recv_buffer.begin + start_pos); } if (m_state == read_body) diff --git a/libtorrent/src/peer_connection.cpp b/libtorrent/src/peer_connection.cpp index e2df0cc37..d07ed5dee 100755 --- a/libtorrent/src/peer_connection.cpp +++ b/libtorrent/src/peer_connection.cpp @@ -2023,6 +2023,9 @@ namespace libtorrent if (t) { + // make sure we keep all the stats! + t->add_stats(statistics()); + if (t->has_picker()) { piece_picker& picker = t->picker(); diff --git a/libtorrent/src/web_peer_connection.cpp b/libtorrent/src/web_peer_connection.cpp index e79f6b9bf..71b4b1082 100755 --- a/libtorrent/src/web_peer_connection.cpp +++ b/libtorrent/src/web_peer_connection.cpp @@ -66,6 +66,7 @@ namespace libtorrent : peer_connection(ses, t, s, remote, peerinfo) , m_url(url) , m_first_request(true) + , m_range_pos(0) { INVARIANT_CHECK; @@ -345,14 +346,20 @@ namespace libtorrent if (!header_finished) { boost::tie(payload, protocol) = m_parser.incoming(recv_buffer); - m_statistics.received_bytes(payload, protocol); + m_statistics.received_bytes(0, protocol); + bytes_transferred -= protocol; TORRENT_ASSERT(recv_buffer.left() == 0 || *recv_buffer.begin == 'H'); TORRENT_ASSERT(recv_buffer.left() <= packet_size()); // this means the entire status line hasn't been received yet - if (m_parser.status_code() == -1) break; + if (m_parser.status_code() == -1) + { + TORRENT_ASSERT(payload == 0); + TORRENT_ASSERT(bytes_transferred == 0); + break; + } // if the status code is not one of the accepted ones, abort if (m_parser.status_code() != 206 // partial content @@ -376,15 +383,16 @@ namespace libtorrent } throw std::runtime_error(error_msg); } - if (!m_parser.header_finished()) break; + if (!m_parser.header_finished()) + { + TORRENT_ASSERT(payload == 0); + TORRENT_ASSERT(bytes_transferred == 0); + break; + } m_body_start = m_parser.body_start(); m_received_body = 0; } - else - { - m_statistics.received_bytes(bytes_transferred, 0); - } // we just completed reading the header if (!header_finished) @@ -441,9 +449,11 @@ namespace libtorrent m_body_start = m_parser.body_start(); m_received_body = 0; + m_range_pos = 0; } recv_buffer.begin += m_body_start; + // we only received the header, no data if (recv_buffer.left() == 0) break; @@ -476,6 +486,13 @@ namespace libtorrent } } + int left_in_response = range_end - range_start - m_range_pos; + int payload_transferred = (std::min)(left_in_response, int(bytes_transferred)); + m_statistics.received_bytes(payload_transferred, 0); + bytes_transferred -= payload_transferred; + m_range_pos += payload_transferred;; + if (m_range_pos > range_end - range_start) m_range_pos = range_end - range_start; + // std::cerr << "REQUESTS: m_requests: " << m_requests.size() // << " file_requests: " << m_file_requests.size() << std::endl; @@ -614,8 +631,9 @@ namespace libtorrent m_received_body = 0; continue; } - break; + if (bytes_transferred == 0) break; } + TORRENT_ASSERT(bytes_transferred == 0); } void web_peer_connection::get_specific_peer_info(peer_info& p) const