From 49fa11dfc1b9885d7500c9a62aef0fea87449a7f Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Mon, 3 Jul 2023 15:16:54 +0200 Subject: [PATCH] UDP port can be a constant --- core/backends/lan/lanlinkprovider.cpp | 16 +++++++--------- core/backends/lan/lanlinkprovider.h | 7 +------ 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/core/backends/lan/lanlinkprovider.cpp b/core/backends/lan/lanlinkprovider.cpp index c7b3d1843..66ba29251 100644 --- a/core/backends/lan/lanlinkprovider.cpp +++ b/core/backends/lan/lanlinkprovider.cpp @@ -41,12 +41,10 @@ static const int MAX_REMEMBERED_IDENTITY_PACKETS = 42; static const long MILLIS_DELAY_BETWEEN_CONNECTIONS_TO_SAME_DEVICE = 500; -LanLinkProvider::LanLinkProvider(bool testMode, quint16 udpBroadcastPort, quint16 udpListenPort) +LanLinkProvider::LanLinkProvider(bool testMode) : m_server(new Server(this)) , m_udpSocket(this) , m_tcpPort(0) - , m_udpBroadcastPort(udpBroadcastPort) - , m_udpListenPort(udpListenPort) , m_testMode(testMode) , m_combineNetworkChangeTimer(this) #ifdef KDECONNECT_MDNS @@ -98,12 +96,12 @@ void LanLinkProvider::onStart() { const QHostAddress bindAddress = m_testMode ? QHostAddress::LocalHost : QHostAddress::Any; - bool success = m_udpSocket.bind(bindAddress, m_udpListenPort, QUdpSocket::ShareAddress); + bool success = m_udpSocket.bind(bindAddress, UDP_PORT, QUdpSocket::ShareAddress); if (!success) { QAbstractSocket::SocketError sockErr = m_udpSocket.error(); // Refer to https://doc.qt.io/qt-5/qabstractsocket.html#SocketError-enum to decode socket error number QString errorMessage = QString::fromLatin1(QMetaEnum::fromType().valueToKey(sockErr)); - qCritical(KDECONNECT_CORE) << "Failed to bind UDP socket on port" << m_udpListenPort << "with error" << errorMessage; + qCritical(KDECONNECT_CORE) << "Failed to bind UDP socket on port" << UDP_PORT << "with error" << errorMessage; } Q_ASSERT(success); @@ -229,7 +227,7 @@ void LanLinkProvider::sendUdpIdentityPacket(QUdpSocket &socket, const QListerrorString(); NetworkPacket np = KdeConnectConfig::instance().deviceInfo().toIdentityPacket(); np.set(QStringLiteral("tcpPort"), m_tcpPort); - m_udpSocket.writeDatagram(np.serialize(), m_receivedIdentityPackets[socket].sender, m_udpBroadcastPort); + m_udpSocket.writeDatagram(np.serialize(), m_receivedIdentityPackets[socket].sender, UDP_PORT); // The socket we created didn't work, and we didn't manage // to create a LanDeviceLink from it, deleting everything. @@ -380,7 +378,7 @@ void LanLinkProvider::tcpSocketConnected() // The socket doesn't seem to work, so we can't create the connection. qCDebug(KDECONNECT_CORE) << "Fallback (2), try reverse connection (send udp packet)"; - m_udpSocket.writeDatagram(np2.serialize(), m_receivedIdentityPackets[socket].sender, m_udpBroadcastPort); + m_udpSocket.writeDatagram(np2.serialize(), m_receivedIdentityPackets[socket].sender, UDP_PORT); // Disconnect should trigger deleteLater, which should remove the socket from m_receivedIdentityPackets socket->disconnectFromHost(); diff --git a/core/backends/lan/lanlinkprovider.h b/core/backends/lan/lanlinkprovider.h index fd084b1be..aae13eb10 100644 --- a/core/backends/lan/lanlinkprovider.h +++ b/core/backends/lan/lanlinkprovider.h @@ -34,10 +34,8 @@ class KDECONNECTCORE_EXPORT LanLinkProvider : public LinkProvider public: /** * @param testMode Some special overrides needed while testing - * @param udpBroadcastPort Port which should be used for *sending* identity packets - * @param udpListenPort Port which should be used for *receiving* identity packets */ - LanLinkProvider(bool testMode = false, quint16 udpBroadcastPort = UDP_PORT, quint16 udpListenPort = UDP_PORT); + LanLinkProvider(bool testMode = false); ~LanLinkProvider() override; QString name() override @@ -83,9 +81,6 @@ private: QUdpSocket m_udpSocket; quint16 m_tcpPort; - quint16 m_udpBroadcastPort; - quint16 m_udpListenPort; - QMap m_links; struct PendingConnect {