From 0de43b954edd6eb06d256e5b537c74841359d638 Mon Sep 17 00:00:00 2001 From: radius Date: Fri, 20 Jan 2017 16:04:10 -0500 Subject: [PATCH] can now connect when starting a game too --- command.c | 15 +++++++++++++++ command.h | 2 ++ menu/cbs/menu_cbs_ok.c | 24 ++++++++++++++++-------- network/netplay/netplay.h | 2 ++ network/netplay/netplay_frontend.c | 24 +++++++++++++++++++++++- 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/command.c b/command.c index c5ec6d3e7a..c40d703ea5 100644 --- a/command.c +++ b/command.c @@ -2441,6 +2441,20 @@ bool command_event(enum event_command cmd, void *data) } } break; + case CMD_EVENT_NETPLAY_INIT_DIRECT_DEFERRED: + { + char *hostname = (char *) data; + + settings_t *settings = config_get_ptr(); + command_event(CMD_EVENT_NETPLAY_DEINIT, NULL); + if (!init_netplay_deferred( + hostname, settings->netplay.port)) + { + command_event(CMD_EVENT_NETPLAY_DEINIT, NULL); + return false; + } + } + break; case CMD_EVENT_NETPLAY_FLIP_PLAYERS: netplay_driver_ctl(RARCH_NETPLAY_CTL_FLIP_PLAYERS, NULL); break; @@ -2453,6 +2467,7 @@ bool command_event(enum event_command cmd, void *data) case CMD_EVENT_NETWORK_INIT: case CMD_EVENT_NETPLAY_INIT: case CMD_EVENT_NETPLAY_INIT_DIRECT: + case CMD_EVENT_NETPLAY_INIT_DIRECT_DEFERRED: case CMD_EVENT_NETPLAY_FLIP_PLAYERS: case CMD_EVENT_NETPLAY_GAME_WATCH: return false; diff --git a/command.h b/command.h index 19d8009c86..f15b108388 100644 --- a/command.h +++ b/command.h @@ -171,6 +171,8 @@ enum event_command CMD_EVENT_NETPLAY_INIT, /* Initializes netplay system with a direct host specified. */ CMD_EVENT_NETPLAY_INIT_DIRECT, + /* Initializes netplay system with a direct host specified after loading content. */ + CMD_EVENT_NETPLAY_INIT_DIRECT_DEFERRED, /* Deinitializes netplay system. */ CMD_EVENT_NETPLAY_DEINIT, /* Flip netplay players. */ diff --git a/menu/cbs/menu_cbs_ok.c b/menu/cbs/menu_cbs_ok.c index 10f126ddeb..cbfdbc6e6b 100644 --- a/menu/cbs/menu_cbs_ok.c +++ b/menu/cbs/menu_cbs_ok.c @@ -3125,21 +3125,29 @@ static int action_ok_netplay_connect_room(const char *path, command_event(CMD_EVENT_NETPLAY_DEINIT, NULL); netplay_driver_ctl(RARCH_NETPLAY_CTL_ENABLE_CLIENT, NULL); + char tmp_hostname[512]; + + /* For testing purposes + strlcpy(tmp_hostname, "192.168.1.241", sizeof(tmp_hostname));*/ + strlcpy(tmp_hostname, netplay_room_list[idx - 1].address, sizeof(tmp_hostname)); + /* If we haven't yet started, this will load on its own */ if (!content_is_inited()) { runloop_msg_queue_push( msg_hash_to_str(MENU_ENUM_LABEL_VALUE_NETPLAY_START_WHEN_LOADED), 1, 480, true); - return 0; + /* Enable Netplay itself */ + + if (!command_event(CMD_EVENT_NETPLAY_INIT_DIRECT_DEFERRED, (void *) tmp_hostname)) + return -1; + } + else + { + /* Enable Netplay itself */ + if (!command_event(CMD_EVENT_NETPLAY_INIT, (void *) tmp_hostname)) + return -1; } - - char tmp_hostname[512]; - strlcpy(tmp_hostname, netplay_room_list[idx - 1].address, sizeof(tmp_hostname)); - - /* Enable Netplay itself */ - if (!command_event(CMD_EVENT_NETPLAY_INIT, (void *) tmp_hostname)) - return -1; #else return -1; diff --git a/network/netplay/netplay.h b/network/netplay/netplay.h index c7e27def05..5d285cb4d9 100644 --- a/network/netplay/netplay.h +++ b/network/netplay/netplay.h @@ -57,6 +57,8 @@ void audio_sample_net(int16_t left, int16_t right); size_t audio_sample_batch_net(const int16_t *data, size_t frames); +bool init_netplay_deferred(const char* server, unsigned port); + /** * init_netplay * @direct_host : Host to connect to directly, if applicable (client only) diff --git a/network/netplay/netplay_frontend.c b/network/netplay/netplay_frontend.c index 7157f2375e..619d7f3ca4 100644 --- a/network/netplay/netplay_frontend.c +++ b/network/netplay/netplay_frontend.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "netplay_private.h" @@ -43,6 +44,11 @@ static netplay_t *netplay_data = NULL; /* Used to avoid recursive netplay calls */ static bool in_netplay = false; +/* Used for deferred netplay initialization */ +static bool netplay_client_deferred = false; +static char server_address_deferred[512] = ""; +static unsigned server_port_deferred = 0; + /** * netplay_is_alive: * @netplay : pointer to netplay object @@ -156,6 +162,22 @@ static bool get_self_input_state(netplay_t *netplay) return true; } +bool init_netplay_deferred(const char* server, unsigned port) +{ + + RARCH_LOG("deferred! %s\n", server); + if (!string_is_empty(server) && port != 0) + { + strlcpy(server_address_deferred, server, sizeof(server_address_deferred)); + server_port_deferred = port; + netplay_client_deferred = true; + } + else + netplay_client_deferred = false; + return netplay_client_deferred; +} + + /** * netplay_poll: * @netplay : pointer to netplay object @@ -924,7 +946,7 @@ bool init_netplay(void *direct_host, const char *server, unsigned port) netplay_data = (netplay_t*)netplay_new( netplay_is_client ? direct_host : NULL, - netplay_is_client ? server : NULL, + netplay_is_client ? (!netplay_client_deferred ? server : server_address_deferred) : NULL, port ? port : RARCH_DEFAULT_PORT, settings->netplay.stateless_mode, settings->netplay.check_frames, &cbs, settings->netplay.nat_traversal, settings->username,