diff --git a/Core/CmdLine.cpp b/Core/CmdLine.cpp index 7ea12e14a1..0e92a5d902 100644 --- a/Core/CmdLine.cpp +++ b/Core/CmdLine.cpp @@ -258,6 +258,9 @@ static const CommandLineParam g_autoParams[] = { {POFF(realtime), CmdParamType::Bool, "realtime", '\0', "Run at PSP speed instead of as fast as possible", CmdLineMode::Headless}, {POFF(adhocServer), CmdParamType::Bool, "adhoc-server", '\0', "Run the built-in ad hoc server"}, {POFF(nickname), CmdParamType::String, "nickname", '\0', "The PSP's nickname (System settings), for this run only"}, + {POFF(adhocDelayMs), CmdParamType::Int, "adhoc-delay", '\0', "Delay ad hoc data received through the relay by MS milliseconds, to simulate a distant player"}, + {POFF(adhocJitterMs), CmdParamType::Int, "adhoc-jitter", '\0', "With --adhoc-delay, up to MS more at random"}, + {POFF(adhocLossPercent), CmdParamType::Int, "adhoc-loss", '\0', "Drop this percentage of ad hoc datagrams received through the relay"}, {POFF(lagSync), CmdParamType::Bool, "lag-sync", '\0', "Keep emulated time in step with the wall clock every millisecond, not every frame (the Force real clock sync setting)"}, {POFF(frameStats), CmdParamType::Bool, "frame-stats", '\0', "Print flips per emulated second at the end of the run, counted from --state's load", CmdLineMode::Headless}, {POFF(maxScreenshotError), CmdParamType::Double, "max-mse", '\0', "Maximum allowed MSE error for screenshot comparison", CmdLineMode::Headless}, @@ -579,6 +582,18 @@ void CommandLineOptions::ApplyToConfig() const { g_Config.sNickName = nickname.value(); g_Config.DoNotSaveSetting(&g_Config.sNickName); } + if (adhocDelayMs.has_value()) { + g_Config.iAdhocSimDelayMs = adhocDelayMs.value(); + g_Config.DoNotSaveSetting(&g_Config.iAdhocSimDelayMs); + } + if (adhocJitterMs.has_value()) { + g_Config.iAdhocSimJitterMs = adhocJitterMs.value(); + g_Config.DoNotSaveSetting(&g_Config.iAdhocSimJitterMs); + } + if (adhocLossPercent.has_value()) { + g_Config.iAdhocSimLossPercent = adhocLossPercent.value(); + g_Config.DoNotSaveSetting(&g_Config.iAdhocSimLossPercent); + } if (lagSync.has_value()) { g_Config.bForceLagSync = lagSync.value(); g_Config.DoNotSaveSetting(&g_Config.bForceLagSync); diff --git a/Core/CmdLine.h b/Core/CmdLine.h index cfb79b2be8..e46b5679c1 100644 --- a/Core/CmdLine.h +++ b/Core/CmdLine.h @@ -202,6 +202,9 @@ struct CommandLineOptions { std::optional adhocServer; std::optional lagSync; std::optional nickname; + std::optional adhocDelayMs; + std::optional adhocJitterMs; + std::optional adhocLossPercent; std::optional screenshotFilename; std::optional screenshotFilenameSave; diff --git a/Core/Config.cpp b/Core/Config.cpp index 5a887b91fe..42ec99eb8e 100644 --- a/Core/Config.cpp +++ b/Core/Config.cpp @@ -1419,6 +1419,9 @@ static constexpr ConfigSetting networkSettings[] = { ConfigSetting("PortOffset", SETTING(g_Config, iPortOffset), 10000, CfgFlag::PER_GAME) .Label(I18NCat::NETWORKING, "Port offset").Range(0, 60000, 100), ConfigSetting("PrimaryDNSServer", SETTING(g_Config, sInfrastructureDNSServer), "67.222.156.250", CfgFlag::PER_GAME), + ConfigSetting("AdhocSimDelayMs", SETTING(g_Config, iAdhocSimDelayMs), 0, CfgFlag::DEFAULT), + ConfigSetting("AdhocSimJitterMs", SETTING(g_Config, iAdhocSimJitterMs), 0, CfgFlag::DEFAULT), + ConfigSetting("AdhocSimLossPercent", SETTING(g_Config, iAdhocSimLossPercent), 0, CfgFlag::DEFAULT), ConfigSetting("MinTimeout", SETTING(g_Config, iMinTimeout), 0, CfgFlag::PER_GAME) .Label(I18NCat::NETWORKING, "Minimum Timeout", "Minimum Timeout (override in ms, 0 = default)").Range(0, 15000, 50).Units("ms"), ConfigSetting("ForcedFirstConnect", SETTING(g_Config, bForcedFirstConnect), false, CfgFlag::PER_GAME) diff --git a/Core/Config.h b/Core/Config.h index 3a981eafd4..3cfc4be197 100644 --- a/Core/Config.h +++ b/Core/Config.h @@ -630,6 +630,10 @@ public: bool bInfrastructureAutoDNS; bool bAllowSavestateWhileConnected; // Developer option, ini-only. No normal users need this, it's always wrong to save/load state when online. bool bAllowSpeedControlWhileConnected; // Useful in some games but not recommended. + // Developer options, ini-only: simulate a slower network for ad hoc data received through the relay. + int iAdhocSimDelayMs; // Added to every datagram's delivery. + int iAdhocSimJitterMs; // Up to this much more, at random (order is kept). + int iAdhocSimLossPercent; // Datagrams dropped. std::string sAdhocServerListUrl; std::vector vCustomAdhocServerList; diff --git a/Core/HLE/sceNetAdhoc.cpp b/Core/HLE/sceNetAdhoc.cpp index 83e5396c5d..cf5cda7e43 100644 --- a/Core/HLE/sceNetAdhoc.cpp +++ b/Core/HLE/sceNetAdhoc.cpp @@ -22,8 +22,12 @@ #include "ppsspp_config.h" +#include +#include #include +#include #include +#include #include "Common/Net/SocketCompat.h" #include "Common/Data/Format/JSONReader.h" @@ -825,13 +829,99 @@ static void *pdp_postoffice_recover(int idx) { return internal->postofficeHandle; } +// Developer options (--adhoc-delay, --adhoc-jitter, --adhoc-loss): datagrams from the relay wait in a +// queue of our own until their simulated arrival time, measured on the wall clock. +struct SimDelayedPdp { + double due; + SceNetEtherAddr mac; + int port; // As the relay reports it. + std::vector data; +}; +static std::map> simDelayedPdp; // By socket index. +static std::mt19937 simRandom; + +static bool simPdpInUse(int idx) { + if (g_Config.iAdhocSimDelayMs > 0 || g_Config.iAdhocSimJitterMs > 0 || g_Config.iAdhocSimLossPercent > 0) { + return true; + } + // Turned off with datagrams still waiting: hand those out first. + auto it = simDelayedPdp.find(idx); + return it != simDelayedPdp.end() && !it->second.empty(); +} + +// Moves what the relay has delivered into the socket's queue. False if the session died. +static bool simDrainPdp(int idx, void *pdp_sock) { + auto &queue = simDelayedPdp[idx]; + const double now = time_now_d(); + while (true) { + const int size = pdp_peek_next_size(pdp_sock); + if (size == AEMU_POSTOFFICE_CLIENT_SESSION_DEAD) { + return false; + } + if (size <= 0) { + return true; + } + SimDelayedPdp pdp{}; + pdp.data.resize(size); + int len = size; + const int status = pdp_recv(pdp_sock, (char *)&pdp.mac, &pdp.port, (char *)pdp.data.data(), &len, true); + if (status == AEMU_POSTOFFICE_CLIENT_SESSION_DEAD) { + return false; + } + if (status != AEMU_POSTOFFICE_CLIENT_OK) { + return true; + } + if ((int)(simRandom() % 100) < g_Config.iAdhocSimLossPercent) { + continue; + } + int delayMs = std::max(0, g_Config.iAdhocSimDelayMs); + if (g_Config.iAdhocSimJitterMs > 0) { + delayMs += (int)(simRandom() % (g_Config.iAdhocSimJitterMs + 1)); + } + // Kept in order, like the relay's stream. + pdp.due = now + delayMs / 1000.0; + if (!queue.empty()) { + pdp.due = std::max(pdp.due, queue.back().due); + } + queue.push_back(std::move(pdp)); + } +} + +// The size of the first datagram that's arrived, or 0. Like pdp_peek_next_size(), including SESSION_DEAD. +static int simPeekPdp(int idx, void *pdp_sock) { + if (!simDrainPdp(idx, pdp_sock)) { + return AEMU_POSTOFFICE_CLIENT_SESSION_DEAD; + } + const auto &queue = simDelayedPdp[idx]; + return !queue.empty() && queue.front().due <= time_now_d() ? (int)queue.front().data.size() : 0; +} + +// Like pdp_buffered_data_size(), counting only what's arrived. +static int pdp_buffered_postoffice(int idx, void *pdp_sock) { + if (!simPdpInUse(idx)) { + return pdp_buffered_data_size(pdp_sock); + } + if (!simDrainPdp(idx, pdp_sock)) { + return AEMU_POSTOFFICE_CLIENT_SESSION_DEAD; + } + const double now = time_now_d(); + int total = 0; + for (const auto &pdp : simDelayedPdp[idx]) { + if (pdp.due > now) { + break; + } + total += (int)pdp.data.size(); + } + return total; +} + static int pdp_peek_next_size_postoffice(int idx) { AdhocSocket *internal = adhocSockets[idx]; void *pdp_sock = pdp_postoffice_recover(idx); if (pdp_sock == NULL) { return 0; } - int peek_status = pdp_peek_next_size(pdp_sock); + int peek_status = simPdpInUse(idx) ? simPeekPdp(idx, pdp_sock) : pdp_peek_next_size(pdp_sock); if (peek_status == AEMU_POSTOFFICE_CLIENT_SESSION_DEAD) { pdp_delete(internal->postofficeHandle); internal->postofficeHandle = NULL; @@ -889,7 +979,24 @@ static int pdp_recv_postoffice(int idx, SceNetEtherAddr *saddr, uint16_t *sport, SceNetEtherAddr saddr_copy = {0}; int len_copy = *len; - int pdp_recv_status = pdp_recv(pdp_sock, (char *)&saddr_copy, &sport_copy, (char *)data, &len_copy, true); + int pdp_recv_status; + if (simPdpInUse(idx)) { + pdp_recv_status = simPeekPdp(idx, pdp_sock); + if (pdp_recv_status > 0) { + auto &queue = simDelayedPdp[idx]; + SimDelayedPdp &pdp = queue.front(); + saddr_copy = pdp.mac; + sport_copy = pdp.port; + len_copy = std::min(len_copy, (int)pdp.data.size()); + memcpy(data, pdp.data.data(), len_copy); + queue.pop_front(); + pdp_recv_status = AEMU_POSTOFFICE_CLIENT_OK; + } else if (pdp_recv_status == 0) { + pdp_recv_status = AEMU_POSTOFFICE_CLIENT_SESSION_WOULD_BLOCK; + } + } else { + pdp_recv_status = pdp_recv(pdp_sock, (char *)&saddr_copy, &sport_copy, (char *)data, &len_copy, true); + } if (pdp_recv_status == AEMU_POSTOFFICE_CLIENT_SESSION_DEAD) { handle_relay_connect_failure(); pdp_delete(internal->postofficeHandle); @@ -2232,6 +2339,7 @@ static void aemu_postoffice_server_log(const char *format, ...) { } void __NetAdhocInit() { + simDelayedPdp.clear(); friendFinderRunning = false; netAdhocInited = false; netAdhocctlInited = false; @@ -3343,7 +3451,7 @@ static bool postoffice_can_recv_or_listen_has_request(int idx) { if (pdp_handle == NULL) { return false; } - int peek_len = pdp_buffered_data_size(pdp_handle); + int peek_len = pdp_buffered_postoffice(idx, pdp_handle); if (peek_len == AEMU_POSTOFFICE_CLIENT_SESSION_DEAD) { internal->postofficeHandle = NULL; pdp_delete(pdp_handle); @@ -3605,6 +3713,7 @@ int sceNetAdhocPollSocket(MIPSState *mips, u32 socketStructAddr, int count, int } static int pdp_delete_postoffice(int idx) { + simDelayedPdp.erase(idx); AdhocSocket *internal = adhocSockets[idx]; if (internal->postofficeHandle != NULL) { pdp_delete(internal->postofficeHandle); @@ -4541,7 +4650,7 @@ static int sceNetAdhocGetPdpStat(MIPSState *mips, u32 structSize, u32 structAddr if (serverHasRelay) { void *postofficeHandle = pdp_postoffice_recover(j); if (postofficeHandle != NULL) { - sock->data.pdp.rcv_sb_cc = pdp_buffered_data_size(postofficeHandle); + sock->data.pdp.rcv_sb_cc = pdp_buffered_postoffice(j, postofficeHandle); } } else { sock->data.pdp.rcv_sb_cc = getAvailToRecv(sock->data.pdp.id, sock->buffer_size);