From 705e9e0152c8e337632bce6113a417f9b68a0de2 Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Wed, 15 Feb 2023 17:03:05 +0900 Subject: [PATCH] optimized test --- db/Database.cpp | 10 ++-------- rpc/gRPC.cpp | 29 +++++++++++++++++++---------- rpc/gRPC.h | 3 ++- translations/fa_IR.ts | 2 +- ui/edit/dialog_edit_profile.cpp | 4 ++-- ui/mainwindow.cpp | 9 +++++---- ui/mainwindow_grpc.cpp | 12 +++++++----- 7 files changed, 38 insertions(+), 31 deletions(-) diff --git a/db/Database.cpp b/db/Database.cpp index 389b9e6..4cb00a1 100644 --- a/db/Database.cpp +++ b/db/Database.cpp @@ -198,10 +198,7 @@ namespace NekoRay { } QSharedPointer ProfileManager::GetProfile(int id) { - if (profiles.contains(id)) { - return profiles[id]; - } - return nullptr; + return profiles.value(id, nullptr); } // Group @@ -264,10 +261,7 @@ namespace NekoRay { } QSharedPointer ProfileManager::GetGroup(int id) { - if (groups.contains(id)) { - return groups[id]; - } - return nullptr; + return groups.value(id, nullptr); } QSharedPointer ProfileManager::CurrentGroup() { diff --git a/rpc/gRPC.cpp b/rpc/gRPC.cpp index 9903126..9fbcfc9 100644 --- a/rpc/gRPC.cpp +++ b/rpc/gRPC.cpp @@ -49,7 +49,7 @@ namespace QtGrpc { } }; - class Http2GrpcChannelPrivate : public QObject { + class Http2GrpcChannelPrivate { private: QThread *thread; QNetworkAccessManager *nm; @@ -117,7 +117,7 @@ namespace QtGrpc { abortTimer = new QTimer; abortTimer->setSingleShot(true); abortTimer->setInterval(timeout_ms); - connect(abortTimer, &QTimer::timeout, abortTimer, [=]() { + QObject::connect(abortTimer, &QTimer::timeout, abortTimer, [=]() { networkReply->abort(); }); abortTimer->start(); @@ -156,6 +156,13 @@ namespace QtGrpc { thread->start(); } + ~Http2GrpcChannelPrivate() { + nm->deleteLater(); + thread->quit(); + thread->wait(); + thread->deleteLater(); + } + QNetworkReply::NetworkError Call(const QString &methodName, const google::protobuf::Message &req, google::protobuf::Message *rsp, int timeout_ms = 0) { @@ -194,8 +201,10 @@ namespace QtGrpc { } // namespace QtGrpc namespace NekoRay::rpc { + Client::Client(std::function onError, const QString &target, const QString &token) { - this->grpc_channel = std::make_unique(target, token, "libcore.LibcoreService"); + this->make_grpc_channel = [=]() { return std::make_unique(target, token, "libcore.LibcoreService"); }; + this->default_grpc_channel = make_grpc_channel(); this->onError = std::move(onError); } @@ -206,12 +215,12 @@ namespace NekoRay::rpc { void Client::Exit() { libcore::EmptyReq request; libcore::EmptyResp reply; - grpc_channel->Call("Exit", request, &reply, 500); + default_grpc_channel->Call("Exit", request, &reply, 500); } QString Client::Start(bool *rpcOK, const libcore::LoadConfigReq &request) { libcore::ErrorResp reply; - auto status = grpc_channel->Call("Start", request, &reply, 3000); + auto status = default_grpc_channel->Call("Start", request, &reply, 3000); if (status == QNetworkReply::NoError) { *rpcOK = true; @@ -225,7 +234,7 @@ namespace NekoRay::rpc { QString Client::Stop(bool *rpcOK) { libcore::EmptyReq request; libcore::ErrorResp reply; - auto status = grpc_channel->Call("Stop", request, &reply, 3000); + auto status = default_grpc_channel->Call("Stop", request, &reply, 3000); if (status == QNetworkReply::NoError) { *rpcOK = true; @@ -242,7 +251,7 @@ namespace NekoRay::rpc { request.set_direct(direct); libcore::QueryStatsResp reply; - auto status = grpc_channel->Call("QueryStats", request, &reply, 500); + auto status = default_grpc_channel->Call("QueryStats", request, &reply, 500); if (status == QNetworkReply::NoError) { return reply.traffic(); @@ -254,7 +263,7 @@ namespace NekoRay::rpc { std::string Client::ListConnections() { libcore::EmptyReq request; libcore::ListConnectionsResp reply; - auto status = grpc_channel->Call("ListConnections", request, &reply, 500); + auto status = default_grpc_channel->Call("ListConnections", request, &reply, 500); if (status == QNetworkReply::NoError) { return reply.nekoray_connections_json(); @@ -267,7 +276,7 @@ namespace NekoRay::rpc { libcore::TestResp Client::Test(bool *rpcOK, const libcore::TestReq &request) { libcore::TestResp reply; - auto status = grpc_channel->Call("Test", request, &reply); + auto status = make_grpc_channel()->Call("Test", request, &reply); if (status == QNetworkReply::NoError) { *rpcOK = true; @@ -280,7 +289,7 @@ namespace NekoRay::rpc { libcore::UpdateResp Client::Update(bool *rpcOK, const libcore::UpdateReq &request) { libcore::UpdateResp reply; - auto status = grpc_channel->Call("Update", request, &reply); + auto status = default_grpc_channel->Call("Update", request, &reply); if (status == QNetworkReply::NoError) { *rpcOK = true; diff --git a/rpc/gRPC.h b/rpc/gRPC.h index e3443d6..872e4cf 100644 --- a/rpc/gRPC.h +++ b/rpc/gRPC.h @@ -33,7 +33,8 @@ namespace NekoRay::rpc { libcore::UpdateResp Update(bool *rpcOK, const libcore::UpdateReq &request); private: - std::unique_ptr grpc_channel; + std::function()> make_grpc_channel; + std::unique_ptr default_grpc_channel; std::function onError; }; diff --git a/translations/fa_IR.ts b/translations/fa_IR.ts index e172dfe..75c7542 100644 --- a/translations/fa_IR.ts +++ b/translations/fa_IR.ts @@ -694,7 +694,7 @@ Certificate - + گواهی Insecure concurrency diff --git a/ui/edit/dialog_edit_profile.cpp b/ui/edit/dialog_edit_profile.cpp index a4160ae..29f2975 100644 --- a/ui/edit/dialog_edit_profile.cpp +++ b/ui/edit/dialog_edit_profile.cpp @@ -75,9 +75,9 @@ DialogEditProfile::DialogEditProfile(const QString &_type, int profileOrGroupId, if (IS_NEKO_BOX) { ui->header_type->setVisible(false); ui->header_type_l->setVisible(false); - ui->utlsFingerprint->addItems({"", "chrome", "firefox", "edge", "safari", "360", "qq", "ios", "android", "random"}); + if (!ui->utlsFingerprint->count()) ui->utlsFingerprint->addItems({"", "chrome", "firefox", "edge", "safari", "360", "qq", "ios", "android", "random"}); } else { - ui->utlsFingerprint->addItems({"", "randomized", "randomizedalpn", "randomizednoalpn", "firefox_auto", "firefox_55", "firefox_56", "firefox_63", "firefox_65", "firefox_99", "firefox_102", "firefox_105", "chrome_auto", "chrome_58", "chrome_62", "chrome_70", "chrome_72", "chrome_83", "chrome_87", "chrome_96", "chrome_100", "chrome_102", "ios_auto", "ios_11_1", "ios_12_1", "ios_13", "ios_14", "android_11_okhttp", "edge_auto", "edge_85", "edge_106", "safari_auto", "safari_16_0", "360_auto", "360_7_5", "360_11_0", "qq_auto", "qq_11_1"}); + if (!ui->utlsFingerprint->count()) ui->utlsFingerprint->addItems({"", "randomized", "randomizedalpn", "randomizednoalpn", "firefox_auto", "firefox_55", "firefox_56", "firefox_63", "firefox_65", "firefox_99", "firefox_102", "firefox_105", "chrome_auto", "chrome_58", "chrome_62", "chrome_70", "chrome_72", "chrome_83", "chrome_87", "chrome_96", "chrome_100", "chrome_102", "ios_auto", "ios_11_1", "ios_12_1", "ios_13", "ios_14", "android_11_okhttp", "edge_auto", "edge_85", "edge_106", "safari_auto", "safari_16_0", "360_auto", "360_7_5", "360_11_0", "qq_auto", "qq_11_1"}); } // 传输设置 是否可见 int networkBoxVisible = 0; diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index 6351190..0a72296 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -883,16 +883,17 @@ void MainWindow::refresh_proxy_list_impl(const int &id, NekoRay::GroupSortAction void MainWindow::refresh_proxy_list_impl_refresh_data(const int &id) { // 绘制或更新item(s) for (int row = 0; row < ui->proxyListTable->rowCount(); row++) { - auto profile = NekoRay::profileManager->GetProfile(ui->proxyListTable->row2Id[row]); + auto profileId = ui->proxyListTable->row2Id[row]; + if (id >= 0 && profileId != id) continue; // refresh ONE item + auto profile = NekoRay::profileManager->GetProfile(profileId); if (profile == nullptr) continue; - if (id >= 0 && profile->id != id) continue; // refresh ONE item auto f0 = std::make_unique(); - f0->setData(114514, profile->id); + f0->setData(114514, profileId); // Check state auto check = f0->clone(); - check->setText(profile->id == NekoRay::dataStore->started_id ? "✓" : Int2String(row + 1)); + check->setText(profileId == NekoRay::dataStore->started_id ? "✓" : Int2String(row + 1)); ui->proxyListTable->setVerticalHeaderItem(row, check); // C0: Type diff --git a/ui/mainwindow_grpc.cpp b/ui/mainwindow_grpc.cpp index 2fe1759..87d06c5 100644 --- a/ui/mainwindow_grpc.cpp +++ b/ui/mainwindow_grpc.cpp @@ -131,11 +131,13 @@ void MainWindow::speedtest_current_group(int mode) { profile->full_test_report = result.full_report().c_str(); profile->Save(); - runOnUiThread([=] { - if (!result.error().empty()) { - show_log_impl(tr("[%1] test error: %2").arg(profile->bean->DisplayTypeAndName(), result.error().c_str())); - } - refresh_proxy_list(profile->id); + if (!result.error().empty()) { + MW_show_log(tr("[%1] test error: %2").arg(profile->bean->DisplayTypeAndName(), result.error().c_str())); + } + + auto profileId = profile->id; + runOnUiThread([this, profileId] { + refresh_proxy_list(profileId); }); } });