Make the socket manager just manage the server, leave the socket controller in charge of controlling the socket

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2022-12-22 14:37:30 +01:00
parent ffed4d7933
commit c5560db690
No known key found for this signature in database
GPG key ID: C839200C384636B0
7 changed files with 118 additions and 108 deletions

View file

@ -287,9 +287,9 @@ IF( APPLE )
macOS/fileproviderdomainmanager_mac.mm
macOS/fileprovidersocketcontroller.h
macOS/fileprovidersocketcontroller.cpp
macOS/fileprovidersocketmanager.h
macOS/fileprovidersocketmanager.cpp
macOS/fileprovidersocketmanager_mac.mm)
macOS/fileprovidersocketserver.h
macOS/fileprovidersocketserver.cpp
macOS/fileprovidersocketserver_mac.mm)
if(SPARKLE_FOUND AND BUILD_UPDATER)
# Define this, we need to check in updater.cpp

View file

@ -14,17 +14,36 @@
#include "fileprovidersocketcontroller.h"
namespace OCC
{
#include <QLoggingCategory>
namespace Mac
{
namespace OCC {
namespace Mac {
Q_LOGGING_CATEGORY(lcFileProviderSocketController, "nextcloud.gui.macos.fileprovider.socketcontroller", QtInfoMsg)
FileProviderSocketController::FileProviderSocketController(QLocalSocket *socket, QObject *parent)
: QObject{parent}
, _socket(socket)
{
connect(socket, &QLocalSocket::disconnected,
this, &FileProviderSocketController::slotOnDisconnected);
connect(socket, &QLocalSocket::destroyed,
this, &FileProviderSocketController::slotSocketDestroyed);
}
void FileProviderSocketController::slotOnDisconnected()
{
qCInfo(lcFileProviderSocketController) << "File provider socket disconnected";
_socket->deleteLater();
Q_EMIT socketDestroyed(_socket);
}
void FileProviderSocketController::slotSocketDestroyed(QObject *object)
{
Q_UNUSED(object)
qCInfo(lcFileProviderSocketController) << "File provider socket object has been destroyed, destroying controller";
Q_EMIT socketDestroyed(_socket);
}
}

View file

@ -18,11 +18,9 @@
#include <QPointer>
#include <QLocalSocket>
namespace OCC
{
namespace OCC {
namespace Mac
{
namespace Mac {
class FileProviderSocketController : public QObject
{
@ -31,6 +29,13 @@ class FileProviderSocketController : public QObject
public:
explicit FileProviderSocketController(QLocalSocket *socket, QObject *parent = nullptr);
signals:
void socketDestroyed(const QLocalSocket * const socket);
private slots:
void slotOnDisconnected();
void slotSocketDestroyed(QObject *object);
private:
QPointer<QLocalSocket> _socket;
};

View file

@ -1,90 +0,0 @@
/*
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "fileprovidersocketmanager.h"
#include <QLocalSocket>
#include "fileprovidersocketcontroller.h"
namespace OCC
{
namespace Mac
{
Q_LOGGING_CATEGORY(lcFileProviderSocketManager, "nextcloud.gui.macos.fileprovider.socketmanager", QtInfoMsg)
FileProviderSocketManager::FileProviderSocketManager(QObject *parent)
: QObject{parent}
{
#ifdef Q_OS_MACOS
_socketPath = fileProviderSocketPath();
#endif
startListening();
}
void FileProviderSocketManager::startListening()
{
QLocalServer::removeServer(_socketPath);
const auto serverStarted = _socketServer.listen(_socketPath);
if (!serverStarted) {
qCWarning(lcFileProviderSocketManager) << "Could not start file provider socket server"
<< _socketPath;
} else {
qCInfo(lcFileProviderSocketManager) << "File provider socket server started, listening"
<< _socketPath;
}
connect(&_socketServer, &QLocalServer::newConnection,
this, &FileProviderSocketManager::slotNewConnection);
}
void FileProviderSocketManager::slotNewConnection()
{
if (!_socketServer.hasPendingConnections()) {
return;
}
const auto socket = _socketServer.nextPendingConnection();
if (!socket) {
return;
}
connect(socket, &QLocalSocket::disconnected,
this, &FileProviderSocketManager::slotOnDisconnected);
connect(socket, &QLocalSocket::destroyed,
this, &FileProviderSocketManager::slotSocketDestroyed);
const FileProviderSocketControllerPtr controller(new FileProviderSocketController(socket));
_socketControllers.insert(socket, controller);
}
void FileProviderSocketManager::slotOnDisconnected()
{
const auto socket = qobject_cast<QLocalSocket *>(sender());
Q_ASSERT(socket);
socket->deleteLater();
}
void FileProviderSocketManager::slotSocketDestroyed(QObject *object)
{
const auto socket = qobject_cast<QLocalSocket *>(object);
_socketControllers.remove(socket);
}
} // namespace Mac
} // namespace OCC

View file

@ -0,0 +1,78 @@
/*
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "fileprovidersocketserver.h"
#include <QLocalSocket>
#include "fileprovidersocketcontroller.h"
namespace OCC {
namespace Mac {
Q_LOGGING_CATEGORY(lcFileProviderSocketServer, "nextcloud.gui.macos.fileprovider.socketserver", QtInfoMsg)
FileProviderSocketServer::FileProviderSocketServer(QObject *parent)
: QObject{parent}
{
#ifdef Q_OS_MACOS
_socketPath = fileProviderSocketPath();
#endif
startListening();
}
void FileProviderSocketServer::startListening()
{
QLocalServer::removeServer(_socketPath);
const auto serverStarted = _socketServer.listen(_socketPath);
if (!serverStarted) {
qCWarning(lcFileProviderSocketServer) << "Could not start file provider socket server"
<< _socketPath;
} else {
qCInfo(lcFileProviderSocketServer) << "File provider socket server started, listening"
<< _socketPath;
}
connect(&_socketServer, &QLocalServer::newConnection,
this, &FileProviderSocketServer::slotNewConnection);
}
void FileProviderSocketServer::slotNewConnection()
{
if (!_socketServer.hasPendingConnections()) {
return;
}
qCInfo(lcFileProviderSocketServer) << "New connection in file provider socket server";
const auto socket = _socketServer.nextPendingConnection();
if (!socket) {
return;
}
const FileProviderSocketControllerPtr socketController(new FileProviderSocketController(socket, this));
connect(socketController.data(), &FileProviderSocketController::socketDestroyed,
this, &FileProviderSocketServer::slotSocketDestroyed);
_socketControllers.insert(socket, socketController);
}
void FileProviderSocketServer::slotSocketDestroyed(const QLocalSocket * const socket)
{
_socketControllers.remove(socket);
}
} // namespace Mac
} // namespace OCC

View file

@ -18,8 +18,7 @@
#include <QLocalServer>
#include <QLoggingCategory>
namespace OCC
{
namespace OCC {
namespace Mac {
@ -30,23 +29,22 @@ using FileProviderSocketControllerPtr = QSharedPointer<FileProviderSocketControl
QString fileProviderSocketPath();
#endif
class FileProviderSocketManager : public QObject
class FileProviderSocketServer : public QObject
{
Q_OBJECT
public:
explicit FileProviderSocketManager(QObject *parent = nullptr);
explicit FileProviderSocketServer(QObject *parent = nullptr);
private slots:
void startListening();
void slotNewConnection();
void slotOnDisconnected();
void slotSocketDestroyed(QObject *object);
void slotSocketDestroyed(const QLocalSocket * const socket);
private:
QString _socketPath;
QLocalServer _socketServer;
QHash<QLocalSocket*, FileProviderSocketControllerPtr> _socketControllers;
QHash<const QLocalSocket*, FileProviderSocketControllerPtr> _socketControllers;
};
} // namespace Mac