Skip to content

Commit

Permalink
Merge pull request #235 from inada-s/improve-match-report
Browse files Browse the repository at this point in the history
Improve match report
  • Loading branch information
inada-s authored Jul 31, 2023
2 parents d451e06 + 0cd7bee commit 3762c2e
Show file tree
Hide file tree
Showing 10 changed files with 743 additions and 257 deletions.
103 changes: 7 additions & 96 deletions core/gdxsv/gdxsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,19 @@ std::vector<u8> Gdxsv::GeneratePlatformInfoPacket() {
ss << "machine_id=" << std::hex << digest << std::dec << "\n";
}

if (gcp_ping_test_mutex_.try_lock()) {
for (const auto &res : gcp_ping_test_result_) {
if (gcp_ping_test_result_.valid()) {
for (const auto &res : gcp_ping_test_result_.get()) {
ss << res.first << "=" << res.second << "\n";
}
gcp_ping_test_mutex_.unlock();
}

if (future_is_ready(public_ipv4_)) {
if (public_ipv4_.valid()) {
if (public_ipv4_.get().first) {
ss << "public_ipv4=" << public_ipv4_.get().second << "\n";
}
}

if (future_is_ready(public_ipv6_)) {
if (public_ipv6_.valid()) {
if (public_ipv6_.get().first) {
ss << "public_ipv6=" << public_ipv6_.get().second << "\n";
}
Expand Down Expand Up @@ -369,7 +368,7 @@ std::vector<u8> Gdxsv::GenerateP2PMatchReportPacket() {
for (const auto &line : lines) {
ss << line;
}
rbk_report.set_log(ss.str());
rbk_report.set_after_log(ss.str());

std::string data;
if (rbk_report.SerializeToString(&data)) {
Expand Down Expand Up @@ -506,6 +505,8 @@ void Gdxsv::HandleRPC() {
gdxsv_WriteMem32(symbols_["is_online"], netmode_ != NetMode::Offline);
}

void Gdxsv::StartPingTest() { gcp_ping_test_result_ = gcp_ping_test().share(); }

void Gdxsv::FetchPublicIP() {
public_ipv4_ = get_public_ip_address(false).share();
public_ipv6_ = get_public_ip_address(true).share();
Expand Down Expand Up @@ -550,96 +551,6 @@ void Gdxsv::NotifyWanPort() const {
udp.Close();
}

void Gdxsv::StartPingTest() {
std::thread([this]() {
std::this_thread::sleep_for(std::chrono::seconds(3));
GcpPingTest();
}).detach();
}

void Gdxsv::GcpPingTest() {
std::map<std::string, int> test_result;

// powered by https://github.com/GoogleCloudPlatform/gcping
static const std::string get_path = "/api/ping";
static const std::map<std::string, std::string> gcp_region_hosts = {
{"asia-east1", "asia-east1-5tkroniexa-de.a.run.app"},
{"asia-east2", "asia-east2-5tkroniexa-df.a.run.app"},
{"asia-northeast1", "asia-northeast1-5tkroniexa-an.a.run.app"},
{"asia-northeast2", "asia-northeast2-5tkroniexa-dt.a.run.app"},
{"asia-northeast3", "asia-northeast3-5tkroniexa-du.a.run.app"},
{"asia-south1", "asia-south1-5tkroniexa-el.a.run.app"},
{"asia-southeast1", "asia-southeast1-5tkroniexa-as.a.run.app"},
{"australia-southeast1", "australia-southeast1-5tkroniexa-ts.a.run.app"},
{"europe-north1", "europe-north1-5tkroniexa-lz.a.run.app"},
{"europe-west1", "europe-west1-5tkroniexa-ew.a.run.app"},
{"europe-west2", "europe-west2-5tkroniexa-nw.a.run.app"},
{"europe-west3", "europe-west3-5tkroniexa-ey.a.run.app"},
{"europe-west4", "europe-west4-5tkroniexa-ez.a.run.app"},
{"europe-west6", "europe-west6-5tkroniexa-oa.a.run.app"},
{"northamerica-northeast1", "northamerica-northeast1-5tkroniexa-nn.a.run.app"},
{"southamerica-east1", "southamerica-east1-5tkroniexa-rj.a.run.app"},
{"us-central1", "us-central1-5tkroniexa-uc.a.run.app"},
{"us-east1", "us-east1-5tkroniexa-ue.a.run.app"},
{"us-east4", "us-east4-5tkroniexa-uk.a.run.app"},
{"us-west1", "us-west1-5tkroniexa-uw.a.run.app"},
{"us-west2", "us-west2-5tkroniexa-wl.a.run.app"},
{"us-west3", "us-west3-5tkroniexa-wm.a.run.app"},
};

for (const auto &region_host : gcp_region_hosts) {
TcpClient client;
std::stringstream ss;
ss << "HEAD " << get_path << " HTTP/1.1"
<< "\r\n";
ss << "Host: " << region_host.second << "\r\n";
ss << "User-Agent: flycast for gdxsv"
<< "\r\n";
ss << "Accept: */*"
<< "\r\n";
ss << "\r\n"; // end of header

if (!client.Connect(region_host.second.c_str(), 80)) {
ERROR_LOG(COMMON, "connect failed : %s", region_host.first.c_str());
continue;
}

auto request_header = ss.str();
auto t1 = std::chrono::high_resolution_clock::now();
int n = client.Send(request_header.c_str(), request_header.size());
if (n < request_header.size()) {
ERROR_LOG(COMMON, "send failed : %s", region_host.first.c_str());
client.Close();
continue;
}

char buf[1024] = {0};
n = client.Recv(buf, 1024);
if (n <= 0) {
ERROR_LOG(COMMON, "recv failed : %s", region_host.first.c_str());
client.Close();
continue;
}

auto t2 = std::chrono::high_resolution_clock::now();
int rtt = (int)std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();
const std::string response_header(buf, n);
if (response_header.find("200 OK") == std::string::npos && response_header.find("302 Found") == std::string::npos) {
ERROR_LOG(COMMON, "error response : %s", response_header.c_str());
} else {
test_result[region_host.first] = rtt;
char latency_str[256];
snprintf(latency_str, 256, "%s : %d[ms]", region_host.first.c_str(), rtt);
NOTICE_LOG(COMMON, "%s", latency_str);
}
client.Close();
}

gcp_ping_test_mutex_.lock();
gcp_ping_test_result_.swap(test_result);
gcp_ping_test_mutex_.unlock();
}

void Gdxsv::AddPortMapping() {
if (config::EnableUPnP) {
int port = config::GdxLocalPort;
Expand Down
4 changes: 1 addition & 3 deletions core/gdxsv/gdxsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ class Gdxsv {
MiniUPnP& UPnP() { return upnp_; }

private:
void GcpPingTest();
void AddPortMapping();
static std::string GenerateLoginKey();
std::vector<u8> GeneratePlatformInfoPacket();
Expand All @@ -71,10 +70,9 @@ class Gdxsv {
std::string user_id_;
std::map<std::string, u32> symbols_;
proto::GamePatchList patch_list_;
std::map<std::string, int> gcp_ping_test_result_;
std::mutex gcp_ping_test_mutex_;
bool going_to_battle_ = false;

std::shared_future<std::map<std::string, int>> gcp_ping_test_result_;
std::shared_future<std::pair<bool, std::string>> public_ipv4_, public_ipv6_;

MiniUPnP upnp_;
Expand Down
Loading

0 comments on commit 3762c2e

Please sign in to comment.