Skip to content

Commit

Permalink
Merge pull request flyinghead#214 from inada-s/code-cleaning-20230715
Browse files Browse the repository at this point in the history
Code cleaning 20230715
  • Loading branch information
inada-s committed Jul 15, 2023
2 parents 67c0657 + 66136be commit 663cac6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 68 deletions.
17 changes: 9 additions & 8 deletions core/gdxsv/gdxsv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<u8> &out) {
z_stream z{};
Expand Down Expand Up @@ -147,6 +146,7 @@ void Gdxsv::Reset() {
id[i] = lbs_msg.body[2 + i];
}
user_id_ = id;
lbs_net_.Send(GenerateP2PMatchReportPacket());
NotifyWanPort();
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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_;

Expand Down Expand Up @@ -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());
}
}
Expand All @@ -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"

Expand Down
8 changes: 4 additions & 4 deletions core/gdxsv/gdxsv_backend_replay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand All @@ -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());
}
}
Expand Down
8 changes: 5 additions & 3 deletions core/gdxsv/gdxsv_backend_rollback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
72 changes: 36 additions & 36 deletions core/gdxsv/gdxsv_network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ std::string sockaddr_to_string(const sockaddr *addr) {
const auto a = reinterpret_cast<const sockaddr_in6 *>(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 "";
}
Expand All @@ -136,12 +136,12 @@ bool is_private_addr(const sockaddr *addr) {
}
if (addr->sa_family == AF_INET) {
const auto a = reinterpret_cast<const sockaddr_in *>(addr);
const auto ip4 = reinterpret_cast<const uint8_t*>(&a->sin_addr);
const auto ip4 = reinterpret_cast<const uint8_t *>(&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<const sockaddr_in6 *>(addr);
const auto ip6 = reinterpret_cast<const uint8_t*>(&a->sin6_addr);
const auto ip6 = reinterpret_cast<const uint8_t *>(&a->sin6_addr);
return (ip6[0] & 0xfe) == 0xfc;
}
return false;
Expand Down Expand Up @@ -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<int> 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<std::chrono::milliseconds>(now - start_time_).count();
auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time_).count();

while (true) {
Packet recv{};
Expand Down Expand Up @@ -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<std::recursive_mutex> lock(mutex_);

Packet p{};
Expand All @@ -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<const char*>(&p), sizeof(p), c.remote);
client_.SendTo(reinterpret_cast<const char *>(&p), sizeof(p), c.remote);
}
}
}
Expand All @@ -673,11 +666,12 @@ void UdpPingPong::Start(uint32_t session_id, uint8_t peer_id, int port, int dura
.count();
auto rtt = static_cast<int>(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<uint8_t>(std::min(255, (int)std::ceil(c.rtt)));
Expand All @@ -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()) {
Expand All @@ -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<std::recursive_mutex> lock(mutex_);
for (int i = 0; i < std::min<int>(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;
Expand All @@ -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<std::recursive_mutex> 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();
}

Expand Down Expand Up @@ -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;
Expand Down
28 changes: 12 additions & 16 deletions core/gdxsv/libs.h
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion core/hw/sh4/dyna/blockmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ void bm_RamWriteAccess(u32 addr)
std::vector<RuntimeBlockInfo*> 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());
Expand Down

0 comments on commit 663cac6

Please sign in to comment.