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 <noreply@anthropic.com>
This commit is contained in:
Henrik RydgårdandClaude Opus 5.5 committed 2026-10-31 00:44:36 +01:00
1 parent be7a3261b5
commit 58d051130c
2 files changed
+45 -1

No files matched your search

+43 -1
View File
@@ -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;
+2
View File
@@ -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