Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2005:
[Freeciv-Dev] Re: (PR#12563) patch: sernet.c cleanup
Home

[Freeciv-Dev] Re: (PR#12563) patch: sernet.c cleanup

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] Re: (PR#12563) patch: sernet.c cleanup
From: "Benoit Hudson" <bh@xxxxxxxxxxxxxxxxxxx>
Date: Sun, 20 Mar 2005 14:26:12 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12563 >

On Sun, Mar 20, 2005 at 01:00:35PM -0800, Raimar Falke wrote:
> You know that you removed the time statistics?!

Oops; that's sloppy of me.  New patch attached. 

        -- Benoît

Index: server/sernet.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/sernet.c,v
retrieving revision 1.139
diff -b -u -p -r1.139 sernet.c
--- server/sernet.c     16 Mar 2005 04:40:32 -0000      1.139
+++ server/sernet.c     20 Mar 2005 22:23:55 -0000
@@ -324,6 +324,73 @@ void flush_packets(void)
 }
 
 /*****************************************************************************
+  Simplify a loop by wrapping get_packet_from_connection.
+*****************************************************************************/
+struct packet_to_handle {
+  void *data;
+  enum packet_type type;
+};
+
+static bool get_packet(struct connection *pconn, 
+                       struct packet_to_handle *ppacket)
+{
+  bool got_packet;
+
+  ppacket->data = get_packet_from_connection(pconn, &ppacket->type, 
+                                             &got_packet);
+  return got_packet;
+}
+
+/*****************************************************************************
+  Handle all incoming packets on a client connection.
+  Precondition - we have read_socket_data.
+  Postcondition - there are no more packets to handle on this connection.
+*****************************************************************************/
+static void handle_incoming_client_packets(struct connection *pconn) 
+{
+  struct packet_to_handle packet;
+#if PROCESSING_TIME_STATISTICS
+  struct timer *request_time = NULL;
+#endif
+
+  while (get_packet(pconn, &packet)) {
+    bool command_ok;
+    int request_id;
+
+#if PROCESSING_TIME_STATISTICS
+    request_time = renew_timer_start(request_time, TIMER_USER, TIMER_ACTIVE);
+#endif
+
+    request_id = pconn->server.last_request_id_seen =
+      get_next_request_id(pconn->server.last_request_id_seen);
+
+    connection_do_buffer(pconn);
+    start_processing_request(pconn, pconn->server.last_request_id_seen);
+
+    command_ok = handle_packet_input(pconn, packet.data, packet.type);
+    free(packet.data);
+
+    finish_processing_request(pconn);
+    connection_do_unbuffer(pconn);
+
+#if PROCESSING_TIME_STATISTICS
+    freelog(LOG_NORMAL,
+            "processed request %d in %gms", request_id, 
+            read_timer_seconds(request_time) * 1000.0);
+#endif
+
+    if (!command_ok) {
+      close_connection(pconn);
+    }
+  }
+
+#if PROCESSING_TIME_STATISTICS
+  free_timer(request_time);
+#endif
+}
+
+
+/*****************************************************************************
 Get and handle:
 - new connections,
 - input from connections,
@@ -588,69 +655,21 @@ int sniff_packets(void)
 #endif /* !SOCKET_ZERO_ISNT_STDIN */
      
     {                             /* input from a player */
-      for(i=0; i<MAX_NUM_CONNECTIONS; i++) {
+      for(i = 0; i < MAX_NUM_CONNECTIONS; i++) {
        struct connection *pconn = &connections[i];
-       if(pconn->used && FD_ISSET(pconn->sock, &readfs)) {
-         if(read_socket_data(pconn->sock, pconn->buffer)>=0) {
-           void *packet;
-           enum packet_type type;
-           bool result;
 
-           while (TRUE) {
-             packet = get_packet_from_connection(pconn, &type, &result);
-             if (result) {
-               bool command_ok;
-
-               pconn->server.last_request_id_seen =
-                   get_next_request_id(pconn->server.
-                                       last_request_id_seen);
-#if PROCESSING_TIME_STATISTICS
-               {
-                 int err;
-                 struct timeval start, end;
-                 struct timezone tz;
-                 long us;
-
-                 err = gettimeofday(&start, &tz);
-                 assert(!err);
-#endif
-               connection_do_buffer(pconn);
-               start_processing_request(pconn,
-                                        pconn->server.
-                                        last_request_id_seen);
-               command_ok = handle_packet_input(pconn, packet, type);
-               if (packet) {
-                 free(packet);
-                 packet = NULL;
-               }
-
-               finish_processing_request(pconn);
-               connection_do_unbuffer(pconn);
-               if (!command_ok) {
-                 close_connection(pconn);
+        if (!pconn->used || !FD_ISSET(pconn->sock, &readfs)) {
+          continue;
                }
-#if PROCESSING_TIME_STATISTICS
-                 err = gettimeofday(&end, &tz);
-                 assert(!err);
-
-                 us = (end.tv_sec - start.tv_sec) * 1000000 +
-                     end.tv_usec - start.tv_usec;
 
-                 freelog(LOG_NORMAL,
-                         "processed request %d in %ld.%03ldms",
-                         pconn->server.last_request_id_seen, us / 1000,
-                         us % 1000);
-                }
-#endif
-             } else {
-               break;
-             }
-           }
+        if(read_socket_data(pconn->sock, pconn->buffer) >= 0) {
+          /* We read packets; now handle them. */
+          handle_incoming_client_packets(pconn);
          } else {
+          /* Read failure; the connection is closed. */
            close_socket_callback(pconn);
          }
        }
-      }
 
       for(i=0; i<MAX_NUM_CONNECTIONS; i++) {
         struct connection *pconn = &connections[i];

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