lt sync 2116

This commit is contained in:
Marcos Pinto 2008-03-27 22:42:39 +00:00
parent 6b0952764d
commit 86856b44de
11 changed files with 62 additions and 24 deletions

View File

@ -45,6 +45,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include <boost/filesystem/operations.hpp> #include <boost/filesystem/operations.hpp>
#include <boost/intrusive_ptr.hpp> #include <boost/intrusive_ptr.hpp>
#include <boost/detail/atomic_count.hpp> #include <boost/detail/atomic_count.hpp>
#include <boost/thread/mutex.hpp>
#include "libtorrent/kademlia/node.hpp" #include "libtorrent/kademlia/node.hpp"
#include "libtorrent/kademlia/node_id.hpp" #include "libtorrent/kademlia/node_id.hpp"
@ -125,6 +126,12 @@ namespace libtorrent { namespace dht
dht_settings const& m_settings; dht_settings const& m_settings;
int m_refresh_bucket; int m_refresh_bucket;
// The mutex is used to abort the dht node
// it's only used to set m_abort to true
typedef boost::mutex mutex_t;
mutable mutex_t m_mutex;
bool m_abort;
// used to resolve hostnames for nodes // used to resolve hostnames for nodes
udp::resolver m_host_resolver; udp::resolver m_host_resolver;

View File

@ -84,12 +84,11 @@ protected:
struct result struct result
{ {
result(node_id const& id, udp::endpoint addr, unsigned char f = 0) result(node_id const& id, udp::endpoint addr, unsigned char f = 0)
: id(id), addr(addr), flags(f) : id(id), addr(addr), flags(f) {}
{}
node_id id; node_id id;
udp::endpoint addr; udp::endpoint addr;
enum { queried = 1, initial = 2 }; enum { queried = 1, initial = 2, no_id = 4 };
unsigned char flags; unsigned char flags;
}; };
@ -150,7 +149,7 @@ traversal_algorithm::traversal_algorithm(
for (routing_table::router_iterator i = table.router_begin() for (routing_table::router_iterator i = table.router_begin()
, end(table.router_end()); i != end; ++i) , end(table.router_end()); i != end; ++i)
{ {
add_entry(generate_id(), *i, result::initial); add_entry(node_id(0), *i, result::initial);
} }
} }

View File

@ -1594,11 +1594,6 @@ namespace libtorrent
, boost::bind(&session_impl::free_disk_buffer , boost::bind(&session_impl::free_disk_buffer
, boost::ref(m_ses), _1)); , boost::ref(m_ses), _1));
/*
buffer::interval i = allocate_send_buffer(r.length);
std::memcpy(i.begin, buffer, r.length);
t->filesystem().free_buffer(buffer);
*/
m_payloads.push_back(range(send_buffer_size() - r.length, r.length)); m_payloads.push_back(range(send_buffer_size() - r.length, r.length));
setup_send(); setup_send();
} }

View File

@ -110,12 +110,14 @@ void closest_nodes::invoke(node_id const& id, udp::endpoint addr)
void closest_nodes::done() void closest_nodes::done()
{ {
std::vector<node_entry> results; std::vector<node_entry> results;
int result_size = m_table.bucket_size(); int num_results = m_max_results;
if (result_size > (int)m_results.size()) result_size = (int)m_results.size();
for (std::vector<result>::iterator i = m_results.begin() for (std::vector<result>::iterator i = m_results.begin()
, end(m_results.begin() + result_size); i != end; ++i) , end(m_results.end()); i != end && num_results >= 0; ++i)
{ {
if (i->flags & result::no_id) continue;
if ((i->flags & result::queried) == 0) continue;
results.push_back(node_entry(i->id, i->addr)); results.push_back(node_entry(i->id, i->addr));
--num_results;
} }
m_done_callback(results); m_done_callback(results);
} }

View File

@ -158,6 +158,7 @@ namespace libtorrent { namespace dht
, m_refresh_timer(ios) , m_refresh_timer(ios)
, m_settings(settings) , m_settings(settings)
, m_refresh_bucket(160) , m_refresh_bucket(160)
, m_abort(false)
, m_host_resolver(ios) , m_host_resolver(ios)
, m_refs(0) , m_refs(0)
{ {
@ -220,6 +221,8 @@ namespace libtorrent { namespace dht
void dht_tracker::stop() void dht_tracker::stop()
{ {
mutex_t::scoped_lock l(m_mutex);
m_abort = true;
m_timer.cancel(); m_timer.cancel();
m_connection_timer.cancel(); m_connection_timer.cancel();
m_refresh_timer.cancel(); m_refresh_timer.cancel();
@ -237,7 +240,9 @@ namespace libtorrent { namespace dht
void dht_tracker::connection_timeout(asio::error_code const& e) void dht_tracker::connection_timeout(asio::error_code const& e)
try try
{ {
if (e) return; mutex_t::scoped_lock l(m_mutex);
if (e || m_abort) return;
if (!m_socket.is_open()) return; if (!m_socket.is_open()) return;
time_duration d = m_dht.connection_timeout(); time_duration d = m_dht.connection_timeout();
m_connection_timer.expires_from_now(d); m_connection_timer.expires_from_now(d);
@ -255,7 +260,9 @@ namespace libtorrent { namespace dht
void dht_tracker::refresh_timeout(asio::error_code const& e) void dht_tracker::refresh_timeout(asio::error_code const& e)
try try
{ {
if (e) return; mutex_t::scoped_lock l(m_mutex);
if (e || m_abort) return;
if (!m_socket.is_open()) return; if (!m_socket.is_open()) return;
time_duration d = m_dht.refresh_timeout(); time_duration d = m_dht.refresh_timeout();
m_refresh_timer.expires_from_now(d); m_refresh_timer.expires_from_now(d);
@ -281,7 +288,9 @@ namespace libtorrent { namespace dht
void dht_tracker::tick(asio::error_code const& e) void dht_tracker::tick(asio::error_code const& e)
try try
{ {
if (e) return; mutex_t::scoped_lock l(m_mutex);
if (e || m_abort) return;
if (!m_socket.is_open()) return; if (!m_socket.is_open()) return;
m_timer.expires_from_now(minutes(tick_period)); m_timer.expires_from_now(minutes(tick_period));
m_timer.async_wait(m_strand.wrap(bind(&dht_tracker::tick, self(), _1))); m_timer.async_wait(m_strand.wrap(bind(&dht_tracker::tick, self(), _1)));

View File

@ -174,6 +174,12 @@ void node_impl::refresh(node_id const& id
void node_impl::bootstrap(std::vector<udp::endpoint> const& nodes void node_impl::bootstrap(std::vector<udp::endpoint> const& nodes
, boost::function0<void> f) , boost::function0<void> f)
{ {
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(node) << "bootrapping: " << nodes.size();
for (std::vector<udp::endpoint>::const_iterator i = nodes.begin()
, end(nodes.end()); i != end; ++i)
TORRENT_LOG(node) << " " << *i;
#endif
std::vector<node_entry> start; std::vector<node_entry> start;
start.reserve(nodes.size()); start.reserve(nodes.size());
std::copy(nodes.begin(), nodes.end(), std::back_inserter(start)); std::copy(nodes.begin(), nodes.end(), std::back_inserter(start));
@ -254,6 +260,11 @@ namespace
, int listen_port, sha1_hash const& ih , int listen_port, sha1_hash const& ih
, boost::function<void(std::vector<tcp::endpoint> const&, sha1_hash const&)> f) , boost::function<void(std::vector<tcp::endpoint> const&, sha1_hash const&)> f)
{ {
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(node) << "announce response [ ih: " << ih
<< " p: " << listen_port
<< " nodes: " << v.size() << " ]" ;
#endif
bool nodes = false; bool nodes = false;
// only store on the first k nodes // only store on the first k nodes
for (std::vector<node_entry>::const_iterator i = v.begin() for (std::vector<node_entry>::const_iterator i = v.begin()
@ -271,6 +282,9 @@ namespace
void node_impl::add_router_node(udp::endpoint router) void node_impl::add_router_node(udp::endpoint router)
{ {
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(node) << "adding router node: " << router;
#endif
m_table.add_router_node(router); m_table.add_router_node(router);
} }
@ -288,6 +302,9 @@ void node_impl::add_node(udp::endpoint node)
void node_impl::announce(sha1_hash const& info_hash, int listen_port void node_impl::announce(sha1_hash const& info_hash, int listen_port
, boost::function<void(std::vector<tcp::endpoint> const&, sha1_hash const&)> f) , boost::function<void(std::vector<tcp::endpoint> const&, sha1_hash const&)> f)
{ {
#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(node) << "announcing [ ih: " << info_hash << " p: " << listen_port << " ]" ;
#endif
// search for nodes with ids close to id, and then invoke the // search for nodes with ids close to id, and then invoke the
// get_peers and then announce_peer rpc on them. // get_peers and then announce_peer rpc on them.
closest_nodes::initiate(info_hash, m_settings.search_branching closest_nodes::initiate(info_hash, m_settings.search_branching

View File

@ -436,7 +436,8 @@ void routing_table::find_node(node_id const& target
routing_table::iterator routing_table::begin() const routing_table::iterator routing_table::begin() const
{ {
return iterator(m_buckets.begin(), m_buckets.end()); // +1 to avoid ourself
return iterator(m_buckets.begin() + 1, m_buckets.end());
} }
routing_table::iterator routing_table::end() const routing_table::iterator routing_table::end() const

View File

@ -51,7 +51,12 @@ void traversal_algorithm::add_entry(node_id const& id, udp::endpoint addr, unsig
{ {
if (m_failed.find(addr) != m_failed.end()) return; if (m_failed.find(addr) != m_failed.end()) return;
result const entry(id, addr, flags); result entry(id, addr, flags);
if (entry.id.is_all_zeros())
{
entry.id = generate_id();
entry.flags |= result::no_id;
}
std::vector<result>::iterator i = std::lower_bound( std::vector<result>::iterator i = std::lower_bound(
m_results.begin() m_results.begin()
@ -83,6 +88,7 @@ boost::pool<>& traversal_algorithm::allocator() const
void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr) void traversal_algorithm::traverse(node_id const& id, udp::endpoint addr)
{ {
TORRENT_ASSERT(!id.is_all_zeros());
add_entry(id, addr, 0); add_entry(id, addr, 0);
} }
@ -100,6 +106,7 @@ void traversal_algorithm::failed(node_id const& id, bool prevent_request)
{ {
m_invoke_count--; m_invoke_count--;
TORRENT_ASSERT(!id.is_all_zeros());
std::vector<result>::iterator i = std::find_if( std::vector<result>::iterator i = std::find_if(
m_results.begin() m_results.begin()
, m_results.end() , m_results.end()
@ -119,6 +126,10 @@ void traversal_algorithm::failed(node_id const& id, bool prevent_request)
#ifdef TORRENT_DHT_VERBOSE_LOGGING #ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(traversal) << "failed: " << i->id << " " << i->addr; TORRENT_LOG(traversal) << "failed: " << i->id << " " << i->addr;
#endif #endif
// don't tell the routing table about
// node ids that we just generated ourself
if ((i->flags & result::no_id) == 0)
m_table.node_failed(id);
m_results.erase(i); m_results.erase(i);
} }
if (prevent_request) if (prevent_request)
@ -126,10 +137,6 @@ void traversal_algorithm::failed(node_id const& id, bool prevent_request)
--m_branch_factor; --m_branch_factor;
if (m_branch_factor <= 0) m_branch_factor = 1; if (m_branch_factor <= 0) m_branch_factor = 1;
} }
else
{
m_table.node_failed(id);
}
add_requests(); add_requests();
if (m_invoke_count == 0) done(); if (m_invoke_count == 0) done();
} }

View File

@ -56,7 +56,7 @@ namespace libtorrent
lsd::lsd(io_service& ios, address const& listen_interface lsd::lsd(io_service& ios, address const& listen_interface
, peer_callback_t const& cb) , peer_callback_t const& cb)
: m_callback(cb) : m_callback(cb)
, m_retry_count(0) , m_retry_count(1)
, m_socket(ios, udp::endpoint(address_v4::from_string("239.192.152.143"), 6771) , m_socket(ios, udp::endpoint(address_v4::from_string("239.192.152.143"), 6771)
, bind(&lsd::on_announce, self(), _1, _2, _3)) , bind(&lsd::on_announce, self(), _1, _2, _3))
, m_broadcast_timer(ios) , m_broadcast_timer(ios)
@ -81,7 +81,7 @@ void lsd::announce(sha1_hash const& ih, int listen_port)
"\r\n\r\n"; "\r\n\r\n";
std::string const& msg = btsearch.str(); std::string const& msg = btsearch.str();
m_retry_count = 0; m_retry_count = 1;
asio::error_code ec; asio::error_code ec;
m_socket.send(msg.c_str(), int(msg.size()), ec); m_socket.send(msg.c_str(), int(msg.size()), ec);
if (ec) if (ec)

View File

@ -2400,6 +2400,7 @@ namespace libtorrent
if (ret != r.length || m_torrent.expired()) if (ret != r.length || m_torrent.expired())
{ {
if (j.buffer) m_ses.free_disk_buffer(j.buffer);
boost::shared_ptr<torrent> t = m_torrent.lock(); boost::shared_ptr<torrent> t = m_torrent.lock();
if (!t) if (!t)
{ {

View File

@ -240,7 +240,7 @@ namespace detail
if (m_ses.m_alerts.should_post(alert::info)) if (m_ses.m_alerts.should_post(alert::info))
{ {
m_ses.m_alerts.post_alert(torrent_checked_alert( m_ses.m_alerts.post_alert(torrent_checked_alert(
processing->torrent_ptr->get_handle() t->torrent_ptr->get_handle()
, "torrent finished checking")); , "torrent finished checking"));
} }
if (t->torrent_ptr->is_seed() && m_ses.m_alerts.should_post(alert::info)) if (t->torrent_ptr->is_seed() && m_ses.m_alerts.should_post(alert::info))