From 354b31dbbecb46aeaa9c5139b4f49da701b63ec1 Mon Sep 17 00:00:00 2001 From: Andrew Resch Date: Sun, 20 Apr 2008 00:39:26 +0000 Subject: [PATCH] lt sync 2205 --- libtorrent/include/libtorrent/lazy_entry.hpp | 13 +++++- libtorrent/src/lazy_bdecode.cpp | 44 +++++++++++++++++++- libtorrent/src/policy.cpp | 19 ++++++--- 3 files changed, 67 insertions(+), 9 deletions(-) diff --git a/libtorrent/include/libtorrent/lazy_entry.hpp b/libtorrent/include/libtorrent/lazy_entry.hpp index 4c47b4a43..5d4eedf06 100644 --- a/libtorrent/include/libtorrent/lazy_entry.hpp +++ b/libtorrent/include/libtorrent/lazy_entry.hpp @@ -36,7 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include "libtorrent/assert.hpp" -#include +#include "libtorrent/size_type.hpp" #include namespace libtorrent @@ -71,7 +71,7 @@ namespace libtorrent m_end = start + length + 1; // include 'e' } - boost::int64_t int_value() const; + size_type int_value() const; // string functions // ================ @@ -119,6 +119,12 @@ namespace libtorrent lazy_entry* dict_find(char const* name); lazy_entry const* dict_find(char const* name) const { return const_cast(this)->dict_find(name); } + + std::string dict_find_string_value(char const* name) const; + size_type dict_find_int_value(char const* name, size_type default_val = 0) const; + lazy_entry const* dict_find_dict(char const* name) const; + lazy_entry const* dict_find_list(char const* name) const; + std::pair dict_at(int i) const { TORRENT_ASSERT(m_type == dict_t); @@ -154,6 +160,9 @@ namespace libtorrent lazy_entry const* list_at(int i) const { return const_cast(this)->list_at(i); } + std::string list_string_value_at(int i) const; + size_type list_int_value_at(int i, size_type default_val = 0) const; + int list_size() const { TORRENT_ASSERT(m_type == list_t); diff --git a/libtorrent/src/lazy_bdecode.cpp b/libtorrent/src/lazy_bdecode.cpp index 2e2662585..b5e2b11e7 100644 --- a/libtorrent/src/lazy_bdecode.cpp +++ b/libtorrent/src/lazy_bdecode.cpp @@ -158,7 +158,7 @@ namespace libtorrent return 0; } - boost::int64_t lazy_entry::int_value() const + size_type lazy_entry::int_value() const { TORRENT_ASSERT(m_type == int_t); boost::int64_t val = 0; @@ -241,6 +241,34 @@ namespace libtorrent } } + std::string lazy_entry::dict_find_string_value(char const* name) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::string_t) return std::string(); + return e->string_value(); + } + + size_type lazy_entry::dict_find_int_value(char const* name, size_type default_val) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::int_t) return default_val; + return e->int_value(); + } + + lazy_entry const* lazy_entry::dict_find_dict(char const* name) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::dict_t) return 0; + return e; + } + + lazy_entry const* lazy_entry::dict_find_list(char const* name) const + { + lazy_entry const* e = dict_find(name); + if (e == 0 || e->type() != lazy_entry::list_t) return 0; + return e; + } + lazy_entry* lazy_entry::dict_find(char const* name) { TORRENT_ASSERT(m_type == dict_t); @@ -279,6 +307,20 @@ namespace libtorrent return m_data.list + (m_size++); } + std::string lazy_entry::list_string_value_at(int i) const + { + lazy_entry const* e = list_at(i); + if (e == 0 || e->type() != lazy_entry::string_t) return std::string(); + return e->string_value(); + } + + size_type lazy_entry::list_int_value_at(int i, size_type default_val) const + { + lazy_entry const* e = list_at(i); + if (e == 0 || e->type() != lazy_entry::int_t) return default_val; + return e->int_value(); + } + void lazy_entry::clear() { switch (m_type) diff --git a/libtorrent/src/policy.cpp b/libtorrent/src/policy.cpp index 94c9ad549..02dd5cd93 100755 --- a/libtorrent/src/policy.cpp +++ b/libtorrent/src/policy.cpp @@ -450,8 +450,7 @@ namespace libtorrent policy::iterator policy::find_connect_candidate() { -// too expensive -// INVARIANT_CHECK; + INVARIANT_CHECK; ptime now = time_now(); ptime min_connect_time(now); @@ -834,8 +833,15 @@ namespace libtorrent { TORRENT_ASSERT(m_peers.count(p->ip.address()) == 1); } + bool was_conn_cand = is_connect_candidate(*p, m_torrent->is_finished()); p->ip.port(port); p->source |= src; + + if (was_conn_cand != is_connect_candidate(*p, m_torrent->is_finished())) + { + m_num_connect_candidates += was_conn_cand ? -1 : 1; + if (m_num_connect_candidates < 0) m_num_connect_candidates = 0; + } return true; } @@ -853,8 +859,7 @@ namespace libtorrent policy::peer* policy::peer_from_tracker(tcp::endpoint const& remote, peer_id const& pid , int src, char flags) { -// too expensive -// INVARIANT_CHECK; + INVARIANT_CHECK; // just ignore the obviously invalid entries if (remote.address() == address() || remote.port() == 0) @@ -963,8 +968,10 @@ namespace libtorrent #endif if (was_conn_cand != is_connect_candidate(i->second, m_torrent->is_finished())) - if (was_conn_cand) --m_num_connect_candidates; - else ++m_num_connect_candidates; + { + m_num_connect_candidates += was_conn_cand ? -1 : 1; + if (m_num_connect_candidates < 0) m_num_connect_candidates = 0; + } } return &i->second;