From addff325d06028a1c3e81baceed97e65714720ee Mon Sep 17 00:00:00 2001 From: Gregor Richards Date: Fri, 2 Dec 2016 18:56:29 -0500 Subject: [PATCH] Netplay discovery LAN scanning task (incomplete) --- Makefile.common | 1 + menu/menu_driver.h | 1 + msg_hash.h | 3 + network/netplay/netplay.h | 1 + network/netplay/netplay_common.c | 1 + network/netplay/netplay_discovery.h | 56 +++++++++++++++++ network/netplay/netplay_private.h | 1 + tasks/task_netplay_lan_scan.c | 96 +++++++++++++++++++++++++++++ tasks/tasks_internal.h | 2 + 9 files changed, 162 insertions(+) create mode 100644 network/netplay/netplay_discovery.h create mode 100644 tasks/task_netplay_lan_scan.c diff --git a/Makefile.common b/Makefile.common index 9abfa0afc3..40fe16f938 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1100,6 +1100,7 @@ ifeq ($(HAVE_NETWORKING), 1) $(LIBRETRO_COMM_DIR)/net/net_natt.o \ network/net_http_special.o \ tasks/task_http.o \ + tasks/task_netplay_lan_scan.o \ tasks/task_wifi.o ifneq ($(HAVE_SOCKET_LEGACY),1) diff --git a/menu/menu_driver.h b/menu/menu_driver.h index 997ece0916..ffcc5935f5 100644 --- a/menu/menu_driver.h +++ b/menu/menu_driver.h @@ -183,6 +183,7 @@ enum menu_settings_type MENU_SETTING_SUBGROUP, MENU_SETTING_HORIZONTAL_MENU, MENU_WIFI, + MENU_NETPLAY_LAN_SCAN, MENU_INFO_MESSAGE, MENU_SETTINGS_SHADER_PARAMETER_0, MENU_SETTINGS_SHADER_PARAMETER_LAST = MENU_SETTINGS_SHADER_PARAMETER_0 + (GFX_MAX_PARAMETERS - 1), diff --git a/msg_hash.h b/msg_hash.h index d4921acefc..13989391bc 100644 --- a/msg_hash.h +++ b/msg_hash.h @@ -141,6 +141,7 @@ enum msg_hash_enums MSG_NETPLAY_USERS_HAS_FLIPPED, MSG_UNKNOWN_NETPLAY_COMMAND_RECEIVED, MSG_CONNECTING_TO_NETPLAY_HOST, + MSG_NETPLAY_LAN_SCANNING, MSG_WAITING_FOR_CLIENT, MSG_AUTODETECT, MSG_AUDIO_VOLUME, @@ -809,6 +810,7 @@ enum msg_hash_enums MENU_ENUM_LABEL_DEFERRED_UPDATER_SETTINGS_LIST, MENU_ENUM_LABEL_DEFERRED_WIFI_SETTINGS_LIST, MENU_ENUM_LABEL_DEFERRED_NETWORK_SETTINGS_LIST, + MENU_ENUM_LABEL_DEFERRED_NETPLAY_LAN_SCAN_SETTINGS_LIST, MENU_ENUM_LABEL_DEFERRED_LAKKA_SERVICES_LIST, MENU_ENUM_LABEL_DEFERRED_USER_SETTINGS_LIST, MENU_ENUM_LABEL_DEFERRED_DIRECTORY_SETTINGS_LIST, @@ -875,6 +877,7 @@ enum msg_hash_enums MENU_LABEL(NETPLAY_ENABLE_CLIENT), MENU_LABEL(NETPLAY_DISCONNECT), MENU_LABEL(NETPLAY_SETTINGS), + MENU_LABEL(NETPLAY_CONNECT_TO), MENU_ENUM_LABEL_RESET, MENU_ENUM_LABEL_SLOWMOTION, diff --git a/network/netplay/netplay.h b/network/netplay/netplay.h index 41bc60f743..5793efae1f 100644 --- a/network/netplay/netplay.h +++ b/network/netplay/netplay.h @@ -1,6 +1,7 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2011-2016 - Daniel De Matteis + * Copyright (C) 2016 - 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- diff --git a/network/netplay/netplay_common.c b/network/netplay/netplay_common.c index f3edd0ab14..67b07a70d8 100644 --- a/network/netplay/netplay_common.c +++ b/network/netplay/netplay_common.c @@ -471,6 +471,7 @@ bool netplay_ad_server(netplay_t *netplay, int ad_fd) break; /* Somebody queried, so check that it's valid */ + addr_size = sizeof(their_addr); if (recvfrom(ad_fd, (char*)ad_packet_buffer, AD_PACKET_MAX_SIZE, 0, &their_addr, &addr_size) >= (ssize_t) (2*sizeof(uint32_t))) { diff --git a/network/netplay/netplay_discovery.h b/network/netplay/netplay_discovery.h new file mode 100644 index 0000000000..f0b7358c78 --- /dev/null +++ b/network/netplay/netplay_discovery.h @@ -0,0 +1,56 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2016 - 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 . + */ + + +#ifndef __RARCH_NETPLAY_DISCOVERY_H +#define __RARCH_NETPLAY_DISCOVERY_H + +#include + +#define NETPLAY_HOST_STR_LEN 32 + +enum rarch_netplay_discovery_ctl_state +{ + RARCH_NETPLAY_DISCOVERY_CTL_NONE = 0, + RARCH_NETPLAY_DISCOVERY_CTL_LAN_SEND_QUERY, + RARCH_NETPLAY_DISCOVERY_CTL_LAN_GET_RESPONSES, + RARCH_NETPLAY_DISCOVERY_CTL_LAN_CLEAR_RESPONSES +}; + +struct netplay_host { + struct sockaddr addr; + socklen_t addrlen; + + char nick[NETPLAY_HOST_STR_LEN]; + char core[NETPLAY_HOST_STR_LEN]; + char core_version[NETPLAY_HOST_STR_LEN]; + char content[NETPLAY_HOST_STR_LEN]; +}; + +struct netplay_host_list { + struct netplay_host *hosts; + size_t size; +}; + +/** Initialize Netplay discovery */ +bool init_netplay_discovery(void); + +/** Deinitialize and free Netplay discovery */ +void deinit_netplay_discovery(void); + +/** Discovery control */ +bool netplay_discovery_driver_ctl(enum rarch_netplay_discovery_ctl_state state, void *data); + +#endif diff --git a/network/netplay/netplay_private.h b/network/netplay/netplay_private.h index 3ac24227c4..b24c8cccdf 100644 --- a/network/netplay/netplay_private.h +++ b/network/netplay/netplay_private.h @@ -1,6 +1,7 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2010-2014 - Hans-Kristian Arntzen * Copyright (C) 2011-2016 - Daniel De Matteis + * Copyright (C) 2016 - 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- diff --git a/tasks/task_netplay_lan_scan.c b/tasks/task_netplay_lan_scan.c new file mode 100644 index 0000000000..cd2462d58f --- /dev/null +++ b/tasks/task_netplay_lan_scan.c @@ -0,0 +1,96 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2016 - Jean-André Santoni + * + * 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 . + */ + +#include + +#include "tasks_internal.h" +#include "../verbosity.h" +#include "../network/netplay/netplay_discovery.h" +#include "../menu/menu_entries.h" +#include "../menu/menu_driver.h" + +static void netplay_lan_scan_callback(void *task_data, + void *user_data, const char *error) +{ + unsigned i; + unsigned menu_type = 0; + const char *path = NULL; + const char *label = NULL; + enum msg_hash_enums enum_idx = MSG_UNKNOWN; + file_list_t *file_list = NULL; + struct netplay_host_list *netplay_hosts = NULL; + + menu_entries_get_last_stack(&path, &label, &menu_type, &enum_idx, NULL); + + /* Don't push the results if we left the LAN scan menu */ + if (!string_is_equal(label, + msg_hash_to_str(MENU_ENUM_LABEL_DEFERRED_NETPLAY_LAN_SCAN_SETTINGS_LIST))) + return; + + file_list = menu_entries_get_selection_buf_ptr(0); + + menu_entries_ctl(MENU_ENTRIES_CTL_CLEAR, file_list); + + if (netplay_discovery_driver_ctl(RARCH_NETPLAY_DISCOVERY_CTL_LAN_GET_RESPONSES, (void *) &netplay_hosts)) + { + for (i = 0; i < netplay_hosts->size; i++) + { + struct netplay_host *host = &netplay_hosts->hosts[i]; + menu_entries_append_enum(file_list, + host->nick, + msg_hash_to_str(MENU_ENUM_LABEL_NETPLAY_CONNECT_TO), + MENU_ENUM_LABEL_NETPLAY_CONNECT_TO, + MENU_NETPLAY_LAN_SCAN, 0, 0); + } + } +} + +static void task_netplay_lan_scan_handler(retro_task_t *task) +{ + if (init_netplay_discovery()) + { + netplay_discovery_driver_ctl(RARCH_NETPLAY_DISCOVERY_CTL_LAN_CLEAR_RESPONSES, NULL); + netplay_discovery_driver_ctl(RARCH_NETPLAY_DISCOVERY_CTL_LAN_SEND_QUERY, NULL); + } + + task->progress = 100; + task->title = strdup(msg_hash_to_str(MSG_NETPLAY_LAN_SCANNING)); + task->finished = true; + + return; +} + +bool task_push_netplay_lan_scan(void) +{ + retro_task_t *task = (retro_task_t*)calloc(1, sizeof(*task)); + + if (!task) + goto error; + + task->type = TASK_TYPE_BLOCKING; + task->handler = task_netplay_lan_scan_handler; + task->callback = netplay_lan_scan_callback; + task->title = strdup(msg_hash_to_str(MSG_NETPLAY_LAN_SCANNING)); + + task_queue_ctl(TASK_QUEUE_CTL_PUSH, task); + + return true; + +error: + if (task) + free(task); + + return false; +} diff --git a/tasks/tasks_internal.h b/tasks/tasks_internal.h index d2b1fb16f1..cc14596739 100644 --- a/tasks/tasks_internal.h +++ b/tasks/tasks_internal.h @@ -95,6 +95,8 @@ task_retriever_info_t *http_task_get_transfer_list(void); bool task_push_wifi_scan(void); +bool task_push_netplay_lan_scan(void); + #endif bool task_push_image_load(const char *fullpath,