Changeset View
Changeset View
Standalone View
Standalone View
core/backends/lan/lanlinkprovider.cpp
Show All 22 Lines | |||||
23 | 23 | | |||
24 | #ifndef Q_OS_WIN | 24 | #ifndef Q_OS_WIN | ||
25 | #include <sys/socket.h> | 25 | #include <sys/socket.h> | ||
26 | #include <netinet/in.h> | 26 | #include <netinet/in.h> | ||
27 | #include <netinet/tcp.h> | 27 | #include <netinet/tcp.h> | ||
28 | #include <netdb.h> | 28 | #include <netdb.h> | ||
29 | #endif | 29 | #endif | ||
30 | 30 | | |||
31 | #include <KDNSSD/DNSSD/PublicService> | ||||
32 | | ||||
31 | #include <QHostInfo> | 33 | #include <QHostInfo> | ||
32 | #include <QTcpServer> | 34 | #include <QTcpServer> | ||
33 | #include <QNetworkProxy> | 35 | #include <QNetworkProxy> | ||
34 | #include <QUdpSocket> | 36 | #include <QUdpSocket> | ||
35 | #include <QNetworkSession> | 37 | #include <QNetworkSession> | ||
36 | #include <QNetworkConfigurationManager> | 38 | #include <QNetworkConfigurationManager> | ||
37 | #include <QSslCipher> | 39 | #include <QSslCipher> | ||
38 | #include <QSslConfiguration> | 40 | #include <QSslConfiguration> | ||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Line(s) | |||||
103 | 105 | | |||
104 | void LanLinkProvider::onStop() | 106 | void LanLinkProvider::onStop() | ||
105 | { | 107 | { | ||
106 | qCDebug(KDECONNECT_CORE) << "onStop"; | 108 | qCDebug(KDECONNECT_CORE) << "onStop"; | ||
107 | m_udpSocket.close(); | 109 | m_udpSocket.close(); | ||
108 | m_server->close(); | 110 | m_server->close(); | ||
109 | } | 111 | } | ||
110 | 112 | | |||
113 | void LanLinkProvider::initializeMDNS() | ||||
114 | { | ||||
115 | // We leverage the existing identity packet to avoid duplicating code for reading the wanted attributes. | ||||
116 | NetworkPacket np(QLatin1String("")); | ||||
117 | NetworkPacket::createIdentityPacket(&np); | ||||
sredman: Avoiding code reuse is definitely good, but maybe it would make even more sense to have a… | |||||
Indeed, this was just a PoC to see how easy it would be to add support for this. A simple identity wrapper sounds like a way to go, I'll see what I can do. rytilahti: Indeed, this was just a PoC to see how easy it would be to add support for this. A simple… | |||||
118 | | ||||
119 | QMap<QString, QByteArray> data; | ||||
120 | data["protocolVersion"] = np.get(QStringLiteral("protocolVersion"), QStringLiteral("unknown")).toUtf8(); | ||||
121 | data["type"] = np.get(QStringLiteral("deviceType"), QStringLiteral("unknown")).toUtf8(); | ||||
122 | data["name"] = np.get(QStringLiteral("deviceName"), QStringLiteral("unknown")).toUtf8(); | ||||
123 | data["id"] = np.get(QStringLiteral("deviceId"), QStringLiteral("unknown")).toUtf8(); | ||||
124 | | ||||
125 | // Restart the publishing. | ||||
126 | delete m_publisher; | ||||
127 | m_publisher = nullptr; | ||||
128 | m_publisher = new KDNSSD::PublicService("KDEConnect on " + data["name"], "_kdeconnect._tcp", m_tcpPort); | ||||
129 | m_publisher->setTextData(data); | ||||
130 | | ||||
131 | if (m_publisher->publish()) { | ||||
132 | qCDebug(KDECONNECT_CORE) << "mDNS: successful publishing" << data["name"]; | ||||
133 | } else { | ||||
134 | qCDebug(KDECONNECT_CORE) << "mDNS: failed to publish."; | ||||
135 | } | ||||
136 | } | ||||
137 | | ||||
111 | void LanLinkProvider::onNetworkChange() | 138 | void LanLinkProvider::onNetworkChange() | ||
112 | { | 139 | { | ||
113 | if (m_combineBroadcastsTimer.isActive()) { | 140 | if (m_combineBroadcastsTimer.isActive()) { | ||
114 | qCDebug(KDECONNECT_CORE()) << "Preventing duplicate broadcasts"; | 141 | qCDebug(KDECONNECT_CORE()) << "Preventing duplicate broadcasts"; | ||
115 | return; | 142 | return; | ||
116 | } | 143 | } | ||
117 | m_combineBroadcastsTimer.start(); | 144 | m_combineBroadcastsTimer.start(); | ||
145 | | ||||
146 | initializeMDNS(); | ||||
118 | } | 147 | } | ||
119 | 148 | | |||
120 | //I'm in a new network, let's be polite and introduce myself | 149 | //I'm in a new network, let's be polite and introduce myself | ||
121 | void LanLinkProvider::broadcastToNetwork() | 150 | void LanLinkProvider::broadcastToNetwork() | ||
122 | { | 151 | { | ||
123 | 152 | | |||
124 | if (!m_server->isListening()) { | 153 | if (!m_server->isListening()) { | ||
125 | //Not started | 154 | //Not started | ||
▲ Show 20 Lines • Show All 416 Lines • Show Last 20 Lines |
Avoiding code reuse is definitely good, but maybe it would make even more sense to have a higher-level helper function, something like "get device identity", which maybe returns some custom "identity" class(/struct) and is used by both this and the createIdentityPacket method? That just feels a little nicer that "ad-hoc" using the identity packet, and is robust to the potential of that packet changing in the future.