diff --git a/core/backends/lan/server.cpp b/core/backends/lan/server.cpp index 04340955a..f80be666c 100644 --- a/core/backends/lan/server.cpp +++ b/core/backends/lan/server.cpp @@ -36,6 +36,7 @@ Server::Server(QObject * parent) void Server::incomingConnection(qintptr socketDescriptor) { QSslSocket* serverSocket = new QSslSocket(parent()); if (serverSocket->setSocketDescriptor(socketDescriptor)) { + QObject::connect(this, &Server::closed, serverSocket, &QSslSocket::abort); addPendingConnection(serverSocket); } else { qWarning() << "setSocketDescriptor failed" << serverSocket->errorString(); @@ -51,3 +52,9 @@ void Server::errorFound(QAbstractSocket::SocketError socketError) { qDebug() << "error:" << socketError; } + +void Server::close() +{ + QTcpServer::close(); + Q_EMIT closed(); +} diff --git a/core/backends/lan/server.h b/core/backends/lan/server.h index e77340aa1..d15ff85a4 100644 --- a/core/backends/lan/server.h +++ b/core/backends/lan/server.h @@ -37,6 +37,10 @@ public: ~Server() override = default; QSslSocket* nextPendingConnection() override; + void close(); + +Q_SIGNALS: + void closed(); protected: void incomingConnection(qintptr socketDescriptor) override; diff --git a/core/backends/linkprovider.cpp b/core/backends/linkprovider.cpp index fd876874d..bc51c0edd 100644 --- a/core/backends/linkprovider.cpp +++ b/core/backends/linkprovider.cpp @@ -19,9 +19,31 @@ */ #include "linkprovider.h" +#include +#include LinkProvider::LinkProvider() { - //gcc complains if we don't add something to compile on a class with virtual functions + // Terminate connections when we sleep or shut down. + QDBusConnection::systemBus().connect(QStringLiteral("org.freedesktop.login1"), + QStringLiteral("/org/freedesktop/login1"), + QStringLiteral("org.freedesktop.login1.Manager"), + QStringLiteral("PrepareForSleep"), + this, SLOT(suspend(bool))); + QDBusConnection::systemBus().connect(QStringLiteral("org.freedesktop.login1"), + QStringLiteral("/org/freedesktop/login1"), + QStringLiteral("org.freedesktop.login1.Manager"), + QStringLiteral("PrepareForShutdown"), + this, SLOT(suspend(bool))); } +void LinkProvider::suspend(bool suspend) +{ + if (suspend) { + qCDebug(KDECONNECT_CORE) << "Stopping connection for suspension"; + onStop(); + } else { + qCDebug(KDECONNECT_CORE) << "Restarting connection after suspension"; + onStart(); + } +} diff --git a/core/backends/linkprovider.h b/core/backends/linkprovider.h index 9ee6c93bc..eb2ae435a 100644 --- a/core/backends/linkprovider.h +++ b/core/backends/linkprovider.h @@ -50,6 +50,8 @@ public Q_SLOTS: virtual void onStop() = 0; virtual void onNetworkChange() = 0; + void suspend(bool suspend); + Q_SIGNALS: //NOTE: The provider will destroy the DeviceLink when it's no longer accessible, // and every user should listen to the destroyed signal to remove its references.