diff --git a/gui/ipcpreviewworker.cpp b/gui/ipcpreviewworker.cpp index 236111d..92e32e9 100644 --- a/gui/ipcpreviewworker.cpp +++ b/gui/ipcpreviewworker.cpp @@ -3,29 +3,24 @@ #include "previewgeneratormapfunctor.h" IPCPreviewWorker::IPCPreviewWorker() { + this->connect(&previewWorkerWatcher, &QFutureWatcher::resultReadyAt, this, + [this](int index) { emit previewGenerated(previewWorkerWatcher.resultAt(index)); }); + connect(&previewWorkerWatcher, &QFutureWatcher::finished, this, [this] { emit finished(); }); } void IPCPreviewWorker::start(RenderConfig config, const QVector &targets, QLocalSocket *peer) { - connect(&previewWorkerWatcher, &QFutureWatcher::resultReadyAt, this, - [peer, this](int index) - { - QDataStream stream{peer}; - stream << previewWorkerWatcher.resultAt(index); - peer->flush(); - }); - connect(&previewWorkerWatcher, &QFutureWatcher::finished, this, - [peer] - { - /* TODO / - - /*peer->waitForBytesWritten(); - peer->disconnectFromServer(); - peer->deleteLater();*/ - }); + stop(); + /* TODO: memleak */ auto mapFunctor = new PreviewGeneratorMapFunctor(); mapFunctor->setRenderConfig(config); previewWorkerWatcher.setFuture(QtConcurrent::mapped(targets, *mapFunctor)); } + +void IPCPreviewWorker::stop() +{ + previewWorkerWatcher.cancel(); + previewWorkerWatcher.waitForFinished(); +} diff --git a/gui/ipcpreviewworker.h b/gui/ipcpreviewworker.h index f4c8b56..cc7959c 100644 --- a/gui/ipcpreviewworker.h +++ b/gui/ipcpreviewworker.h @@ -15,6 +15,10 @@ class IPCPreviewWorker : public QObject public: IPCPreviewWorker(); void start(RenderConfig config, const QVector &targets, QLocalSocket *peer); + void stop(); + signals: + void previewGenerated(QByteArray); + void finished(); }; #endif // IPCPREVIEWWORKER_H diff --git a/gui/ipcserver.cpp b/gui/ipcserver.cpp index be2892d..89d82aa 100644 --- a/gui/ipcserver.cpp +++ b/gui/ipcserver.cpp @@ -15,7 +15,11 @@ IpcServer::IpcServer() { + /* Only 1, we are doing work for the GUI, not a service for general availability */ + this->spawningServer.setMaxPendingConnections(1); connect(&this->spawningServer, &QLocalServer::newConnection, this, &IpcServer::spawnerNewConnection); + connect(&this->previewWorker, &IPCPreviewWorker::previewGenerated, this, &IpcServer::handlePreviewGenerated); + connect(&this->previewWorker, &IPCPreviewWorker::finished, this, [this] { this->currentSocket->flush(); }); } bool IpcServer::startSpawner(QString socketPath) @@ -28,6 +32,7 @@ void IpcServer::spawnerNewConnection() { QLocalSocket *socket = this->spawningServer.nextPendingConnection(); connect(socket, &QLocalSocket::disconnected, socket, &QLocalSocket::deleteLater); + this->currentSocket = socket; if(socket != nullptr) { if(!socket->waitForReadyRead()) @@ -39,10 +44,8 @@ void IpcServer::spawnerNewConnection() stream >> command; if(command == GeneratePreviews) { - IPCPreviewWorker *worker = new IPCPreviewWorker(); RenderConfig renderConfig; QVector targets; - do { /* TODO: this is not entirely robust */ @@ -53,7 +56,18 @@ void IpcServer::spawnerNewConnection() stream << targets.count(); socket->flush(); - worker->start(renderConfig, targets, socket); + previewWorker.start(renderConfig, targets, socket); + } + if(command == StopGeneratePreviews) + { + previewWorker.stop(); } } } + +void IpcServer::handlePreviewGenerated(QByteArray ba) +{ + QDataStream stream{this->currentSocket}; + stream << ba; + this->currentSocket->flush(); +} diff --git a/gui/ipcserver.h b/gui/ipcserver.h index 8274e2a..572ec00 100644 --- a/gui/ipcserver.h +++ b/gui/ipcserver.h @@ -4,14 +4,19 @@ #include #include "ipc.h" #include "filesaver.h" +#include "ipcpreviewworker.h" + class IpcServer : public QObject { Q_OBJECT private: + IPCPreviewWorker previewWorker; QLocalServer spawningServer; + QLocalSocket *currentSocket = nullptr; SaveFileResult addFile(QString file); private slots: void spawnerNewConnection(); + void handlePreviewGenerated(QByteArray ba); public: IpcServer();