2025-12-05 16:31:30 +00:00
|
|
|
/*
|
|
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2025-2026 Symless Ltd.
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "IpcClient.h"
|
|
|
|
|
|
2026-03-26 14:17:56 +00:00
|
|
|
#include "common/VersionInfo.h"
|
|
|
|
|
|
2025-12-05 16:31:30 +00:00
|
|
|
#include <QDebug>
|
|
|
|
|
#include <QLocalSocket>
|
2026-03-26 12:34:18 +00:00
|
|
|
#include <QTimer>
|
2025-12-05 16:31:30 +00:00
|
|
|
|
|
|
|
|
namespace deskflow::gui::ipc {
|
|
|
|
|
|
2026-03-31 09:06:00 +00:00
|
|
|
IpcClient::IpcClient(QObject *parent, const QString &socketName, const QString &typeName)
|
2025-12-05 16:31:30 +00:00
|
|
|
: QObject(parent),
|
|
|
|
|
m_socket{new QLocalSocket(this)},
|
2026-03-31 09:06:00 +00:00
|
|
|
m_socketName(socketName), // NOSONAR - Qt memory
|
|
|
|
|
m_typeName(typeName)
|
2025-12-05 16:31:30 +00:00
|
|
|
{
|
|
|
|
|
connect(m_socket, &QLocalSocket::disconnected, this, &IpcClient::handleDisconnected);
|
|
|
|
|
connect(m_socket, &QLocalSocket::errorOccurred, this, &IpcClient::handleErrorOccurred);
|
2026-03-26 12:34:18 +00:00
|
|
|
connect(m_socket, &QLocalSocket::readyRead, this, &IpcClient::handleReadyRead);
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
void IpcClient::connectToServer()
|
2025-12-05 16:31:30 +00:00
|
|
|
{
|
|
|
|
|
if (m_state == State::Connecting) {
|
2026-03-31 09:06:00 +00:00
|
|
|
qWarning().noquote() << QStringLiteral("%1 ipc client already connecting to server").arg(m_typeName);
|
2026-03-26 12:34:18 +00:00
|
|
|
return;
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_state != State::Unconnected) {
|
2026-03-31 09:06:00 +00:00
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client not in unconnected state, disconnecting").arg(m_typeName);
|
2025-12-05 16:31:30 +00:00
|
|
|
disconnectFromServer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_socket->state() != QLocalSocket::UnconnectedState) {
|
2026-03-31 09:06:00 +00:00
|
|
|
qWarning().noquote(
|
|
|
|
|
) << QStringLiteral("%1 ipc client socket not in unconnected state, disconnecting").arg(m_typeName);
|
2025-12-05 16:31:30 +00:00
|
|
|
disconnectFromServer();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
m_retryCount = 0;
|
|
|
|
|
attemptConnection();
|
|
|
|
|
}
|
2025-12-05 16:31:30 +00:00
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
void IpcClient::attemptConnection()
|
|
|
|
|
{
|
2026-03-26 14:17:56 +00:00
|
|
|
const auto kRetryLimit = 3;
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
if (m_retryCount >= kRetryLimit) {
|
2026-03-31 09:06:00 +00:00
|
|
|
qWarning().noquote() << QStringLiteral("%1 ipc client failed to connect after %2 attempts")
|
|
|
|
|
.arg(m_typeName, QString::number(kRetryLimit));
|
2026-03-26 12:34:18 +00:00
|
|
|
m_state = State::Unconnected;
|
|
|
|
|
Q_EMIT connectionFailed();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-12-05 16:31:30 +00:00
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
if (m_retryCount == 0) {
|
2026-03-31 09:06:00 +00:00
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client connecting to server: %2").arg(m_typeName, m_socketName);
|
2026-03-26 12:34:18 +00:00
|
|
|
} else {
|
2026-03-31 09:06:00 +00:00
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client retrying connection, attempt: %2")
|
|
|
|
|
.arg(m_typeName, QString::number(m_retryCount + 1));
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
m_state = State::Connecting;
|
|
|
|
|
m_retryCount++;
|
|
|
|
|
|
|
|
|
|
connect(
|
|
|
|
|
m_socket, &QLocalSocket::connected, this,
|
|
|
|
|
[this] {
|
2026-03-26 14:17:56 +00:00
|
|
|
const auto versionId = QStringLiteral("%1+%2").arg(kVersion, kVersionGitSha);
|
2026-03-31 09:06:00 +00:00
|
|
|
m_socket->write(QStringLiteral("hello=%1\n").arg(versionId).toUtf8());
|
|
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client sent hello with version: %2").arg(m_typeName, versionId);
|
2026-03-26 12:34:18 +00:00
|
|
|
},
|
|
|
|
|
Qt::SingleShotConnection
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
connect(
|
|
|
|
|
m_socket, &QLocalSocket::errorOccurred, this,
|
|
|
|
|
[this] {
|
2026-03-31 09:06:00 +00:00
|
|
|
qWarning().noquote(
|
|
|
|
|
) << QStringLiteral("%1 ipc client failed to connect: %2").arg(m_typeName, m_socket->errorString());
|
2026-03-26 12:34:18 +00:00
|
|
|
m_socket->disconnectFromServer();
|
|
|
|
|
m_state = State::Unconnected;
|
|
|
|
|
QTimer::singleShot(0, this, &IpcClient::attemptConnection);
|
|
|
|
|
},
|
|
|
|
|
Qt::SingleShotConnection
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
m_socket->connectToServer(m_socketName);
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IpcClient::disconnectFromServer()
|
|
|
|
|
{
|
|
|
|
|
m_state = State::Disconnecting;
|
2026-03-31 09:06:00 +00:00
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client disconnecting from server").arg(m_typeName);
|
2025-12-05 16:31:30 +00:00
|
|
|
m_socket->disconnectFromServer();
|
|
|
|
|
m_state = State::Unconnected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IpcClient::handleDisconnected()
|
|
|
|
|
{
|
2026-03-26 12:34:18 +00:00
|
|
|
if (m_state == State::Connecting) {
|
|
|
|
|
return;
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-31 09:06:00 +00:00
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client disconnected from server").arg(m_typeName);
|
2026-03-26 12:34:18 +00:00
|
|
|
const auto wasConnected = m_state == State::Connected;
|
2025-12-05 16:31:30 +00:00
|
|
|
m_state = State::Unconnected;
|
2026-03-26 12:34:18 +00:00
|
|
|
|
|
|
|
|
if (wasConnected) {
|
|
|
|
|
Q_EMIT connectionFailed();
|
|
|
|
|
}
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IpcClient::handleErrorOccurred()
|
|
|
|
|
{
|
2026-03-26 12:34:18 +00:00
|
|
|
if (m_state == State::Connecting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-31 09:06:00 +00:00
|
|
|
qWarning().noquote() << QStringLiteral("%1 ipc client error: %2").arg(m_typeName, m_socket->errorString());
|
2025-12-05 16:31:30 +00:00
|
|
|
|
|
|
|
|
if (m_state == State::Connected) {
|
2026-03-26 12:34:18 +00:00
|
|
|
disconnectFromServer();
|
2025-12-05 16:31:30 +00:00
|
|
|
Q_EMIT connectionFailed();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
void IpcClient::handleReadyRead()
|
2025-12-05 16:31:30 +00:00
|
|
|
{
|
2026-03-26 12:34:18 +00:00
|
|
|
QByteArray data = m_readBuffer + m_socket->readAll();
|
|
|
|
|
m_readBuffer.clear();
|
|
|
|
|
|
|
|
|
|
while (data.contains('\n')) {
|
|
|
|
|
const auto index = data.indexOf('\n');
|
|
|
|
|
const auto message = QString::fromUtf8(data.left(index));
|
|
|
|
|
data.remove(0, index + 1);
|
|
|
|
|
|
2026-03-31 09:06:00 +00:00
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client message: %2").arg(m_typeName, message);
|
2026-03-26 12:34:18 +00:00
|
|
|
const auto parts = message.split('=');
|
|
|
|
|
if (parts.isEmpty()) {
|
2026-03-31 09:06:00 +00:00
|
|
|
qWarning().noquote() << QStringLiteral("%1 ipc client got invalid message: %2").arg(m_typeName, message);
|
2026-03-26 12:34:18 +00:00
|
|
|
continue;
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 14:17:56 +00:00
|
|
|
if (m_state == State::Connecting) {
|
|
|
|
|
handleHandshakeMessage(parts);
|
2026-03-26 12:34:18 +00:00
|
|
|
continue;
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
processCommand(parts.at(0), parts);
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
if (!data.isEmpty()) {
|
|
|
|
|
m_readBuffer = data;
|
|
|
|
|
}
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 14:17:56 +00:00
|
|
|
void IpcClient::handleHandshakeMessage(const QStringList &parts)
|
|
|
|
|
{
|
|
|
|
|
if (parts.at(0) != "hello") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto versionId = QStringLiteral("%1+%2").arg(kVersion, kVersionGitSha);
|
|
|
|
|
const auto serverVersion = parts.size() >= 2 ? parts.at(1) : QString();
|
|
|
|
|
if (serverVersion != versionId) {
|
2026-03-31 09:06:00 +00:00
|
|
|
qCritical().noquote(
|
|
|
|
|
) << QStringLiteral("%1 ipc version mismatch (client: %2 , server: %3)").arg(m_typeName, versionId, serverVersion);
|
2026-03-26 14:17:56 +00:00
|
|
|
disconnectFromServer();
|
|
|
|
|
Q_EMIT connectionFailed();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_state = State::Connected;
|
2026-03-31 09:06:00 +00:00
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client connected").arg(m_typeName);
|
2026-03-26 14:17:56 +00:00
|
|
|
Q_EMIT connected();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
void IpcClient::sendMessage(const QString &message)
|
2025-12-05 16:31:30 +00:00
|
|
|
{
|
2026-03-26 12:34:18 +00:00
|
|
|
if (m_state != State::Connected) {
|
2026-03-31 09:06:00 +00:00
|
|
|
qWarning().noquote() << QStringLiteral("%1 cannot send command, ipc client not connected").arg(m_typeName);
|
2026-03-26 12:34:18 +00:00
|
|
|
return;
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 12:34:18 +00:00
|
|
|
m_socket->write(message.toUtf8() + "\n");
|
2026-03-31 09:06:00 +00:00
|
|
|
qDebug().noquote() << QStringLiteral("%1 ipc client sent message: %2").arg(m_typeName, message);
|
2025-12-05 16:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace deskflow::gui::ipc
|