reduced time to wait for UDP broadcast answer to auto-detect network server
[rocksndiamonds.git] / src / network.c
index 742cea5792e6a0cd639b851b12a4c92f2916c329..cabcfd3030485ce0b55fc24ba40f363f171bb89f 100644 (file)
@@ -9,19 +9,14 @@
 // network.c
 // ============================================================================
 
-#include "libgame/platform.h"
-
-#if defined(NETWORK_AVALIABLE)
-
 #include <signal.h>
 #include <sys/time.h>
 
-#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"
@@ -54,6 +49,62 @@ static byte *buffer = realbuffer + 4;
 static int nread = 0, nwrite = 0;
 static boolean stop_network_game = FALSE;
 
+static void DrawNetworkTextExt(char *message, int font_nr, boolean initialize)
+{
+  static int xpos = 0, ypos = 0;
+  int font_width = getFontWidth(font_nr);
+  int font_height = getFontHeight(font_nr);
+
+  if (initialize)
+  {
+    xpos = (WIN_XSIZE - getTextWidth(message, font_nr)) / 2;
+    ypos = 120;
+
+    DrawText(xpos, ypos, message, font_nr);
+
+    xpos = 0;
+    ypos = 150;
+
+    Error(ERR_DEBUG, "========== %s ==========", message);
+  }
+  else
+  {
+    int max_chars_per_line = WIN_XSIZE / font_width;
+    int max_lines_per_text = 10;
+    int num_lines_spacing = (font_nr == FC_YELLOW ? 1 : 3);
+    int num_lines_printed = DrawTextBuffer(xpos, ypos, message, font_nr,
+                                          max_chars_per_line, -1,
+                                          max_lines_per_text, 0, -1,
+                                          TRUE, TRUE, FALSE);
+
+    ypos += (num_lines_printed + num_lines_spacing) * font_height;
+
+    Error(ERR_DEBUG, "%s", message);
+  }
+
+  BackToFront();
+}
+
+static void DrawNetworkText(char *message)
+{
+  DrawNetworkTextExt(message, FC_YELLOW, FALSE);
+}
+
+static void DrawNetworkText_Success(char *message)
+{
+  DrawNetworkTextExt(message, FC_GREEN, FALSE);
+}
+
+static void DrawNetworkText_Failed(char *message)
+{
+  DrawNetworkTextExt(message, FC_RED, FALSE);
+}
+
+static void DrawNetworkText_Title(char *message)
+{
+  DrawNetworkTextExt(message, FC_GREEN, TRUE);
+}
+
 static void SendBufferToServer(int size)
 {
   if (!network.enabled)
@@ -125,6 +176,8 @@ boolean ConnectToServer(char *hostname, int port)
   int server_host = 0;
   int i;
 
+  DrawNetworkText_Title("Initializing Network");
+
   if (port == 0)
     port = DEFAULT_SERVER_PORT;
 
@@ -164,26 +217,26 @@ boolean ConnectToServer(char *hostname, int port)
 
     SDLNet_UDP_Send(udp, -1, &packet);
 
-    Error(ERR_DEBUG, "doing UDP broadcast for local network server ...");
+    DrawNetworkText("Looking for local network server ...");
 
-    if (SDLNet_CheckSockets(udp_socket_set, 1000) == 1)
+    if (SDLNet_CheckSockets(udp_socket_set, 500) == 1)
     {
       int num_packets = SDLNet_UDP_Recv(udp, &packet);
 
       if (num_packets == 1)
       {
-        Error(ERR_DEBUG, "network server found");
+       DrawNetworkText_Success("Network server found!");
 
         server_host = SDLNet_Read32(&packet.address.host);
       }
       else
       {
-       Error(ERR_DEBUG, "no answer from network server");
+       DrawNetworkText_Failed("No answer from network server!");
       }
     }
     else
     {
-      Error(ERR_DEBUG, "no network server found");
+      DrawNetworkText_Failed("No network server found!");
     }
   }
 
@@ -197,6 +250,8 @@ boolean ConnectToServer(char *hostname, int port)
       Error(ERR_EXIT, "cannot locate host '%s'", hostname);
     else
       server_host = SDLNet_Read32(&ip.host);
+
+    DrawNetworkText("Connecting to remote host ...");
   }
   else
   {
@@ -207,6 +262,8 @@ boolean ConnectToServer(char *hostname, int port)
 
     SDLNet_Write32(server_host, &ip.host);
     SDLNet_Write16(port,        &ip.port);
+
+    DrawNetworkText("Connecting to local host ...");
   }
 
   Error(ERR_DEBUG, "trying to connect to network server at %d.%d.%d.%d ...",
@@ -220,31 +277,41 @@ boolean ConnectToServer(char *hostname, int port)
   if (sfd)
   {
     SDLNet_TCP_AddSocket(rfds, sfd);
+
+    DrawNetworkText_Success("Successfully connected!");
+
     return TRUE;
   }
   else
   {
+    DrawNetworkText_Failed("Failed to connect to network server!");
+
     printf("SDLNet_TCP_Open(): %s\n", SDLNet_GetError());
   }
 
   if (hostname)                        /* connect to specified server failed */
     return FALSE;
 
-  printf("No rocksndiamonds server on localhost -- starting up one ...\n");
+  DrawNetworkText("Starting new local network server ...");
+
   StartNetworkServer(port);
 
   /* wait for server to start up and try connecting several times */
-  for (i = 0; i < 6; i++)
+  for (i = 0; i < 30; i++)
   {
-    Delay(500);                        /* wait 500 ms == 0.5 seconds */
-
     if ((sfd = SDLNet_TCP_Open(&ip)))          /* connected */
     {
+      DrawNetworkText_Success("Successfully connected!");
+
       SDLNet_TCP_AddSocket(rfds, sfd);
       return TRUE;
     }
+
+    Delay(100);
   }
 
+  DrawNetworkText_Failed("Failed to connect to network server!");
+
   /* when reaching this point, connect to newly started server has failed */
   return FALSE;
 }
@@ -546,7 +613,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 +737,88 @@ 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_playing = FALSE;
+
+  network.enabled = FALSE;
+  network.connected = FALSE;
+
+  setup.network_mode = 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");
+      Request(error_message, REQ_CONFIRM | REQ_STAY_CLOSED);
 
-      if (r == 0)
-       Error(ERR_EXIT, "connection to network server lost");
+      SetGameStatus(GAME_MODE_MAIN);
 
-      nread += r;
-
-      HandleNetworkingMessages();
+      DrawMainMenu();
+    }
+    else
+    {
+      Request(error_message, REQ_CONFIRM);
     }
   }
-  while (r > 0);
 }
 
-#endif /* NETWORK_AVALIABLE */
+void DisconnectFromNetworkServer()
+{
+  DrawNetworkText_Title("Terminating Network");
+  DrawNetworkText("Disconnecting from network server ...");
+
+  HandleNetworkingDisconnect();
+
+  DrawNetworkText_Success("Successfully disconnected!");
+
+  /* short time to recognize result of network initialization */
+  Delay(1000);
+}