From aca437ece23aadc205712c63ea68a142022bb063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Mon, 23 Sep 2024 16:40:34 -0400 Subject: [PATCH] ConnectionManager: send beacon to existing device connections on connect Change-Id: I78c8b85a62172e8304b4bf21d4d3647f17aefbed --- src/connectionmanager.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/connectionmanager.cpp b/src/connectionmanager.cpp index 652a1056..138afc5f 100644 --- a/src/connectionmanager.cpp +++ b/src/connectionmanager.cpp @@ -185,6 +185,14 @@ struct DeviceInfo { } return {}; } + std::vector> getConnectedInfos() const { + std::vector> ret; + for (auto& [id, ci] : info) { + if (ci->socket_) + ret.emplace_back(ci); + } + return ret; + } std::vector extractPendingOperations(dht::Value::Id vid, const std::shared_ptr& sock, bool accepted = true) { @@ -1202,11 +1210,23 @@ ConnectionManager::Impl::onTlsNegotiationDone(const std::shared_ptr& // Note: do not remove pending there it's done in sendChannelRequest std::unique_lock lk2 {dinfo->mtx_}; auto pendingIds = dinfo->requestPendingOps(); + auto previousConnections = dinfo->getConnectedInfos(); lk2.unlock(); std::unique_lock lk {info->mutex_}; addNewMultiplexedSocket(dinfo, deviceId, vid, info); - // Finally, open the channel and launch pending callbacks lk.unlock(); + // send beacon to existing connections for this device + if (config_->logger and not previousConnections.empty()) + config_->logger->warn("[device {}] Sending beacon to {} existing connections", + deviceId, + previousConnections.size()); + for (const auto& cinfo: previousConnections) { + std::lock_guard lk {cinfo->mutex_}; + if (cinfo->socket_) { + cinfo->socket_->sendBeacon(); + } + } + // Finally, launch pending callbacks for (const auto& [id, name]: pendingIds) { if (config_->logger) config_->logger->debug("[device {}] Send request on TLS socket for channel {}",