diff --git a/src/twister.cpp b/src/twister.cpp index 0bd38a42..77da2a95 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -1339,6 +1339,7 @@ Value dhtget(const Array& params, bool fHelp) time_duration timeToWait = seconds(10); time_duration timeToWaitMulti = milliseconds(100); int minMultiReplies = 3; + int lastSeq = -1; if( params.size() > 3 ) timeToWait = milliseconds(params[3].get_int()); @@ -1372,12 +1373,17 @@ Value dhtget(const Array& params, bool fHelp) libtorrent::entry &e = *it; hexcapeDht( e ); string sig_p = safeGetEntryString(e, "sig_p"); - if( !sig_p.length() ) { + int seq = (multi) ? 0 : safeGetEntryInt( safeGetEntryDict(e,"p"), "seq" ); + bool acceptEntry = (multi) ? (!sig_p.length() || !uniqueSigPs.count(sig_p)) : + (seq > lastSeq); + if( acceptEntry ) { + if( !multi) { + ret.clear(); + } ret.push_back( entryToJson(e) ); - } else { - if( !uniqueSigPs.count(sig_p) ) { + lastSeq = seq; + if( sig_p.length() ) { uniqueSigPs.insert(sig_p); - ret.push_back( entryToJson(e) ); } } } @@ -1387,13 +1393,9 @@ Value dhtget(const Array& params, bool fHelp) break; } - if( multi ) { - if( repliesReceived++ < minMultiReplies && uniqueSigPs.size() ) { - timeToWait = timeToWaitMulti; - //printf("dhtget: wait again %d\n", repliesReceived); - } else { - break; - } + if( repliesReceived++ < minMultiReplies ) { + timeToWait = timeToWaitMulti; + //printf("dhtget: wait again repliesReceived=%d lastSeq=%d\n", repliesReceived, lastSeq); } else { break; } diff --git a/src/twister_utils.cpp b/src/twister_utils.cpp index 5260510b..a150cc2e 100644 --- a/src/twister_utils.cpp +++ b/src/twister_utils.cpp @@ -329,7 +329,7 @@ void unHexcapeDht(libtorrent::entry &e) } } -std::string safeGetEntryString(libtorrent::entry &e, std::string const& key) +std::string safeGetEntryString(libtorrent::entry const &e, std::string const& key) { if( e.type() == libtorrent::entry::dictionary_t && e.find_key(key) && e[key].type() == libtorrent::entry::string_t ) { @@ -339,3 +339,24 @@ std::string safeGetEntryString(libtorrent::entry &e, std::string const& key) } } +int safeGetEntryInt(libtorrent::entry const &e, std::string const& key) +{ + if( e.type() == libtorrent::entry::dictionary_t && + e.find_key(key) && e[key].type() == libtorrent::entry::int_t ) { + return e[key].integer(); + } else { + return 0; + } +} + +libtorrent::entry safeGetEntryDict(libtorrent::entry const &e, std::string const& key) +{ + static libtorrent::entry::dictionary_type dummy; + if( e.type() == libtorrent::entry::dictionary_t && + e.find_key(key) && e[key].type() == libtorrent::entry::dictionary_t ) { + return e[key].dict(); + } else { + return dummy; + } +} + diff --git a/src/twister_utils.h b/src/twister_utils.h index 75d286e6..aa6c61a7 100644 --- a/src/twister_utils.h +++ b/src/twister_utils.h @@ -44,6 +44,8 @@ void unHexcapePost(libtorrent::entry &e); void hexcapeDht(libtorrent::entry &e); void unHexcapeDht(libtorrent::entry &e); -std::string safeGetEntryString(libtorrent::entry &e, std::string const& key); +std::string safeGetEntryString(libtorrent::entry const &e, std::string const& key); +int safeGetEntryInt(libtorrent::entry const &e, std::string const& key); +libtorrent::entry safeGetEntryDict(libtorrent::entry const &e, std::string const& key); #endif // TWISTER_UTILS_H