Core: Improve communication single-instance
This commit is contained in:
@@ -28,7 +28,7 @@
|
||||
# include <QAbstractSpinBox>
|
||||
# include <QByteArray>
|
||||
# include <QComboBox>
|
||||
# include <QDataStream>
|
||||
# include <QTextStream>
|
||||
# include <QFileInfo>
|
||||
# include <QFileOpenEvent>
|
||||
# include <QSessionManager>
|
||||
@@ -218,7 +218,7 @@ public:
|
||||
QTimer *timer;
|
||||
QLocalServer *server{nullptr};
|
||||
QString serverName;
|
||||
QList<QByteArray> messages;
|
||||
QList<QString> messages;
|
||||
bool running{false};
|
||||
};
|
||||
|
||||
@@ -237,15 +237,16 @@ bool GUISingleApplication::isRunning() const
|
||||
return d_ptr->running;
|
||||
}
|
||||
|
||||
bool GUISingleApplication::sendMessage(const QByteArray &message, int timeout)
|
||||
bool GUISingleApplication::sendMessage(const QString &message, int timeout)
|
||||
{
|
||||
QLocalSocket socket;
|
||||
bool connected = false;
|
||||
for(int i = 0; i < 2; i++) {
|
||||
socket.connectToServer(d_ptr->serverName);
|
||||
connected = socket.waitForConnected(timeout/2);
|
||||
if (connected || i > 0)
|
||||
if (connected || i > 0) {
|
||||
break;
|
||||
}
|
||||
int ms = 250;
|
||||
#if defined(Q_OS_WIN)
|
||||
Sleep(DWORD(ms));
|
||||
@@ -253,41 +254,56 @@ bool GUISingleApplication::sendMessage(const QByteArray &message, int timeout)
|
||||
usleep(ms*1000);
|
||||
#endif
|
||||
}
|
||||
if (!connected)
|
||||
if (!connected) {
|
||||
return false;
|
||||
}
|
||||
|
||||
QDataStream ds(&socket);
|
||||
ds << message;
|
||||
socket.waitForBytesWritten(timeout);
|
||||
return true;
|
||||
QTextStream ts(&socket);
|
||||
#if QT_VERSION <= QT_VERSION_CHECK(6, 0, 0)
|
||||
ts.setCodec("UTF-8");
|
||||
#else
|
||||
ts.setEncoding(QStringConverter::Utf8);
|
||||
#endif
|
||||
ts << message << Qt::endl;
|
||||
|
||||
return socket.waitForBytesWritten(timeout);
|
||||
}
|
||||
|
||||
void GUISingleApplication::readFromSocket()
|
||||
{
|
||||
auto socket = qobject_cast<QLocalSocket*>(sender());
|
||||
if (socket) {
|
||||
QTextStream in(socket);
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
in.setCodec("UTF-8");
|
||||
#else
|
||||
in.setEncoding(QStringConverter::Utf8);
|
||||
#endif
|
||||
while (socket->canReadLine()) {
|
||||
d_ptr->timer->stop();
|
||||
QString message = in.readLine();
|
||||
Base::Console().Log("Received message: %s\n", message.toStdString());
|
||||
d_ptr->messages.push_back(message);
|
||||
d_ptr->timer->start(1000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GUISingleApplication::receiveConnection()
|
||||
{
|
||||
QLocalSocket *socket = d_ptr->server->nextPendingConnection();
|
||||
if (!socket)
|
||||
if (!socket) {
|
||||
return;
|
||||
}
|
||||
|
||||
connect(socket, &QLocalSocket::disconnected,
|
||||
socket, &QLocalSocket::deleteLater);
|
||||
if (socket->waitForReadyRead()) {
|
||||
QDataStream in(socket);
|
||||
if (!in.atEnd()) {
|
||||
d_ptr->timer->stop();
|
||||
QByteArray message;
|
||||
in >> message;
|
||||
Base::Console().Log("Received message: %s\n", message.constData());
|
||||
d_ptr->messages.push_back(message);
|
||||
d_ptr->timer->start(1000);
|
||||
}
|
||||
}
|
||||
|
||||
socket->disconnectFromServer();
|
||||
connect(socket, &QLocalSocket::readyRead, this, &GUISingleApplication::readFromSocket);
|
||||
}
|
||||
|
||||
void GUISingleApplication::processMessages()
|
||||
{
|
||||
QList<QByteArray> msg = d_ptr->messages;
|
||||
QList<QString> msg = d_ptr->messages;
|
||||
d_ptr->messages.clear();
|
||||
Q_EMIT messageReceived(msg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user