Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2002:
[Freeciv-Dev] (PR#2273) Server restart patch v3
Home

[Freeciv-Dev] (PR#2273) Server restart patch v3

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Cc: freeciv-dev@xxxxxxxxxxx
Subject: [Freeciv-Dev] (PR#2273) Server restart patch v3
From: "Per I. Mathisen via RT" <rt@xxxxxxxxxxxxxx>
Date: Thu, 7 Nov 2002 06:24:50 -0800
Reply-to: rt@xxxxxxxxxxxxxx

New version of the server restart patch (which makes the server
re-entrant). I've committed the missing initializations of city names
and the style fixes from the previous version, and moved a function so
that diff generates less noise. This version should be _much_ more readable.

Also fixed a bug in previous version which stopped autogames from
working properly.

I think the patch now should be ready to go in.
Index: server/sernet.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/sernet.c,v
retrieving revision 1.96
diff -u -r1.96 sernet.c
--- server/sernet.c     18 Oct 2002 09:58:49 -0000      1.96
+++ server/sernet.c     7 Nov 2002 14:23:35 -0000
@@ -362,8 +362,7 @@
       return 2;
     }
 
-
-    /* quit server if no players for 'srvarg.quitidle' seconds */
+    /* end server if no players for 'srvarg.quitidle' seconds */
     if (srvarg.quitidle != 0 && server_state != PRE_GAME_STATE) {
       static time_t last_noplayers;
       if(conn_list_size(&game.est_connections) == 0) {
@@ -374,8 +373,11 @@
            freelog(LOG_NORMAL, srvarg.metaserver_info_line);
            (void) send_server_info_to_metaserver(TRUE, FALSE);
 
-           close_connections_and_socket();
-           exit(EXIT_SUCCESS);
+            server_state = GAME_OVER_STATE;
+            conn_list_iterate(game.est_connections, pconn) {
+              lost_connection_to_client(pconn);
+              close_connection(pconn);
+            } conn_list_iterate_end;
          }
        } else {
          last_noplayers = time(NULL);
@@ -414,11 +416,6 @@
     /* Don't wait if timeout == -1 (i.e. on auto games) */
     if (server_state != PRE_GAME_STATE && game.timeout == -1) {
       (void) send_server_info_to_metaserver(FALSE, FALSE);
-
-      /* kick out of the srv_main loop */
-      if (server_state == GAME_OVER_STATE) {
-       server_state = PRE_GAME_STATE;
-      }
       return 0;
     }
 
Index: server/srv_main.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/srv_main.c,v
retrieving revision 1.101
diff -u -r1.101 srv_main.c
--- server/srv_main.c   7 Nov 2002 14:01:02 -0000       1.101
+++ server/srv_main.c   7 Nov 2002 14:23:35 -0000
@@ -115,7 +115,7 @@
 static void handle_turn_done(struct player *pplayer);
 static void send_select_nation(struct player *pplayer);
 static void check_for_full_turn_done(void);
-
+static void srv_loop(void);
 
 /* this is used in strange places, and is 'extern'd where
    needed (hence, it is not 'extern'd in srv_main.h) */
@@ -1797,8 +1797,6 @@
 **************************************************************************/
 void srv_main(void)
 {
-  int i;
-
   /* make sure it's initialized */
   if (!has_been_srv_init) {
     srv_init();
@@ -1839,7 +1837,7 @@
   (void) send_server_info_to_metaserver(TRUE, FALSE);
 
   /* accept new players, wait for serverop to start..*/
-  server_state=PRE_GAME_STATE;
+  server_state = PRE_GAME_STATE;
 
   /* load a script file */
   if (srvarg.script_filename
@@ -1847,6 +1845,48 @@
     exit(EXIT_FAILURE);
   }
 
+  /* Run server loop */
+  while (TRUE) {
+    srv_loop();
+    if (game.timeout == -1) {
+      game_free();
+      close_connections_and_socket();
+      exit(EXIT_SUCCESS);
+    }
+
+    send_game_state(&game.est_connections, CLIENT_GAME_OVER_STATE);
+    report_scores(TRUE);
+    show_map_to_all();
+    notify_player(NULL, _("Game: The game is over..."));
+    gamelog(GAMELOG_NORMAL, _("The game is over!"));
+    if (game.save_nturns > 0) {
+      save_game_auto();
+    }
+
+    /* Remain in GAME_OVER_STATE until players log out */
+    while (conn_list_size(&game.est_connections) > 0) {
+      (void) sniff_packets();
+    }
+
+    /* Reset server */
+    server_game_free();
+    game_init();
+    game.is_new_game = TRUE;
+    server_state = PRE_GAME_STATE;
+  }
+
+  /* Technically, we won't ever get here... */
+  game_free();
+  close_connections_and_socket();
+}
+
+/**************************************************************************
+  Server loop, run to set up one game.
+**************************************************************************/
+static void srv_loop(void)
+{
+  int i;
+
   freelog(LOG_NORMAL, _("Now accepting new client connections."));
   while(server_state == PRE_GAME_STATE) {
     sniff_packets(); /* Accepting commands. */
@@ -2007,22 +2047,6 @@
 
   /*** Where the action is. ***/
   main_loop();
-
-  send_game_state(&game.est_connections, CLIENT_GAME_OVER_STATE);
-
-  report_scores(TRUE);
-  show_map_to_all();
-  notify_player(NULL, _("Game: The game is over..."));
-  gamelog(GAMELOG_NORMAL, _("The game is over!"));
-  if (game.save_nturns > 0) save_game_auto();
-
-  while (server_state == GAME_OVER_STATE) {
-    force_end_of_sniff = FALSE;
-    sniff_packets();
-  }
-
-  close_connections_and_socket();
-  server_game_free();
 }
 
 /**************************************************************************

[Prev in Thread] Current Thread [Next in Thread]