mirror of
https://github.com/amnezia-vpn/amnezia-client.git
synced 2026-05-08 14:33:23 +00:00
* fix: replace localsocket by QtRO-embedded one * fix: make IpcClient initialization lazy
72 lines
2.1 KiB
C++
72 lines
2.1 KiB
C++
#ifndef IPCCLIENT_H
|
|
#define IPCCLIENT_H
|
|
|
|
#include <QLocalSocket>
|
|
#include <QObject>
|
|
|
|
#include "rep_ipc_interface_replica.h"
|
|
#include "rep_ipc_process_tun2socks_replica.h"
|
|
|
|
#include "privileged_process.h"
|
|
|
|
class IpcClient : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit IpcClient(QObject *parent = nullptr);
|
|
|
|
static IpcClient& Instance();
|
|
|
|
static QSharedPointer<IpcInterfaceReplica> Interface();
|
|
static QSharedPointer<IpcProcessTun2SocksReplica> InterfaceTun2Socks();
|
|
static QSharedPointer<PrivilegedProcess> CreatePrivilegedProcess();
|
|
|
|
template <typename Func>
|
|
static auto withInterface(Func func)
|
|
{
|
|
QSharedPointer<IpcInterfaceReplica> iface = Instance().m_interface;
|
|
using ReturnType = decltype(func(std::declval<QSharedPointer<IpcInterfaceReplica>>()));
|
|
|
|
if (iface.isNull() || !iface->waitForSource(1000) || !iface->isReplicaValid()) {
|
|
qWarning() << "IpcClient::withInterface(): Service is not running";
|
|
|
|
if constexpr (std::is_void_v<ReturnType>)
|
|
return;
|
|
else
|
|
return ReturnType{};
|
|
}
|
|
|
|
return func(iface);
|
|
}
|
|
|
|
template <typename OnSuccess, typename OnFailure>
|
|
static auto withInterface(OnSuccess onSuccess, OnFailure onFailure)
|
|
{
|
|
QSharedPointer<IpcInterfaceReplica> iface = Instance().m_interface;
|
|
if (iface.isNull() || !iface->waitForSource(1000) || !iface->isReplicaValid()) {
|
|
return onFailure();
|
|
}
|
|
|
|
return onSuccess(iface);
|
|
}
|
|
signals:
|
|
|
|
private:
|
|
QRemoteObjectNode m_node;
|
|
QSharedPointer<IpcInterfaceReplica> m_interface;
|
|
QSharedPointer<IpcProcessTun2SocksReplica> m_tun2socks;
|
|
|
|
struct ProcessDescriptor {
|
|
ProcessDescriptor () {
|
|
replicaNode = QSharedPointer<QRemoteObjectNode>(new QRemoteObjectNode());
|
|
ipcProcess = QSharedPointer<PrivilegedProcess>();
|
|
localSocket = QSharedPointer<QLocalSocket>();
|
|
}
|
|
QSharedPointer<PrivilegedProcess> ipcProcess;
|
|
QSharedPointer<QRemoteObjectNode> replicaNode;
|
|
QSharedPointer<QLocalSocket> localSocket;
|
|
};
|
|
};
|
|
|
|
#endif // IPCCLIENT_H
|