refactor core_process

This commit is contained in:
arm64v8a
2022-09-09 08:00:00 +08:00
parent 31079f20fd
commit 9f43406402
2 changed files with 49 additions and 52 deletions

View File

@@ -348,64 +348,60 @@ MainWindow::MainWindow(QWidget *parent)
NekoRay::dataStore->core_port = MkPort(); NekoRay::dataStore->core_port = MkPort();
if (NekoRay::dataStore->core_port <= 0) NekoRay::dataStore->core_port = 19810; if (NekoRay::dataStore->core_port <= 0) NekoRay::dataStore->core_port = 19810;
runOnNewThread([=]() { QString starting_info;
QString starting_info; auto core_path = NekoRay::dataStore->core_path;
auto core_path = NekoRay::dataStore->core_path;
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
if (!core_path.endsWith(".exe")) core_path += ".exe"; if (!core_path.endsWith(".exe")) core_path += ".exe";
#endif #endif
if (NekoRay::dataStore->flag_use_appdata) { if (NekoRay::dataStore->flag_use_appdata) {
core_path = QApplication::applicationDirPath() + "/nekoray_core"; core_path = QApplication::applicationDirPath() + "/nekoray_core";
} else if (!QFile(core_path).exists()) { } else if (!QFile(core_path).exists()) {
starting_info = "(not found)"; starting_info = "(not found)";
core_path = ""; core_path = "";
} }
QStringList args; QStringList args;
args.push_back("nekoray"); args.push_back("nekoray");
args.push_back("-port"); args.push_back("-port");
args.push_back(Int2String(NekoRay::dataStore->core_port)); args.push_back(Int2String(NekoRay::dataStore->core_port));
#ifdef NKR_DEBUG #ifdef NKR_DEBUG
args.push_back("-debug"); args.push_back("-debug");
#endif #endif
for (int retry = 0; retry < 10; retry++) { showLog("Starting nekoray core " + starting_info + "\n");
showLog("Starting nekoray core " + starting_info + "\n");
auto core_process = new QProcess; if (!core_path.isEmpty()) {
core_process_show_stderr = false; core_process = new QProcess;
connect(core_process, &QProcess::readyReadStandardOutput, this, core_process_show_stderr = false;
[=]() {
showLog(core_process->readAllStandardOutput().trimmed()); connect(core_process, &QProcess::readyReadStandardOutput, this, [&]() {
}); showLog(core_process->readAllStandardOutput().trimmed());
connect(core_process, &QProcess::readyReadStandardError, this, });
[=]() { connect(core_process, &QProcess::readyReadStandardError, this, [&]() {
auto log = core_process->readAllStandardError().trimmed(); auto log = core_process->readAllStandardError().trimmed();
if (core_process_show_stderr) { if (core_process_show_stderr) {
showLog(log); showLog(log);
return; return;
}
if (log.contains("token is set")) {
core_process_show_stderr = true;
}
});
if (core_path.isEmpty()) break;
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); if (log.contains("token is set")) {
core_process->write((NekoRay::dataStore->core_token + "\n").toUtf8()); core_process_show_stderr = true;
core_process->waitForFinished(-1); }
if (core_process_killed) return; });
// TODO Not retrying can avoid crash? connect(core_process, &QProcess::stateChanged, this, [&](QProcess::ProcessState state) {
core_process->deleteLater(); if (!prepare_exit_core && state == QProcess::NotRunning) {
QtGrpc::core_crashed = true; QtGrpc::core_crashed = true;
showLog("[ERROR] nekoray_core crashed, please restart the program."); showLog("[Error] nekoray_core crashed, please restart the program.");
return; }
});
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());
}
setup_grpc(); setup_grpc();
@@ -567,7 +563,7 @@ void MainWindow::on_menu_exit_triggered() {
// //
on_commitDataRequest(); on_commitDataRequest();
// //
core_process_killed = true; prepare_exit_core = true;
hide(); hide();
ExitNekorayCore(); ExitNekorayCore();
// //

View File

@@ -120,7 +120,8 @@ private:
QShortcut *shortcut_ctrl_f = new QShortcut(QKeySequence("Ctrl+F"), this); QShortcut *shortcut_ctrl_f = new QShortcut(QKeySequence("Ctrl+F"), this);
QShortcut *shortcut_esc = new QShortcut(QKeySequence("Esc"), this); QShortcut *shortcut_esc = new QShortcut(QKeySequence("Esc"), this);
// //
bool core_process_killed = false; QProcess *core_process;
bool prepare_exit_core = false;
bool core_process_show_stderr = false; bool core_process_show_stderr = false;
qint64 vpn_pid = 0; qint64 vpn_pid = 0;
QFileSystemWatcher *watcher = nullptr; QFileSystemWatcher *watcher = nullptr;