From b3ac0df63042218ba6d5379ff2ad7bd78a34e666 Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Mon, 31 Jul 2023 09:57:44 +0200 Subject: [PATCH] Better handling of network changes for MDNS --- core/backends/lan/lanlinkprovider.cpp | 34 ++++++++++----------------- core/backends/lan/lanlinkprovider.h | 7 ++---- core/backends/lan/mdnsdiscovery.cpp | 27 ++++++++++----------- core/backends/lan/mdnsdiscovery.h | 13 +++------- 4 files changed, 29 insertions(+), 52 deletions(-) diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index a4837ee6f..c7b3d1843 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -48,14 +48,14 @@ LanLinkProvider::LanLinkProvider(bool testMode, quint16 udpBroadcastPort, quint1 , m_udpBroadcastPort(udpBroadcastPort) , m_udpListenPort(udpListenPort) , m_testMode(testMode) - , m_combineBroadcastsTimer(this) + , m_combineNetworkChangeTimer(this) #ifdef KDECONNECT_MDNS , m_mdnsDiscovery(this) #endif { - m_combineBroadcastsTimer.setInterval(0); // increase this if waiting a single event-loop iteration is not enough - m_combineBroadcastsTimer.setSingleShot(true); - connect(&m_combineBroadcastsTimer, &QTimer::timeout, this, &LanLinkProvider::broadcastToNetwork); + m_combineNetworkChangeTimer.setInterval(0); // increase this if waiting a single event-loop iteration is not enough + m_combineNetworkChangeTimer.setSingleShot(true); + connect(&m_combineNetworkChangeTimer, &QTimer::timeout, this, &LanLinkProvider::combinedOnNetworkChange); connect(&m_udpSocket, &QIODevice::readyRead, this, &LanLinkProvider::udpBroadcastReceived); @@ -71,21 +71,14 @@ LanLinkProvider::LanLinkProvider(bool testMode, quint16 udpBroadcastPort, quint1 #if QT_VERSION_MAJOR < 6 QNetworkConfigurationManager *networkManager = new QNetworkConfigurationManager(this); connect(networkManager, &QNetworkConfigurationManager::configurationChanged, this, [this](QNetworkConfiguration config) { - if (m_lastConfig != config && config.state() == QNetworkConfiguration::Active) { - m_lastConfig = config; + if (config.state() == QNetworkConfiguration::Active) { onNetworkChange(); -#ifdef KDECONNECT_MDNS - m_mdnsDiscovery.onNetworkChange(); -#endif } }); #else const auto checkNetworkChange = [this]() { if (QNetworkInformation::instance()->reachability() == QNetworkInformation::Reachability::Online) { onNetworkChange(); -#ifdef KDECONNECT_MDNS - m_mdnsDiscovery.onNetworkChange(); -#endif } }; // Detect when a network interface changes status, so we announce ourselves in the new network @@ -127,8 +120,7 @@ void LanLinkProvider::onStart() broadcastUdpIdentityPacket(); #ifdef KDECONNECT_MDNS - m_mdnsDiscovery.startAnnouncing(); - m_mdnsDiscovery.startDiscovering(); + m_mdnsDiscovery.onStart(); #endif qCDebug(KDECONNECT_CORE) << "LanLinkProvider started"; @@ -137,8 +129,7 @@ void LanLinkProvider::onStart() void LanLinkProvider::onStop() { #ifdef KDECONNECT_MDNS - m_mdnsDiscovery.stopAnnouncing(); - m_mdnsDiscovery.stopDiscovering(); + m_mdnsDiscovery.onStop(); #endif m_udpSocket.close(); m_server->close(); @@ -147,15 +138,15 @@ void LanLinkProvider::onStop() void LanLinkProvider::onNetworkChange() { - if (m_combineBroadcastsTimer.isActive()) { - qCDebug(KDECONNECT_CORE) << "Preventing duplicate broadcasts"; + if (m_combineNetworkChangeTimer.isActive()) { + qCDebug(KDECONNECT_CORE) << "Device discovery triggered too fast, ignoring"; return; } - m_combineBroadcastsTimer.start(); + m_combineNetworkChangeTimer.start(); } // I'm in a new network, let's be polite and introduce myself -void LanLinkProvider::broadcastToNetwork() +void LanLinkProvider::combinedOnNetworkChange() { if (!m_server->isListening()) { qWarning() << "TCP server not listening, not broadcasting"; @@ -166,8 +157,7 @@ void LanLinkProvider::broadcastToNetwork() broadcastUdpIdentityPacket(); #ifdef KDECONNECT_MDNS - m_mdnsDiscovery.stopDiscovering(); - m_mdnsDiscovery.startDiscovering(); + m_mdnsDiscovery.onNetworkChange(); #endif } diff --git a/core/backends/lan/lanlinkprovider.h b/core/backends/lan/lanlinkprovider.h index d984a0883..fd084b1be 100644 --- a/core/backends/lan/lanlinkprovider.h +++ b/core/backends/lan/lanlinkprovider.h @@ -71,7 +71,7 @@ private Q_SLOTS: void newConnection(); void dataReceived(); void sslErrors(const QList &errors); - void broadcastToNetwork(); + void combinedOnNetworkChange(); private: void addLink(QSslSocket *socket, const DeviceInfo &deviceInfo); @@ -95,14 +95,11 @@ private: QMap m_receivedIdentityPackets; QMap m_lastConnectionTime; const bool m_testMode; - QTimer m_combineBroadcastsTimer; + QTimer m_combineNetworkChangeTimer; #ifdef KDECONNECT_MDNS MdnsDiscovery m_mdnsDiscovery; #endif -#if QT_VERSION_MAJOR < 6 - QNetworkConfiguration m_lastConfig; -#endif }; #endif diff --git a/core/backends/lan/mdnsdiscovery.cpp b/core/backends/lan/mdnsdiscovery.cpp index c356ce9bb..44bd6a014 100644 --- a/core/backends/lan/mdnsdiscovery.cpp +++ b/core/backends/lan/mdnsdiscovery.cpp @@ -15,8 +15,7 @@ const QString kServiceType = QStringLiteral("_kdeconnect._udp.local"); MdnsDiscovery::MdnsDiscovery(LanLinkProvider *lanLinkProvider) - : lanLinkProvider(lanLinkProvider) - , mdnsAnnouncer(KdeConnectConfig::instance().deviceId(), kServiceType, LanLinkProvider::UDP_PORT) + : mdnsAnnouncer(KdeConnectConfig::instance().deviceId(), kServiceType, LanLinkProvider::UDP_PORT) { KdeConnectConfig &config = KdeConnectConfig::instance(); mdnsAnnouncer.putTxtRecord(QStringLiteral("id"), config.deviceId()); @@ -36,28 +35,26 @@ MdnsDiscovery::MdnsDiscovery(LanLinkProvider *lanLinkProvider) MdnsDiscovery::~MdnsDiscovery() { - stopAnnouncing(); - stopDiscovering(); + onStop(); } -void MdnsDiscovery::startAnnouncing() +void MdnsDiscovery::onStart() { mdnsAnnouncer.startAnnouncing(); -} - -void MdnsDiscovery::stopAnnouncing() -{ - mdnsAnnouncer.stopAnnouncing(); -} - -void MdnsDiscovery::startDiscovering() -{ mdnsDiscoverer.startDiscovering(kServiceType); } -void MdnsDiscovery::stopDiscovering() +void MdnsDiscovery::onStop() { + mdnsAnnouncer.stopAnnouncing(); mdnsDiscoverer.stopDiscovering(); } +void MdnsDiscovery::onNetworkChange() +{ + mdnsAnnouncer.onNetworkChange(); + mdnsDiscoverer.stopDiscovering(); + mdnsDiscoverer.startDiscovering(kServiceType); +} + #include "moc_mdnsdiscovery.cpp" diff --git a/core/backends/lan/mdnsdiscovery.h b/core/backends/lan/mdnsdiscovery.h index 3d6f8ab0f..f48cb00c3 100644 --- a/core/backends/lan/mdnsdiscovery.h +++ b/core/backends/lan/mdnsdiscovery.h @@ -23,20 +23,13 @@ public: explicit MdnsDiscovery(LanLinkProvider *parent); ~MdnsDiscovery(); - void startDiscovering(); - void stopDiscovering(); - - void stopAnnouncing(); - void startAnnouncing(); + void onStart(); + void onStop(); public Q_SLOTS: - void onNetworkChange() - { - mdnsAnnouncer.onNetworkChange(); - } + void onNetworkChange(); private: - LanLinkProvider *lanLinkProvider = nullptr; MdnsWrapper::Discoverer mdnsDiscoverer; MdnsWrapper::Announcer mdnsAnnouncer; };