Close all sockets before suspension

This commit is contained in:
Abdel-Rahman Abdel-Rahman 2020-04-06 02:38:19 +02:00 committed by Albert Vaca Cintora
parent dc8a2cf5f4
commit 2728f8d864
4 changed files with 36 additions and 1 deletions

View file

@ -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();
}

View file

@ -37,6 +37,10 @@ public:
~Server() override = default;
QSslSocket* nextPendingConnection() override;
void close();
Q_SIGNALS:
void closed();
protected:
void incomingConnection(qintptr socketDescriptor) override;

View file

@ -19,9 +19,31 @@
*/
#include "linkprovider.h"
#include <QDBusConnection>
#include <core_debug.h>
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();
}
}

View file

@ -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.