kdeconnect-kde/core/backends/lan/mdnsdiscovery.cpp

62 lines
1.8 KiB
C++
Raw Normal View History

2023-06-22 10:53:42 +01:00
/**
* SPDX-FileCopyrightText: 2023 Albert Vaca <albertvaka@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "mdnsdiscovery.h"
#include "core_debug.h"
#include "kdeconnectconfig.h"
#include "lanlinkprovider.h"
#include "mdns_wrapper.h"
2023-06-22 10:53:42 +01:00
2023-07-08 12:22:45 +01:00
QString kServiceType = QStringLiteral("_kdeconnect._udp.local");
2023-06-22 10:53:42 +01:00
MdnsDiscovery::MdnsDiscovery(LanLinkProvider *lanLinkProvider)
: lanLinkProvider(lanLinkProvider)
2023-07-08 12:22:45 +01:00
, mdnsAnnouncer(KdeConnectConfig::instance().deviceId(), kServiceType, LanLinkProvider::UDP_PORT)
2023-06-22 10:53:42 +01:00
{
2023-07-08 12:22:45 +01:00
KdeConnectConfig &config = KdeConnectConfig::instance();
mdnsAnnouncer.putTxtRecord(QStringLiteral("id"), config.deviceId());
mdnsAnnouncer.putTxtRecord(QStringLiteral("name"), config.name());
mdnsAnnouncer.putTxtRecord(QStringLiteral("type"), config.deviceType().toString());
mdnsAnnouncer.putTxtRecord(QStringLiteral("protocol"), QString::number(NetworkPacket::s_protocolVersion));
connect(&mdnsWrapper, &MdnsWrapper::serviceFound, [lanLinkProvider](const MdnsWrapper::MdnsService &service) {
2023-07-07 17:03:01 +01:00
if (KdeConnectConfig::instance().deviceId() == service.name) {
qCDebug(KDECONNECT_CORE) << "Discovered myself, ignoring";
return;
}
lanLinkProvider->sendUdpIdentityPacket(QList<QHostAddress>{service.address});
2023-07-07 17:03:01 +01:00
qCDebug(KDECONNECT_CORE) << "Discovered" << service.name << "at" << service.address;
});
2023-06-22 10:53:42 +01:00
}
MdnsDiscovery::~MdnsDiscovery()
{
stopAnnouncing();
stopDiscovering();
}
void MdnsDiscovery::startAnnouncing()
{
2023-07-08 12:22:45 +01:00
mdnsAnnouncer.startAnnouncing();
2023-06-22 10:53:42 +01:00
}
void MdnsDiscovery::stopAnnouncing()
{
2023-07-08 12:22:45 +01:00
mdnsAnnouncer.stopAnnouncing();
2023-06-22 10:53:42 +01:00
}
void MdnsDiscovery::startDiscovering()
{
2023-07-08 12:22:45 +01:00
mdnsWrapper.startDiscovering(kServiceType);
2023-06-22 10:53:42 +01:00
}
void MdnsDiscovery::stopDiscovering()
{
mdnsWrapper.stopDiscovering();
2023-06-22 10:53:42 +01:00
}