From fdbb975aee39b32064a9fdc461467e733c7854c9 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Fri, 15 Jun 2018 00:01:30 +0200 Subject: [PATCH] improved handling network errors (show message instead of stopping program) --- src/network.c | 72 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/src/network.c b/src/network.c index 2bc8f5c4..0e5955c6 100644 --- a/src/network.c +++ b/src/network.c @@ -664,31 +664,71 @@ static void HandleNetworkingMessages() SendToServer_StopPlaying(NETWORK_STOP_BY_ERROR); } -/* TODO */ +static char *HandleNetworkingPackets() +{ + while (1) + { + /* ---------- check network server for activity ---------- */ + + int num_active_sockets = SDLNet_CheckSockets(rfds, 1); + + if (num_active_sockets < 0) + return "Error checking network sockets!"; + + if (num_active_sockets == 0) + break; // no active sockets, stop here + + /* ---------- read packets from network server ---------- */ + + int num_bytes = SDLNet_TCP_Recv(sfd, readbuffer + nread, 1); + + if (num_bytes < 0) + return "Error reading from network server!"; + + if (num_bytes == 0) + return "Connection to network server lost!"; + + nread += num_bytes; + + HandleNetworkingMessages(); + } + + return NULL; +} + +static void HandleNetworkingDisconnect() +{ + int i; + + SDLNet_TCP_DelSocket(rfds, sfd); + SDLNet_TCP_Close(sfd); + + network.enabled = FALSE; + network_playing = FALSE; + + for (i = 0; i < MAX_PLAYERS; i++) + stored_player[i].connected_network = FALSE; +} void HandleNetworking() { - int r = 0; + char *error_message = HandleNetworkingPackets(); - do + if (error_message != NULL) { - if ((r = SDLNet_CheckSockets(rfds, 1)) < 0) - Error(ERR_EXIT, "HandleNetworking(): SDLNet_CheckSockets() failed"); + HandleNetworkingDisconnect(); - if (r > 0) + if (game_status == GAME_MODE_PLAYING) { - r = SDLNet_TCP_Recv(sfd, readbuffer + nread, 1); - - if (r < 0) - Error(ERR_EXIT, "error reading from network server"); - - if (r == 0) - Error(ERR_EXIT, "connection to network server lost"); + Request(error_message, REQ_CONFIRM | REQ_STAY_CLOSED); - nread += r; + SetGameStatus(GAME_MODE_MAIN); - HandleNetworkingMessages(); + DrawMainMenu(); + } + else + { + Request(error_message, REQ_CONFIRM); } } - while (r > 0); } -- 2.34.1