diff --git a/network/netplay/netplay_frontend.c b/network/netplay/netplay_frontend.c index 186f20d67e..a4668a8afe 100644 --- a/network/netplay/netplay_frontend.c +++ b/network/netplay/netplay_frontend.c @@ -71,10 +71,6 @@ static bool netplay_disconnect(netplay_t *netplay); extern bool discord_is_inited; #endif -#define NETPLAY_ENABLE_CLIENT() \ - netplay_enabled = true; \ - netplay_is_client = true - /** * netplay_is_alive: * @netplay : pointer to netplay object @@ -143,14 +139,13 @@ static bool get_self_input_state(netplay_t *netplay, struct delta_frame *ptr) continue; /* Find an appropriate local device */ - dev_type = netplay->config_devices[devi] & RETRO_DEVICE_MASK; + dev_type = netplay->config_devices[devi]&RETRO_DEVICE_MASK; for (local_device = 0; local_device < MAX_INPUT_DEVICES; local_device++) { if (used_devices & (1 << local_device)) continue; - if ((netplay->config_devices[local_device] & RETRO_DEVICE_MASK) - == dev_type) + if ((netplay->config_devices[local_device]&RETRO_DEVICE_MASK) == dev_type) break; } @@ -175,8 +170,7 @@ static bool get_self_input_state(netplay_t *netplay, struct delta_frame *ptr) { uint32_t *state = istate->data; retro_input_state_t cb = netplay->cbs.state_cb; - unsigned dtype = netplay->config_devices[devi] & - RETRO_DEVICE_MASK; + unsigned dtype = netplay->config_devices[devi]&RETRO_DEVICE_MASK; switch (dtype) { @@ -225,9 +219,8 @@ static bool get_self_input_state(netplay_t *netplay, struct delta_frame *ptr) for (key = 1; key < NETPLAY_KEY_LAST; key++) { state[word] |= - cb(local_device, RETRO_DEVICE_KEYBOARD, - 0, netplay_key_ntoh(key)) ? - (1U << bit) : 0; + cb(local_device, RETRO_DEVICE_KEYBOARD, 0, netplay_key_ntoh(key)) ? + (1U << bit) : 0; bit++; if (bit >= 32) { @@ -246,19 +239,16 @@ static bool get_self_input_state(netplay_t *netplay, struct delta_frame *ptr) ptr->have_local = true; if (netplay->self_mode == NETPLAY_CONNECTION_PLAYING) { - ptr->have_real[netplay->self_client_num] = true; - netplay->read_ptr[netplay->self_client_num] = - NEXT_PTR(netplay->self_ptr); - netplay->read_frame_count[netplay->self_client_num] = - netplay->self_frame_count + 1; + ptr->have_real[netplay->self_client_num] = true; + netplay->read_ptr[netplay->self_client_num] = NEXT_PTR(netplay->self_ptr); + netplay->read_frame_count[netplay->self_client_num] = netplay->self_frame_count + 1; } /* And send this input to our peers */ for (i = 0; i < netplay->connections_size; i++) { struct netplay_connection *connection = &netplay->connections[i]; - if ( connection->active && - connection->mode >= NETPLAY_CONNECTION_CONNECTED) + if (connection->active && connection->mode >= NETPLAY_CONNECTION_CONNECTED) netplay_send_cur_input(netplay, &netplay->connections[i]); } @@ -273,8 +263,7 @@ bool init_netplay_deferred(const char* server, unsigned port) { if (!string_is_empty(server) && port != 0) { - strlcpy(server_address_deferred, server, - sizeof(server_address_deferred)); + strlcpy(server_address_deferred, server, sizeof(server_address_deferred)); server_port_deferred = port; netplay_client_deferred = true; } @@ -390,23 +379,23 @@ static bool netplay_poll(netplay_t *netplay) switch (netplay->stall) { case NETPLAY_STALL_RUNNING_FAST: - if (netplay->unread_frame_count + NETPLAY_MAX_STALL_FRAMES - 2 - > netplay->self_frame_count) { - netplay->stall = NETPLAY_STALL_NONE; - for (i = 0; i < netplay->connections_size; i++) + if (netplay->unread_frame_count + NETPLAY_MAX_STALL_FRAMES - 2 + > netplay->self_frame_count) { - struct netplay_connection *connection = - &netplay->connections[i]; - if (connection->active && connection->stall) - connection->stall = NETPLAY_STALL_NONE; + netplay->stall = NETPLAY_STALL_NONE; + for (i = 0; i < netplay->connections_size; i++) + { + struct netplay_connection *connection = &netplay->connections[i]; + if (connection->active && connection->stall) + connection->stall = NETPLAY_STALL_NONE; + } } + break; } - break; case NETPLAY_STALL_SPECTATOR_WAIT: - if (netplay->self_mode == NETPLAY_CONNECTION_PLAYING - || netplay->unread_frame_count > netplay->self_frame_count) + if (netplay->self_mode == NETPLAY_CONNECTION_PLAYING || netplay->unread_frame_count > netplay->self_frame_count) netplay->stall = NETPLAY_STALL_NONE; break; @@ -421,7 +410,7 @@ static bool netplay_poll(netplay_t *netplay) { /* Stop stalling! */ netplay->connections[0].stall = NETPLAY_STALL_NONE; - netplay->stall = NETPLAY_STALL_NONE; + netplay->stall = NETPLAY_STALL_NONE; } else netplay->connections[0].stall_frame--; @@ -441,7 +430,7 @@ static bool netplay_poll(netplay_t *netplay) netplay->connected_players && netplay->run_frame_count + netplay->input_latency_frames > netplay->self_frame_count) { - netplay->stall = NETPLAY_STALL_INPUT_LATENCY; + netplay->stall = NETPLAY_STALL_INPUT_LATENCY; netplay->stall_time = 0; } @@ -460,15 +449,13 @@ static bool netplay_poll(netplay_t *netplay) struct netplay_connection *connection; if (!(netplay->connected_players & (1 << client))) continue; - if (netplay->read_frame_count[client] - > netplay->unread_frame_count) + if (netplay->read_frame_count[client] > netplay->unread_frame_count) continue; connection = &netplay->connections[client-1]; - if (connection->active && connection->mode == NETPLAY_CONNECTION_PLAYING) { - connection->stall = NETPLAY_STALL_RUNNING_FAST; + connection->stall = NETPLAY_STALL_RUNNING_FAST; connection->stall_time = netplay->stall_time; } } @@ -482,7 +469,7 @@ static bool netplay_poll(netplay_t *netplay) netplay->self_mode == NETPLAY_CONNECTION_SLAVE) && netplay->unread_frame_count <= netplay->self_frame_count) { - netplay->stall = NETPLAY_STALL_SPECTATOR_WAIT; + netplay->stall = NETPLAY_STALL_SPECTATOR_WAIT; netplay->stall_time = cpu_features_get_time_usec(); } } @@ -496,34 +483,34 @@ static bool netplay_poll(netplay_t *netplay) if (netplay->remote_paused) netplay->stall_time = now; else if (now - netplay->stall_time >= - (netplay->is_server - ? MAX_SERVER_STALL_TIME_USEC - : MAX_CLIENT_STALL_TIME_USEC) - ) + (netplay->is_server ? MAX_SERVER_STALL_TIME_USEC : + MAX_CLIENT_STALL_TIME_USEC)) { - bool fixed = false; /* Stalled out! */ - if (!netplay->is_server) - goto catastrophe; - - for (i = 0; i < netplay->connections_size; i++) + if (netplay->is_server) { - struct netplay_connection *connection = &netplay->connections[i]; - if (connection->active && - connection->mode == NETPLAY_CONNECTION_PLAYING && - connection->stall) + bool fixed = false; + for (i = 0; i < netplay->connections_size; i++) { - netplay_hangup(netplay, connection); - fixed = true; + struct netplay_connection *connection = &netplay->connections[i]; + if (connection->active && + connection->mode == NETPLAY_CONNECTION_PLAYING && + connection->stall) + { + netplay_hangup(netplay, connection); + fixed = true; + } + } + + if (fixed) + { + /* Not stalled now :) */ + netplay->stall = NETPLAY_STALL_NONE; + return true; } } - - if (fixed) - { - /* Not stalled now :) */ - netplay->stall = NETPLAY_STALL_NONE; - return true; - } + else + goto catastrophe; return false; } } @@ -689,8 +676,8 @@ static void netplay_announce_cb(retro_task_t *task, if (!string_is_empty(line)) { struct string_list *kv = string_split(line, "="); - const char *key = NULL; - const char *val = NULL; + const char *key = NULL; + const char *val = NULL; if (!kv) continue; @@ -770,14 +757,14 @@ static void netplay_announce_cb(retro_task_t *task, port_len = (unsigned)strlen(mitm_port); /* Enable Netplay client mode */ - if (netplay_data) + if (netplay_driver_ctl(RARCH_NETPLAY_CTL_IS_DATA_INITED, NULL)) { command_event(CMD_EVENT_NETPLAY_DEINIT, NULL); - is_mitm = true; + is_mitm = true; host_room->host_method = NETPLAY_HOST_METHOD_MITM; } - NETPLAY_ENABLE_CLIENT(); + netplay_driver_ctl(RARCH_NETPLAY_CTL_ENABLE_CLIENT, NULL); host_string = (char*)calloc(1, ip_len + port_len + 2); @@ -866,10 +853,9 @@ static void netplay_announce(void) net_http_urlencode(&username, discord_get_own_username()); else #endif - net_http_urlencode(&username, settings->paths.username); - - net_http_urlencode(&corename, system->library_name); - net_http_urlencode(&coreversion, system->library_version); + net_http_urlencode(&username, settings->paths.username); + net_http_urlencode(&corename, system->library_name); + net_http_urlencode(&coreversion, system->library_version); net_http_urlencode(&frontend_ident, frontend_architecture); buf[0] = '\0'; @@ -885,8 +871,7 @@ static void netplay_announce(void) *settings->paths.netplay_spectate_password ? 1 : 0, settings->bools.netplay_use_mitm_server, PACKAGE_VERSION, frontend_architecture, subsystemname); - task_push_http_post_transfer(url, buf, true, NULL, - netplay_announce_cb, NULL); + task_push_http_post_transfer(url, buf, true, NULL, netplay_announce_cb, NULL); if (username) free(username); @@ -910,6 +895,32 @@ int16_t input_state_net(unsigned port, unsigned device, } /* ^^^ Netplay polling callbacks */ +/** + * netplay_command: + * @netplay : pointer to netplay object + * @cmd : command to send + * @data : data to send as argument + * @sz : size of data + * @command_str : name of action + * @success_msg : message to display upon success + * + * Sends a single netplay command and waits for response. Only actually used + * for player flipping. FIXME: Should probably just be removed. + */ +bool netplay_command(netplay_t* netplay, struct netplay_connection *connection, + enum netplay_cmd cmd, void* data, size_t sz, const char* command_str, + const char* success_msg) +{ + retro_assert(netplay); + + if (!netplay_send_raw_cmd(netplay, connection, cmd, data, sz)) + return false; + + runloop_msg_queue_push(success_msg, 1, 180, false, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); + + return true; +} + /** * netplay_frontend_paused * @netplay : pointer to netplay object @@ -974,7 +985,7 @@ static void netplay_frontend_paused(netplay_t *netplay, bool paused) * Returns: true (1) if the frontend is cleared to emulate the frame, false (0) * if we're stalled or paused **/ -static bool netplay_pre_frame(netplay_t *netplay) +bool netplay_pre_frame(netplay_t *netplay) { bool sync_stalled = false; settings_t *settings = config_get_ptr(); @@ -1052,7 +1063,7 @@ static bool netplay_pre_frame(netplay_t *netplay) * We check if we have new input and replay from recorded input. * Call this after running retro_run(). **/ -static void netplay_post_frame(netplay_t *netplay) +void netplay_post_frame(netplay_t *netplay) { size_t i; retro_assert(netplay); @@ -1128,7 +1139,7 @@ static void netplay_force_future(netplay_t *netplay) * Send a loaded savestate to those connected peers using the given compression * scheme. */ -static void netplay_send_savestate(netplay_t *netplay, +void netplay_send_savestate(netplay_t *netplay, retro_ctx_serialize_info_t *serial_info, uint32_t cx, struct compression_transcoder *z) { @@ -1485,12 +1496,13 @@ bool netplay_driver_ctl(enum rarch_netplay_ctl_state state, void *data) switch (state) { case RARCH_NETPLAY_CTL_ENABLE_SERVER: - netplay_enabled = true; + netplay_enabled = true; netplay_is_client = false; goto done; case RARCH_NETPLAY_CTL_ENABLE_CLIENT: - NETPLAY_ENABLE_CLIENT(); + netplay_enabled = true; + netplay_is_client = true; break; case RARCH_NETPLAY_CTL_DISABLE: