|
@@ -1,9 +1,11 @@
|
|
|
|
+#include <QDebug>
|
|
#include <QGuiApplication>
|
|
#include <QGuiApplication>
|
|
#include <csignal>
|
|
#include <csignal>
|
|
#include <cstdio>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <iostream>
|
|
#include <poll.h>
|
|
#include <poll.h>
|
|
|
|
+#include <string>
|
|
#include <sys/prctl.h>
|
|
#include <sys/prctl.h>
|
|
#include <sys/wait.h>
|
|
#include <sys/wait.h>
|
|
#include <thread>
|
|
#include <thread>
|
|
@@ -20,34 +22,33 @@ using namespace std;
|
|
|
|
|
|
int inpipefd[2];
|
|
int inpipefd[2];
|
|
int outpipefd[2];
|
|
int outpipefd[2];
|
|
|
|
+
|
|
char buf[1025];
|
|
char buf[1025];
|
|
QUrl sendFileUrl = QUrl("");
|
|
QUrl sendFileUrl = QUrl("");
|
|
QString _IP = "";
|
|
QString _IP = "";
|
|
bool _CLI_RUNNING = false;
|
|
bool _CLI_RUNNING = false;
|
|
bool _RESTART = false;
|
|
bool _RESTART = false;
|
|
|
|
+pid_t childpid;
|
|
|
|
|
|
bool programActive = true;
|
|
bool programActive = true;
|
|
|
|
|
|
QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
|
|
QMLHandler::QMLHandler(QObject *parent) : QObject(parent) {}
|
|
|
|
|
|
-void QMLHandler::closeCLI() {
|
|
|
|
- close(outpipefd[1]);
|
|
|
|
- close(inpipefd[0]);
|
|
|
|
- _CLI_RUNNING = false;
|
|
|
|
-}
|
|
|
|
|
|
+void QMLHandler::closeCLI() { _CLI_RUNNING = false; }
|
|
|
|
|
|
void QMLHandler::reopenCLI(QString ip) {
|
|
void QMLHandler::reopenCLI(QString ip) {
|
|
if (_CLI_RUNNING) {
|
|
if (_CLI_RUNNING) {
|
|
|
|
+ waitpid(childpid, NULL, 0);
|
|
closeCLI();
|
|
closeCLI();
|
|
}
|
|
}
|
|
|
|
|
|
_IP = ip;
|
|
_IP = ip;
|
|
- pid_t pid = 0;
|
|
|
|
|
|
|
|
pipe(inpipefd);
|
|
pipe(inpipefd);
|
|
pipe(outpipefd);
|
|
pipe(outpipefd);
|
|
- pid = fork();
|
|
|
|
- if (pid == 0) {
|
|
|
|
|
|
+
|
|
|
|
+ childpid = fork();
|
|
|
|
+ if (childpid == 0) {
|
|
// Child
|
|
// Child
|
|
dup2(outpipefd[0], STDIN_FILENO);
|
|
dup2(outpipefd[0], STDIN_FILENO);
|
|
dup2(inpipefd[1], STDOUT_FILENO);
|
|
dup2(inpipefd[1], STDOUT_FILENO);
|
|
@@ -67,7 +68,7 @@ void QMLHandler::reopenCLI(QString ip) {
|
|
|
|
|
|
close(outpipefd[0]);
|
|
close(outpipefd[0]);
|
|
close(inpipefd[1]);
|
|
close(inpipefd[1]);
|
|
- std::thread(&QMLHandler::readPipeLoop, this, pid).detach();
|
|
|
|
|
|
+ std::thread(&QMLHandler::readPipeLoop, this).detach();
|
|
}
|
|
}
|
|
|
|
|
|
std::vector<std::string> tokenizeByNewlines(std::string in) {
|
|
std::vector<std::string> tokenizeByNewlines(std::string in) {
|
|
@@ -145,7 +146,7 @@ void QMLHandler::handleJSON(string buffer) {
|
|
QString errorMessage = QString::fromStdString(
|
|
QString errorMessage = QString::fromStdString(
|
|
string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
|
|
string("Version mismatch: \nClient: " + root["clientversion"].asString() + "\nServer: " + root["serverversion"].asString()));
|
|
emit ipPopupSetStatus(errorMessage);
|
|
emit ipPopupSetStatus(errorMessage);
|
|
- reopenCLI(_IP);
|
|
|
|
|
|
+ closeCLI();
|
|
emit ipPopupEnableConnectButton();
|
|
emit ipPopupEnableConnectButton();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -164,7 +165,7 @@ void QMLHandler::handleJSON(string buffer) {
|
|
if (root["accept"] == true) {
|
|
if (root["accept"] == true) {
|
|
emit loginSignupPopupClose();
|
|
emit loginSignupPopupClose();
|
|
} else {
|
|
} else {
|
|
- emit loginSetStatus(root["error"].asString().c_str());
|
|
|
|
|
|
+ emit signupSetStatus(root["error"].asString().c_str());
|
|
reopenCLI(_IP);
|
|
reopenCLI(_IP);
|
|
emit signupEnableRegisterButton();
|
|
emit signupEnableRegisterButton();
|
|
}
|
|
}
|
|
@@ -204,7 +205,7 @@ void QMLHandler::handleJSON(string buffer) {
|
|
// If will read the Input Pipe, which containts the string that get printed
|
|
// If will read the Input Pipe, which containts the string that get printed
|
|
// on stdout by the CLI/Server, and calls handleJSON with the read content
|
|
// on stdout by the CLI/Server, and calls handleJSON with the read content
|
|
// one it reads a newline.
|
|
// one it reads a newline.
|
|
-void QMLHandler::readPipeLoop(pid_t childid) {
|
|
|
|
|
|
+void QMLHandler::readPipeLoop() {
|
|
unsigned int readOffset = 0;
|
|
unsigned int readOffset = 0;
|
|
unsigned int pollCount = 0;
|
|
unsigned int pollCount = 0;
|
|
struct pollfd inPipeStatus;
|
|
struct pollfd inPipeStatus;
|
|
@@ -225,7 +226,7 @@ void QMLHandler::readPipeLoop(pid_t childid) {
|
|
pollCount++;
|
|
pollCount++;
|
|
}
|
|
}
|
|
|
|
|
|
- if (pollCount > 9 && (readOffset || pipeInput.size())) {
|
|
|
|
|
|
+ if (pollCount > 4 && (readOffset || pipeInput.size())) {
|
|
pipeInput.append(buf);
|
|
pipeInput.append(buf);
|
|
inputs = tokenizeByNewlines(pipeInput);
|
|
inputs = tokenizeByNewlines(pipeInput);
|
|
for (string s : inputs) {
|
|
for (string s : inputs) {
|
|
@@ -237,14 +238,13 @@ void QMLHandler::readPipeLoop(pid_t childid) {
|
|
memset(buf, 0, 1025);
|
|
memset(buf, 0, 1025);
|
|
pollCount = 0;
|
|
pollCount = 0;
|
|
readOffset = 0;
|
|
readOffset = 0;
|
|
- if (waitpid(childid, NULL, WNOHANG)) {
|
|
|
|
|
|
+ if (waitpid(childpid, NULL, WNOHANG)) {
|
|
// nonzero means error or childid has changed state
|
|
// nonzero means error or childid has changed state
|
|
// for us that means child has exited -> CLI is dead
|
|
// for us that means child has exited -> CLI is dead
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // Fixme
|
|
|
|
if (readOffset >= 1024) {
|
|
if (readOffset >= 1024) {
|
|
pipeInput.append(buf);
|
|
pipeInput.append(buf);
|
|
readOffset = 0;
|
|
readOffset = 0;
|