core/ipc: add ipc server/client

Currently can only kill a remote instance.
This commit is contained in:
outfoxxed 2024-08-30 21:45:20 -07:00
parent 13b6eeaa22
commit da043e092a
Signed by: outfoxxed
GPG key ID: 4C88A185FB89301E
14 changed files with 710 additions and 120 deletions

74
src/core/ipc.hpp Normal file
View file

@ -0,0 +1,74 @@
#pragma once
#include <functional>
#include <qlocalserver.h>
#include <qlocalsocket.h>
#include <qobject.h>
#include <qtmetamacros.h>
namespace qs::ipc {
enum class IpcCommand : quint8 {
Unknown = 0,
Kill,
};
class IpcServer: public QObject {
Q_OBJECT;
public:
explicit IpcServer(const QString& path);
static void start();
private slots:
void onNewConnection();
private:
QLocalServer server;
};
class IpcServerConnection: public QObject {
Q_OBJECT;
public:
explicit IpcServerConnection(QLocalSocket* socket, IpcServer* server);
private slots:
void onDisconnected();
void onReadyRead();
private:
QLocalSocket* socket;
QDataStream stream;
};
class IpcClient: public QObject {
Q_OBJECT;
public:
explicit IpcClient(const QString& path);
[[nodiscard]] bool isConnected() const;
void waitForConnected();
void waitForDisconnected();
void kill();
[[nodiscard]] static bool
connect(const QString& id, const std::function<void(IpcClient& client)>& callback);
signals:
void connected();
void disconnected();
private slots:
static void onError(QLocalSocket::LocalSocketError error);
private:
QLocalSocket socket;
QDataStream stream;
};
} // namespace qs::ipc