From be43212a733acfb19294491a497d93a2e74db9b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Thu, 10 Mar 2022 19:45:29 -0500 Subject: [PATCH] c: add callback to dht_runner_ping --- c/opendht.cpp | 17 +++++++++++++---- c/opendht_c.h | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/c/opendht.cpp b/c/opendht.cpp index 9ed06564..f91f7834 100644 --- a/c/opendht.cpp +++ b/c/opendht.cpp @@ -305,9 +305,15 @@ int dht_runner_run_config(dht_runner* r, in_port_t port, const dht_runner_config return 0; } -void dht_runner_ping(dht_runner* r, struct sockaddr* addr, socklen_t addr_len) { +void dht_runner_ping(dht_runner* r, struct sockaddr* addr, socklen_t addr_len, dht_done_cb done_cb, void* cb_user_data) { auto runner = reinterpret_cast<dht::DhtRunner*>(r); - runner->bootstrap(dht::SockAddr(addr, addr_len)); + if (done_cb) { + runner->bootstrap(dht::SockAddr(addr, addr_len), [done_cb, cb_user_data](bool ok){ + done_cb(ok, cb_user_data); + }); + } else { + runner->bootstrap(dht::SockAddr(addr, addr_len)); + } } void dht_runner_bootstrap(dht_runner* r, const char* host, const char* service) { @@ -346,8 +352,11 @@ dht_op_token* dht_runner_listen(dht_runner* r, const dht_infohash* h, dht_value_ auto runner = reinterpret_cast<dht::DhtRunner*>(r); auto hash = reinterpret_cast<const dht::InfoHash*>(h); auto fret = new std::future<size_t>; - auto guard = done_cb ? std::make_shared<ScopeGuardCb>(done_cb, cb_user_data) : std::shared_ptr<ScopeGuardCb>{}; - *fret = runner->listen(*hash, [cb,cb_user_data, guard](const std::vector<std::shared_ptr<dht::Value>>& values, bool expired) { + *fret = runner->listen(*hash, [ + cb, + cb_user_data, + guard = done_cb ? std::make_shared<ScopeGuardCb>(done_cb, cb_user_data) : std::shared_ptr<ScopeGuardCb>{} + ](const std::vector<std::shared_ptr<dht::Value>>& values, bool expired) { for (const auto& value : values) { if (not cb(reinterpret_cast<const dht_value*>(&value), expired, cb_user_data)) return false; diff --git a/c/opendht_c.h b/c/opendht_c.h index be594677..942c4010 100644 --- a/c/opendht_c.h +++ b/c/opendht_c.h @@ -141,7 +141,7 @@ OPENDHT_C_PUBLIC void dht_runner_delete(dht_runner* runner); OPENDHT_C_PUBLIC int dht_runner_run(dht_runner* runner, in_port_t port); /* Returns 0 on success, standard error code on failure */ OPENDHT_C_PUBLIC int dht_runner_run_config(dht_runner* runner, in_port_t port, const dht_runner_config* config); -OPENDHT_C_PUBLIC void dht_runner_ping(dht_runner* runner, struct sockaddr* addr, socklen_t addr_len); +OPENDHT_C_PUBLIC void dht_runner_ping(dht_runner* runner, struct sockaddr* addr, socklen_t addr_len, dht_done_cb done_cb, void* cb_user_data); OPENDHT_C_PUBLIC void dht_runner_bootstrap(dht_runner* runner, const char* host, const char* service); OPENDHT_C_PUBLIC void dht_runner_get(dht_runner* runner, const dht_infohash* hash, dht_get_cb cb, dht_done_cb done_cb, void* cb_user_data); OPENDHT_C_PUBLIC dht_op_token* dht_runner_listen(dht_runner* runner, const dht_infohash* hash, dht_value_cb cb, dht_shutdown_cb done_cb, void* cb_user_data); -- GitLab