fixed runtime-check that msvc adds if that option is checked. Fixed problem that occured if the max_transactions limit was reached.

This commit is contained in:
Arvid Norberg 2006-08-01 22:23:05 +00:00
parent e340485974
commit 2d83cdfa59
6 changed files with 41 additions and 16 deletions

View File

@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent { namespace dht namespace libtorrent { namespace dht
{ {
#ifdef TORRENT_DHT_VERBOSE_LOGGING #ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_DECLARE_LOG(refresh); TORRENT_DECLARE_LOG(refresh);
#endif #endif

View File

@ -161,7 +161,7 @@ public:
private: private:
enum { max_transactions = 1024 }; enum { max_transactions = 2048 };
unsigned int new_transaction_id(); unsigned int new_transaction_id();
void update_oldest_transaction_id(); void update_oldest_transaction_id();
@ -185,8 +185,6 @@ private:
routing_table& m_table; routing_table& m_table;
boost::posix_time::ptime m_timer; boost::posix_time::ptime m_timer;
node_id m_random_number; node_id m_random_number;
// a cache of connection ids
std::map<udp::endpoint, boost::uint32_t> m_connection_id;
}; };
} } // namespace libtorrent::dht } } // namespace libtorrent::dht

View File

@ -59,9 +59,16 @@ using boost::posix_time::time_duration;
namespace libtorrent { namespace dht namespace libtorrent { namespace dht
{ {
#ifdef _MSC_VER
namespace
{
char rand() { return (char)std::rand(); }
}
#endif
typedef boost::shared_ptr<observer> observer_ptr; typedef boost::shared_ptr<observer> observer_ptr;
// TODO: configurable // TODO: configurable?
enum { announce_interval = 30 }; enum { announce_interval = 30 };
using asio::ip::udp; using asio::ip::udp;
@ -74,7 +81,11 @@ node_id generate_id()
{ {
char random[20]; char random[20];
std::srand(std::time(0)); std::srand(std::time(0));
#ifdef _MSC_VER
std::generate(random, random + 20, &rand);
#else
std::generate(random, random + 20, &std::rand); std::generate(random, random + 20, &std::rand);
#endif
hasher h; hasher h;
h.update(random, 20); h.update(random, 20);

View File

@ -46,7 +46,9 @@ namespace libtorrent { namespace dht
using asio::ip::udp; using asio::ip::udp;
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_DEFINE_LOG(refresh) TORRENT_DEFINE_LOG(refresh)
#endif
typedef boost::shared_ptr<observer> observer_ptr; typedef boost::shared_ptr<observer> observer_ptr;
@ -125,11 +127,11 @@ void ping_observer::timeout()
m_algorithm->ping_timeout(m_self); m_algorithm->ping_timeout(m_self);
} }
void refresh::invoke(node_id const& id, udp::endpoint addr) void refresh::invoke(node_id const& nid, udp::endpoint addr)
{ {
observer_ptr p(new refresh_observer( observer_ptr p(new refresh_observer(
this this
, id , nid
, m_target , m_target
)); ));
@ -144,13 +146,13 @@ void refresh::done()
invoke_pings_or_finish(); invoke_pings_or_finish();
} }
void refresh::ping_reply(node_id id) void refresh::ping_reply(node_id nid)
{ {
m_active_pings--; m_active_pings--;
invoke_pings_or_finish(); invoke_pings_or_finish();
} }
void refresh::ping_timeout(node_id id) void refresh::ping_timeout(node_id nid)
{ {
m_active_pings--; m_active_pings--;
invoke_pings_or_finish(); invoke_pings_or_finish();

View File

@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/date_time/posix_time/posix_time_types.hpp> #include <boost/date_time/posix_time/posix_time_types.hpp>
#include <boost/date_time/posix_time/ptime.hpp> #include <boost/date_time/posix_time/ptime.hpp>
#include <boost/bind.hpp>
#include <libtorrent/io.hpp> #include <libtorrent/io.hpp>
#include <libtorrent/invariant_check.hpp> #include <libtorrent/invariant_check.hpp>
@ -48,6 +49,7 @@ using boost::posix_time::microsec_clock;
using boost::posix_time::seconds; using boost::posix_time::seconds;
using boost::posix_time::milliseconds; using boost::posix_time::milliseconds;
using boost::shared_ptr; using boost::shared_ptr;
using boost::bind;
namespace libtorrent { namespace dht namespace libtorrent { namespace dht
{ {
@ -88,9 +90,10 @@ void rpc_manager::check_invariant() const
assert(m_oldest_transaction_id < max_transactions); assert(m_oldest_transaction_id < max_transactions);
assert(m_next_transaction_id >= 0); assert(m_next_transaction_id >= 0);
assert(m_next_transaction_id < max_transactions); assert(m_next_transaction_id < max_transactions);
assert(!m_transactions[m_next_transaction_id]);
for (int i = m_next_transaction_id; i != m_oldest_transaction_id; for (int i = (m_next_transaction_id + 1) % max_transactions;
i = (i + 1) % max_transactions) i != m_oldest_transaction_id; i = (i + 1) % max_transactions)
{ {
assert(!m_transactions[i]); assert(!m_transactions[i]);
} }
@ -198,7 +201,7 @@ time_duration rpc_manager::tick()
assert(m_oldest_transaction_id >= 0); assert(m_oldest_transaction_id >= 0);
assert(m_oldest_transaction_id < max_transactions); assert(m_oldest_transaction_id < max_transactions);
boost::shared_ptr<observer>& o = m_transactions[m_oldest_transaction_id]; boost::shared_ptr<observer> o = m_transactions[m_oldest_transaction_id];
if (!o) continue; if (!o) continue;
time_duration diff = o->sent + milliseconds(timeout_ms) time_duration diff = o->sent + milliseconds(timeout_ms)
@ -209,8 +212,8 @@ time_duration rpc_manager::tick()
return diff; return diff;
} }
m_transactions[m_oldest_transaction_id].reset();
o->timeout(); o->timeout();
o.reset();
} }
return milliseconds(timeout_ms); return milliseconds(timeout_ms);
} }
@ -221,16 +224,19 @@ unsigned int rpc_manager::new_transaction_id()
unsigned int tid = m_next_transaction_id; unsigned int tid = m_next_transaction_id;
m_next_transaction_id = (m_next_transaction_id + 1) % max_transactions; m_next_transaction_id = (m_next_transaction_id + 1) % max_transactions;
assert(!m_transactions[tid]); // boost::shared_ptr<observer> o = m_transactions[m_next_transaction_id];
if (m_transactions[m_next_transaction_id]) if (m_transactions[m_next_transaction_id])
{ {
m_transactions[m_next_transaction_id]->timeout();
m_transactions[m_next_transaction_id].reset(); m_transactions[m_next_transaction_id].reset();
assert(m_oldest_transaction_id == m_next_transaction_id); assert(m_oldest_transaction_id == m_next_transaction_id);
} }
if (m_oldest_transaction_id == m_next_transaction_id) if (m_oldest_transaction_id == m_next_transaction_id)
{ {
m_oldest_transaction_id = (m_oldest_transaction_id + 1) % max_transactions; m_oldest_transaction_id = (m_oldest_transaction_id + 1) % max_transactions;
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(rpc) << "WARNING: transaction limit reached! Too many concurrent"
" messages! limit: " << (int)max_transactions;
#endif
update_oldest_transaction_id(); update_oldest_transaction_id();
} }
@ -243,6 +249,12 @@ unsigned int rpc_manager::new_transaction_id()
} }
#endif #endif
// hopefully this wouldn't happen, but unfortunately, the
// traversal algorithm will simply fail in case its connections
// are overwritten. If timeout() is called, it will likely spawn
// another connection, which in turn will close the next one
// and so on.
// if (o) o->timeout();
return tid; return tid;
} }
@ -250,6 +262,7 @@ void rpc_manager::update_oldest_transaction_id()
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
assert(m_oldest_transaction_id != m_next_transaction_id);
while (!m_transactions[m_oldest_transaction_id]) while (!m_transactions[m_oldest_transaction_id])
{ {
m_oldest_transaction_id = (m_oldest_transaction_id + 1) m_oldest_transaction_id = (m_oldest_transaction_id + 1)

View File

@ -79,7 +79,7 @@ void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr)
void traversal_algorithm::finished(node_id const& id) void traversal_algorithm::finished(node_id const& id)
{ {
m_invoke_count--; --m_invoke_count;
add_requests(); add_requests();
if (m_invoke_count == 0) done(); if (m_invoke_count == 0) done();
} }
@ -114,7 +114,7 @@ void traversal_algorithm::failed(node_id const& id)
void traversal_algorithm::add_request(node_id const& id, udp::endpoint addr) void traversal_algorithm::add_request(node_id const& id, udp::endpoint addr)
{ {
invoke(id, addr); invoke(id, addr);
m_invoke_count++; ++m_invoke_count;
} }
namespace namespace