Files
amnezia-client/client/core/ipcclient.cpp
T

112 lines
3.3 KiB
C++
Raw Normal View History

2021-02-02 01:47:40 +03:00
#include "ipcclient.h"
#include <QRemoteObjectNode>
2021-10-26 12:59:20 +03:00
IpcClient *IpcClient::m_instance = nullptr;
IpcClient::IpcClient(QObject *parent) : QObject(parent)
{
}
IpcClient::~IpcClient()
{
if (m_localSocket) m_localSocket->close();
}
bool IpcClient::isSocketConnected() const
2021-02-02 01:47:40 +03:00
{
2021-10-26 12:59:20 +03:00
return m_isSocketConnected;
2021-02-02 01:47:40 +03:00
}
2021-10-26 12:59:20 +03:00
IpcClient *IpcClient::Instance()
2021-02-18 15:00:41 +03:00
{
2021-10-26 12:59:20 +03:00
return m_instance;
}
2021-02-18 15:00:41 +03:00
2021-10-26 12:59:20 +03:00
QSharedPointer<IpcInterfaceReplica> IpcClient::Interface()
{
if (!Instance()) return nullptr;
return Instance()->m_ipcClient;
}
bool IpcClient::init(IpcClient *instance)
{
m_instance = instance;
Instance()->m_localSocket = new QLocalSocket(Instance());
connect(Instance()->m_localSocket.data(), &QLocalSocket::connected, &Instance()->m_ClientNode, []() {
Instance()->m_ClientNode.addClientSideConnection(Instance()->m_localSocket.data());
Instance()->m_ipcClient.reset(Instance()->m_ClientNode.acquire<IpcInterfaceReplica>());
Instance()->m_ipcClient->waitForSource(1000);
if (!Instance()->m_ipcClient->isReplicaValid()) {
qWarning() << "IpcClient replica is not connected!";
}
});
connect(Instance()->m_localSocket, &QLocalSocket::disconnected, [instance](){
instance->m_isSocketConnected = false;
});
Instance()->m_localSocket->connectToServer(amnezia::getIpcServiceUrl());
Instance()->m_localSocket->waitForConnected();
if (!Instance()->m_ipcClient) {
2021-02-18 15:00:41 +03:00
qDebug() << "IpcClient::init failed";
return false;
}
2021-10-26 12:59:20 +03:00
qDebug() << "IpcClient::init succeed";
return Instance()->m_ipcClient->isReplicaValid();
2021-02-18 15:00:41 +03:00
}
2021-11-19 19:02:39 +03:00
QSharedPointer<PrivilegedProcess> IpcClient::CreatePrivilegedProcess()
2021-02-02 01:47:40 +03:00
{
2021-10-26 12:59:20 +03:00
if (! Instance()->m_ipcClient || ! Instance()->m_ipcClient->isReplicaValid()) {
2021-02-03 15:42:36 +03:00
qWarning() << "IpcClient::createPrivilegedProcess : IpcClient IpcClient replica is not valid";
return nullptr;
}
2021-02-02 01:47:40 +03:00
2021-10-26 12:59:20 +03:00
QRemoteObjectPendingReply<int> futureResult = Instance()->m_ipcClient->createPrivilegedProcess();
2024-03-20 22:53:56 +02:00
futureResult.waitForFinished(5000);
2021-02-02 01:47:40 +03:00
int pid = futureResult.returnValue();
2021-02-03 15:42:36 +03:00
2021-02-18 15:00:41 +03:00
auto pd = QSharedPointer<ProcessDescriptor>(new ProcessDescriptor());
2021-10-26 12:59:20 +03:00
Instance()->m_processNodes.insert(pid, pd);
2021-02-03 15:42:36 +03:00
2021-02-18 15:00:41 +03:00
pd->localSocket.reset(new QLocalSocket(pd->replicaNode.data()));
2021-02-03 15:42:36 +03:00
2021-02-18 15:00:41 +03:00
connect(pd->localSocket.data(), &QLocalSocket::connected, pd->replicaNode.data(), [pd]() {
pd->replicaNode->addClientSideConnection(pd->localSocket.data());
2021-02-03 15:42:36 +03:00
2021-11-19 19:02:39 +03:00
IpcProcessInterfaceReplica *repl = pd->replicaNode->acquire<IpcProcessInterfaceReplica>();
PrivilegedProcess *priv = static_cast<PrivilegedProcess *>(repl);
pd->ipcProcess.reset(priv);
2021-02-18 15:00:41 +03:00
if (!pd->ipcProcess) {
2021-11-19 19:02:39 +03:00
qWarning() << "Acquire PrivilegedProcess failed";
2021-02-18 15:00:41 +03:00
}
else {
pd->ipcProcess->waitForSource(1000);
if (!pd->ipcProcess->isReplicaValid()) {
2021-11-19 19:02:39 +03:00
qWarning() << "PrivilegedProcess replica is not connected!";
2021-02-18 15:00:41 +03:00
}
2021-11-19 19:02:39 +03:00
QObject::connect(pd->ipcProcess.data(), &PrivilegedProcess::destroyed, pd->ipcProcess.data(), [pd](){
2021-02-18 15:00:41 +03:00
pd->replicaNode->deleteLater();
});
2021-02-03 15:42:36 +03:00
}
2021-02-02 01:47:40 +03:00
});
2021-02-18 15:00:41 +03:00
pd->localSocket->connectToServer(amnezia::getIpcProcessUrl(pid));
pd->localSocket->waitForConnected();
2021-02-03 15:42:36 +03:00
2023-04-11 09:50:44 -04:00
auto processReplica = QSharedPointer<PrivilegedProcess>(pd->ipcProcess);
return processReplica;
2021-02-02 01:47:40 +03:00
}
2021-02-03 15:42:36 +03:00