Better handling of network changes for MDNS
This commit is contained in:
parent
3485e3aa44
commit
b3ac0df630
4 changed files with 29 additions and 52 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@ private Q_SLOTS:
|
|||
void newConnection();
|
||||
void dataReceived();
|
||||
void sslErrors(const QList<QSslError> &errors);
|
||||
void broadcastToNetwork();
|
||||
void combinedOnNetworkChange();
|
||||
|
||||
private:
|
||||
void addLink(QSslSocket *socket, const DeviceInfo &deviceInfo);
|
||||
|
@ -95,14 +95,11 @@ private:
|
|||
QMap<QSslSocket *, PendingConnect> m_receivedIdentityPackets;
|
||||
QMap<QString, qint64> 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
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue