1 /* RetroArch - A frontend for libretro. 2 * Copyright (C) 2010-2014 - Hans-Kristian Arntzen 3 * Copyright (C) 2011-2017 - Daniel De Matteis 4 * Copyright (C) 2016-2017 - Gregor Richards 5 * 6 * RetroArch is free software: you can redistribute it and/or modify it under the terms 7 * of the GNU General Public License as published by the Free Software Found- 8 * ation, either version 3 of the License, or (at your option) any later version. 9 * 10 * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 12 * PURPOSE. See the GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License along with RetroArch. 15 * If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef __RARCH_NETPLAY_PRIVATE_H 19 #define __RARCH_NETPLAY_PRIVATE_H 20 21 #include "netplay.h" 22 23 #include <net/net_compat.h> 24 #include <net/net_natt.h> 25 #include <features/features_cpu.h> 26 #include <streams/trans_stream.h> 27 28 #include "../../msg_hash.h" 29 #include "../../verbosity.h" 30 31 #define NETPLAY_PROTOCOL_VERSION 5 32 33 #define RARCH_DEFAULT_PORT 55435 34 #define RARCH_DEFAULT_NICK "Anonymous" 35 36 #define NETPLAY_NICK_LEN 32 37 #define NETPLAY_PASS_LEN 128 38 #define NETPLAY_PASS_HASH_LEN 64 /* length of a SHA-256 hash */ 39 40 #define MAX_SERVER_STALL_TIME_USEC (5*1000*1000) 41 #define MAX_CLIENT_STALL_TIME_USEC (10*1000*1000) 42 #define CATCH_UP_CHECK_TIME_USEC (500*1000) 43 #define MAX_RETRIES 16 44 #define RETRY_MS 500 45 #define MAX_INPUT_DEVICES 16 46 47 /* We allow only 32 clients to fit into a 32-bit bitmap */ 48 #define MAX_CLIENTS 32 49 typedef uint32_t client_bitmap_t; 50 51 /* Because the callback keyboard reverses some assumptions, when the keyboard 52 * callbacks are in use, we assign a pseudodevice for it */ 53 #define RETRO_DEVICE_NETPLAY_KEYBOARD RETRO_DEVICE_SUBCLASS(RETRO_DEVICE_KEYBOARD, 65535) 54 55 #define NETPLAY_MAX_STALL_FRAMES 60 56 #define NETPLAY_FRAME_RUN_TIME_WINDOW 120 57 #define NETPLAY_MAX_REQ_STALL_TIME 60 58 #define NETPLAY_MAX_REQ_STALL_FREQUENCY 120 59 60 #define PREV_PTR(x) ((x) == 0 ? netplay->buffer_size - 1 : (x) - 1) 61 #define NEXT_PTR(x) ((x + 1) % netplay->buffer_size) 62 63 /* Quirks mandated by how particular cores save states. This is distilled from 64 * the larger set of quirks that the quirks environment can communicate. */ 65 #define NETPLAY_QUIRK_NO_SAVESTATES (1<<0) 66 #define NETPLAY_QUIRK_NO_TRANSMISSION (1<<1) 67 #define NETPLAY_QUIRK_INITIALIZATION (1<<2) 68 #define NETPLAY_QUIRK_ENDIAN_DEPENDENT (1<<3) 69 #define NETPLAY_QUIRK_PLATFORM_DEPENDENT (1<<4) 70 71 /* Mapping of serialization quirks to netplay quirks. */ 72 #define NETPLAY_QUIRK_MAP_UNDERSTOOD \ 73 (RETRO_SERIALIZATION_QUIRK_INCOMPLETE \ 74 |RETRO_SERIALIZATION_QUIRK_CORE_VARIABLE_SIZE \ 75 |RETRO_SERIALIZATION_QUIRK_MUST_INITIALIZE \ 76 |RETRO_SERIALIZATION_QUIRK_SINGLE_SESSION \ 77 |RETRO_SERIALIZATION_QUIRK_ENDIAN_DEPENDENT \ 78 |RETRO_SERIALIZATION_QUIRK_PLATFORM_DEPENDENT) 79 #define NETPLAY_QUIRK_MAP_NO_SAVESTATES \ 80 (RETRO_SERIALIZATION_QUIRK_INCOMPLETE) 81 #define NETPLAY_QUIRK_MAP_NO_TRANSMISSION \ 82 (RETRO_SERIALIZATION_QUIRK_SINGLE_SESSION) 83 #define NETPLAY_QUIRK_MAP_INITIALIZATION \ 84 (RETRO_SERIALIZATION_QUIRK_MUST_INITIALIZE) 85 #define NETPLAY_QUIRK_MAP_ENDIAN_DEPENDENT \ 86 (RETRO_SERIALIZATION_QUIRK_ENDIAN_DEPENDENT) 87 #define NETPLAY_QUIRK_MAP_PLATFORM_DEPENDENT \ 88 (RETRO_SERIALIZATION_QUIRK_PLATFORM_DEPENDENT) 89 90 /* Compression protocols supported */ 91 #define NETPLAY_COMPRESSION_ZLIB (1<<0) 92 #if HAVE_ZLIB 93 #define NETPLAY_COMPRESSION_SUPPORTED NETPLAY_COMPRESSION_ZLIB 94 #else 95 #define NETPLAY_COMPRESSION_SUPPORTED 0 96 #endif 97 98 enum netplay_cmd 99 { 100 /* Basic commands */ 101 102 /* Acknowlegement response */ 103 NETPLAY_CMD_ACK = 0x0000, 104 105 /* Failed acknowlegement response */ 106 NETPLAY_CMD_NAK = 0x0001, 107 108 /* Gracefully disconnects from host */ 109 NETPLAY_CMD_DISCONNECT = 0x0002, 110 111 /* Input data */ 112 NETPLAY_CMD_INPUT = 0x0003, 113 114 /* Non-input data */ 115 NETPLAY_CMD_NOINPUT = 0x0004, 116 117 /* Initialization commands */ 118 119 /* Inform the other side of our nick (must be first command) */ 120 NETPLAY_CMD_NICK = 0x0020, 121 122 /* Give the connection password */ 123 NETPLAY_CMD_PASSWORD = 0x0021, 124 125 /* Give core/content info */ 126 NETPLAY_CMD_INFO = 0x0022, 127 128 /* Initial synchronization info (frame, sram, player info) */ 129 NETPLAY_CMD_SYNC = 0x0023, 130 131 /* Join spectator mode */ 132 NETPLAY_CMD_SPECTATE = 0x0024, 133 134 /* Join play mode */ 135 NETPLAY_CMD_PLAY = 0x0025, 136 137 /* Report player mode */ 138 NETPLAY_CMD_MODE = 0x0026, 139 140 /* Report player mode refused */ 141 NETPLAY_CMD_MODE_REFUSED = 0x0027, 142 143 /* Loading and synchronization */ 144 145 /* Send the CRC hash of a frame's state */ 146 NETPLAY_CMD_CRC = 0x0040, 147 148 /* Request a savestate */ 149 NETPLAY_CMD_REQUEST_SAVESTATE = 0x0041, 150 151 /* Send a savestate for the client to load */ 152 NETPLAY_CMD_LOAD_SAVESTATE = 0x0042, 153 154 /* Pauses the game, takes no arguments */ 155 NETPLAY_CMD_PAUSE = 0x0043, 156 157 /* Resumes the game, takes no arguments */ 158 NETPLAY_CMD_RESUME = 0x0044, 159 160 /* Request that a client stall because it's running fast */ 161 NETPLAY_CMD_STALL = 0x0045, 162 163 /* Request a core reset */ 164 NETPLAY_CMD_RESET = 0x0046, 165 166 /* Sends over cheats enabled on client (unsupported) */ 167 NETPLAY_CMD_CHEATS = 0x0047, 168 169 /* Misc. commands */ 170 171 /* Sends multiple config requests over, 172 * See enum netplay_cmd_cfg */ 173 NETPLAY_CMD_CFG = 0x0061, 174 175 /* CMD_CFG streamlines sending multiple 176 configurations. This acknowledges 177 each one individually */ 178 NETPLAY_CMD_CFG_ACK = 0x0062 179 }; 180 181 #define NETPLAY_CMD_SYNC_BIT_PAUSED (1U<<31) 182 #define NETPLAY_CMD_PLAY_BIT_SLAVE (1U<<31) 183 #define NETPLAY_CMD_MODE_BIT_YOU (1U<<31) 184 #define NETPLAY_CMD_MODE_BIT_PLAYING (1U<<30) 185 #define NETPLAY_CMD_MODE_BIT_SLAVE (1U<<29) 186 187 /* These are the reasons given for mode changes to be rejected */ 188 enum netplay_cmd_mode_reasons 189 { 190 /* Other/unknown reason */ 191 NETPLAY_CMD_MODE_REFUSED_REASON_OTHER, 192 193 /* You don't have permission to play */ 194 NETPLAY_CMD_MODE_REFUSED_REASON_UNPRIVILEGED, 195 196 /* There are no free player slots */ 197 NETPLAY_CMD_MODE_REFUSED_REASON_NO_SLOTS, 198 199 /* You're changing modes too fast */ 200 NETPLAY_CMD_MODE_REFUSED_REASON_TOO_FAST, 201 202 /* You requested a particular port but it's not available */ 203 NETPLAY_CMD_MODE_REFUSED_REASON_NOT_AVAILABLE 204 }; 205 206 /* Real preferences for sharing devices */ 207 enum rarch_netplay_share_preference 208 { 209 /* Prefer not to share, shouldn't be set as a sharing mode for an shared device */ 210 NETPLAY_SHARE_NO_SHARING = 0x0, 211 212 /* No preference. Only for requests. Set if sharing is requested but either 213 * digital or analog doesn't have a preference. */ 214 NETPLAY_SHARE_NO_PREFERENCE = 0x1, 215 216 /* For digital devices */ 217 NETPLAY_SHARE_DIGITAL_BITS = 0x1C, 218 NETPLAY_SHARE_DIGITAL_OR = 0x4, 219 NETPLAY_SHARE_DIGITAL_XOR = 0x8, 220 NETPLAY_SHARE_DIGITAL_VOTE = 0xC, 221 222 /* For analog devices */ 223 NETPLAY_SHARE_ANALOG_BITS = 0xE0, 224 NETPLAY_SHARE_ANALOG_MAX = 0x20, 225 NETPLAY_SHARE_ANALOG_AVERAGE = 0x40 226 }; 227 228 /* The current status of a connection */ 229 enum rarch_netplay_connection_mode 230 { 231 NETPLAY_CONNECTION_NONE = 0, 232 233 NETPLAY_CONNECTION_DELAYED_DISCONNECT, /* The connection is dead, but data 234 is still waiting to be forwarded */ 235 236 /* Initialization: */ 237 NETPLAY_CONNECTION_INIT, /* Waiting for header */ 238 NETPLAY_CONNECTION_PRE_NICK, /* Waiting for nick */ 239 NETPLAY_CONNECTION_PRE_PASSWORD, /* Waiting for password */ 240 NETPLAY_CONNECTION_PRE_INFO, /* Waiting for core/content info */ 241 NETPLAY_CONNECTION_PRE_SYNC, /* Waiting for sync */ 242 243 /* Ready: */ 244 NETPLAY_CONNECTION_CONNECTED, /* Modes above this are connected */ 245 NETPLAY_CONNECTION_SPECTATING, /* Spectator mode */ 246 NETPLAY_CONNECTION_SLAVE, /* Playing in slave mode */ 247 NETPLAY_CONNECTION_PLAYING /* Normal ready state */ 248 }; 249 250 enum rarch_netplay_stall_reason 251 { 252 NETPLAY_STALL_NONE = 0, 253 254 /* We're so far ahead that we can't read more data without overflowing the 255 * buffer */ 256 NETPLAY_STALL_RUNNING_FAST, 257 258 /* We're in spectator or slave mode and are running ahead at all */ 259 NETPLAY_STALL_SPECTATOR_WAIT, 260 261 /* Our actual execution is catching up with latency-adjusted input frames */ 262 NETPLAY_STALL_INPUT_LATENCY, 263 264 /* The server asked us to stall */ 265 NETPLAY_STALL_SERVER_REQUESTED, 266 267 /* We have no connection and must have one to proceed */ 268 NETPLAY_STALL_NO_CONNECTION 269 }; 270 271 /* Input state for a particular client-device pair */ 272 typedef struct netplay_input_state 273 { 274 /* The next input state (forming a list) */ 275 struct netplay_input_state *next; 276 277 /* Whose data is this? */ 278 uint32_t client_num; 279 280 /* How many words of input data do we have? */ 281 uint32_t size; 282 283 /* Is this a buffer with real data? */ 284 bool used; 285 286 /* The input data itself (note: should expand beyond 1 by overallocating). */ 287 uint32_t data[1]; 288 289 /* Warning: No members allowed past this point, due to dynamic resizing. */ 290 } *netplay_input_state_t; 291 292 struct delta_frame 293 { 294 /* The resolved input, i.e., what's actually going to the core. One input 295 * per device. */ 296 netplay_input_state_t resolved_input[MAX_INPUT_DEVICES]; /* ptr alignment */ 297 298 /* The real input */ 299 netplay_input_state_t real_input[MAX_INPUT_DEVICES]; /* ptr alignment */ 300 301 /* The serialized state of the core at this frame, before input */ 302 void *state; 303 304 uint32_t frame; 305 306 /* The CRC-32 of the serialized state if we've calculated it, else 0 */ 307 uint32_t crc; 308 309 /* The simulated input. is_real here means the simulation is done, i.e., 310 * it's a real simulation, not real input. */ 311 netplay_input_state_t simlated_input[MAX_INPUT_DEVICES]; 312 313 /* Have we read local input? */ 314 bool have_local; 315 316 /* Have we read the real (remote) input? */ 317 bool have_real[MAX_CLIENTS]; 318 319 bool used; /* a bit derpy, but this is how we know if the delta's been used at all */ 320 }; 321 322 struct socket_buffer 323 { 324 unsigned char *data; 325 size_t bufsz; 326 size_t start; 327 size_t end; 328 size_t read; 329 }; 330 331 /* Each connection gets a connection struct */ 332 struct netplay_connection 333 { 334 /* Is this connection stalling? */ 335 retro_time_t stall_time; 336 337 /* Address of peer */ 338 struct sockaddr_storage addr; 339 340 /* Buffers for sending and receiving data */ 341 struct socket_buffer send_packet_buffer, recv_packet_buffer; 342 343 /* fd associated with this connection */ 344 int fd; 345 346 /* If the mode is a DELAYED_DISCONNECT or SPECTATOR, the transmission of the 347 * mode change may have to wait for data to be forwarded. This is the frame 348 * to wait for, or 0 if no delay is active. */ 349 uint32_t delay_frame; 350 351 /* What compression does this peer support? */ 352 uint32_t compression_supported; 353 354 /* For the server: When was the last time we requested this client to stall? 355 * For the client: How many frames of stall do we have left? */ 356 uint32_t stall_frame; 357 358 /* Salt associated with password transaction */ 359 uint32_t salt; 360 361 /* Is this connection stalling? */ 362 enum rarch_netplay_stall_reason stall; 363 364 /* Mode of the connection */ 365 enum rarch_netplay_connection_mode mode; 366 367 /* Nickname of peer */ 368 char nick[NETPLAY_NICK_LEN]; 369 370 /* Is this player paused? */ 371 bool paused; 372 373 /* Is this connection allowed to play (server only)? */ 374 bool can_play; 375 376 /* Is this connection buffer in use? */ 377 bool active; 378 }; 379 380 /* Compression transcoder */ 381 struct compression_transcoder 382 { 383 const struct trans_stream_backend *compression_backend; 384 void *compression_stream; 385 const struct trans_stream_backend *decompression_backend; 386 void *decompression_stream; 387 }; 388 389 struct netplay 390 { 391 /* When did we start falling behind? */ 392 retro_time_t catch_up_time; 393 /* How long have we been stalled? */ 394 retro_time_t stall_time; 395 396 /* We stall if we're far enough ahead that we couldn't transparently rewind. 397 * To know if we could transparently rewind, we need to know how long 398 * running a frame takes. We record that every frame and get a running 399 * (window) average */ 400 retro_time_t frame_run_time[NETPLAY_FRAME_RUN_TIME_WINDOW]; 401 retro_time_t frame_run_time_sum, frame_run_time_avg; 402 403 struct netplay_connection one_connection; /* Client only */ /* retro_time_t alignment */ 404 405 /* TCP connection for listening (server only) */ 406 int listen_fd; 407 408 /* Our client number */ 409 uint32_t self_client_num; 410 411 /* All of our connections */ 412 struct netplay_connection *connections; 413 size_t connections_size; 414 415 /* Bitmap of clients with input devices */ 416 uint32_t connected_players; 417 418 /* Bitmap of clients playing in slave mode (should be a subset of 419 * connected_players) */ 420 uint32_t connected_slaves; 421 422 /* For each client, the bitmap of devices they're connected to */ 423 uint32_t client_devices[MAX_CLIENTS]; 424 425 /* For each device, the bitmap of clients connected */ 426 client_bitmap_t device_clients[MAX_INPUT_DEVICES]; 427 428 /* Our own device bitmap */ 429 uint32_t self_devices; 430 431 /* Number of desync operations we're currently performing. If set, we don't 432 * attempt to stay in sync. */ 433 uint32_t desync; 434 435 /* The device types for every connected device. We store them and ignore any 436 * menu changes, as netplay needs fixed devices. */ 437 uint32_t config_devices[MAX_INPUT_DEVICES]; 438 439 struct retro_callbacks cbs; 440 441 /* NAT traversal info (if NAT traversal is used and serving) */ 442 struct natt_status nat_traversal_state; 443 444 struct delta_frame *buffer; 445 size_t buffer_size; 446 447 /* Compression transcoder */ 448 struct compression_transcoder compress_nil, 449 compress_zlib; 450 451 /* Size of savestates */ 452 size_t state_size; 453 454 /* A buffer into which to compress frames for transfer */ 455 uint8_t *zbuffer; 456 size_t zbuffer_size; 457 458 /* The size of our packet buffers */ 459 size_t packet_buffer_size; 460 461 /* The frame we're currently inputting */ 462 size_t self_ptr; 463 uint32_t self_frame_count; 464 465 /* The frame we're currently running, which may be behind the frame we're 466 * currently inputting if we're using input latency */ 467 size_t run_ptr; 468 uint32_t run_frame_count; 469 470 /* The first frame at which some data might be unreliable */ 471 size_t other_ptr; 472 uint32_t other_frame_count; 473 474 /* Pointer to the first frame for which we're missing the data of at least 475 * one connected player excluding ourself. 476 * Generally, other_ptr <= unread_ptr <= self_ptr, but unread_ptr can get ahead 477 * of self_ptr if the peer is running fast. */ 478 size_t unread_ptr; 479 uint32_t unread_frame_count; 480 481 /* Pointer to the next frame to read from each client */ 482 size_t read_ptr[MAX_CLIENTS]; 483 uint32_t read_frame_count[MAX_CLIENTS]; 484 485 /* Pointer to the next frame to read from the server (as it might not be a 486 * player but still synchronizes) */ 487 size_t server_ptr; 488 uint32_t server_frame_count; 489 490 /* A pointer used temporarily for replay. */ 491 size_t replay_ptr; 492 uint32_t replay_frame_count; 493 494 /* Our local socket info */ 495 struct addrinfo *addr; 496 497 /* Counter for timeouts */ 498 unsigned timeout_cnt; 499 500 int frame_run_time_ptr; 501 502 /* Latency frames; positive to hide network latency, negative to hide input latency */ 503 int input_latency_frames; 504 505 /* Frequency with which to check CRCs */ 506 int check_frames; 507 508 /* How far behind did we fall? */ 509 uint32_t catch_up_behind; 510 511 /* Are we stalled? */ 512 enum rarch_netplay_stall_reason stall; 513 514 /* Our mode and status */ 515 enum rarch_netplay_connection_mode self_mode; 516 517 /* TCP port (only set if serving) */ 518 uint16_t tcp_port; 519 520 /* The sharing mode for each device */ 521 uint8_t device_share_modes[MAX_INPUT_DEVICES]; 522 523 /* Our nickname */ 524 char nick[NETPLAY_NICK_LEN]; 525 526 bool nat_traversal, nat_traversal_task_oustanding; 527 528 /* Set to true if we have a device that most cores translate to "up/down" 529 * actions, typically a keyboard. We need to keep track of this because with 530 * such a device, we need to "fix" the input state to the frame BEFORE a 531 * state load, then perform the state load, and the up/down states will 532 * proceed as expected */ 533 bool have_updown_device; 534 535 /* Are we replaying old frames? */ 536 bool is_replay; 537 538 /* We don't want to poll several times on a frame. */ 539 bool can_poll; 540 541 /* Force a rewind to other_frame_count/other_ptr. This is for synchronized 542 * events, such as restarting or savestate loading. */ 543 bool force_rewind; 544 545 /* Force a reset */ 546 bool force_reset; 547 548 /* Quirks in the savestate implementation */ 549 uint64_t quirks; 550 551 /* Force our state to be sent to all connections */ 552 bool force_send_savestate; 553 554 /* Have we requested a savestate as a sync point? */ 555 bool savestate_request_outstanding; 556 557 558 /* Netplay pausing */ 559 bool local_paused; 560 bool remote_paused; 561 562 /* If true, never progress without peer input (stateless/rewindless mode) */ 563 bool stateless_mode; 564 565 566 /* Opposite of stalling, should we be catching up? */ 567 bool catch_up; 568 569 /* Have we checked whether CRCs are valid at all? */ 570 bool crc_validity_checked; 571 572 /* Are they valid? */ 573 bool crcs_valid; 574 575 /* Are we the server? */ 576 bool is_server; 577 578 /* Are we the connected? */ 579 bool is_connected; 580 581 }; 582 583 /*************************************************************** 584 * NETPLAY-BUF.C 585 **************************************************************/ 586 587 /** 588 * netplay_send 589 * 590 * Queue the given data for sending. 591 */ 592 bool netplay_send(struct socket_buffer *sbuf, int sockfd, const void *buf, 593 size_t len); 594 595 /** 596 * netplay_send_flush 597 * 598 * Flush unsent data in the given socket buffer, blocking to do so if 599 * requested. 600 * 601 * Returns false only on socket failures, true otherwise. 602 */ 603 bool netplay_send_flush(struct socket_buffer *sbuf, int sockfd, bool block); 604 605 /** 606 * netplay_recv 607 * 608 * Receive buffered or fresh data. 609 * 610 * Returns number of bytes returned, which may be short or 0, or -1 on error. 611 */ 612 ssize_t netplay_recv(struct socket_buffer *sbuf, int sockfd, void *buf, 613 size_t len, bool block); 614 615 /** 616 * netplay_recv_reset 617 * 618 * Reset our recv buffer so that future netplay_recvs will read the same data 619 * again. 620 */ 621 void netplay_recv_reset(struct socket_buffer *sbuf); 622 623 /** 624 * netplay_recv_flush 625 * 626 * Flush our recv buffer, so a future netplay_recv_reset will reset to this 627 * point. 628 */ 629 void netplay_recv_flush(struct socket_buffer *sbuf); 630 631 /*************************************************************** 632 * NETPLAY-DELTA.C 633 **************************************************************/ 634 635 /** 636 * netplay_delta_frame_ready 637 * 638 * Prepares, if possible, a delta frame for input, and reports whether it is 639 * ready. 640 * 641 * Returns: True if the delta frame is ready for input at the given frame, 642 * false otherwise. 643 */ 644 bool netplay_delta_frame_ready(netplay_t *netplay, struct delta_frame *delta, 645 uint32_t frame); 646 647 /** 648 * netplay_input_state_for 649 * 650 * Get an input state for a particular client 651 */ 652 netplay_input_state_t netplay_input_state_for(netplay_input_state_t *list, 653 uint32_t client_num, size_t size, bool must_create, bool must_not_create); 654 655 /** 656 * netplay_expected_input_size 657 * 658 * Size in words for a given set of devices. 659 */ 660 uint32_t netplay_expected_input_size(netplay_t *netplay, uint32_t devices); 661 662 /*************************************************************** 663 * NETPLAY-DISCOVERY.C 664 **************************************************************/ 665 666 /** 667 * netplay_lan_ad_server 668 * 669 * Respond to any LAN ad queries that the netplay server has received. 670 */ 671 bool netplay_lan_ad_server(netplay_t *netplay); 672 673 /*************************************************************** 674 * NETPLAY-FRONTEND.C 675 **************************************************************/ 676 677 /** 678 * netplay_load_savestate 679 * @netplay : pointer to netplay object 680 * @serial_info : the savestate being loaded, NULL means 681 * "load it yourself" 682 * @save : Whether to save the provided serial_info 683 * into the frame buffer 684 * 685 * Inform Netplay of a savestate load and send it to the other side 686 **/ 687 void netplay_load_savestate(netplay_t *netplay, 688 retro_ctx_serialize_info_t *serial_info, bool save); 689 690 /** 691 * input_poll_net 692 * 693 * Poll the network if necessary. 694 */ 695 void input_poll_net(void); 696 697 /*************************************************************** 698 * NETPLAY-HANDSHAKE.C 699 **************************************************************/ 700 701 /** 702 * netplay_handshake_init_send 703 * 704 * Initialize our handshake and send the first part of the handshake protocol. 705 */ 706 bool netplay_handshake_init_send(netplay_t *netplay, 707 struct netplay_connection *connection); 708 709 /** 710 * netplay_handshake 711 * 712 * Data receiver for all handshake states. 713 */ 714 bool netplay_handshake(netplay_t *netplay, 715 struct netplay_connection *connection, bool *had_input); 716 717 /*************************************************************** 718 * NETPLAY-INIT.C 719 **************************************************************/ 720 721 /** 722 * netplay_try_init_serialization 723 * 724 * Try to initialize serialization. For quirky cores. 725 * 726 * Returns true if serialization is now ready, false otherwise. 727 */ 728 bool netplay_try_init_serialization(netplay_t *netplay); 729 730 /** 731 * netplay_wait_and_init_serialization 732 * 733 * Try very hard to initialize serialization, simulating multiple frames if 734 * necessary. For quirky cores. 735 * 736 * Returns true if serialization is now ready, false otherwise. 737 */ 738 bool netplay_wait_and_init_serialization(netplay_t *netplay); 739 740 /** 741 * netplay_new: 742 * @direct_host : Netplay host discovered from scanning. 743 * @server : IP address of server. 744 * @port : Port of server. 745 * @stateless_mode : Shall we run in stateless mode? 746 * @check_frames : Frequency with which to check CRCs. 747 * @cb : Libretro callbacks. 748 * @nat_traversal : If true, attempt NAT traversal. 749 * @nick : Nickname of user. 750 * @quirks : Netplay quirks required for this session. 751 * 752 * Creates a new netplay handle. A NULL server means we're 753 * hosting. 754 * 755 * Returns: new netplay data. 756 */ 757 netplay_t *netplay_new(void *direct_host, const char *server, uint16_t port, 758 bool stateless_mode, int check_frames, 759 const struct retro_callbacks *cb, bool nat_traversal, const char *nick, 760 uint64_t quirks); 761 762 /** 763 * netplay_free 764 * @netplay : pointer to netplay object 765 * 766 * Frees netplay data/ 767 */ 768 void netplay_free(netplay_t *netplay); 769 770 /*************************************************************** 771 * NETPLAY-IO.C 772 **************************************************************/ 773 774 /** 775 * netplay_hangup: 776 * 777 * Disconnects an active Netplay connection due to an error 778 */ 779 void netplay_hangup(netplay_t *netplay, struct netplay_connection *connection); 780 781 /** 782 * netplay_delayed_state_change: 783 * 784 * Handle any pending state changes which are ready as of the beginning of the 785 * current frame. 786 */ 787 void netplay_delayed_state_change(netplay_t *netplay); 788 789 /** 790 * netplay_send_cur_input 791 * 792 * Send the current input frame to a given connection. 793 * 794 * Returns true if successful, false otherwise. 795 */ 796 bool netplay_send_cur_input(netplay_t *netplay, 797 struct netplay_connection *connection); 798 799 /** 800 * netplay_send_raw_cmd 801 * 802 * Send a raw Netplay command to the given connection. 803 * 804 * Returns true on success, false on failure. 805 */ 806 bool netplay_send_raw_cmd(netplay_t *netplay, 807 struct netplay_connection *connection, uint32_t cmd, const void *data, 808 size_t size); 809 810 /** 811 * netplay_send_raw_cmd_all 812 * 813 * Send a raw Netplay command to all connections, optionally excluding one 814 * (typically the client that the relevant command came from) 815 */ 816 void netplay_send_raw_cmd_all(netplay_t *netplay, 817 struct netplay_connection *except, uint32_t cmd, const void *data, 818 size_t size); 819 820 /** 821 * netplay_cmd_mode 822 * 823 * Send a mode change request. As a server, the request is to ourself, and so 824 * honored instantly. 825 */ 826 bool netplay_cmd_mode(netplay_t *netplay, 827 enum rarch_netplay_connection_mode mode); 828 829 /** 830 * netplay_poll_net_input 831 * 832 * Poll input from the network 833 */ 834 int netplay_poll_net_input(netplay_t *netplay, bool block); 835 836 /** 837 * netplay_handle_slaves 838 * 839 * Handle any slave connections 840 */ 841 void netplay_handle_slaves(netplay_t *netplay); 842 843 /** 844 * netplay_announce_nat_traversal 845 * 846 * Announce successful NAT traversal. 847 */ 848 void netplay_announce_nat_traversal(netplay_t *netplay); 849 850 /** 851 * netplay_init_nat_traversal 852 * 853 * Initialize the NAT traversal library and try to open a port 854 */ 855 void netplay_init_nat_traversal(netplay_t *netplay); 856 857 /*************************************************************** 858 * NETPLAY-KEYBOARD.C 859 **************************************************************/ 860 861 /* The keys supported by netplay */ 862 enum netplay_keys { 863 NETPLAY_KEY_UNKNOWN = 0, 864 #define K(k) NETPLAY_KEY_ ## k, 865 #define KL(k,l) K(k) 866 #include "netplay_keys.h" 867 #undef KL 868 #undef K 869 NETPLAY_KEY_LAST 870 }; 871 872 /* The mapping of keys from netplay (network) to libretro (host) */ 873 extern const uint16_t netplay_key_ntoh_mapping[]; 874 #define NETPLAY_KEY_NTOH(k) (netplay_key_ntoh_mapping[k]) 875 876 /* The mapping of keys from libretro (host) to netplay (network) */ 877 uint32_t netplay_key_hton(unsigned key); 878 879 /* Because the hton keymapping has to be generated, call this before using 880 * netplay_key_hton */ 881 void netplay_key_hton_init(void); 882 883 /*************************************************************** 884 * NETPLAY-SYNC.C 885 **************************************************************/ 886 887 /** 888 * netplay_update_unread_ptr 889 * 890 * Update the global unread_ptr and unread_frame_count to correspond to the 891 * earliest unread frame count of any connected player 892 */ 893 void netplay_update_unread_ptr(netplay_t *netplay); 894 895 /** 896 * netplay_resolve_input 897 * @netplay : pointer to netplay object 898 * @sim_ptr : frame pointer for which to resolve input 899 * @resim : are we resimulating, or simulating this frame for the 900 * first time? 901 * 902 * "Simulate" input by assuming it hasn't changed since the last read input. 903 * Returns true if the resolved input changed from the last time it was 904 * resolved. 905 */ 906 bool netplay_resolve_input(netplay_t *netplay, size_t sim_ptr, bool resim); 907 908 /** 909 * netplay_sync_pre_frame 910 * @netplay : pointer to netplay object 911 * 912 * Pre-frame for Netplay synchronization. 913 */ 914 bool netplay_sync_pre_frame(netplay_t *netplay); 915 916 /** 917 * netplay_sync_post_frame 918 * @netplay : pointer to netplay object 919 * @stalled : true if we're currently stalled 920 * 921 * Post-frame for Netplay synchronization. 922 * We check if we have new input and replay from recorded input. 923 */ 924 void netplay_sync_post_frame(netplay_t *netplay, bool stalled); 925 926 #endif 927