refactor core_process (1)

This commit is contained in:
arm64v8a
2022-09-11 11:38:48 +08:00
parent dbbd54077b
commit 06b0163319
7 changed files with 120 additions and 103 deletions

View File

@@ -52,10 +52,6 @@
#include <QFileInfo>
#include <QFileSystemWatcher>
namespace QtGrpc {
extern bool core_crashed;
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow) {
mainwindow = this;
@@ -343,21 +339,14 @@ MainWindow::MainWindow(QWidget *parent)
connect(ui->menu_full_test, &QAction::triggered, this, [=]() { speedtest_current_group(2); });
refresh_status();
// Start Core
// Prepare core
NekoRay::dataStore->core_token = GetRandomString(32);
NekoRay::dataStore->core_port = MkPort();
if (NekoRay::dataStore->core_port <= 0) NekoRay::dataStore->core_port = 19810;
QString starting_info;
auto core_path = NekoRay::dataStore->core_path;
#ifdef Q_OS_WIN
if (!core_path.endsWith(".exe")) core_path += ".exe";
#endif
if (NekoRay::dataStore->flag_use_appdata) {
core_path = QApplication::applicationDirPath() + "/nekoray_core";
} else if (!QFile(core_path).exists()) {
starting_info = "(not found)";
core_path = "";
}
QStringList args;
@@ -368,40 +357,9 @@ MainWindow::MainWindow(QWidget *parent)
args.push_back("-debug");
#endif
showLog("Starting nekoray core " + starting_info + "\n");
if (!core_path.isEmpty()) {
core_process = new QProcess;
core_process_show_stderr = false;
connect(core_process, &QProcess::readyReadStandardOutput, this, [&]() {
showLog(core_process->readAllStandardOutput().trimmed());
});
connect(core_process, &QProcess::readyReadStandardError, this, [&]() {
auto log = core_process->readAllStandardError().trimmed();
if (core_process_show_stderr) {
showLog(log);
return;
}
if (log.contains("token is set")) {
core_process_show_stderr = true;
}
});
connect(core_process, &QProcess::stateChanged, this, [&](QProcess::ProcessState state) {
if (!prepare_exit_core && state == QProcess::NotRunning) {
QtGrpc::core_crashed = true;
showLog("[Error] nekoray_core crashed, please restart the program.");
}
});
if (!NekoRay::dataStore->v2ray_asset_dir.isEmpty()) {
core_process->setEnvironment(QStringList{
"V2RAY_LOCATION_ASSET=" + NekoRay::dataStore->v2ray_asset_dir
});
}
core_process->start(core_path, args);
core_process->write((NekoRay::dataStore->core_token + "\n").toUtf8());
}
// Start core
core_process = new NekoRay::sys::CoreProcess(core_path, args);
core_process->Start();
setup_grpc();
@@ -563,7 +521,7 @@ void MainWindow::on_menu_exit_triggered() {
//
on_commitDataRequest();
//
prepare_exit_core = true;
NekoRay::dataStore->core_prepare_exit = true;
hide();
ExitNekorayCore();
//