General improvements and bug fixes
This commit is contained in:
parent
15730b470e
commit
43028953e2
19 changed files with 208 additions and 114 deletions
|
|
@ -6,21 +6,21 @@
|
|||
#include "localserver.h"
|
||||
#include "utils.h"
|
||||
|
||||
LocalServer::LocalServer(const QString& name, QObject *parent) : QObject(parent),
|
||||
m_clientConnected(false),
|
||||
m_clientConnection(nullptr)
|
||||
LocalServer::LocalServer(QObject *parent) : QObject(parent),
|
||||
m_clientConnection(nullptr),
|
||||
m_clientConnected(false)
|
||||
{
|
||||
m_server = new QLocalServer(this);
|
||||
m_server->setSocketOptions(QLocalServer::WorldAccessOption);
|
||||
|
||||
if (!m_server->listen(name)) {
|
||||
if (!m_server->listen(Utils::serverName())) {
|
||||
qDebug() << QString("Unable to start the server: %1.").arg(m_server->errorString());
|
||||
return;
|
||||
}
|
||||
|
||||
connect(m_server, &QLocalServer::newConnection, this, &LocalServer::onNewConnection);
|
||||
|
||||
qDebug() << "Local server started";
|
||||
qDebug().noquote() << QString("Local server started on '%1'").arg(m_server->serverName());
|
||||
}
|
||||
|
||||
LocalServer::~LocalServer()
|
||||
|
|
@ -28,6 +28,8 @@ LocalServer::~LocalServer()
|
|||
m_clientConnected = false;
|
||||
m_server->disconnect();
|
||||
|
||||
QFile::remove(Utils::serverName());
|
||||
|
||||
qDebug() << "Local server stopped";
|
||||
}
|
||||
|
||||
|
|
@ -46,7 +48,7 @@ void LocalServer::onNewConnection()
|
|||
connect(m_clientConnection, &QLocalSocket::disconnected, this, &LocalServer::onDisconnected);
|
||||
m_clientConnected = true;
|
||||
|
||||
qDebug() << "On new connection";
|
||||
qDebug() << "New connection";
|
||||
|
||||
for(;;) {
|
||||
qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
|
||||
|
|
@ -69,7 +71,7 @@ void LocalServer::onNewConnection()
|
|||
|
||||
switch (icomingMessage.state()) {
|
||||
case Message::State::Initialize:
|
||||
sendMessage(Message(Message::State::Initialize, QStringList({"Pong"})));
|
||||
sendMessage(Message(Message::State::Initialize, QStringList({"Server"})));
|
||||
break;
|
||||
case Message::State::StartRequest:
|
||||
startProcess(icomingMessage.args());
|
||||
|
|
@ -161,7 +163,7 @@ void LocalServer::sendMessage(const Message& message)
|
|||
}
|
||||
|
||||
const QString data = message.toString();
|
||||
bool status = m_clientConnection->write(QString(data + "\n").toLocal8Bit());
|
||||
bool status = m_clientConnection->write(QString(data + "\n").toUtf8());
|
||||
|
||||
qDebug().noquote() << QString("Send message '%1', status '%2'").arg(data).arg(Utils::toString(status));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class LocalServer : public QObject
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit LocalServer(const QString& name, QObject* parent = nullptr);
|
||||
explicit LocalServer(QObject* parent = nullptr);
|
||||
~LocalServer();
|
||||
|
||||
bool isRunning() const;
|
||||
|
|
|
|||
|
|
@ -1,33 +1,42 @@
|
|||
#include <QSettings>
|
||||
#include <QDir>
|
||||
|
||||
#include "systemservice.h"
|
||||
#include "log.h"
|
||||
#include "defines.h"
|
||||
#include "localserver.h"
|
||||
#include "log.h"
|
||||
#include "systemservice.h"
|
||||
#include "utils.h"
|
||||
|
||||
int runApplication(int argc, char** argv)
|
||||
{
|
||||
QCoreApplication app(argc,argv);
|
||||
LocalServer localServer;
|
||||
if (!localServer.isRunning()) {
|
||||
return -1;
|
||||
}
|
||||
return app.exec();
|
||||
}
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#if !defined(Q_OS_WIN)
|
||||
// QtService stores service settings in SystemScope, which normally require root privileges.
|
||||
// To allow testing this example as non-root, we change the directory of the SystemScope settings file.
|
||||
QSettings::setPath(QSettings::NativeFormat, QSettings::SystemScope, QDir::tempPath());
|
||||
qWarning("(Example uses dummy settings file: %s/QtSoftware.conf)", QDir::tempPath().toLatin1().constData());
|
||||
#endif
|
||||
Utils::initializePath(Utils::systemLogPath());
|
||||
|
||||
Log::initialize();
|
||||
|
||||
if (argc == 2) {
|
||||
qInfo() << "Started as console application";
|
||||
QCoreApplication app(argc,argv);
|
||||
LocalServer localServer(SERVICE_NAME);
|
||||
if (!localServer.isRunning()) {
|
||||
return -1;
|
||||
}
|
||||
return app.exec();
|
||||
return runApplication(argc, argv);
|
||||
} else {
|
||||
qInfo() << "Started as system service";
|
||||
#ifdef Q_OS_WIN
|
||||
SystemService systemService(argc, argv);
|
||||
return systemService.exec();
|
||||
|
||||
#else
|
||||
//daemon(0,0);
|
||||
return runApplication(argc, argv);
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
// Never reached
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ SystemService::SystemService(int argc, char **argv)
|
|||
void SystemService::start()
|
||||
{
|
||||
QCoreApplication* app = application();
|
||||
m_localServer = new LocalServer(SERVICE_NAME);
|
||||
m_localServer = new LocalServer();
|
||||
|
||||
if (!m_localServer->isRunning()) {
|
||||
app->quit();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue