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_udpBroadcastPort(udpBroadcastPort)
|
||||||
, m_udpListenPort(udpListenPort)
|
, m_udpListenPort(udpListenPort)
|
||||||
, m_testMode(testMode)
|
, m_testMode(testMode)
|
||||||
, m_combineBroadcastsTimer(this)
|
, m_combineNetworkChangeTimer(this)
|
||||||
#ifdef KDECONNECT_MDNS
|
#ifdef KDECONNECT_MDNS
|
||||||
, m_mdnsDiscovery(this)
|
, m_mdnsDiscovery(this)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
m_combineBroadcastsTimer.setInterval(0); // increase this if waiting a single event-loop iteration is not enough
|
m_combineNetworkChangeTimer.setInterval(0); // increase this if waiting a single event-loop iteration is not enough
|
||||||
m_combineBroadcastsTimer.setSingleShot(true);
|
m_combineNetworkChangeTimer.setSingleShot(true);
|
||||||
connect(&m_combineBroadcastsTimer, &QTimer::timeout, this, &LanLinkProvider::broadcastToNetwork);
|
connect(&m_combineNetworkChangeTimer, &QTimer::timeout, this, &LanLinkProvider::combinedOnNetworkChange);
|
||||||
|
|
||||||
connect(&m_udpSocket, &QIODevice::readyRead, this, &LanLinkProvider::udpBroadcastReceived);
|
connect(&m_udpSocket, &QIODevice::readyRead, this, &LanLinkProvider::udpBroadcastReceived);
|
||||||
|
|
||||||
|
@ -71,21 +71,14 @@ LanLinkProvider::LanLinkProvider(bool testMode, quint16 udpBroadcastPort, quint1
|
||||||
#if QT_VERSION_MAJOR < 6
|
#if QT_VERSION_MAJOR < 6
|
||||||
QNetworkConfigurationManager *networkManager = new QNetworkConfigurationManager(this);
|
QNetworkConfigurationManager *networkManager = new QNetworkConfigurationManager(this);
|
||||||
connect(networkManager, &QNetworkConfigurationManager::configurationChanged, this, [this](QNetworkConfiguration config) {
|
connect(networkManager, &QNetworkConfigurationManager::configurationChanged, this, [this](QNetworkConfiguration config) {
|
||||||
if (m_lastConfig != config && config.state() == QNetworkConfiguration::Active) {
|
if (config.state() == QNetworkConfiguration::Active) {
|
||||||
m_lastConfig = config;
|
|
||||||
onNetworkChange();
|
onNetworkChange();
|
||||||
#ifdef KDECONNECT_MDNS
|
|
||||||
m_mdnsDiscovery.onNetworkChange();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
#else
|
#else
|
||||||
const auto checkNetworkChange = [this]() {
|
const auto checkNetworkChange = [this]() {
|
||||||
if (QNetworkInformation::instance()->reachability() == QNetworkInformation::Reachability::Online) {
|
if (QNetworkInformation::instance()->reachability() == QNetworkInformation::Reachability::Online) {
|
||||||
onNetworkChange();
|
onNetworkChange();
|
||||||
#ifdef KDECONNECT_MDNS
|
|
||||||
m_mdnsDiscovery.onNetworkChange();
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Detect when a network interface changes status, so we announce ourselves in the new network
|
// Detect when a network interface changes status, so we announce ourselves in the new network
|
||||||
|
@ -127,8 +120,7 @@ void LanLinkProvider::onStart()
|
||||||
broadcastUdpIdentityPacket();
|
broadcastUdpIdentityPacket();
|
||||||
|
|
||||||
#ifdef KDECONNECT_MDNS
|
#ifdef KDECONNECT_MDNS
|
||||||
m_mdnsDiscovery.startAnnouncing();
|
m_mdnsDiscovery.onStart();
|
||||||
m_mdnsDiscovery.startDiscovering();
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
qCDebug(KDECONNECT_CORE) << "LanLinkProvider started";
|
qCDebug(KDECONNECT_CORE) << "LanLinkProvider started";
|
||||||
|
@ -137,8 +129,7 @@ void LanLinkProvider::onStart()
|
||||||
void LanLinkProvider::onStop()
|
void LanLinkProvider::onStop()
|
||||||
{
|
{
|
||||||
#ifdef KDECONNECT_MDNS
|
#ifdef KDECONNECT_MDNS
|
||||||
m_mdnsDiscovery.stopAnnouncing();
|
m_mdnsDiscovery.onStop();
|
||||||
m_mdnsDiscovery.stopDiscovering();
|
|
||||||
#endif
|
#endif
|
||||||
m_udpSocket.close();
|
m_udpSocket.close();
|
||||||
m_server->close();
|
m_server->close();
|
||||||
|
@ -147,15 +138,15 @@ void LanLinkProvider::onStop()
|
||||||
|
|
||||||
void LanLinkProvider::onNetworkChange()
|
void LanLinkProvider::onNetworkChange()
|
||||||
{
|
{
|
||||||
if (m_combineBroadcastsTimer.isActive()) {
|
if (m_combineNetworkChangeTimer.isActive()) {
|
||||||
qCDebug(KDECONNECT_CORE) << "Preventing duplicate broadcasts";
|
qCDebug(KDECONNECT_CORE) << "Device discovery triggered too fast, ignoring";
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_combineBroadcastsTimer.start();
|
m_combineNetworkChangeTimer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
// I'm in a new network, let's be polite and introduce myself
|
// I'm in a new network, let's be polite and introduce myself
|
||||||
void LanLinkProvider::broadcastToNetwork()
|
void LanLinkProvider::combinedOnNetworkChange()
|
||||||
{
|
{
|
||||||
if (!m_server->isListening()) {
|
if (!m_server->isListening()) {
|
||||||
qWarning() << "TCP server not listening, not broadcasting";
|
qWarning() << "TCP server not listening, not broadcasting";
|
||||||
|
@ -166,8 +157,7 @@ void LanLinkProvider::broadcastToNetwork()
|
||||||
|
|
||||||
broadcastUdpIdentityPacket();
|
broadcastUdpIdentityPacket();
|
||||||
#ifdef KDECONNECT_MDNS
|
#ifdef KDECONNECT_MDNS
|
||||||
m_mdnsDiscovery.stopDiscovering();
|
m_mdnsDiscovery.onNetworkChange();
|
||||||
m_mdnsDiscovery.startDiscovering();
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -71,7 +71,7 @@ private Q_SLOTS:
|
||||||
void newConnection();
|
void newConnection();
|
||||||
void dataReceived();
|
void dataReceived();
|
||||||
void sslErrors(const QList<QSslError> &errors);
|
void sslErrors(const QList<QSslError> &errors);
|
||||||
void broadcastToNetwork();
|
void combinedOnNetworkChange();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void addLink(QSslSocket *socket, const DeviceInfo &deviceInfo);
|
void addLink(QSslSocket *socket, const DeviceInfo &deviceInfo);
|
||||||
|
@ -95,14 +95,11 @@ private:
|
||||||
QMap<QSslSocket *, PendingConnect> m_receivedIdentityPackets;
|
QMap<QSslSocket *, PendingConnect> m_receivedIdentityPackets;
|
||||||
QMap<QString, qint64> m_lastConnectionTime;
|
QMap<QString, qint64> m_lastConnectionTime;
|
||||||
const bool m_testMode;
|
const bool m_testMode;
|
||||||
QTimer m_combineBroadcastsTimer;
|
QTimer m_combineNetworkChangeTimer;
|
||||||
|
|
||||||
#ifdef KDECONNECT_MDNS
|
#ifdef KDECONNECT_MDNS
|
||||||
MdnsDiscovery m_mdnsDiscovery;
|
MdnsDiscovery m_mdnsDiscovery;
|
||||||
#endif
|
#endif
|
||||||
#if QT_VERSION_MAJOR < 6
|
|
||||||
QNetworkConfiguration m_lastConfig;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -15,8 +15,7 @@
|
||||||
const QString kServiceType = QStringLiteral("_kdeconnect._udp.local");
|
const QString kServiceType = QStringLiteral("_kdeconnect._udp.local");
|
||||||
|
|
||||||
MdnsDiscovery::MdnsDiscovery(LanLinkProvider *lanLinkProvider)
|
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();
|
KdeConnectConfig &config = KdeConnectConfig::instance();
|
||||||
mdnsAnnouncer.putTxtRecord(QStringLiteral("id"), config.deviceId());
|
mdnsAnnouncer.putTxtRecord(QStringLiteral("id"), config.deviceId());
|
||||||
|
@ -36,28 +35,26 @@ MdnsDiscovery::MdnsDiscovery(LanLinkProvider *lanLinkProvider)
|
||||||
|
|
||||||
MdnsDiscovery::~MdnsDiscovery()
|
MdnsDiscovery::~MdnsDiscovery()
|
||||||
{
|
{
|
||||||
stopAnnouncing();
|
onStop();
|
||||||
stopDiscovering();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MdnsDiscovery::startAnnouncing()
|
void MdnsDiscovery::onStart()
|
||||||
{
|
{
|
||||||
mdnsAnnouncer.startAnnouncing();
|
mdnsAnnouncer.startAnnouncing();
|
||||||
}
|
|
||||||
|
|
||||||
void MdnsDiscovery::stopAnnouncing()
|
|
||||||
{
|
|
||||||
mdnsAnnouncer.stopAnnouncing();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MdnsDiscovery::startDiscovering()
|
|
||||||
{
|
|
||||||
mdnsDiscoverer.startDiscovering(kServiceType);
|
mdnsDiscoverer.startDiscovering(kServiceType);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MdnsDiscovery::stopDiscovering()
|
void MdnsDiscovery::onStop()
|
||||||
{
|
{
|
||||||
|
mdnsAnnouncer.stopAnnouncing();
|
||||||
mdnsDiscoverer.stopDiscovering();
|
mdnsDiscoverer.stopDiscovering();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MdnsDiscovery::onNetworkChange()
|
||||||
|
{
|
||||||
|
mdnsAnnouncer.onNetworkChange();
|
||||||
|
mdnsDiscoverer.stopDiscovering();
|
||||||
|
mdnsDiscoverer.startDiscovering(kServiceType);
|
||||||
|
}
|
||||||
|
|
||||||
#include "moc_mdnsdiscovery.cpp"
|
#include "moc_mdnsdiscovery.cpp"
|
||||||
|
|
|
@ -23,20 +23,13 @@ public:
|
||||||
explicit MdnsDiscovery(LanLinkProvider *parent);
|
explicit MdnsDiscovery(LanLinkProvider *parent);
|
||||||
~MdnsDiscovery();
|
~MdnsDiscovery();
|
||||||
|
|
||||||
void startDiscovering();
|
void onStart();
|
||||||
void stopDiscovering();
|
void onStop();
|
||||||
|
|
||||||
void stopAnnouncing();
|
|
||||||
void startAnnouncing();
|
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
void onNetworkChange()
|
void onNetworkChange();
|
||||||
{
|
|
||||||
mdnsAnnouncer.onNetworkChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LanLinkProvider *lanLinkProvider = nullptr;
|
|
||||||
MdnsWrapper::Discoverer mdnsDiscoverer;
|
MdnsWrapper::Discoverer mdnsDiscoverer;
|
||||||
MdnsWrapper::Announcer mdnsAnnouncer;
|
MdnsWrapper::Announcer mdnsAnnouncer;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue