lt sync 2374

This commit is contained in:
Marcos Pinto 2008-06-03 20:24:42 +00:00
parent 6b6a2646b9
commit e78aa7dcc3
5 changed files with 43 additions and 12 deletions

View File

@ -170,6 +170,7 @@ namespace libtorrent
unfinished_pieces); unfinished_pieces);
stat statistics() const { return m_stat; } stat statistics() const { return m_stat; }
void add_stats(stat const& s) { m_stat += s; }
size_type bytes_left() const; size_type bytes_left() const;
boost::tuples::tuple<size_type, size_type> bytes_done() const; boost::tuples::tuple<size_type, size_type> bytes_done() const;
size_type quantized_bytes_done() const; size_type quantized_bytes_done() const;

View File

@ -174,6 +174,9 @@ namespace libtorrent
// response. used to know where in the buffer the // response. used to know where in the buffer the
// next response starts // next response starts
int m_received_body; int m_received_body;
// position in the current range response
int m_range_pos;
}; };
} }

View File

@ -121,9 +121,10 @@ namespace libtorrent
{ {
TORRENT_ASSERT(recv_buffer.left() >= m_recv_buffer.left()); TORRENT_ASSERT(recv_buffer.left() >= m_recv_buffer.left());
boost::tuple<int, int> ret(0, 0); boost::tuple<int, int> ret(0, 0);
int start_pos = m_recv_buffer.left();
// early exit if there's nothing new in the receive buffer // 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; m_recv_buffer = recv_buffer;
char const* pos = recv_buffer.begin + m_recv_pos; char const* pos = recv_buffer.begin + m_recv_pos;
@ -132,7 +133,11 @@ namespace libtorrent
TORRENT_ASSERT(!m_finished); TORRENT_ASSERT(!m_finished);
char const* newline = std::find(pos, recv_buffer.end, '\n'); char const* newline = std::find(pos, recv_buffer.end, '\n');
// if we don't have a full line yet, wait. // 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) if (newline == pos)
throw std::runtime_error("unexpected newline in HTTP response"); throw std::runtime_error("unexpected newline in HTTP response");
@ -144,7 +149,7 @@ namespace libtorrent
++newline; ++newline;
int incoming = (int)std::distance(pos, newline); int incoming = (int)std::distance(pos, newline);
m_recv_pos += incoming; m_recv_pos += incoming;
boost::get<1>(ret) += incoming; boost::get<1>(ret) += newline - (m_recv_buffer.begin + start_pos);
pos = newline; pos = newline;
line >> m_protocol; line >> m_protocol;
@ -162,6 +167,7 @@ namespace libtorrent
m_status_code = 0; m_status_code = 0;
} }
m_state = read_header; m_state = read_header;
start_pos = pos - recv_buffer.begin;
} }
if (m_state == read_header) if (m_state == read_header)
@ -179,7 +185,6 @@ namespace libtorrent
line.assign(pos, line_end); line.assign(pos, line_end);
++newline; ++newline;
m_recv_pos += newline - pos; m_recv_pos += newline - pos;
boost::get<1>(ret) += newline - pos;
pos = newline; pos = newline;
std::string::size_type separator = line.find(':'); std::string::size_type separator = line.find(':');
@ -234,6 +239,7 @@ namespace libtorrent
TORRENT_ASSERT(m_recv_pos <= (int)recv_buffer.left()); TORRENT_ASSERT(m_recv_pos <= (int)recv_buffer.left());
newline = std::find(pos, recv_buffer.end, '\n'); newline = std::find(pos, recv_buffer.end, '\n');
} }
boost::get<1>(ret) += newline - (m_recv_buffer.begin + start_pos);
} }
if (m_state == read_body) if (m_state == read_body)

View File

@ -2023,6 +2023,9 @@ namespace libtorrent
if (t) if (t)
{ {
// make sure we keep all the stats!
t->add_stats(statistics());
if (t->has_picker()) if (t->has_picker())
{ {
piece_picker& picker = t->picker(); piece_picker& picker = t->picker();

View File

@ -66,6 +66,7 @@ namespace libtorrent
: peer_connection(ses, t, s, remote, peerinfo) : peer_connection(ses, t, s, remote, peerinfo)
, m_url(url) , m_url(url)
, m_first_request(true) , m_first_request(true)
, m_range_pos(0)
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
@ -345,14 +346,20 @@ namespace libtorrent
if (!header_finished) if (!header_finished)
{ {
boost::tie(payload, protocol) = m_parser.incoming(recv_buffer); 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() == 0 || *recv_buffer.begin == 'H');
TORRENT_ASSERT(recv_buffer.left() <= packet_size()); TORRENT_ASSERT(recv_buffer.left() <= packet_size());
// this means the entire status line hasn't been received yet // 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 the status code is not one of the accepted ones, abort
if (m_parser.status_code() != 206 // partial content if (m_parser.status_code() != 206 // partial content
@ -376,15 +383,16 @@ namespace libtorrent
} }
throw std::runtime_error(error_msg); 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_body_start = m_parser.body_start();
m_received_body = 0; m_received_body = 0;
} }
else
{
m_statistics.received_bytes(bytes_transferred, 0);
}
// we just completed reading the header // we just completed reading the header
if (!header_finished) if (!header_finished)
@ -441,9 +449,11 @@ namespace libtorrent
m_body_start = m_parser.body_start(); m_body_start = m_parser.body_start();
m_received_body = 0; m_received_body = 0;
m_range_pos = 0;
} }
recv_buffer.begin += m_body_start; recv_buffer.begin += m_body_start;
// we only received the header, no data // we only received the header, no data
if (recv_buffer.left() == 0) break; 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() // std::cerr << "REQUESTS: m_requests: " << m_requests.size()
// << " file_requests: " << m_file_requests.size() << std::endl; // << " file_requests: " << m_file_requests.size() << std::endl;
@ -614,8 +631,9 @@ namespace libtorrent
m_received_body = 0; m_received_body = 0;
continue; continue;
} }
break; if (bytes_transferred == 0) break;
} }
TORRENT_ASSERT(bytes_transferred == 0);
} }
void web_peer_connection::get_specific_peer_info(peer_info& p) const void web_peer_connection::get_specific_peer_info(peer_info& p) const