From 58d051130c328bb739c6ee64da7967471aeb8d96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 30 Oct 2026 23:39:25 +0100 Subject: [PATCH] sceNetAdhoc: Wait for a relay PTP connect at a fixed point in emulated time The connect PtpOpen starts goes through the relay on a host thread, so when it finished in emulated time depended on the emulation speed and on how busy the host was. At --speed=800 on a loaded CI runner it often wasn't up within the 100 ms net/adhoc/pair/ptppoll polls for, while on a PSP it's up a few ms after PtpOpen. The relay's per-millisecond poll now waits (at most a second, once per connect) for a connect in progress to finish, filling local listeners' backlogs meanwhile, since the listener may be on the same console. Co-Authored-By: Claude Opus 5.5 --- Core/HLE/AdhocTransportRelay.cpp | 44 +++++++++++++++++++++++++++++++- Core/HLE/proAdhoc.h | 2 ++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Core/HLE/AdhocTransportRelay.cpp b/Core/HLE/AdhocTransportRelay.cpp index fde62063f7..0e324ca783 100644 --- a/Core/HLE/AdhocTransportRelay.cpp +++ b/Core/HLE/AdhocTransportRelay.cpp @@ -514,7 +514,7 @@ bool RelayTransport::PtpPeerClosed(AdhocSocket *sock) { return sock->postofficeHandle != NULL && ptp_is_dead(sock->postofficeHandle); } -void RelayTransport::Poll() { +static void fillRelayBacklogs() { for (int i = 0; i < MAX_SOCKET; i++) { AdhocSocket *sock = adhocSockets[i]; if (sock != NULL && sock->type == SOCK_PTP && sock->data.ptp.state == ADHOC_PTP_STATE_LISTEN && sock->postofficeHandle != NULL) { @@ -523,6 +523,47 @@ void RelayTransport::Poll() { } } +// Whether a PTP connect the emulation hasn't waited for yet is still going. With markWaited, notes that it +// has now. +static bool relayConnectInFlight(bool markWaited = false) { + bool found = false; + for (int i = 0; i < MAX_SOCKET; i++) { + AdhocSocket *sock = adhocSockets[i]; + if (sock != NULL && sock->type == SOCK_PTP && sock->connectThread != NULL && !sock->connectThreadDone && !sock->connectThreadWaited) { + found = true; + if (markWaited) { + sock->connectThreadWaited = true; + } + } + } + return found; +} + +// How long the emulation waits for a PTP connect through the relay, in real time. +static const double RELAY_CONNECT_WAIT_SECONDS = 1.0; + +void RelayTransport::Poll() { + fillRelayBacklogs(); + + // A connect takes a few round trips through the relay, in real time, and the PSP has an opened + // connection up a few ms after PtpOpen (pspautotests net/adhoc/pair/ptppoll). So wait for it here, at + // most a millisecond of emulated time after it started, instead of letting the emulation speed decide + // when the game sees it. The listener may be on this console, so keep accepting meanwhile. + if (!relayConnectInFlight()) { + return; + } + double start = time_now_d(); + while (relayConnectInFlight()) { + if (time_now_d() - start > RELAY_CONNECT_WAIT_SECONDS) { + WARN_LOG(Log::sceNet, "Relay: a PTP connect took over %.1f s, no longer waiting for it", RELAY_CONNECT_WAIT_SECONDS); + relayConnectInFlight(true); + return; + } + sleep_ms(1, "relay-connect"); + fillRelayBacklogs(); + } +} + static int ptp_accept_postoffice(int idx, SceNetEtherAddr *saddr, uint16_t *sport) { fillRelayBacklog(idx); RelayBacklog &backlog = relayBacklogs[adhocSockets[idx]]; @@ -626,6 +667,7 @@ static int ptp_connect_postoffice(int idx, const char *caller) { internal->connectThreadDone = false; internal->connectThreadResult = 0; + internal->connectThreadWaited = false; internal->connectThread = new std::thread([internal, addr, idx] { int state; diff --git a/Core/HLE/proAdhoc.h b/Core/HLE/proAdhoc.h index 5f83a2bfc0..1b2289dfa3 100644 --- a/Core/HLE/proAdhoc.h +++ b/Core/HLE/proAdhoc.h @@ -340,6 +340,8 @@ typedef struct AdhocSocket { std::thread *connectThread; bool connectThreadDone; int connectThreadResult; + // Whether the emulation has already waited for the connect in progress. + bool connectThreadWaited; } PACK AdhocSocket; // Gamemode Optional Peer Buffer Data