Port away from deprecated QtNetwork classes

This commit is contained in:
Alexander Lohnau 2023-04-28 14:32:31 +02:00
parent 907d007f16
commit 44be5caf5b
2 changed files with 28 additions and 21 deletions

View file

@ -13,15 +13,13 @@
#include <netinet/tcp.h>
#include <sys/socket.h>
#else
#include <winsock2.h>
#include <mstcpip.h>
#include <winsock2.h>
#endif
#include <QHostInfo>
#include <QMetaEnum>
#include <QNetworkConfigurationManager>
#include <QNetworkProxy>
#include <QNetworkSession>
#include <QSslCipher>
#include <QSslConfiguration>
#include <QSslKey>
@ -60,17 +58,22 @@ LanLinkProvider::LanLinkProvider(bool testMode, quint16 udpBroadcastPort, quint1
m_udpSocket.setProxy(QNetworkProxy::NoProxy);
// Detect when a network interface changes status, so we announce ourselves in the new network
#if QT_VERSION_MAJOR < 6
QNetworkConfigurationManager *networkManager = new QNetworkConfigurationManager(this);
connect(networkManager, &QNetworkConfigurationManager::configurationChanged, this, &LanLinkProvider::onNetworkConfigurationChanged);
}
void LanLinkProvider::onNetworkConfigurationChanged(const QNetworkConfiguration &config)
{
if (m_lastConfig != config && config.state() == QNetworkConfiguration::Active) {
m_lastConfig = config;
onNetworkChange();
}
connect(networkManager, &QNetworkConfigurationManager::configurationChanged, this, [this](QNetworkConfiguration config) {
if (m_lastConfig != config && config.state() == QNetworkConfiguration::Active) {
m_lastConfig = config;
onNetworkChange();
}
});
#else
// Detect when a network interface changes status, so we announce ourselves in the new network
connect(QNetworkInformation::instance(), &QNetworkInformation::reachabilityChanged, this, [this]() {
if (QNetworkInformation::instance()->reachability() == QNetworkInformation::Reachability::Online) {
onNetworkChange();
}
});
#endif
}
LanLinkProvider::~LanLinkProvider()
@ -602,7 +605,7 @@ void LanLinkProvider::configureSocket(QSslSocket *socket)
#endif
#if defined(Q_OS_WIN)
int maxIdle = 5 * 60 * 1000; // 5 minutes of idle before sending keep-alives
int maxIdle = 5 * 60 * 1000; // 5 minutes of idle before sending keep-alive
int interval = 5 * 1000; // 5 seconds interval between probes after 5 minute delay
DWORD nop;
@ -610,12 +613,10 @@ void LanLinkProvider::configureSocket(QSslSocket *socket)
struct tcp_keepalive keepalive = {
1 /* true */,
maxIdle,
interval
interval,
};
int rv = WSAIoctl(socket->socketDescriptor(), SIO_KEEPALIVE_VALS, &keepalive,
sizeof(keepalive), nullptr, 0, &nop,
nullptr, nullptr);
int rv = WSAIoctl(socket->socketDescriptor(), SIO_KEEPALIVE_VALS, &keepalive, sizeof(keepalive), nullptr, 0, &nop, nullptr, nullptr);
if (!rv) {
int error = WSAGetLastError();
qCDebug(KDECONNECT_CORE) << "Could not enable TCP Keep-Alive: " << error;

View file

@ -7,12 +7,17 @@
#ifndef LANLINKPROVIDER_H
#define LANLINKPROVIDER_H
#include <QNetworkSession>
#include <QObject>
#include <QSslSocket>
#include <QTcpServer>
#include <QTimer>
#include <QUdpSocket>
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QNetworkConfigurationManager>
#include <QNetworkSession>
#else
#include <QNetworkInformation>
#endif
#include "backends/linkprovider.h"
#include "kdeconnectcore_export.h"
@ -75,7 +80,6 @@ private Q_SLOTS:
private:
LanPairingHandler *createPairingHandler(DeviceLink *link);
void onNetworkConfigurationChanged(const QNetworkConfiguration &config);
void addLink(const QString &deviceId, QSslSocket *socket, NetworkPacket *receivedPacket, LanDeviceLink::ConnectionStarted connectionOrigin);
QList<QHostAddress> getBroadcastAddresses();
void sendBroadcasts(QUdpSocket &socket, const NetworkPacket &np, const QList<QHostAddress> &addresses);
@ -95,9 +99,11 @@ private:
QHostAddress sender;
};
QMap<QSslSocket *, PendingConnect> m_receivedIdentityPackets;
QNetworkConfiguration m_lastConfig;
const bool m_testMode;
QTimer m_combineBroadcastsTimer;
#if QT_VERSION_MAJOR < 6
QNetworkConfiguration m_lastConfig;
#endif
};
#endif