From 900655b11fd62099f7766e56d5266dfd70061bea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Sat, 18 Jan 2020 13:57:23 -0500 Subject: [PATCH] dht: give current time through periodic --- include/opendht/dht.h | 6 +++--- include/opendht/dht_interface.h | 4 ++-- include/opendht/dht_proxy_client.h | 6 +++--- include/opendht/scheduler.h | 1 + include/opendht/securedht.h | 8 ++++---- src/dht.cpp | 4 ++-- src/dht_proxy_client.cpp | 2 +- src/dhtrunner.cpp | 4 ++-- 8 files changed, 18 insertions(+), 17 deletions(-) diff --git a/include/opendht/dht.h b/include/opendht/dht.h index 6e0380d7..45257fcf 100644 --- a/include/opendht/dht.h +++ b/include/opendht/dht.h @@ -123,9 +123,9 @@ public: void pingNode(SockAddr, DoneCallbackSimple&& cb={}) override; - time_point periodic(const uint8_t *buf, size_t buflen, SockAddr) override; - time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen) override { - return periodic(buf, buflen, SockAddr(from, fromlen)); + time_point periodic(const uint8_t *buf, size_t buflen, SockAddr, const time_point& now) override; + time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override { + return periodic(buf, buflen, SockAddr(from, fromlen), now); } /** diff --git a/include/opendht/dht_interface.h b/include/opendht/dht_interface.h index f82eb90e..b5bccc51 100644 --- a/include/opendht/dht_interface.h +++ b/include/opendht/dht_interface.h @@ -79,8 +79,8 @@ public: virtual void pingNode(SockAddr, DoneCallbackSimple&& cb={}) = 0; - virtual time_point periodic(const uint8_t *buf, size_t buflen, SockAddr) = 0; - virtual time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen) = 0; + virtual time_point periodic(const uint8_t *buf, size_t buflen, SockAddr, const time_point& now) = 0; + virtual time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) = 0; /** * Get a value by searching on all available protocols (IPv4, IPv6), diff --git a/include/opendht/dht_proxy_client.h b/include/opendht/dht_proxy_client.h index fc13cf36..265f405d 100644 --- a/include/opendht/dht_proxy_client.h +++ b/include/opendht/dht_proxy_client.h @@ -197,9 +197,9 @@ public: */ void pushNotificationReceived(const std::map& notification) override; - time_point periodic(const uint8_t*, size_t, SockAddr) override; - time_point periodic(const uint8_t* buf, size_t buflen, const sockaddr* from, socklen_t fromlen) override { - return periodic(buf, buflen, SockAddr(from, fromlen)); + time_point periodic(const uint8_t*, size_t, SockAddr, const time_point& now) override; + time_point periodic(const uint8_t* buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override { + return periodic(buf, buflen, SockAddr(from, fromlen), now); } /** diff --git a/include/opendht/scheduler.h b/include/opendht/scheduler.h index 2d522945..adfdd019 100644 --- a/include/opendht/scheduler.h +++ b/include/opendht/scheduler.h @@ -114,6 +114,7 @@ public: */ inline const time_point& time() const { return now; } inline time_point syncTime() { return (now = clock::now()); } + inline void syncTime(const time_point& n) { now = n; } private: time_point now {clock::now()}; diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h index b9f9a193..0c209164 100644 --- a/include/opendht/securedht.h +++ b/include/opendht/securedht.h @@ -195,11 +195,11 @@ public: std::vector getPublicAddress(sa_family_t family = 0) override { return dht_->getPublicAddress(family); } - time_point periodic(const uint8_t *buf, size_t buflen, SockAddr sa) override { - return dht_->periodic(buf, buflen, std::move(sa)); + time_point periodic(const uint8_t *buf, size_t buflen, SockAddr sa, const time_point& now) override { + return dht_->periodic(buf, buflen, std::move(sa), now); } - time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen) override { - return dht_->periodic(buf, buflen, from, fromlen); + time_point periodic(const uint8_t *buf, size_t buflen, const sockaddr* from, socklen_t fromlen, const time_point& now) override { + return dht_->periodic(buf, buflen, from, fromlen, now); } NodeStatus getStatus(sa_family_t af) const override { return dht_->getStatus(af); diff --git a/src/dht.cpp b/src/dht.cpp index 294be440..e757888b 100644 --- a/src/dht.cpp +++ b/src/dht.cpp @@ -1957,9 +1957,9 @@ Dht::maintainStorage(decltype(store)::value_type& storage, bool force, const Don } time_point -Dht::periodic(const uint8_t *buf, size_t buflen, SockAddr from) +Dht::periodic(const uint8_t *buf, size_t buflen, SockAddr from, const time_point& now) { - scheduler.syncTime(); + scheduler.syncTime(now); if (buflen) { try { network_engine.processMessage(buf, buflen, std::move(from)); diff --git a/src/dht_proxy_client.cpp b/src/dht_proxy_client.cpp index f662909c..1cf8a9cd 100644 --- a/src/dht_proxy_client.cpp +++ b/src/dht_proxy_client.cpp @@ -280,7 +280,7 @@ DhtProxyClient::isRunning(sa_family_t af) const } time_point -DhtProxyClient::periodic(const uint8_t*, size_t, SockAddr) +DhtProxyClient::periodic(const uint8_t*, size_t, SockAddr, const time_point& now) { // Exec all currently stored callbacks decltype(callbacks_) callbacks; diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index e9b8b924..27919347 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -621,13 +621,13 @@ DhtRunner::loop_() if (now - pkt.received > net::RX_QUEUE_MAX_DELAY) dropped++; else - wakeup = dht->periodic(pkt.data.data(), pkt.data.size(), std::move(pkt.from)); + wakeup = dht->periodic(pkt.data.data(), pkt.data.size(), std::move(pkt.from), now); pkt.data.clear(); } received_treated.splice(received_treated.end(), std::move(received)); } else { // Or just run the scheduler - wakeup = dht->periodic(nullptr, 0, nullptr, 0); + wakeup = dht->periodic(nullptr, 0, nullptr, 0, clock::now()); } if (not received_treated.empty()) {