From f264d20d1ecab630ba104077a292ac260c80fbc3 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 5 Nov 2021 17:50:18 +0100 Subject: [PATCH] Move some stray globals to netplay state --- network/netplay/netplay.h | 17 +++++++- network/netplay/netplay_frontend.c | 65 +++++++++++++----------------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/network/netplay/netplay.h b/network/netplay/netplay.h index 919502c44b..a8c8e0541c 100644 --- a/network/netplay/netplay.h +++ b/network/netplay/netplay.h @@ -24,6 +24,14 @@ #include #include +#ifdef HAVE_CONFIG_H +#include "../../config.h" +#endif + +#ifdef HAVE_NETPLAYDISCOVERY +#include "netplay_discovery.h" +#endif + #include "../../core.h" enum rarch_netplay_ctl_state @@ -115,8 +123,16 @@ typedef struct { netplay_t *data; /* Used while Netplay is running */ struct netplay_room host_room; /* ptr alignment */ + netplay_t *handshake_password; + /* List of discovered hosts */ + struct netplay_host_list discovered_hosts; +#ifdef HAVE_NETPLAYDISCOVERY + size_t discovered_hosts_allocated; +#endif int reannounce; unsigned server_port_deferred; + uint16_t mapping[RETROK_LAST]; + char server_address_deferred[512]; /* Only used before init_netplay */ bool netplay_enabled; bool netplay_is_client; @@ -124,7 +140,6 @@ typedef struct bool in_netplay; bool netplay_client_deferred; bool is_mitm; - char server_address_deferred[512]; bool has_set_netplay_mode; bool has_set_netplay_ip_address; bool has_set_netplay_ip_port; diff --git a/network/netplay/netplay_frontend.c b/network/netplay/netplay_frontend.c index 257b9dad76..4548bac3a4 100644 --- a/network/netplay/netplay_frontend.c +++ b/network/netplay/netplay_frontend.c @@ -156,7 +156,6 @@ struct ad_packet /* TODO/FIXME - globals */ struct netplay_room *netplay_room_list = NULL; int netplay_room_count = 0; -static netplay_t *handshake_password_netplay = NULL; static unsigned long simple_rand_next = 1; #ifdef HAVE_NETPLAYDISCOVERY @@ -166,11 +165,6 @@ static int lan_ad_client_fd = -1; /* Packet buffer for advertisement and responses */ static struct ad_packet ad_packet_buffer; - -/* List of discovered hosts */ -static struct netplay_host_list discovered_hosts; - -static size_t discovered_hosts_allocated; #endif /* The mapping of keys from netplay (network) to libretro (host) */ @@ -184,9 +178,6 @@ const uint16_t netplay_key_ntoh_mapping[] = { 0 }; -/* TODO/FIXME - static global variables */ -static uint16_t netplay_mapping[RETROK_LAST]; - static net_driver_state_t networking_driver_st = {0}; net_driver_state_t *networking_state_get_ptr(void) @@ -219,7 +210,8 @@ static bool netplay_lan_ad_client(void) fd_set fds; socklen_t addr_size; struct sockaddr their_addr; - struct timeval tmp_tv = {0}; + struct timeval tmp_tv = {0}; + net_driver_state_t *net_st = &networking_driver_st; if (lan_ad_client_fd < 0) return false; @@ -282,9 +274,9 @@ static bool netplay_lan_ad_client(void) continue; /* Allocate space for it */ - if (discovered_hosts.size >= discovered_hosts_allocated) + if (net_st->discovered_hosts.size >= net_st->discovered_hosts_allocated) { - size_t allocated = discovered_hosts_allocated; + size_t allocated = net_st->discovered_hosts_allocated; struct netplay_host *new_hosts = NULL; if (allocated == 0) @@ -292,9 +284,9 @@ static bool netplay_lan_ad_client(void) else allocated *= 2; - if (discovered_hosts.hosts) + if (net_st->discovered_hosts.hosts) new_hosts = (struct netplay_host *) - realloc(discovered_hosts.hosts, allocated * sizeof(struct + realloc(net_st->discovered_hosts.hosts, allocated * sizeof(struct netplay_host)); else /* Should be equivalent to realloc, @@ -305,12 +297,12 @@ static bool netplay_lan_ad_client(void) if (!new_hosts) return false; - discovered_hosts.hosts = new_hosts; - discovered_hosts_allocated = allocated; + net_st->discovered_hosts.hosts = new_hosts; + net_st->discovered_hosts_allocated = allocated; } /* Get our host structure */ - host = &discovered_hosts.hosts[discovered_hosts.size++]; + host = &net_st->discovered_hosts.hosts[net_st->discovered_hosts.size++]; /* Copy in the response */ memset(host, 0, sizeof(struct netplay_host)); @@ -389,7 +381,8 @@ bool netplay_discovery_driver_ctl( { int ret; char port_str[6]; - unsigned k = 0; + unsigned k = 0; + net_driver_state_t *net_st = &networking_driver_st; if (lan_ad_client_fd < 0) return false; @@ -444,11 +437,11 @@ bool netplay_discovery_driver_ctl( case RARCH_NETPLAY_DISCOVERY_CTL_LAN_GET_RESPONSES: if (!netplay_lan_ad_client()) return false; - *((struct netplay_host_list **) data) = &discovered_hosts; + *((struct netplay_host_list **) data) = &net_st->discovered_hosts; break; case RARCH_NETPLAY_DISCOVERY_CTL_LAN_CLEAR_RESPONSES: - discovered_hosts.size = 0; + net_st->discovered_hosts.size = 0; break; default: @@ -770,7 +763,7 @@ static uint32_t netplay_platform_magic(void) { uint32_t ret = ((1 == htonl(1)) << 30) - |(sizeof(size_t) << 15) + |(sizeof(size_t) << 15) |(sizeof(long)); return ret; } @@ -792,20 +785,12 @@ static int simple_rand(void) return((unsigned)(simple_rand_next / 65536) % 32768); } -static void simple_srand(unsigned int seed) -{ - simple_rand_next = seed; -} - static uint32_t simple_rand_uint32(void) { - uint32_t parts[3]; - parts[0] = simple_rand(); - parts[1] = simple_rand(); - parts[2] = simple_rand(); - return ((parts[0] << 30) + - (parts[1] << 15) + - parts[2]); + uint32_t part0 = simple_rand(); + uint32_t part1 = simple_rand(); + uint32_t part2 = simple_rand(); + return ((part0 << 30) + (part1 << 15) + part2); } /* @@ -861,7 +846,7 @@ bool netplay_handshake_init_send(netplay_t *netplay, { /* Demand a password */ if (simple_rand_next == 1) - simple_srand((unsigned int) time(NULL)); + simple_rand_next = (unsigned int)time(NULL); connection->salt = simple_rand_uint32(); if (connection->salt == 0) connection->salt = 1; @@ -884,7 +869,8 @@ static void handshake_password(void *ignore, const char *line) struct password_buf_s password_buf; char password[8+NETPLAY_PASS_LEN]; /* 8 for salt, 128 for password */ char hash[NETPLAY_PASS_HASH_LEN+1]; /* + NULL terminator */ - netplay_t *netplay = handshake_password_netplay; + net_driver_state_t *net_st = &networking_driver_st; + netplay_t *netplay = net_st->handshake_password; struct netplay_connection *connection = &netplay->connections[0]; snprintf(password, sizeof(password), "%08lX", (unsigned long)connection->salt); @@ -926,6 +912,7 @@ bool netplay_handshake_init(netplay_t *netplay, uint32_t compression = 0; struct compression_transcoder *ctrans = NULL; const char *dmsg = NULL; + net_driver_state_t *net_st = &networking_driver_st; settings_t *settings = config_get_ptr(); bool extra_notifications = settings->bools.notification_show_netplay_extra; @@ -1062,7 +1049,7 @@ bool netplay_handshake_init(netplay_t *netplay, retroarch_menu_running(); #endif - handshake_password_netplay = netplay; + net_st->handshake_password = netplay; #ifdef HAVE_MENU memset(&line, 0, sizeof(line)); @@ -1855,9 +1842,10 @@ bool netplay_handshake(netplay_t *netplay, /* The mapping of keys from libretro (host) to netplay (network) */ uint32_t netplay_key_hton(unsigned key) { + net_driver_state_t *net_st = &networking_driver_st; if (key >= RETROK_LAST) return NETPLAY_KEY_UNKNOWN; - return netplay_mapping[key]; + return net_st->mapping[key]; } /* Because the hton keymapping has to be generated, call this before using @@ -1865,12 +1853,13 @@ uint32_t netplay_key_hton(unsigned key) void netplay_key_hton_init(void) { static bool mapping_defined = false; + net_driver_state_t *net_st = &networking_driver_st; if (!mapping_defined) { uint16_t i; for (i = 0; i < NETPLAY_KEY_LAST; i++) - netplay_mapping[NETPLAY_KEY_NTOH(i)] = i; + net_st->mapping[NETPLAY_KEY_NTOH(i)] = i; mapping_defined = true; } }