code cleanup (handling of network socket activity)
[rocksndiamonds.git] / src / netserv.c
index 6555e0497922502843b7ef909c34cfc69be28bf1..7c38ceed788b44a2f5921dc6a9c011c2a134cc73 100644 (file)
@@ -433,6 +433,13 @@ static void Handle_OP_MOVE_PLAYER(struct NetworkServerPlayerInfo *player)
   ServerFrameCounter++;
 }
 
+void ExitNetworkServer(int exit_value)
+{
+  Error(ERR_NETWORK_SERVER, "exiting network server");
+
+  exit(exit_value);
+}
+
 /* the following is not used for a standalone server;
    the pointer points to an integer containing the port-number */
 int NetworkServerThread(void *ptr)
@@ -445,7 +452,6 @@ int NetworkServerThread(void *ptr)
 
 void NetworkServer(int port, int serveronly)
 {
-  int sl;
   struct NetworkServerPlayerInfo *player;
   int r; 
   unsigned int len;
@@ -458,6 +464,10 @@ void NetworkServer(int port, int serveronly)
   if (port == 0)
     port = DEFAULT_SERVER_PORT;
 
+  // if only running the network server, exit on Ctrl-C
+  if (serveronly)
+    signal(SIGINT, ExitNetworkServer);
+
   if (!serveronly)
     onceonly = 1;
 
@@ -496,17 +506,8 @@ void NetworkServer(int port, int serveronly)
     for (player = first_player; player; player = player->next)
       flushuser(player);
 
-    if ((sl = SDLNet_CheckSockets(fds, 500000)) < 1)
-    {
-      Error(ERR_NETWORK_SERVER, "SDLNet_CheckSockets failed: %s",
-           SDLNet_GetError());
-      perror("SDLNet_CheckSockets");
-    }
-
-    if (sl < 0)
-      continue;
-    
-    if (sl == 0)
+    // wait for 100 ms for activity on open network sockets
+    if (SDLNet_CheckSockets(fds, 100) < 1)
       continue;
 
     /* accept incoming connections */