Qt Remote objects done
This commit is contained in:
parent
048a673d31
commit
b2392c1943
15 changed files with 240 additions and 423 deletions
|
|
@ -11,7 +11,6 @@ include("3rd/QtSsh/src/botan/botan.pri")
|
|||
|
||||
HEADERS += \
|
||||
../ipc/ipc.h \
|
||||
communicator.h \
|
||||
core/defs.h \
|
||||
core/errorstrings.h \
|
||||
core/ipcclient.h \
|
||||
|
|
@ -19,7 +18,6 @@ HEADERS += \
|
|||
core/servercontroller.h \
|
||||
debug.h \
|
||||
defines.h \
|
||||
localclient.h \
|
||||
managementserver.h \
|
||||
message.h \
|
||||
protocols/shadowsocksvpnprotocol.h \
|
||||
|
|
@ -33,12 +31,10 @@ HEADERS += \
|
|||
protocols/openvpnprotocol.h \
|
||||
|
||||
SOURCES += \
|
||||
communicator.cpp \
|
||||
core/ipcclient.cpp \
|
||||
core/openvpnconfigurator.cpp \
|
||||
core/servercontroller.cpp \
|
||||
debug.cpp \
|
||||
localclient.cpp \
|
||||
main.cpp \
|
||||
managementserver.cpp \
|
||||
message.cpp \
|
||||
|
|
|
|||
|
|
@ -1,79 +0,0 @@
|
|||
#include "communicator.h"
|
||||
#include "defines.h"
|
||||
#include "localclient.h"
|
||||
#include "utils.h"
|
||||
|
||||
Communicator::Communicator(QObject* parent) : QObject(parent),
|
||||
m_localClient(nullptr)
|
||||
{
|
||||
connectToServer();
|
||||
}
|
||||
|
||||
Communicator::~Communicator()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void Communicator::connectToServer()
|
||||
{
|
||||
if (m_localClient) {
|
||||
delete m_localClient;
|
||||
}
|
||||
|
||||
m_localClient = new LocalClient(this);
|
||||
connect(m_localClient, &LocalClient::connected, this, &Communicator::onConnected);
|
||||
connect(m_localClient, &LocalClient::lineAvailable, this, &Communicator::onLineAvailable);
|
||||
|
||||
m_localClient->connectToServer(Utils::serverName());
|
||||
}
|
||||
|
||||
void Communicator::onConnected()
|
||||
{
|
||||
qDebug().noquote() << QString("Connected to local server '%1'").arg(m_localClient->serverName());
|
||||
Message message(Message::State::Initialize, QStringList({"Client"}));
|
||||
sendMessage(message);
|
||||
}
|
||||
|
||||
void Communicator::onLineAvailable(const QString& line)
|
||||
{
|
||||
Message message(line);
|
||||
if (!message.isValid()) {
|
||||
qDebug() << "Message is not valid";
|
||||
return;
|
||||
}
|
||||
|
||||
emit messageReceived(message);
|
||||
}
|
||||
|
||||
bool Communicator::isConnected() const
|
||||
{
|
||||
if (!m_localClient) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return m_localClient->connectedState();
|
||||
}
|
||||
|
||||
QString Communicator::readData()
|
||||
{
|
||||
return QString();
|
||||
}
|
||||
|
||||
bool Communicator::writeData(const QString& data)
|
||||
{
|
||||
return m_localClient->write(data.toUtf8());
|
||||
}
|
||||
|
||||
void Communicator::sendMessage(const Message& message)
|
||||
{
|
||||
if (!isConnected()) {
|
||||
return;
|
||||
}
|
||||
const QString data = message.toString();
|
||||
bool status = writeData(data + "\n");
|
||||
|
||||
qDebug().noquote() << QString("Send message '%1',%2 status '%2'").
|
||||
arg(static_cast<int>(message.state())).
|
||||
arg(data).
|
||||
arg(Utils::toString(status));
|
||||
}
|
||||
|
|
@ -1,41 +0,0 @@
|
|||
#ifndef COMMUNICATOR_H
|
||||
#define COMMUNICATOR_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QStringList>
|
||||
|
||||
#include "message.h"
|
||||
|
||||
class LocalClient;
|
||||
|
||||
class Communicator : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Communicator(QObject* parent = nullptr);
|
||||
~Communicator();
|
||||
|
||||
bool isConnected() const;
|
||||
void sendMessage(const Message& message);
|
||||
|
||||
signals:
|
||||
void messageReceived(const Message& message);
|
||||
|
||||
void comminicatorConnected();
|
||||
void comminicatorDisconnected();
|
||||
|
||||
protected slots:
|
||||
void onConnected();
|
||||
void onLineAvailable(const QString& line);
|
||||
|
||||
protected:
|
||||
QString readData();
|
||||
bool writeData(const QString& data);
|
||||
void connectToServer();
|
||||
|
||||
LocalClient* m_localClient;
|
||||
};
|
||||
|
||||
|
||||
#endif // COMMUNICATOR_H
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
#include <QDebug>
|
||||
#include <QtNetwork>
|
||||
|
||||
#include "localclient.h"
|
||||
|
||||
LocalClient::LocalClient(QObject *parent) : QObject(parent),
|
||||
m_socket(new QLocalSocket(this))
|
||||
{
|
||||
m_in.setDevice(m_socket);
|
||||
m_in.setVersion(QDataStream::Qt_5_10);
|
||||
|
||||
connect(m_socket, &QLocalSocket::readyRead, this, &LocalClient::onReadyRead);
|
||||
connect(m_socket, &QLocalSocket::connected, this, &LocalClient::onConnected);
|
||||
connect(m_socket, QOverload<QLocalSocket::LocalSocketError>::of(&QLocalSocket::error), this, &LocalClient::displayError);
|
||||
}
|
||||
|
||||
void LocalClient::connectToServer(const QString& name)
|
||||
{
|
||||
m_blockSize = 0;
|
||||
m_socket->abort();
|
||||
m_socket->connectToServer(name);
|
||||
}
|
||||
|
||||
QString LocalClient::serverName() const
|
||||
{
|
||||
return m_socket->serverName();
|
||||
}
|
||||
|
||||
void LocalClient::onConnected()
|
||||
{
|
||||
emit connected();
|
||||
}
|
||||
|
||||
bool LocalClient::connectedState() const
|
||||
{
|
||||
return (m_socket->state() == QLocalSocket::ConnectedState);
|
||||
}
|
||||
|
||||
quint64 LocalClient::write(const QByteArray& data)
|
||||
{
|
||||
return m_socket->write(data);
|
||||
}
|
||||
|
||||
void LocalClient::onReadyRead()
|
||||
{
|
||||
if (m_socket->canReadLine()) {
|
||||
char buf[1024];
|
||||
qint64 lineLength = m_socket->readLine(buf, sizeof(buf));
|
||||
if (lineLength != -1) {
|
||||
QString line = buf;
|
||||
line = line.simplified();
|
||||
qDebug().noquote() << QString("Read line: '%1'").arg(line);
|
||||
emit lineAvailable(line);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LocalClient::displayError(QLocalSocket::LocalSocketError socketError)
|
||||
{
|
||||
Q_UNUSED(socketError)
|
||||
qDebug().noquote() << QString("The following error occurred: %1.").arg(m_socket->errorString());
|
||||
}
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
#ifndef LOCALCLIENT_H
|
||||
#define LOCALCLIENT_H
|
||||
|
||||
#include <QDataStream>
|
||||
#include <QLocalSocket>
|
||||
|
||||
class LocalClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LocalClient(QObject *parent = nullptr);
|
||||
|
||||
QString serverName() const;
|
||||
bool connectedState() const;
|
||||
quint64 write(const QByteArray& data);
|
||||
void connectToServer(const QString& name);
|
||||
|
||||
signals:
|
||||
void connected();
|
||||
void lineAvailable(const QString& line);
|
||||
|
||||
private slots:
|
||||
void displayError(QLocalSocket::LocalSocketError socketError);
|
||||
void onConnected();
|
||||
void onReadyRead();
|
||||
|
||||
private:
|
||||
QLocalSocket* m_socket;
|
||||
QDataStream m_in;
|
||||
quint32 m_blockSize;
|
||||
};
|
||||
|
||||
#endif // LOCALCLIENT_H
|
||||
|
|
@ -15,7 +15,7 @@ OpenVpnProtocol::OpenVpnProtocol(const QString& args, QObject* parent) :
|
|||
//m_requestFromUserToStop(false)
|
||||
{
|
||||
setConfigFile(args);
|
||||
connect(m_communicator, &Communicator::messageReceived, this, &OpenVpnProtocol::onMessageReceived);
|
||||
//connect(m_communicator, &Communicator::messageReceived, this, &OpenVpnProtocol::onMessageReceived);
|
||||
connect(&m_managementServer, &ManagementServer::readyRead, this, &OpenVpnProtocol::onReadyReadDataFromManagementServer);
|
||||
}
|
||||
|
||||
|
|
@ -172,14 +172,26 @@ ErrorCode OpenVpnProtocol::start()
|
|||
return ErrorCode::AmneziaServiceConnectionFailed;
|
||||
}
|
||||
process->setProgram(openVpnExecPath());
|
||||
process->setArguments(QStringList() << "--config" << configPath()<<
|
||||
"--management"<< m_managementHost<< QString::number(m_managementPort)<<
|
||||
"--management-client"<<
|
||||
"--log-append"<< vpnLogFileNamePath);
|
||||
QStringList arguments({"--config" , configPath(),
|
||||
"--management", m_managementHost, QString::number(m_managementPort),
|
||||
"--management-client",
|
||||
"--log-append", vpnLogFileNamePath
|
||||
});
|
||||
process->setArguments(arguments);
|
||||
|
||||
qDebug() << arguments.join(" ");
|
||||
connect(process.data(), &IpcProcessInterfaceReplica::errorOccurred, [&](QProcess::ProcessError error) {
|
||||
qDebug() << "IpcProcessInterfaceReplica errorOccurred" << error;
|
||||
});
|
||||
|
||||
connect(process.data(), &IpcProcessInterfaceReplica::stateChanged, [&](QProcess::ProcessState newState) {
|
||||
qDebug() << "IpcProcessInterfaceReplica stateChanged" << newState;
|
||||
});
|
||||
|
||||
process->start();
|
||||
|
||||
//m_communicator->sendMessage(Message(Message::State::StartRequest, args));
|
||||
startTimeoutTimer();
|
||||
//startTimeoutTimer();
|
||||
|
||||
return ErrorCode::NoError;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#include "vpnprotocol.h"
|
||||
#include "core/errorstrings.h"
|
||||
|
||||
Communicator* VpnProtocol::m_communicator = nullptr;
|
||||
//Communicator* VpnProtocol::m_communicator = nullptr;
|
||||
|
||||
VpnProtocol::VpnProtocol(const QString& args, QObject* parent)
|
||||
: QObject(parent),
|
||||
|
|
@ -20,17 +20,17 @@ VpnProtocol::VpnProtocol(const QString& args, QObject* parent)
|
|||
Q_UNUSED(args)
|
||||
}
|
||||
|
||||
void VpnProtocol::initializeCommunicator(QObject* parent)
|
||||
{
|
||||
if (!m_communicator) {
|
||||
m_communicator = new Communicator(parent);
|
||||
}
|
||||
}
|
||||
//void VpnProtocol::initializeCommunicator(QObject* parent)
|
||||
//{
|
||||
// if (!m_communicator) {
|
||||
// m_communicator = new Communicator(parent);
|
||||
// }
|
||||
//}
|
||||
|
||||
Communicator* VpnProtocol::communicator()
|
||||
{
|
||||
return m_communicator;
|
||||
}
|
||||
//Communicator* VpnProtocol::communicator()
|
||||
//{
|
||||
// return m_communicator;
|
||||
//}
|
||||
|
||||
void VpnProtocol::setLastError(ErrorCode lastError)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ public:
|
|||
|
||||
static Communicator* communicator();
|
||||
static QString textConnectionState(ConnectionState connectionState);
|
||||
static void initializeCommunicator(QObject* parent = nullptr);
|
||||
//static void initializeCommunicator(QObject* parent = nullptr);
|
||||
|
||||
|
||||
virtual bool onConnected() const;
|
||||
|
|
@ -54,7 +54,7 @@ protected:
|
|||
virtual void setBytesChanged(quint64 receivedBytes, quint64 sentBytes);
|
||||
virtual void setConnectionState(VpnProtocol::ConnectionState state);
|
||||
|
||||
static Communicator* m_communicator;
|
||||
//static Communicator* m_communicator;
|
||||
|
||||
ConnectionState m_connectionState;
|
||||
QString m_routeGateway;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
VpnConnection::VpnConnection(QObject* parent) : QObject(parent)
|
||||
{
|
||||
VpnProtocol::initializeCommunicator(parent);
|
||||
//VpnProtocol::initializeCommunicator(parent);
|
||||
}
|
||||
|
||||
void VpnConnection::onBytesChanged(quint64 receivedBytes, quint64 sentBytes)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue