Qt remote objects IPC
This commit is contained in:
parent
c4df9c004b
commit
048a673d31
18 changed files with 340 additions and 25 deletions
12
ipc/ipc.h
Normal file
12
ipc/ipc.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef IPC_H
|
||||
#define IPC_H
|
||||
|
||||
#include <QString>
|
||||
|
||||
#define IPC_SERVICE_URL "local:AmneziaVpnIpcInterface"
|
||||
|
||||
namespace amnezia {
|
||||
inline QString getIpcProcessUrl(int pid) { return QString("%1_%2").arg(IPC_SERVICE_URL).arg(pid); }
|
||||
}
|
||||
|
||||
#endif // IPC_H
|
||||
32
ipc/ipcinterface.rep
Normal file
32
ipc/ipcinterface.rep
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include <QtCore>
|
||||
|
||||
class IpcInterface
|
||||
{
|
||||
SLOT( int createPrivilegedProcess() ); // return local pid
|
||||
//SIGNAL(sendMessage(const QByteArray &message));
|
||||
};
|
||||
|
||||
class IpcProcessInterface
|
||||
{
|
||||
SLOT( start(const QString &program, const QStringList &args) );
|
||||
SLOT( start() );
|
||||
SLOT( close() );
|
||||
|
||||
SLOT( setArguments(const QStringList &arguments) );
|
||||
SLOT( setInputChannelMode(QProcess::InputChannelMode mode) );
|
||||
SLOT( setNativeArguments(const QString &arguments) );
|
||||
SLOT( setProcessChannelMode(QProcess::ProcessChannelMode mode) );
|
||||
SLOT( setProgram(const QString &program) );
|
||||
SLOT( setWorkingDirectory(const QString &dir) );
|
||||
|
||||
SLOT( QByteArray readAllStandardError() );
|
||||
SLOT( QByteArray readAllStandardOutput() );
|
||||
|
||||
|
||||
SIGNAL( errorOccurred(QProcess::ProcessError error) );
|
||||
SIGNAL( finished(int exitCode, QProcess::ExitStatus exitStatus) );
|
||||
SIGNAL( readyReadStandardError() );
|
||||
SIGNAL( readyReadStandardOutput() );
|
||||
SIGNAL( started() );
|
||||
SIGNAL( stateChanged(QProcess::ProcessState newState) );
|
||||
};
|
||||
20
ipc/ipcserver.cpp
Normal file
20
ipc/ipcserver.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include "ipcserver.h"
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
IpcServer::IpcServer(QObject *parent):
|
||||
IpcInterfaceSource(parent)
|
||||
{}
|
||||
|
||||
int IpcServer::createPrivilegedProcess()
|
||||
{
|
||||
m_localpid++;
|
||||
|
||||
ProcessDescriptor pd;
|
||||
pd.serverNode->setHostUrl(QUrl(amnezia::getIpcProcessUrl(m_localpid)));
|
||||
pd.serverNode->enableRemoting(pd.ipcProcess.data());
|
||||
|
||||
m_processes.insert(m_localpid, pd);
|
||||
|
||||
return m_localpid;
|
||||
}
|
||||
32
ipc/ipcserver.h
Normal file
32
ipc/ipcserver.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#ifndef IPCSERVER_H
|
||||
#define IPCSERVER_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "ipc.h"
|
||||
#include "ipcserverprocess.h"
|
||||
|
||||
#include "rep_ipcinterface_source.h"
|
||||
|
||||
class IpcServer : public IpcInterfaceSource
|
||||
{
|
||||
public:
|
||||
explicit IpcServer(QObject *parent = nullptr);
|
||||
virtual int createPrivilegedProcess() override;
|
||||
|
||||
private:
|
||||
int m_localpid = 0;
|
||||
|
||||
struct ProcessDescriptor {
|
||||
ProcessDescriptor (QObject *parent = nullptr) {
|
||||
serverNode = QSharedPointer<QRemoteObjectHost>(new QRemoteObjectHost(parent));
|
||||
ipcProcess = QSharedPointer<IpcServerProcess>(new IpcServerProcess(parent));
|
||||
}
|
||||
QSharedPointer<IpcServerProcess> ipcProcess;
|
||||
QSharedPointer<QRemoteObjectHost> serverNode;
|
||||
};
|
||||
|
||||
QMap<int, ProcessDescriptor> m_processes;
|
||||
};
|
||||
|
||||
#endif // IPCSERVER_H
|
||||
71
ipc/ipcserverprocess.cpp
Normal file
71
ipc/ipcserverprocess.cpp
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
#include "ipcserverprocess.h"
|
||||
#include <QProcess>
|
||||
|
||||
IpcServerProcess::IpcServerProcess(QObject *parent) :
|
||||
IpcProcessInterfaceSource(parent),
|
||||
m_process(QSharedPointer<QProcess>(new QProcess(this)))
|
||||
{
|
||||
connect(m_process.data(), &QProcess::errorOccurred, this, &IpcServerProcess::errorOccurred);
|
||||
connect(m_process.data(), QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), this, &IpcServerProcess::finished);
|
||||
connect(m_process.data(), &QProcess::readyReadStandardError, this, &IpcServerProcess::readyReadStandardError);
|
||||
connect(m_process.data(), &QProcess::readyReadStandardOutput, this, &IpcServerProcess::readyReadStandardOutput);
|
||||
connect(m_process.data(), &QProcess::started, this, &IpcServerProcess::started);
|
||||
connect(m_process.data(), &QProcess::stateChanged, this, &IpcServerProcess::stateChanged);
|
||||
}
|
||||
|
||||
void IpcServerProcess::start(const QString &program, const QStringList &args)
|
||||
{
|
||||
m_process->start(program, args);
|
||||
}
|
||||
|
||||
void IpcServerProcess::start()
|
||||
{
|
||||
m_process->start();
|
||||
qDebug() << "IpcServerProcess started, " << m_process->arguments();
|
||||
}
|
||||
|
||||
void IpcServerProcess::close()
|
||||
{
|
||||
m_process->close();
|
||||
}
|
||||
|
||||
void IpcServerProcess::setArguments(const QStringList &arguments)
|
||||
{
|
||||
m_process->setArguments(arguments);
|
||||
qDebug() << "IpcServerProcess started, " << arguments;
|
||||
}
|
||||
|
||||
void IpcServerProcess::setInputChannelMode(QProcess::InputChannelMode mode)
|
||||
{
|
||||
m_process->setInputChannelMode(mode);
|
||||
}
|
||||
|
||||
void IpcServerProcess::setNativeArguments(const QString &arguments)
|
||||
{
|
||||
m_process->setNativeArguments(arguments);
|
||||
}
|
||||
|
||||
void IpcServerProcess::setProcessChannelMode(QProcess::ProcessChannelMode mode)
|
||||
{
|
||||
m_process->setProcessChannelMode(mode);
|
||||
}
|
||||
|
||||
void IpcServerProcess::setProgram(const QString &program)
|
||||
{
|
||||
m_process->setProgram(program);
|
||||
}
|
||||
|
||||
void IpcServerProcess::setWorkingDirectory(const QString &dir)
|
||||
{
|
||||
m_process->setWorkingDirectory(dir);
|
||||
}
|
||||
|
||||
QByteArray IpcServerProcess::readAllStandardError()
|
||||
{
|
||||
return m_process->readAllStandardError();
|
||||
}
|
||||
|
||||
QByteArray IpcServerProcess::readAllStandardOutput()
|
||||
{
|
||||
return m_process->readAllStandardOutput();
|
||||
}
|
||||
34
ipc/ipcserverprocess.h
Normal file
34
ipc/ipcserverprocess.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef IPCSERVERPROCESS_H
|
||||
#define IPCSERVERPROCESS_H
|
||||
|
||||
#include <QObject>
|
||||
#include "rep_ipcinterface_source.h"
|
||||
|
||||
|
||||
class IpcServerProcess : public IpcProcessInterfaceSource
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit IpcServerProcess(QObject *parent = nullptr);
|
||||
|
||||
void start(const QString &program, const QStringList &args) override;
|
||||
void start() override;
|
||||
void close() override;
|
||||
|
||||
void setArguments(const QStringList &arguments) override;
|
||||
void setInputChannelMode(QProcess::InputChannelMode mode) override;
|
||||
void setNativeArguments(const QString &arguments) override;
|
||||
void setProcessChannelMode(QProcess::ProcessChannelMode mode) override;
|
||||
void setProgram(const QString &program) override;
|
||||
void setWorkingDirectory(const QString &dir) override;
|
||||
|
||||
QByteArray readAllStandardError() override;
|
||||
QByteArray readAllStandardOutput() override;
|
||||
|
||||
signals:
|
||||
|
||||
private:
|
||||
QSharedPointer<QProcess> m_process;
|
||||
};
|
||||
|
||||
#endif // IPCSERVERPROCESS_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue