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,9 +348,7 @@ 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";
@@ -370,16 +368,16 @@ MainWindow::MainWindow(QWidget *parent)
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 = new QProcess;
core_process_show_stderr = false; core_process_show_stderr = false;
connect(core_process, &QProcess::readyReadStandardOutput, this,
[=]() { connect(core_process, &QProcess::readyReadStandardOutput, this, [&]() {
showLog(core_process->readAllStandardOutput().trimmed()); 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);
@@ -389,7 +387,13 @@ MainWindow::MainWindow(QWidget *parent)
core_process_show_stderr = true; core_process_show_stderr = true;
} }
}); });
if (core_path.isEmpty()) break; 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()) { if (!NekoRay::dataStore->v2ray_asset_dir.isEmpty()) {
core_process->setEnvironment(QStringList{ core_process->setEnvironment(QStringList{
"V2RAY_LOCATION_ASSET=" + NekoRay::dataStore->v2ray_asset_dir "V2RAY_LOCATION_ASSET=" + NekoRay::dataStore->v2ray_asset_dir
@@ -397,15 +401,7 @@ MainWindow::MainWindow(QWidget *parent)
} }
core_process->start(core_path, args); core_process->start(core_path, args);
core_process->write((NekoRay::dataStore->core_token + "\n").toUtf8()); core_process->write((NekoRay::dataStore->core_token + "\n").toUtf8());
core_process->waitForFinished(-1);
if (core_process_killed) return;
// TODO Not retrying can avoid crash?
core_process->deleteLater();
QtGrpc::core_crashed = true;
showLog("[ERROR] nekoray_core crashed, please restart the program.");
return;
} }
});
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;