diff --git a/include/opendht/dhtrunner.h b/include/opendht/dhtrunner.h index 6bad6f627..d85a413fe 100644 --- a/include/opendht/dhtrunner.h +++ b/include/opendht/dhtrunner.h @@ -409,7 +409,7 @@ class OPENDHT_PUBLIC DhtRunner { void run(const Config& config, Context&& context); void setOnStatusChanged(StatusCallback&& cb) { - statusCb = std::move(cb); + statusCbs.emplace_back(std::move(cb)); } /** @@ -519,7 +519,8 @@ class OPENDHT_PUBLIC DhtRunner { NodeStatus status4 {NodeStatus::Disconnected}, status6 {NodeStatus::Disconnected}; - StatusCallback statusCb {nullptr}; + + std::list statusCbs {}; /** PeerDiscovery Parameters */ std::shared_ptr peerDiscovery_; diff --git a/src/dhtrunner.cpp b/src/dhtrunner.cpp index b171c3d2f..fc424324c 100644 --- a/src/dhtrunner.cpp +++ b/src/dhtrunner.cpp @@ -180,9 +180,8 @@ DhtRunner::run(const Config& config, Context&& context) throw; } - if (context.statusChangedCallback) { - statusCb = std::move(context.statusChangedCallback); - } + statusCbs.clear(); + statusCbs.emplace_back(std::move(context.statusChangedCallback)); if (context.certificateStore) { dht_->setLocalCertificateStore(std::move(context.certificateStore)); } @@ -264,6 +263,13 @@ DhtRunner::run(const Config& config, Context&& context) } } } + if (config.peer_discovery && config.peer_publish) { + statusCbs.emplace_back([this](NodeStatus status4, NodeStatus status6) { + if (status4 == NodeStatus::Disconnected && status6 == NodeStatus::Disconnected) { + peerDiscovery_->connectivityChanged(); + } + }); + } #endif } } @@ -690,8 +696,13 @@ DhtRunner::loop_() if (nstatus4 != status4 || nstatus6 != status6) { status4 = nstatus4; status6 = nstatus6; - if (statusCb) - statusCb(status4, status6); + if (!statusCbs.empty()) + { + for (auto& cb : statusCbs){ + if (cb) + cb(status4, status6); + } + } } return wakeup; @@ -1039,10 +1050,6 @@ DhtRunner::connectivityChanged() std::lock_guard lck(storage_mtx); pending_ops_prio.emplace([=](SecureDht& dht) { dht.connectivityChanged(); -#ifdef OPENDHT_PEER_DISCOVERY - if (peerDiscovery_) - peerDiscovery_->connectivityChanged(); -#endif }); cv.notify_all(); } diff --git a/src/peer_discovery.cpp b/src/peer_discovery.cpp index bf845c481..d020bf4fd 100644 --- a/src/peer_discovery.cpp +++ b/src/peer_discovery.cpp @@ -321,6 +321,8 @@ PeerDiscovery::DomainPeerDiscovery::connectivityChanged() reDiscover(); publish(sockAddrSend_); }); + if (logger_) + logger_->d("PeerDiscovery: connectivity changed"); } PeerDiscovery::PeerDiscovery(in_port_t port, Sp ioContext, Sp logger)