keep track of last "have" for each torrent

This commit is contained in:
Miguel Freitas 2013-09-18 08:47:26 -03:00
parent f631bdf98d
commit 98903d5a73
7 changed files with 38 additions and 2 deletions

View File

@ -212,8 +212,8 @@ namespace libtorrent
// sets all pieces to dont-have // sets all pieces to dont-have
void init(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces); void init(int blocks_per_piece, int blocks_in_last_piece, int total_num_pieces);
void increase_num_pieces(int total_num_pieces); void increase_num_pieces(int total_num_pieces);
int num_pieces() const { return int(m_piece_map.size()); } int num_pieces() const { return int(m_piece_map.size()); }
bool have_piece(int index) const bool have_piece(int index) const
{ {
@ -357,6 +357,8 @@ namespace libtorrent
int num_have() const { return m_num_have; } int num_have() const { return m_num_have; }
int last_have() const { return m_last_have; }
// the number of pieces we want and don't have // the number of pieces we want and don't have
int num_want_left() const { return num_pieces() - m_num_have - m_num_filtered; } int num_want_left() const { return num_pieces() - m_num_have - m_num_filtered; }
@ -611,6 +613,9 @@ namespace libtorrent
// the number of pieces we have // the number of pieces we have
int m_num_have; int m_num_have;
// the index of last have
int m_last_have;
// we have all pieces in the range [0, m_cursor) // we have all pieces in the range [0, m_cursor)
// m_cursor is the first piece we don't have // m_cursor is the first piece we don't have
int m_cursor; int m_cursor;

View File

@ -529,6 +529,13 @@ namespace libtorrent
: m_torrent_file->num_pieces(); : m_torrent_file->num_pieces();
} }
int last_have() const
{
return has_picker()
? m_picker->last_have()
: -1;
}
// when we get a have message, this is called for that piece // when we get a have message, this is called for that piece
void peer_has(int index, peer_connection const* peer) void peer_has(int index, peer_connection const* peer)
{ {

View File

@ -558,6 +558,9 @@ namespace libtorrent
// std::accumulate(pieces->begin(), pieces->end()); // std::accumulate(pieces->begin(), pieces->end());
int num_pieces; int num_pieces;
// index of last piece we have
int last_have;
// the number of bytes of the file we have // the number of bytes of the file we have
// including pieces that may have been filtered // including pieces that may have been filtered
// after we downloaded them // after we downloaded them

View File

@ -71,6 +71,7 @@ namespace libtorrent
, m_num_filtered(0) , m_num_filtered(0)
, m_num_have_filtered(0) , m_num_have_filtered(0)
, m_num_have(0) , m_num_have(0)
, m_last_have(-1)
, m_cursor(0) , m_cursor(0)
, m_reverse_cursor(0) , m_reverse_cursor(0)
, m_sparse_regions(1) , m_sparse_regions(1)
@ -104,6 +105,7 @@ namespace libtorrent
m_num_filtered += m_num_have_filtered; m_num_filtered += m_num_have_filtered;
m_num_have_filtered = 0; m_num_have_filtered = 0;
m_num_have = 0; m_num_have = 0;
m_last_have = -1;
m_dirty = true; m_dirty = true;
for (std::vector<piece_pos>::iterator i = m_piece_map.begin() for (std::vector<piece_pos>::iterator i = m_piece_map.begin()
, end(m_piece_map.end()); i != end; ++i) , end(m_piece_map.end()); i != end; ++i)
@ -1224,6 +1226,11 @@ namespace libtorrent
--m_num_have; --m_num_have;
p.set_not_have(); p.set_not_have();
if (index == m_last_have) {
while( !m_piece_map[m_last_have].have() && m_last_have >= 0 )
m_last_have--;
}
if (m_dirty) return; if (m_dirty) return;
if (p.priority(this) >= 0) add(index); if (p.priority(this) >= 0) add(index);
} }
@ -1288,6 +1295,8 @@ namespace libtorrent
} }
++m_num_have; ++m_num_have;
p.set_have(); p.set_have();
if (index > m_last_have)
m_last_have = index;
if (m_cursor == m_reverse_cursor - 1 && if (m_cursor == m_reverse_cursor - 1 &&
m_cursor == index) m_cursor == index)
{ {

View File

@ -8537,6 +8537,7 @@ namespace libtorrent
st->pieces.resize(num_pieces, true); st->pieces.resize(num_pieces, true);
} }
st->num_pieces = num_have(); st->num_pieces = num_have();
st->last_have = last_have();
st->num_seeds = num_seeds(); st->num_seeds = num_seeds();
if ((flags & torrent_handle::query_distributed_copies) && m_picker.get()) if ((flags & torrent_handle::query_distributed_copies) && m_picker.get())
{ {

View File

@ -104,6 +104,7 @@ namespace libtorrent
, list_peers(0) , list_peers(0)
, connect_candidates(0) , connect_candidates(0)
, num_pieces(0) , num_pieces(0)
, last_have(-1)
, total_done(0) , total_done(0)
, total_wanted_done(0) , total_wanted_done(0)
, total_wanted(0) , total_wanted(0)

View File

@ -147,10 +147,20 @@ torrent_handle startTorrentUser(std::string const &username)
m_userTorrent[username] = ses->add_torrent(tparams); m_userTorrent[username] = ses->add_torrent(tparams);
m_userTorrent[username].force_dht_announce(); m_userTorrent[username].force_dht_announce();
torrent_status status = m_userTorrent[username].status();
} }
return m_userTorrent[username]; return m_userTorrent[username];
} }
int lastPostKfromTorrent(std::string const &username)
{
if( !m_userTorrent.count(username) )
return -1;
torrent_status status = m_userTorrent[username].status();
return status.last_have;
}
void ThreadWaitExtIP() void ThreadWaitExtIP()
{ {
RenameThread("wait-extip"); RenameThread("wait-extip");