From ac1695e7fd2e642775a6f9ef37d063f28951f098 Mon Sep 17 00:00:00 2001 From: radius Date: Thu, 26 Nov 2015 23:07:30 -0500 Subject: [PATCH] open different sockets per-user --- command_event.c | 10 ++++------ configuration.c | 11 ++++++----- configuration.h | 3 ++- input/input_common.c | 5 +++++ remote.c | 35 +++++++++++++++++++++++------------ remote.h | 2 +- 6 files changed, 41 insertions(+), 25 deletions(-) diff --git a/command_event.c b/command_event.c index 83a8014340..c1c51f8e4d 100644 --- a/command_event.c +++ b/command_event.c @@ -81,14 +81,12 @@ static void event_init_remote(void) driver_t *driver = driver_get_ptr(); settings_t *settings = config_get_ptr(); - for(int i=0; i < settings->input.max_users; i++) + if (settings->network_remote_enable) { - if (settings->network_remote_enable[i]) - { - if (!(driver->remote = rarch_remote_new(55400 + i,i))) - RARCH_ERR("Failed to initialize remote gamepad interface.\n"); - } + if (!(driver->remote = rarch_remote_new(settings->network_remote_base_port))) + RARCH_ERR("Failed to initialize remote gamepad interface.\n"); } + } #endif diff --git a/configuration.c b/configuration.c index e0c5f6602b..1fac351114 100644 --- a/configuration.c +++ b/configuration.c @@ -1627,12 +1627,12 @@ static bool config_load_file(const char *path, bool set_defaults) #endif #ifdef HAVE_NETWORK_GAMEPAD - + CONFIG_GET_BOOL_BASE(conf, settings, network_remote_enable, "network_remote_enable"); for (int i=0; i < MAX_USERS; i++) { char tmp[64] = {0}; - snprintf(tmp, sizeof(tmp), "network_remote_enable_p%u", i + 1); - config_get_bool(conf, tmp, &settings->network_remote_enable[i]); + snprintf(tmp, sizeof(tmp), "network_remote_enable_user_p%u", i + 1); + config_get_bool(conf, tmp, &settings->network_remote_enable_user[i]); } CONFIG_GET_INT_BASE(conf, settings, network_remote_base_port, "network_remote_base_port"); @@ -2784,9 +2784,10 @@ bool config_save_file(const char *path) for (int i=0; i < MAX_USERS; i++) { char tmp[64] = {0}; - snprintf(tmp, sizeof(tmp), "network_remote_enable_p%u", i + 1); - config_set_bool(conf, tmp, settings->network_remote_enable[i]); + snprintf(tmp, sizeof(tmp), "network_remote_enable_user_p%u", i + 1); + config_set_bool(conf, tmp, settings->network_remote_enable_user[i]); } + config_set_bool(conf, "network_remote_enable", settings->network_remote_enable); config_set_int(conf, "network_remote_base_port", settings->network_remote_base_port); #endif diff --git a/configuration.h b/configuration.h index a01b5be857..52171001da 100644 --- a/configuration.h +++ b/configuration.h @@ -347,7 +347,8 @@ typedef struct settings bool network_cmd_enable; unsigned network_cmd_port; bool stdin_cmd_enable; - bool network_remote_enable[MAX_USERS]; + bool network_remote_enable; + bool network_remote_enable_user[MAX_USERS]; unsigned network_remote_base_port; bool debug_panel_enable; diff --git a/input/input_common.c b/input/input_common.c index 66ea8698a3..a295e994c8 100644 --- a/input/input_common.c +++ b/input/input_common.c @@ -460,6 +460,11 @@ void input_poll(void) if (driver->command) rarch_cmd_poll(driver->command); #endif + +#ifdef HAVE_NETWORK_GAMEPAD + /*if (driver->remote) + rarch_remote_poll(driver->remote);*/ +#endif } /** diff --git a/remote.c b/remote.c index ff50d3cd6e..b56bb78063 100644 --- a/remote.c +++ b/remote.c @@ -43,7 +43,7 @@ struct rarch_remote { #if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY) - int net_fd; + int net_fd[MAX_USERS]; #endif bool state[RARCH_BIND_LIST_END]; @@ -57,6 +57,8 @@ static bool remote_init_network(rarch_remote_t *handle, uint16_t port, unsigned struct addrinfo *res = NULL; int yes = 1; + port = port + user; + if (!network_init()) return false; @@ -76,17 +78,17 @@ static bool remote_init_network(rarch_remote_t *handle, uint16_t port, unsigned if (getaddrinfo_retro(NULL, port_buf, &hints, &res) < 0) goto error; - handle->net_fd = socket(res->ai_family, + handle->net_fd[user] = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if (handle->net_fd < 0) + if (handle->net_fd[user] < 0) goto error; - if (!socket_nonblock(handle->net_fd)) + if (!socket_nonblock(handle->net_fd[user])) goto error; - setsockopt(handle->net_fd, SOL_SOCKET, + setsockopt(handle->net_fd[user], SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(int)); - if (bind(handle->net_fd, res->ai_addr, res->ai_addrlen) < 0) + if (bind(handle->net_fd[user], res->ai_addr, res->ai_addrlen) < 0) { RARCH_ERR("Failed to bind socket.\n"); goto error; @@ -102,18 +104,24 @@ error: } #endif -rarch_remote_t *rarch_remote_new(uint16_t port, unsigned user) +rarch_remote_t *rarch_remote_new(uint16_t port) { rarch_remote_t *handle = (rarch_remote_t*)calloc(1, sizeof(*handle)); + settings_t *settings = config_get_ptr(); if (!handle) return NULL; (void)port; #if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY) - handle->net_fd = -1; - if (!remote_init_network(handle, port, user)) - goto error; + for(int user = 0; user < MAX_USERS; user ++) + { + handle->net_fd[user] = -1; + if(settings->network_remote_enable_user[user]) + if (!remote_init_network(handle, port, user)) + goto error; + } + #endif return handle; @@ -128,8 +136,11 @@ error: void rarch_remote_free(rarch_remote_t *handle) { #if defined(HAVE_NETWORK_GAMEPAD) && defined(HAVE_NETPLAY) - if (handle && handle->net_fd >= 0) - socket_close(handle->net_fd); + for(int user = 0; user < MAX_USERS; user ++) + { + socket_close(handle->net_fd[user]); + } + #endif free(handle); diff --git a/remote.h b/remote.h index 55740587e2..daad5ea6c0 100644 --- a/remote.h +++ b/remote.h @@ -30,7 +30,7 @@ extern "C" { typedef struct rarch_remote rarch_remote_t; -rarch_remote_t *rarch_remote_new(uint16_t port, unsigned user); +rarch_remote_t *rarch_remote_new(uint16_t port); void rarch_remote_free(rarch_remote_t *handle);