diff --git a/libtorrent/include/libtorrent/kademlia/node.hpp b/libtorrent/include/libtorrent/kademlia/node.hpp index 23d72438..a812f4bf 100644 --- a/libtorrent/include/libtorrent/kademlia/node.hpp +++ b/libtorrent/include/libtorrent/kademlia/node.hpp @@ -203,7 +203,7 @@ class TORRENT_EXTRA_EXPORT node_impl : boost::noncopyable typedef std::map table_t; typedef std::map dht_immutable_table_t; typedef std::map dht_mutable_table_t; -typedef std::vector dht_storage_list_t; +typedef std::list dht_storage_list_t; typedef std::map dht_storage_table_t; public: diff --git a/libtorrent/include/libtorrent/session_settings.hpp b/libtorrent/include/libtorrent/session_settings.hpp index eefbcb9a..9b064e98 100644 --- a/libtorrent/include/libtorrent/session_settings.hpp +++ b/libtorrent/include/libtorrent/session_settings.hpp @@ -990,6 +990,7 @@ namespace libtorrent , max_fail_count(20) , max_torrents(2000) , max_dht_items(700) + , max_entries_per_multi(50) , max_torrent_search_reply(20) , restrict_routing_ips(true) , restrict_search_ips(true) @@ -1027,6 +1028,9 @@ namespace libtorrent // max number of items the DHT will store int max_dht_items; + // max multi entries per DHT item + int max_entries_per_multi; + // the max number of torrents to return in a // torrent search query to the DHT int max_torrent_search_reply; diff --git a/libtorrent/src/kademlia/node.cpp b/libtorrent/src/kademlia/node.cpp index 237803a9..524e85b4 100644 --- a/libtorrent/src/kademlia/node.cpp +++ b/libtorrent/src/kademlia/node.cpp @@ -1290,9 +1290,10 @@ void node_impl::incoming_request(msg const& m, entry& e) } else { dht_storage_list_t & lsto = i->second; - int j; - for( j = 0; j < lsto.size(); j++) { - dht_storage_item &olditem = lsto[j]; + dht_storage_list_t::reverse_iterator j, rend(lsto.rend()); + dht_storage_list_t::iterator insert_pos = lsto.end(); + for( j = lsto.rbegin(); j != rend; ++j) { + dht_storage_item &olditem = *j; lazy_entry p; int pos; @@ -1312,16 +1313,27 @@ void node_impl::incoming_request(msg const& m, entry& e) // compare contents before adding to the list std::pair bufoldv = p.dict_find("v")->data_section(); - if( bufv.second == bufoldv.second && - !memcmp(bufv.first, bufoldv.first,bufv.second)) { + if( bufv.second == bufoldv.second && !memcmp(bufv.first, bufoldv.first,bufv.second) ) { + // break so it wont be inserted break; } + + // if new entry is newer than existing one, it will be inserted before + if( msg_keys[mk_height]->int_value() >= p.dict_find_int_value("height") ) { + insert_pos = j.base(); + insert_pos--; + } } } - if(multi && j == lsto.size()) { + if(multi && j == rend) { // new entry - lsto.push_back(item); + lsto.insert(insert_pos, item); } + + if(lsto.size() > m_settings.max_entries_per_multi) { + lsto.resize(m_settings.max_entries_per_multi); + } + } } else if (strcmp(query, "getData") == 0)