From 67c29c62b0c00c152b39e8710a2b1ae53814339a Mon Sep 17 00:00:00 2001 From: Wit <58151178+Wit-MKW@users.noreply.github.com> Date: Tue, 24 Jun 2025 19:21:38 -0400 Subject: [PATCH 1/2] update P2P synchronisation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit the P2P features of Zero Tours only work in one direction at a time, so it won't do to only receive as many packets as are sent; in fact, it seems that this was never the issue with Pokémon. rather, the issue with Pokémon was that it was receiving a low-level error on remote disconnect, which isn't even a detectable state when the connection is over a phone line. rather, we simply stop sending the game data, allowing it to detect the remote disconnect through a timeout or other method which, to reiterate, was necessary in '01/'02 when these games were connecting over a phone line. --- commands.c | 33 +++++++++------------------------ commands.h | 1 - 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/commands.c b/commands.c index bcb9767..9d14acf 100644 --- a/commands.c +++ b/commands.c @@ -325,7 +325,6 @@ static struct mobile_packet *command_tel_ip(struct mobile_adapter *adapter, stru } s->state = MOBILE_CONNECTION_CALL; - s->call_packets_sent = 0; packet->length = 0; return packet; @@ -361,7 +360,6 @@ static struct mobile_packet *command_tel_relay(struct mobile_adapter *adapter, s } s->state = MOBILE_CONNECTION_CALL; - s->call_packets_sent = 0; packet->length = 0; return packet; @@ -464,7 +462,6 @@ static struct mobile_packet *command_wait_call_ip(struct mobile_adapter *adapter if (!mobile_cb_sock_accept(adapter, 0)) return NULL; s->state = MOBILE_CONNECTION_CALL_RECV; - s->call_packets_sent = 0; packet->length = 0; return packet; @@ -500,7 +497,6 @@ static struct mobile_packet *command_wait_call_relay(struct mobile_adapter *adap } s->state = MOBILE_CONNECTION_CALL_RECV; - s->call_packets_sent = 0; packet->length = 0; return packet; @@ -625,31 +621,20 @@ static struct mobile_packet *command_data(struct mobile_adapter *adapter, struct } return NULL; } - - // Pokémon Crystal expects communications to be "synchronized". - // For this, we only try to receive packets when we've sent one. - // TODO: Check other games with peer to peer functionality. - if (!internet) { - if (s->call_packets_sent < 0xFF) s->call_packets_sent++; - } } - int recv_size = 0; - if (internet || s->call_packets_sent) { - recv_size = mobile_cb_sock_recv(adapter, conn, data, - MOBILE_MAX_TRANSFER_SIZE, NULL); - } + int recv_size = mobile_cb_sock_recv(adapter, conn, data, + MOBILE_MAX_TRANSFER_SIZE, NULL); - if (!internet && recv_size > 0) { - if (s->call_packets_sent > 0) s->call_packets_sent--; - } - - // If connected to the internet, and a disconnect is received, we should - // inform the game about a remote disconnect. - if (internet && recv_size == -2) { + if (recv_size == -2) { mobile_cb_sock_close(adapter, conn); s->connections[conn] = false; - packet->command = MOBILE_COMMAND_DATA_END; + + // If connected to the internet, and a disconnect is received, we + // should inform the game about a remote disconnect. + if (internet) { + packet->command = MOBILE_COMMAND_DATA_END; + } packet->length = 1; return packet; } diff --git a/commands.h b/commands.h index 7005686..d9aaa01 100644 --- a/commands.h +++ b/commands.h @@ -65,7 +65,6 @@ struct mobile_adapter_commands { enum mobile_connection_state state; bool connections[MOBILE_MAX_CONNECTIONS]; bool dns2_use; - unsigned char call_packets_sent; struct mobile_addr4 dns1; struct mobile_addr4 dns2; }; From 0704f56902f23b7ebf05c82c222e0e145e3140b6 Mon Sep 17 00:00:00 2001 From: Wit <58151178+Wit-MKW@users.noreply.github.com> Date: Wed, 25 Jun 2025 01:59:45 -0400 Subject: [PATCH 2/2] keep P2P socket open on remote disconnect this allows for games not to notice a remote disconnect right away, at the expense of calling `sock_recv` one or more times after a remote disconnect. --- commands.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/commands.c b/commands.c index 9d14acf..7b99118 100644 --- a/commands.c +++ b/commands.c @@ -627,12 +627,11 @@ static struct mobile_packet *command_data(struct mobile_adapter *adapter, struct MOBILE_MAX_TRANSFER_SIZE, NULL); if (recv_size == -2) { - mobile_cb_sock_close(adapter, conn); - s->connections[conn] = false; - // If connected to the internet, and a disconnect is received, we // should inform the game about a remote disconnect. if (internet) { + mobile_cb_sock_close(adapter, conn); + s->connections[conn] = false; packet->command = MOBILE_COMMAND_DATA_END; } packet->length = 1;