Files
RetroArch/network/netplay/netplay.h
Nelfe80andlibretroadmin 0afe7f41d1 netplay: record confirmed multi-player input in BSV replays (#19870)
* replay: accept captured normal states for BSV checkpoints

Let netplay supply a frame's saved state after confirmation without serializing the current speculative core state. Preserve the existing on-disk format and local recording path.

* netplay: commit BSV input frames after rollback confirmation

Stage the input queries seen by the core and replace them during rollback. Commit each confirmed frame once, using portable initial states and checkpoints captured at its emulated boundary. Keep local input sampling and stalled iterations out of the movie. Reject unsynchronized starts and stop at state discontinuities.

* test: cover confirmed netplay BSV recording and offline replay

Exercise the shipping recorder and rollback loop with delayed input, ring wrap, mid-session starts, stalls, absent ports and checkpoints. Compare host, client and spectator recordings, then replay through the ordinary BSV reader. Add sanitizer and Windows CI coverage.

* netplay: keep replay state access in the input frontend

Keep recording flags, checkpoint requests and notifications in the input frontend. Exercise the same frontend calls in the netplay BSV harness.

* netplay: free BSV staging and keep netplay movies out of savestates

Free each staged frame's states once it is committed, and drop all
staging when a netplay recording stops, so a session no longer holds a
normal savestate per ring slot. Skip the speculative initial state at
record start, keep netplay movies out of savestates since they lag the
core by unconfirmed frames, and keep the recorder's state in flag words.

The netplay BSV tests check that committed and stopped recordings keep
no states, and the MinGW lane links statically like the other wine lanes.

---------

Co-authored-by: libretroadmin <[email protected]>
2026-10-24 04:34:26 +02:00

272 lines
7.3 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
* Copyright (C) 2016-2017 - Gregor Richards
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __RARCH_NETPLAY_H
#define __RARCH_NETPLAY_H
#include <stdint.h>
#include <stddef.h>
#include <boolean.h>
#include <retro_common_api.h>
#ifdef HAVE_CONFIG_H
#include "../../config.h"
#endif
#include <retro_miscellaneous.h>
#include <net/net_compat.h>
#include "netplay_defines.h"
#include "../../msg_hash.h"
#include "../natt.h"
RETRO_BEGIN_DECLS
typedef struct netplay netplay_t;
#ifdef HAVE_BSV_MOVIE
struct bsv_movie;
bool netplay_bsv_movie_init(struct bsv_movie *movie);
#endif
typedef struct netplay_client_info
{
uint32_t protocol;
uint32_t devices;
uint32_t slowdowns;
int32_t ping;
int id;
enum rarch_netplay_connection_mode mode;
char name[NETPLAY_NICK_LEN];
} netplay_client_info_t;
typedef struct mitm_server
{
const char *name;
enum msg_hash_enums description;
} mitm_server_t;
#ifndef HAVE_DYNAMIC
struct netplay_fork_args
{
size_t size;
char args[PATH_MAX_LENGTH];
};
#endif
struct netplay_room
{
struct netplay_room *next;
int id;
int gamecrc;
int port;
int mitm_port;
int host_method;
int player_count;
int spectator_count;
char nickname[NETPLAY_NICK_LEN];
char frontend[NETPLAY_HOST_STR_LEN];
char corename[NETPLAY_HOST_STR_LEN];
char coreversion[NETPLAY_HOST_STR_LEN];
char retroarch_version[NETPLAY_HOST_STR_LEN];
char gamename[NETPLAY_HOST_LONGSTR_LEN];
char subsystem_name[NETPLAY_HOST_LONGSTR_LEN];
char country[3];
char address[NETPLAY_HOST_LONGSTR_LEN];
char mitm_handle[NETPLAY_HOST_STR_LEN];
char mitm_address[NETPLAY_HOST_LONGSTR_LEN];
char mitm_session[NETPLAY_HOST_STR_LEN];
bool has_password;
bool has_spectate_password;
bool connectable;
bool is_retroarch;
bool lan;
};
struct netplay_rooms
{
struct netplay_room *head;
struct netplay_room *cur;
};
struct netplay_host
{
int content_crc;
int port;
char address[16];
char nick[NETPLAY_NICK_LEN];
char frontend[NETPLAY_HOST_STR_LEN];
char core[NETPLAY_HOST_STR_LEN];
char core_version[NETPLAY_HOST_STR_LEN];
char retroarch_version[NETPLAY_HOST_STR_LEN];
char content[NETPLAY_HOST_LONGSTR_LEN];
char subsystem_name[NETPLAY_HOST_LONGSTR_LEN];
bool has_password;
bool has_spectate_password;
};
struct netplay_host_list
{
struct netplay_host *hosts;
size_t allocated;
size_t size;
};
struct netplay_chat_buffer
{
struct
{
uint8_t alpha;
char nick[NETPLAY_NICK_LEN];
char msg[NETPLAY_CHAT_MAX_SIZE];
} messages[NETPLAY_CHAT_MAX_MESSAGES];
uint32_t color_name;
uint32_t color_msg;
};
typedef struct
{
#ifndef HAVE_DYNAMIC
struct netplay_fork_args fork_args;
#endif
/* NAT traversal info (if NAT traversal is used and serving) */
struct nat_traversal_data nat_traversal_request;
#ifdef HAVE_NETPLAYDISCOVERY
/* List of discovered hosts */
struct netplay_host_list discovered_hosts;
#endif
struct netplay_chat_buffer chat_buffer;
struct netplay_room host_room;
struct netplay_room *room_list;
struct netplay_rooms *rooms_data;
struct retro_netpacket_callback *core_netpacket_interface;
/* Used while Netplay is running */
netplay_t *data;
netplay_client_info_t *client_info;
size_t client_info_count;
#ifdef HAVE_NETPLAYDISCOVERY
/* LAN discovery sockets */
int lan_ad_server_fd;
int lan_ad_client_fd;
#endif
int room_count;
int latest_ping;
unsigned server_port_deferred;
uint8_t flags;
char server_address_deferred[256];
char server_session_deferred[32];
} net_driver_state_t;
net_driver_state_t *networking_state_get_ptr(void);
#ifdef HAVE_NETWORKING
/* The content fingerprint netplay advertises, compares and searches by.
* Derived on first use and cached; see netplay_frontend.c.
*
* Netplay is the only consumer of a content CRC in the frontend, so
* this exists only where netplay does - a target built without
* networking has neither this nor anything that calls it. */
uint32_t netplay_content_crc(void);
#endif
bool netplay_compatible_version(const char *version);
bool netplay_decode_hostname(const char *hostname,
char *address, unsigned *port, char *session, size_t len);
int netplay_rooms_parse(const char *buf, size_t len);
int netplay_rooms_get_count(void);
struct netplay_room *netplay_room_get(int index);
void netplay_rooms_free(void);
/**
* init_netplay
* @server : server address to connect to (client only)
* @port : TCP port to host on/connect to
* @mitm_session : Session id for MITM/tunnel (client only).
*
* Initializes netplay.
*
* If netplay is already initialized, will return false (0).
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool init_netplay(const char *server, unsigned port, const char *mitm_session);
bool init_netplay_deferred(const char *server, unsigned port,
const char *mitm_session);
void deinit_netplay(void);
bool netplay_driver_ctl(enum rarch_netplay_ctl_state state, void *data);
/**
* netplay_mitm_query_prefetch:
*
* Start the relay tunnel query early, when the user commits to
* hosting, rather than when host setup needs the address. The round
* trip then overlaps work that was going to happen anyway, so host
* setup usually finds the answer in hand and is not deferred.
*
* Best effort: if it is skipped or fails, host setup issues the
* query exactly as it did before.
*/
void netplay_mitm_query_prefetch(void);
/**
* netplay_host_setup_defer:
*
* Called by host setup (CMD_EVENT_NETPLAY_INIT) before it runs: when
* hosting through a relay whose address query is still out, puts the
* setup off to a main-thread task that runs it once the answer is in
* (or directly, past the query's bound) and returns true; otherwise
* returns false and the setup goes ahead now.
**/
bool netplay_host_setup_defer(void);
/* True while host setup is deferred on the tunnel query. */
bool netplay_host_setup_pending(void);
bool netplay_reinit_serialization(void);
bool netplay_is_spectating(void);
void netplay_force_send_savestate(void);
#ifdef HAVE_NETPLAYDISCOVERY
/** Initialize Netplay discovery */
bool init_netplay_discovery(void);
/** Deinitialize and free Netplay discovery */
void deinit_netplay_discovery(void);
/**
* netplay_discovery_free_hosts:
*
* Frees the LAN discovery result list. One-time teardown only.
*/
void netplay_discovery_free_hosts(void);
/** Discovery control */
bool netplay_discovery_driver_ctl(enum rarch_netplay_discovery_ctl_state state,
void *data);
#endif
extern const mitm_server_t netplay_mitm_server_list[NETPLAY_MITM_SERVERS];
RETRO_END_DECLS
#endif