mirror of
https://github.com/deskflow/deskflow.git
synced 2026-07-04 21:04:59 +08:00
Some checks failed
CodeQL Analysis / Analyze (cpp) (push) Has been cancelled
Continuous Integration / reuse-lint (push) Has been cancelled
Continuous Integration / pr-comment-flags (push) Has been cancelled
Continuous Integration / ci-passed (push) Has been cancelled
Continuous Integration / test-results (push) Has been cancelled
Continuous Integration / lint-check (push) Has been cancelled
Continuous Integration / analyse-valgrind (push) Has been cancelled
Continuous Integration / analyse-sonarcloud (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-DCMAKE_OSX_ARCHITECTURES="arm64" -DCMAKE_OSX_SYSROOT=/Applications/Xcode_15.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk name:macos-14-arm64 qt-install-dir:/Users/runner runs-o… (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-DCMAKE_OSX_ARCHITECTURES="x86_64" -DCMAKE_OSX_SYSROOT=/Applications/Xcode_15.0.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk name:macos-13-x64 qt-install-dir:/Users/runner runs-on… (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:archlinux:latest like:arch name:archlinux-x86_84 runs-on:ubuntu-latest timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:debian:trixie-slim like:debian name:debian-13-arm64 runs-on:ubuntu-24.04-arm timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:debian:trixie-slim like:debian name:debian-13-x86_64 runs-on:ubuntu-latest timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:fedora:40 like:fedora name:fedora-40-arm64 runs-on:ubuntu-24.04-arm timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:fedora:40 like:fedora name:fedora-40-x86_84 runs-on:ubuntu-latest timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:fedora:41 like:fedora name:fedora-41-arm64 runs-on:ubuntu-24.04-arm timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:fedora:41 like:fedora name:fedora-41-x86_64 runs-on:ubuntu-latest timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:fedora:42 like:fedora name:fedora-42-arm64 runs-on:ubuntu-24.04-arm timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:fedora:42 like:fedora name:fedora-42-x86_64 runs-on:ubuntu-latest timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:opensuse/tumbleweed:latest like:suse name:opensuse-arm64 runs-on:ubuntu-24.04-arm timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:opensuse/tumbleweed:latest like:suse name:opensuse-x86_84 runs-on:ubuntu-latest timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:ubuntu:25.04 like:debian name:ubuntu-25.04-arm64 runs-on:ubuntu-24.04-arm timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja -DCMAKE_INSTALL_PREFIX=/usr container:ubuntu:25.04 like:debian name:ubuntu-25.04-x86_64 runs-on:ubuntu-latest timeout:20]) (push) Has been cancelled
Continuous Integration / ${{ matrix.target.name }} (map[config-args:-G Ninja name:windows-2022-x64 qt-install-dir:C: runs-on:windows-2022 timeout:30]) (push) Has been cancelled
Continuous Integration / unix-${{ matrix.distro.name }} (map[name:freebsd]) (push) Has been cancelled
Continuous Integration / flatpak-${{matrix.flatpak.arch}} (map[arch:aarch64 runs-on:ubuntu-24.04-arm]) (push) Has been cancelled
Continuous Integration / flatpak-${{matrix.flatpak.arch}} (map[arch:x86_64 runs-on:ubuntu-latest]) (push) Has been cancelled
Continuous Integration / release (push) Has been cancelled
Continuous Integration / winget-publish (push) Has been cancelled
254 lines
6.4 KiB
C++
254 lines
6.4 KiB
C++
/*
|
|
* Deskflow -- mouse and keyboard sharing utility
|
|
* SPDX-FileCopyrightText: (C) 2025 Symless Ltd.
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
|
*/
|
|
|
|
#include "DaemonIpcClient.h"
|
|
|
|
#include "common/Constants.h"
|
|
|
|
#include <QDebug>
|
|
#include <QLocalSocket>
|
|
#include <QObject>
|
|
#include <QString>
|
|
|
|
namespace deskflow::gui::ipc {
|
|
|
|
const auto kTimeout = 1000;
|
|
const auto kRetryLimit = 3;
|
|
|
|
DaemonIpcClient::DaemonIpcClient(QObject *parent)
|
|
: QObject(parent),
|
|
m_socket{new QLocalSocket(this)} // NOSONAR - Qt memory
|
|
{
|
|
connect(m_socket, &QLocalSocket::disconnected, this, &DaemonIpcClient::handleDisconnected);
|
|
connect(m_socket, &QLocalSocket::errorOccurred, this, &DaemonIpcClient::handleErrorOccurred);
|
|
}
|
|
|
|
bool DaemonIpcClient::connectToServer()
|
|
{
|
|
if (m_state == State::Connecting) {
|
|
qWarning() << "daemon ipc client already connecting to server";
|
|
return false;
|
|
}
|
|
|
|
if (m_state != State::Unconnected) {
|
|
qDebug() << "daemon ipc client not in unconnected state, disconnecting";
|
|
disconnectFromServer();
|
|
}
|
|
|
|
if (m_socket->state() != QLocalSocket::UnconnectedState) {
|
|
qWarning() << "daemon ipc client socket not in unconnected state, disconnecting";
|
|
disconnectFromServer();
|
|
}
|
|
|
|
for (int i = 0; i < kRetryLimit; ++i) {
|
|
if (i == 0) {
|
|
qDebug() << "daemon ipc client connecting to server:" << kDaemonIpcName;
|
|
} else {
|
|
qDebug() << "daemon ipc client retrying connection, attempt:" << i + 1;
|
|
}
|
|
|
|
m_state = State::Connecting;
|
|
m_socket->connectToServer(kDaemonIpcName);
|
|
|
|
if (!m_socket->waitForConnected(kTimeout)) {
|
|
qWarning() << "daemon ipc client failed to connect";
|
|
disconnectFromServer();
|
|
continue;
|
|
}
|
|
|
|
if (!sendMessage("hello", "hello", false)) {
|
|
qWarning() << "daemon ipc client failed to send hello";
|
|
disconnectFromServer();
|
|
continue;
|
|
}
|
|
|
|
m_state = State::Connected;
|
|
qDebug() << "daemon ipc client connected";
|
|
Q_EMIT connected();
|
|
return true;
|
|
}
|
|
|
|
qWarning() << "daemon ipc client failed to connect after" << kRetryLimit << "attempts";
|
|
disconnectFromServer();
|
|
Q_EMIT connectionFailed();
|
|
return false;
|
|
}
|
|
|
|
void DaemonIpcClient::disconnectFromServer()
|
|
{
|
|
m_state = State::Disconnecting;
|
|
qDebug() << "daemon ipc client disconnecting from server";
|
|
m_socket->disconnectFromServer();
|
|
|
|
if (m_socket->state() != QLocalSocket::UnconnectedState) {
|
|
qDebug() << "daemon ipc client waiting for socket to disconnect";
|
|
m_socket->waitForDisconnected(kTimeout);
|
|
qDebug() << "daemon ipc client disconnected from server";
|
|
} else {
|
|
qDebug() << "daemon ipc client socket already disconnected";
|
|
}
|
|
|
|
m_state = State::Unconnected;
|
|
}
|
|
|
|
void DaemonIpcClient::handleDisconnected()
|
|
{
|
|
qDebug() << "daemon ipc client disconnected from server";
|
|
if (m_state == State::Connected) {
|
|
Q_EMIT connectionFailed();
|
|
}
|
|
|
|
m_state = State::Unconnected;
|
|
}
|
|
|
|
void DaemonIpcClient::handleErrorOccurred()
|
|
{
|
|
qWarning() << "daemon ipc client error:" << m_socket->errorString();
|
|
disconnectFromServer();
|
|
|
|
if (m_state == State::Connected) {
|
|
Q_EMIT connectionFailed();
|
|
}
|
|
}
|
|
|
|
bool DaemonIpcClient::sendMessage(const QString &message, const QString &expectAck, const bool expectConnected)
|
|
{
|
|
if (expectConnected && !isConnected()) {
|
|
qWarning() << "cannot send command, ipc client not connected";
|
|
return false;
|
|
}
|
|
|
|
QByteArray messageData = message.toUtf8() + "\n";
|
|
m_socket->write(messageData);
|
|
if (!m_socket->waitForBytesWritten(kTimeout)) {
|
|
qWarning() << "daemon ipc client failed to write command";
|
|
return false;
|
|
}
|
|
|
|
if (!expectAck.isEmpty()) {
|
|
qDebug() << "daemon ipc client waiting for ack: " << expectAck;
|
|
|
|
if (!m_socket->waitForReadyRead(kTimeout)) {
|
|
qWarning() << "daemon ipc client socket ready read timed out";
|
|
return false;
|
|
}
|
|
|
|
QByteArray response = m_socket->readAll();
|
|
if (response.isEmpty()) {
|
|
qWarning() << "daemon ipc client got empty response";
|
|
return false;
|
|
}
|
|
|
|
QString responseData = QString::fromUtf8(response);
|
|
if (responseData.isEmpty()) {
|
|
qWarning() << "daemon ipc client failed to convert response to string";
|
|
return false;
|
|
}
|
|
|
|
if (responseData != expectAck + "\n") {
|
|
qWarning() << "daemon ipc client got unexpected response: " << responseData;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
qDebug() << "daemon ipc client sent message: " << messageData;
|
|
return true;
|
|
}
|
|
|
|
bool DaemonIpcClient::keepAlive()
|
|
{
|
|
if (!isConnected() && !connectToServer()) {
|
|
qWarning() << "daemon ipc client keep alive failed to connect";
|
|
return false;
|
|
}
|
|
|
|
if (!sendMessage("noop")) {
|
|
qWarning() << "daemon ipc client keep alive ping failed, reconnecting";
|
|
connectToServer();
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool DaemonIpcClient::sendLogLevel(const QString &logLevel)
|
|
{
|
|
if (!keepAlive())
|
|
return false;
|
|
|
|
sendMessage("logLevel=" + logLevel);
|
|
return true;
|
|
}
|
|
|
|
bool DaemonIpcClient::sendStartProcess(const QString &command, bool elevate)
|
|
{
|
|
if (!keepAlive())
|
|
return false;
|
|
|
|
if (!sendMessage("elevate=" + (elevate ? QStringLiteral("yes") : QStringLiteral("no")))) {
|
|
return false;
|
|
}
|
|
|
|
if (!sendMessage("command=" + command)) {
|
|
return false;
|
|
}
|
|
|
|
return sendMessage("start");
|
|
}
|
|
|
|
bool DaemonIpcClient::sendStopProcess()
|
|
{
|
|
return sendMessage("stop");
|
|
}
|
|
|
|
QString DaemonIpcClient::requestLogPath()
|
|
{
|
|
if (!keepAlive())
|
|
return QString();
|
|
|
|
if (!sendMessage("logPath", QString())) {
|
|
return QString();
|
|
}
|
|
|
|
if (!m_socket->waitForReadyRead(kTimeout)) {
|
|
qWarning() << "daemon ipc client failed to read log path response";
|
|
return QString();
|
|
}
|
|
|
|
QByteArray response = m_socket->readAll();
|
|
if (response.isEmpty()) {
|
|
qWarning() << "daemon ipc client got empty log path response";
|
|
return QString();
|
|
}
|
|
|
|
QString responseData = QString::fromUtf8(response);
|
|
if (responseData.isEmpty()) {
|
|
qWarning() << "daemon ipc client failed to convert log path response to string";
|
|
return QString();
|
|
}
|
|
|
|
// Trimming removes newline from end of message.
|
|
QStringList parts = responseData.trimmed().split("=");
|
|
if (parts.size() != 2) {
|
|
qWarning() << "daemon ipc client got invalid log path response: " << responseData;
|
|
return QString();
|
|
}
|
|
|
|
if (parts[0] != "logPath") {
|
|
qWarning() << "daemon ipc client got unexpected log path response: " << responseData;
|
|
return QString();
|
|
}
|
|
|
|
return parts[1];
|
|
}
|
|
|
|
bool DaemonIpcClient::sendClearSettings()
|
|
{
|
|
return sendMessage("clearSettings");
|
|
}
|
|
|
|
} // namespace deskflow::gui::ipc
|