127 satır
2.8 KiB
C++
127 satır
2.8 KiB
C++
|
#include <QLocalSocket>
|
||
|
#include <QApplication>
|
||
|
#include "ipc.h"
|
||
|
#include "ipcpreviewclient.h"
|
||
|
#include "previewresultpdf.h"
|
||
|
#include "previewresultplaintext.h"
|
||
|
|
||
|
bool IPCPreviewClient::connect()
|
||
|
{
|
||
|
if(socket->state() == QLocalSocket::ConnectedState)
|
||
|
{
|
||
|
socket->disconnectFromServer();
|
||
|
if(socket->state() == QLocalSocket::ConnectedState)
|
||
|
{
|
||
|
socket->waitForDisconnected(100);
|
||
|
}
|
||
|
}
|
||
|
socket->connectToServer(socketPath);
|
||
|
socket->waitForConnected(100);
|
||
|
return socket->state() == QLocalSocket::ConnectedState;
|
||
|
}
|
||
|
|
||
|
QSharedPointer<PreviewResult> IPCPreviewClient::deserialize(QByteArray &array)
|
||
|
{
|
||
|
QDataStream stream{&array, QIODevice::ReadOnly};
|
||
|
|
||
|
PreviewResultType type;
|
||
|
stream >> type;
|
||
|
if(type == PreviewResultType::PDF)
|
||
|
{
|
||
|
return PreviewResultPdf::deserialize(array);
|
||
|
}
|
||
|
if(type == PreviewResultType::PlainText)
|
||
|
{
|
||
|
return PreviewResultPlainText::deserialize(array);
|
||
|
}
|
||
|
return QSharedPointer<PreviewResult>(nullptr);
|
||
|
}
|
||
|
|
||
|
IPCPreviewClient::IPCPreviewClient()
|
||
|
{
|
||
|
this->socket = new QLocalSocket(this);
|
||
|
}
|
||
|
|
||
|
void IPCPreviewClient::setSocketPath(QString socketPath)
|
||
|
{
|
||
|
this->socketPath = socketPath;
|
||
|
}
|
||
|
|
||
|
void IPCPreviewClient::startGeneration(RenderConfig config, const QVector<RenderTarget> &targets)
|
||
|
{
|
||
|
this->start(config, targets);
|
||
|
}
|
||
|
|
||
|
void IPCPreviewClient::start(RenderConfig config, const QVector<RenderTarget> &targets)
|
||
|
{
|
||
|
if(targets.count() == 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if(!connect() || !socket->isOpen())
|
||
|
{
|
||
|
// TODO: ERROR
|
||
|
}
|
||
|
|
||
|
if(socket->isOpen() && socket->isWritable())
|
||
|
{
|
||
|
QDataStream stream(socket);
|
||
|
stream << GeneratePreviews;
|
||
|
stream << config;
|
||
|
stream << targets;
|
||
|
socket->flush();
|
||
|
|
||
|
int numTarget = 0;
|
||
|
if(socket->isOpen() && socket->isReadable() && socket->state() == QLocalSocket::ConnectedState)
|
||
|
{
|
||
|
do
|
||
|
{
|
||
|
socket->waitForReadyRead(100);
|
||
|
stream.startTransaction();
|
||
|
stream >> numTarget;
|
||
|
} while(!stream.commitTransaction() && socket->state() == QLocalSocket::ConnectedState);
|
||
|
if(numTarget != targets.count())
|
||
|
{
|
||
|
throw std::runtime_error("Server reports less targets than it should");
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// TODO: ERROR
|
||
|
}
|
||
|
|
||
|
int processed = 0;
|
||
|
++this->currentPreviewGeneration;
|
||
|
while(socket->isOpen() && socket->isReadable() && processed < targets.count())
|
||
|
{
|
||
|
QByteArray array;
|
||
|
do
|
||
|
{
|
||
|
socket->waitForReadyRead(100);
|
||
|
stream.startTransaction();
|
||
|
stream >> array;
|
||
|
} while(!stream.commitTransaction() && socket->state() == QLocalSocket::ConnectedState);
|
||
|
emit previewReceived(deserialize(array), this->currentPreviewGeneration);
|
||
|
++processed;
|
||
|
}
|
||
|
if(processed != targets.count())
|
||
|
{
|
||
|
// TODO: ERROR
|
||
|
}
|
||
|
}
|
||
|
socket->disconnectFromServer();
|
||
|
emit finished();
|
||
|
}
|
||
|
|
||
|
void IPCPreviewClient::stopGeneration()
|
||
|
{
|
||
|
if(!connect() || !socket->isOpen())
|
||
|
{
|
||
|
// TODO: ERROR
|
||
|
}
|
||
|
QDataStream stream(socket);
|
||
|
stream << StopGeneratePreviews;
|
||
|
socket->flush();
|
||
|
}
|