X-Git-Url: https://git.artsoft.org/?a=blobdiff_plain;f=src%2Fnetwork.c;h=2bbdf6b18b1277c576258cee3982775f379e858b;hb=9e08928735ed2195ecceb20747e705cfd3c7c5d2;hp=b55e68e7a584d5a017cd97c4faa0fe8feb99a67d;hpb=77ece4b955d08d19d0f05bf2d8241467a06d4bab;p=rocksndiamonds.git diff --git a/src/network.c b/src/network.c index b55e68e7..2bbdf6b1 100644 --- a/src/network.c +++ b/src/network.c @@ -9,19 +9,14 @@ // network.c // ============================================================================ -#include "libgame/platform.h" - -#if defined(NETWORK_AVALIABLE) - #include #include -#include "main.h" - #include "libgame/libgame.h" #include "network.h" #include "netserv.h" +#include "main.h" #include "game.h" #include "tape.h" #include "files.h" @@ -56,7 +51,7 @@ static boolean stop_network_game = FALSE; static void SendBufferToServer(int size) { - if (!options.network) + if (!network.enabled) return; realbuffer[0] = realbuffer[1] = realbuffer[2] = 0; @@ -546,7 +541,8 @@ static void Handle_OP_STOP_PLAYING() { int client_nr = buffer[0]; int index_nr = client_nr - 1; - boolean stopped_by_remote_player = (index_nr != local_player->index_nr); + struct PlayerInfo *client_player = &stored_player[index_nr]; + boolean stopped_by_remote_player = (!client_player->connected_locally); char *message = (buffer[2] == NETWORK_STOP_BY_PLAYER ? "Network game stopped by player!" : buffer[2] == NETWORK_STOP_BY_ERROR ? @@ -669,33 +665,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); } - -#endif /* NETWORK_AVALIABLE */