From ba76528b8fcf7d6dc6979ad0f29814a0a2b6b008 Mon Sep 17 00:00:00 2001 From: Gregor Richards Date: Sat, 3 Dec 2016 11:52:27 -0500 Subject: [PATCH] A more sophisticated status variable for later making the initial connection nonblocking. --- network/netplay/netplay.c | 19 ++++++++++--------- network/netplay/netplay_net.c | 10 +++++----- network/netplay/netplay_private.h | 19 +++++++++++++++---- network/netplay/netplay_spectate.c | 2 +- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/network/netplay/netplay.c b/network/netplay/netplay.c index 6a89327546..88e969b71e 100644 --- a/network/netplay/netplay.c +++ b/network/netplay/netplay.c @@ -281,7 +281,7 @@ static void hangup(netplay_t *netplay) { if (!netplay) return; - if (!netplay->has_connection) + if (netplay->status == RARCH_NETPLAY_CONNECTION_NONE) return; RARCH_WARN("Netplay has disconnected. Will continue without connection ...\n"); @@ -300,7 +300,7 @@ static void hangup(netplay_t *netplay) } } - netplay->has_connection = false; + netplay->status = RARCH_NETPLAY_CONNECTION_NONE; /* Reset things that will behave oddly if we get a new connection */ netplay->remote_paused = false; @@ -328,7 +328,7 @@ static bool netplay_should_skip(netplay_t *netplay) { if (!netplay) return false; - return netplay->is_replay && netplay->has_connection; + return netplay->is_replay && (netplay->status == RARCH_NETPLAY_CONNECTION_PLAYING); } static bool netplay_can_poll(netplay_t *netplay) @@ -907,7 +907,7 @@ static bool netplay_poll(void) { int res; - if (!netplay_data->has_connection) + if (netplay_data->status == RARCH_NETPLAY_CONNECTION_NONE) return false; netplay_data->can_poll = false; @@ -1009,7 +1009,7 @@ static bool netplay_is_alive(void) { if (!netplay_data) return false; - return netplay_data->has_connection; + return (netplay_data->status == RARCH_NETPLAY_CONNECTION_PLAYING); } static bool netplay_flip_port(netplay_t *netplay, bool port) @@ -1588,7 +1588,7 @@ bool netplay_pre_frame(netplay_t *netplay) if (!netplay->net_cbs->pre_frame(netplay)) return false; - return (!netplay->has_connection || + return ((netplay->status != RARCH_NETPLAY_CONNECTION_PLAYING) || (!netplay->stall && !netplay->remote_paused)); } @@ -1622,7 +1622,8 @@ void netplay_frontend_paused(netplay_t *netplay, bool paused) return; netplay->local_paused = paused; - if (netplay->has_connection && !netplay->spectate.enabled) + if (netplay->status != RARCH_NETPLAY_CONNECTION_NONE && + !netplay->spectate.enabled) { netplay_send_raw_cmd(netplay, paused ? NETPLAY_CMD_PAUSE : NETPLAY_CMD_RESUME, NULL, 0); @@ -1649,7 +1650,7 @@ void netplay_load_savestate(netplay_t *netplay, retro_ctx_serialize_info_t tmp_serial_info; uint32_t rd, wn; - if (!netplay->has_connection) + if (netplay->status != RARCH_NETPLAY_CONNECTION_PLAYING) return; /* Record it in our own buffer */ @@ -1745,7 +1746,7 @@ void netplay_load_savestate(netplay_t *netplay, **/ bool netplay_disconnect(netplay_t *netplay) { - if (!netplay || !netplay->has_connection) + if (!netplay || (netplay->status == RARCH_NETPLAY_CONNECTION_NONE)) return true; hangup(netplay); return true; diff --git a/network/netplay/netplay_net.c b/network/netplay/netplay_net.c index 3847e2f798..0c3d21ba35 100644 --- a/network/netplay/netplay_net.c +++ b/network/netplay/netplay_net.c @@ -105,13 +105,13 @@ static bool netplay_net_pre_frame(netplay_t *netplay) } /* If we can't transmit savestates, we must stall until the client is ready */ - if (!netplay->has_connection && + if (netplay->status != RARCH_NETPLAY_CONNECTION_PLAYING && netplay->self_frame_count > 0 && (netplay->quirks & (NETPLAY_QUIRK_NO_SAVESTATES|NETPLAY_QUIRK_NO_TRANSMISSION))) netplay->stall = RARCH_NETPLAY_STALL_NO_CONNECTION; } - if (netplay->is_server && !netplay->has_connection) + if (netplay->is_server && netplay->status == RARCH_NETPLAY_CONNECTION_NONE) { fd_set fds; struct timeval tmp_tv = {0}; @@ -163,7 +163,7 @@ static bool netplay_net_pre_frame(netplay_t *netplay) /* Establish the connection */ if (netplay_handshake(netplay)) { - netplay->has_connection = true; + netplay->status = RARCH_NETPLAY_CONNECTION_PLAYING; /* FIXME: Not the best place for this, needs to happen after initial * connection in get_info */ @@ -223,7 +223,7 @@ static void netplay_net_post_frame(netplay_t *netplay) netplay->self_frame_count++; /* Only relevant if we're connected */ - if (!netplay->has_connection) + if (netplay->status != RARCH_NETPLAY_CONNECTION_PLAYING) { netplay->read_frame_count = netplay->other_frame_count = netplay->self_frame_count; netplay->read_ptr = netplay->other_ptr = netplay->self_ptr; @@ -353,7 +353,7 @@ static bool netplay_net_info_cb(netplay_t* netplay, unsigned frames) { if (!netplay_handshake(netplay)) return false; - netplay->has_connection = true; + netplay->status = RARCH_NETPLAY_CONNECTION_PLAYING; } return true; diff --git a/network/netplay/netplay_private.h b/network/netplay/netplay_private.h index 9fa61a26a4..19491d9b1f 100644 --- a/network/netplay/netplay_private.h +++ b/network/netplay/netplay_private.h @@ -117,11 +117,20 @@ struct netplay_callbacks { bool (*info_cb) (netplay_t *netplay, unsigned frames); }; +enum rarch_netplay_connection_status +{ + RARCH_NETPLAY_CONNECTION_NONE = 0, + RARCH_NETPLAY_CONNECTION_INIT, /* Waiting for header */ + RARCH_NETPLAY_CONNECTION_PRE_NICK, /* Waiting for nick */ + RARCH_NETPLAY_CONNECTION_PRE_SRAM, /* Waiting for SRAM */ + RARCH_NETPLAY_CONNECTION_PLAYING /* Normal ready state */ +}; + enum rarch_netplay_stall_reasons { - RARCH_NETPLAY_STALL_NONE = 0, - RARCH_NETPLAY_STALL_RUNNING_FAST, - RARCH_NETPLAY_STALL_NO_CONNECTION + RARCH_NETPLAY_STALL_NONE = 0, + RARCH_NETPLAY_STALL_RUNNING_FAST, + RARCH_NETPLAY_STALL_NO_CONNECTION }; struct netplay @@ -130,6 +139,9 @@ struct netplay char other_nick[32]; struct sockaddr_storage other_addr; + /* Status of our connection */ + enum rarch_netplay_connection_status status; + struct retro_callbacks cbs; /* TCP connection for state sending, etc. Also used for commands */ int fd; @@ -140,7 +152,6 @@ struct netplay struct natt_status nat_traversal_state; /* Which port is governed by netplay (other user)? */ unsigned port; - bool has_connection; struct delta_frame *buffer; size_t buffer_size; diff --git a/network/netplay/netplay_spectate.c b/network/netplay/netplay_spectate.c index 81ab4ef21c..d5670cbc7f 100644 --- a/network/netplay/netplay_spectate.c +++ b/network/netplay/netplay_spectate.c @@ -285,7 +285,7 @@ static bool netplay_spectate_info_cb(netplay_t* netplay, unsigned frames) return false; } - netplay->has_connection = true; + netplay->status = RARCH_NETPLAY_CONNECTION_PLAYING; return true; }