diff --git a/core/gdxsv/gdxsv.cpp b/core/gdxsv/gdxsv.cpp index 453cf90e74..d9e0dfb47d 100644 --- a/core/gdxsv/gdxsv.cpp +++ b/core/gdxsv/gdxsv.cpp @@ -11,6 +11,7 @@ #include "emulator.h" #include "gdx_rpc.h" #include "gdxsv_translation.h" +#include "hw/sh4/dyna/blockmanager.h" #include "imgui/imgui.h" #include "libs.h" #include "log/InMemoryListener.h" @@ -21,8 +22,6 @@ #include "rend/boxart/http_client.h" #include "rend/gui.h" #include "version.h" -#include "hw/sh4/dyna/blockmanager.h" - bool encode_zlib_deflate(const char *data, int len, std::vector &out) { z_stream z{}; @@ -147,6 +146,7 @@ void Gdxsv::Reset() { id[i] = lbs_msg.body[2 + i]; } user_id_ = id; + lbs_net_.Send(GenerateP2PMatchReportPacket()); NotifyWanPort(); } @@ -409,7 +409,6 @@ void Gdxsv::HandleRPC() { if (lbs_net_.Connect(server_, port)) { netmode_ = NetMode::Lbs; lbs_net_.Send(GeneratePlatformInfoPacket()); - lbs_net_.Send(GenerateP2PMatchReportPacket()); FetchPublicIP(); AddPortMapping(); } else { @@ -509,7 +508,7 @@ void Gdxsv::NotifyWanPort() const { } const auto lbs_host = lbs_net_.RemoteHost(); - const auto lbs_port= lbs_net_.RemotePort(); + const auto lbs_port = lbs_net_.RemotePort(); const auto udp_port = config::GdxLocalPort.get(); const auto user_id = user_id_; @@ -656,23 +655,23 @@ std::string Gdxsv::GenerateLoginKey() { } void Gdxsv::ApplyOnlinePatch(bool first_time) { - for (const auto& patch : patch_list_.patches()) { + for (const auto &patch : patch_list_.patches()) { if (patch.write_once() && !first_time) { continue; } if (first_time) { NOTICE_LOG(COMMON, "patch apply: %s", patch.name().c_str()); } - for (const auto& code : patch.codes()) { + for (const auto &code : patch.codes()) { gdxsv_WriteMem(code.size(), code.address(), code.changed()); } } } void Gdxsv::RestoreOnlinePatch() { - for (const auto& patch : patch_list_.patches()) { + for (const auto &patch : patch_list_.patches()) { NOTICE_LOG(COMMON, "patch restore: %s", patch.name().c_str()); - for (const auto& code : patch.codes()) { + for (const auto &code : patch.codes()) { gdxsv_WriteMem(code.size(), code.address(), code.original()); } } @@ -688,6 +687,8 @@ void Gdxsv::WritePatch() { if (disk_ == 2) WritePatchDisk2(); if (symbols_["patch_id"] == 0 || gdxsv_ReadMem32(symbols_["patch_id"]) != symbols_[":patch_id"]) { NOTICE_LOG(COMMON, "patch %d %d", gdxsv_ReadMem32(symbols_["patch_id"]), symbols_[":patch_id"]); + bm_ResetCache(); + bm_ResetTempCache(true); #include "gdxsv_patch.inc" diff --git a/core/gdxsv/gdxsv_backend_replay.cpp b/core/gdxsv/gdxsv_backend_replay.cpp index 827a2dd110..579f43cab8 100644 --- a/core/gdxsv/gdxsv_backend_replay.cpp +++ b/core/gdxsv/gdxsv_backend_replay.cpp @@ -523,8 +523,8 @@ void GdxsvBackendReplay::ApplyPatch(bool first_time) { } // Online Patch - for (const auto& patch : log_file_.patches()) { - for (const auto& code : patch.codes()) { + for (const auto &patch : log_file_.patches()) { + for (const auto &code : patch.codes()) { gdxsv_WriteMem(code.size(), code.address(), code.changed()); } } @@ -541,8 +541,8 @@ void GdxsvBackendReplay::RestorePatch() { } // Online Patch - for (const auto& patch : log_file_.patches()) { - for (const auto& code : patch.codes()) { + for (const auto &patch : log_file_.patches()) { + for (const auto &code : patch.codes()) { gdxsv_WriteMem(code.size(), code.address(), code.original()); } } diff --git a/core/gdxsv/gdxsv_backend_rollback.cpp b/core/gdxsv/gdxsv_backend_rollback.cpp index 094a3d6271..b2e9277f7d 100644 --- a/core/gdxsv/gdxsv_backend_rollback.cpp +++ b/core/gdxsv/gdxsv_backend_rollback.cpp @@ -162,7 +162,8 @@ void GdxsvBackendRollback::OnMainUiLoop() { ips[i] = str; ports[i] = ntohs(addr->sin6_port); } - NOTICE_LOG(COMMON, "Peer%d %.2fms IP:%s Port:%d Relay:%d", i, rtt, mask_ip_address(ips[i]).c_str(), ports[i], relays[i]); + NOTICE_LOG(COMMON, "Peer%d %.2fms IP:%s Port:%d Relay:%d", i, rtt, mask_ip_address(ips[i]).c_str(), ports[i], + relays[i]); } else { int relay_rtt = INT_MAX; int relay_peer = -1; @@ -179,12 +180,13 @@ void GdxsvBackendRollback::OnMainUiLoop() { } if (relay_peer != -1 && ping_pong_.GetAvailableAddress(relay_peer, &addr_storage, &rtt)) { - rtt =+ (float)rtt_matrix[relay_peer][i]; + rtt = +(float)rtt_matrix[relay_peer][i]; max_rtt = std::max(max_rtt, rtt); ips[i] = ips[relay_peer]; ports[i] = ports[relay_peer]; relays[i] = true; - NOTICE_LOG(COMMON, "Peer%d %.2fms IP:%s Port:%d Relay:%d", i, rtt, mask_ip_address(ips[i]).c_str(), ports[i], relays[i]); + NOTICE_LOG(COMMON, "Peer%d %.2fms IP:%s Port:%d Relay:%d", i, rtt, mask_ip_address(ips[i]).c_str(), ports[i], + relays[i]); } else { NOTICE_LOG(COMMON, "Peer%d unreachable", i); ok = false; diff --git a/core/gdxsv/gdxsv_network.cpp b/core/gdxsv/gdxsv_network.cpp index e8ee4a5efa..ffacef9a1d 100644 --- a/core/gdxsv/gdxsv_network.cpp +++ b/core/gdxsv/gdxsv_network.cpp @@ -110,7 +110,7 @@ std::string sockaddr_to_string(const sockaddr *addr) { const auto a = reinterpret_cast(addr); char addrbuf[INET6_ADDRSTRLEN]; ::inet_ntop(AF_INET6, &a->sin6_addr, addrbuf, sizeof(addrbuf)); - return std::string(addrbuf) + ":" + std::to_string(ntohs(a->sin6_port)); + return "[" + std::string(addrbuf) + "]:" + std::to_string(ntohs(a->sin6_port)); } return ""; } @@ -136,12 +136,12 @@ bool is_private_addr(const sockaddr *addr) { } if (addr->sa_family == AF_INET) { const auto a = reinterpret_cast(addr); - const auto ip4 = reinterpret_cast(&a->sin_addr); + const auto ip4 = reinterpret_cast(&a->sin_addr); return ip4[0] == 10 || (ip4[0] == 172 && (ip4[1] & 0xf0) == 16) || (ip4[0] == 192 && ip4[1] == 168); } if (addr->sa_family == AF_INET6) { const auto a = reinterpret_cast(addr); - const auto ip6 = reinterpret_cast(&a->sin6_addr); + const auto ip6 = reinterpret_cast(&a->sin6_addr); return (ip6[0] & 0xfe) == 0xfc; } return false; @@ -587,20 +587,13 @@ void UdpPingPong::Start(uint32_t session_id, uint8_t peer_id, int port, int dura NOTICE_LOG(COMMON, "GGPO_NETWORK_DELAY is %d", network_delay); } - std::set peer_ids; - peer_ids.insert(peer_id); - for (const auto &c : candidates_) { - peer_ids.insert(c.peer_id); - } - int peer_count = peer_ids.size(); - - std::thread([this, session_id, peer_id, duration_ms, peer_count, network_delay]() { + std::thread([this, session_id, peer_id, duration_ms, network_delay]() { WARN_LOG(COMMON, "Start UdpPingPong Thread"); start_time_ = std::chrono::high_resolution_clock::now(); for (int loop_count = 0; running_; loop_count++) { auto now = std::chrono::high_resolution_clock::now(); - auto ms = std::chrono::duration_cast(now - start_time_).count(); + auto elapsed_ms = std::chrono::duration_cast(now - start_time_).count(); while (true) { Packet recv{}; @@ -634,7 +627,7 @@ void UdpPingPong::Start(uint32_t session_id, uint8_t peer_id, int port, int dura } if (recv.type == PING) { - NOTICE_LOG(COMMON, "Recv PING from %d", recv.from_peer_id); + DEBUG_LOG(COMMON, "Recv PING from %d", recv.from_peer_id); std::lock_guard lock(mutex_); Packet p{}; @@ -661,7 +654,7 @@ void UdpPingPong::Start(uint32_t session_id, uint8_t peer_id, int port, int dura if (c.remote.Open(sender, addrlen)) { c.peer_id = recv.from_peer_id; candidates_.push_back(c); - client_.SendTo(reinterpret_cast(&p), sizeof(p), c.remote); + client_.SendTo(reinterpret_cast(&p), sizeof(p), c.remote); } } } @@ -673,11 +666,12 @@ void UdpPingPong::Start(uint32_t session_id, uint8_t peer_id, int port, int dura .count(); auto rtt = static_cast(now - recv.ping_timestamp); if (rtt <= 0) rtt = 1; - NOTICE_LOG(COMMON, "Recv PONG from Peer%d %d[ms] %s", recv.from_peer_id, rtt, mask_ip_address(sockaddr_to_string(sender)).c_str()); + DEBUG_LOG(COMMON, "Recv PONG from Peer%d %d[ms] %s", recv.from_peer_id, rtt, + mask_ip_address(sockaddr_to_string(sender)).c_str()); // Pong may come from an address different from one which Ping sent, so update the pong_count based on candidate_idx if (recv.candidate_idx < candidates_.size() && candidates_[recv.candidate_idx].peer_id == recv.from_peer_id) { - auto& c = candidates_[recv.candidate_idx]; + auto &c = candidates_[recv.candidate_idx]; c.rtt = float(c.pong_count * c.rtt + rtt) / float(c.pong_count + 1); c.pong_count++; rtt_matrix_[peer_id][recv.from_peer_id] = static_cast(std::min(255, (int)std::ceil(c.rtt))); @@ -687,7 +681,7 @@ void UdpPingPong::Start(uint32_t session_id, uint8_t peer_id, int port, int dura } // if the remote address not in candidates, add this. - auto it = std::find_if(candidates_.begin(), candidates_.end(), [&recv, &sender](const Candidate& c) { + auto it = std::find_if(candidates_.begin(), candidates_.end(), [&recv, &sender](const Candidate &c) { return c.peer_id == recv.from_peer_id && is_same_addr(sender, c.remote.net_addr()); }); if (it == candidates_.end()) { @@ -700,11 +694,11 @@ void UdpPingPong::Start(uint32_t session_id, uint8_t peer_id, int port, int dura } } - if (ms + 500 < duration_ms && loop_count % 100 == 0) { + if (elapsed_ms + 500 < duration_ms && loop_count % 100 == 0) { std::lock_guard lock(mutex_); for (int i = 0; i < std::min(255, candidates_.size()); i++) { - auto& c = candidates_[i]; - NOTICE_LOG(COMMON, "Send PING to Peer%d %s", c.peer_id, c.remote.masked_addr().c_str()); + auto &c = candidates_[i]; + DEBUG_LOG(COMMON, "Send PING to Peer%d %s", c.peer_id, c.remote.masked_addr().c_str()); if (c.remote.is_open()) { Packet p{}; p.magic = MAGIC; @@ -725,27 +719,33 @@ void UdpPingPong::Start(uint32_t session_id, uint8_t peer_id, int port, int dura } } - if (0 < loop_count && loop_count % 500 == 0) { - std::lock_guard lock(mutex_); - - NOTICE_LOG(COMMON, "RTT MATRIX"); - NOTICE_LOG(COMMON, " %4d%4d%4d%4d", 0, 1, 2, 3); - for (int i = 0; i < 4; i++) { - NOTICE_LOG(COMMON, "%d>%4d%4d%4d%4d", i, rtt_matrix_[i][0], rtt_matrix_[i][1], rtt_matrix_[i][2], rtt_matrix_[i][3]); - } - } - - if (duration_ms < ms) { - NOTICE_LOG(COMMON, "UdpPingTest Finish"); - client_.Close(); - running_ = false; + if (duration_ms < elapsed_ms) { break; } std::this_thread::sleep_for(std::chrono::milliseconds(1)); } - WARN_LOG(COMMON, "End UdpPingPong Thread"); + { + std::lock_guard lock(mutex_); + + NOTICE_LOG(COMMON, "UdpPingTest Finish"); + NOTICE_LOG(COMMON, "RTT MATRIX"); + NOTICE_LOG(COMMON, " %4d%4d%4d%4d", 0, 1, 2, 3); + for (int i = 0; i < 4; i++) { + NOTICE_LOG(COMMON, "%d>%4d%4d%4d%4d", i, rtt_matrix_[i][0], rtt_matrix_[i][1], rtt_matrix_[i][2], rtt_matrix_[i][3]); + } + + NOTICE_LOG(COMMON, "CANDIDATES"); + for (const auto &c : candidates_) { + NOTICE_LOG(COMMON, "[%s] Peer%d %s: ping=%d pong=%d rtt=%.2f addr=%s", 0 < c.pong_count ? "x" : " ", c.peer_id, + peer_to_user_[c.peer_id].c_str(), c.ping_count, c.pong_count, c.rtt, c.remote.masked_addr().c_str()); + } + } + + NOTICE_LOG(COMMON, "End UdpPingPong Thread"); + client_.Close(); + running_ = false; }).detach(); } @@ -786,7 +786,7 @@ bool UdpPingPong::GetAvailableAddress(uint8_t peer_id, sockaddr_storage *dst, fl // Return min rtt address bool found = false; float min_rtt = 1000.0f; - for (auto& c : candidates_) { + for (auto &c : candidates_) { if (c.peer_id == peer_id && 0 < c.pong_count) { if (0 < c.rtt && c.rtt < min_rtt) { min_rtt = c.rtt; diff --git a/core/gdxsv/libs.h b/core/gdxsv/libs.h index d210d9db31..a48d362d4e 100644 --- a/core/gdxsv/libs.h +++ b/core/gdxsv/libs.h @@ -1,19 +1,11 @@ #pragma once -#include "types.h" #include "hw/sh4/sh4_mem.h" +#include "types.h" -inline u32 gdxsv_ReadMem32(u32 addr) { - return ReadMem32_nommu(addr); - -} -inline u16 gdxsv_ReadMem16(u32 addr) { - return ReadMem16_nommu(addr); -} - -inline u8 gdxsv_ReadMem8(u32 addr) { - return ReadMem8_nommu(addr); -} +inline u32 gdxsv_ReadMem32(u32 addr) { return ReadMem32_nommu(addr); } +inline u16 gdxsv_ReadMem16(u32 addr) { return ReadMem16_nommu(addr); } +inline u8 gdxsv_ReadMem8(u32 addr) { return ReadMem8_nommu(addr); } inline u32 gdxsv_ReadMem(int bits, u32 addr) { if (bits == 32) return gdxsv_ReadMem32(addr); @@ -35,8 +27,12 @@ inline void gdxsv_WriteMem8(u32 addr, u8 value) { } inline void gdxsv_WriteMem(int bits, u32 addr, u32 value) { - if (bits == 32) gdxsv_WriteMem32(addr, value); - else if (bits == 16) gdxsv_WriteMem16(addr, value & 0xffffu); - else if (bits == 8) gdxsv_WriteMem8(addr, value & 0xffu); - else verify(false); + if (bits == 32) + gdxsv_WriteMem32(addr, value); + else if (bits == 16) + gdxsv_WriteMem16(addr, value & 0xffffu); + else if (bits == 8) + gdxsv_WriteMem8(addr, value & 0xffu); + else + verify(false); } diff --git a/core/hw/sh4/dyna/blockmanager.cpp b/core/hw/sh4/dyna/blockmanager.cpp index 92f929b80f..8c2535163b 100644 --- a/core/hw/sh4/dyna/blockmanager.cpp +++ b/core/hw/sh4/dyna/blockmanager.cpp @@ -582,7 +582,7 @@ void bm_RamWriteAccess(u32 addr) std::vector list_copy; list_copy.insert(list_copy.begin(), block_list.begin(), block_list.end()); if (!list_copy.empty()) - DEBUG_LOG(DYNAREC, "bm_RamWriteAccess write access to %08x pc %08x", addr, next_pc); + WARN_LOG(DYNAREC, "bm_RamWriteAccess write access to %08x pc %08x", addr, next_pc); for (auto& block : list_copy) bm_DiscardBlock(block); verify(block_list.empty());