Skip to content

Commit

Permalink
optimized test
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Feb 15, 2023
1 parent e88021e commit 705e9e0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 31 deletions.
10 changes: 2 additions & 8 deletions db/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ namespace NekoRay {
}

QSharedPointer<ProxyEntity> ProfileManager::GetProfile(int id) {
if (profiles.contains(id)) {
return profiles[id];
}
return nullptr;
return profiles.value(id, nullptr);
}

// Group
Expand Down Expand Up @@ -264,10 +261,7 @@ namespace NekoRay {
}

QSharedPointer<Group> ProfileManager::GetGroup(int id) {
if (groups.contains(id)) {
return groups[id];
}
return nullptr;
return groups.value(id, nullptr);
}

QSharedPointer<Group> ProfileManager::CurrentGroup() {
Expand Down
29 changes: 19 additions & 10 deletions rpc/gRPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace QtGrpc {
}
};

class Http2GrpcChannelPrivate : public QObject {
class Http2GrpcChannelPrivate {
private:
QThread *thread;
QNetworkAccessManager *nm;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -194,8 +201,10 @@ namespace QtGrpc {
} // namespace QtGrpc

namespace NekoRay::rpc {

Client::Client(std::function<void(const QString &)> onError, const QString &target, const QString &token) {
this->grpc_channel = std::make_unique<QtGrpc::Http2GrpcChannelPrivate>(target, token, "libcore.LibcoreService");
this->make_grpc_channel = [=]() { return std::make_unique<QtGrpc::Http2GrpcChannelPrivate>(target, token, "libcore.LibcoreService"); };
this->default_grpc_channel = make_grpc_channel();
this->onError = std::move(onError);
}

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion rpc/gRPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ namespace NekoRay::rpc {
libcore::UpdateResp Update(bool *rpcOK, const libcore::UpdateReq &request);

private:
std::unique_ptr<QtGrpc::Http2GrpcChannelPrivate> grpc_channel;
std::function<std::unique_ptr<QtGrpc::Http2GrpcChannelPrivate>()> make_grpc_channel;
std::unique_ptr<QtGrpc::Http2GrpcChannelPrivate> default_grpc_channel;
std::function<void(const QString &)> onError;
};

Expand Down
2 changes: 1 addition & 1 deletion translations/fa_IR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@
</message>
<message>
<source>Certificate</source>
<translation type="unfinished"></translation>
<translation type="unfinished">گواهی</translation>
</message>
<message>
<source>Insecure concurrency</source>
Expand Down
4 changes: 2 additions & 2 deletions ui/edit/dialog_edit_profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 5 additions & 4 deletions ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QTableWidgetItem>();
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
Expand Down
12 changes: 7 additions & 5 deletions ui/mainwindow_grpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
Expand Down

0 comments on commit 705e9e0

Please sign in to comment.