Complete.Org: Mailing Lists: Archives: freeciv-dev: November 2005:
[Freeciv-Dev] (PR#14694) use timers for last_write
Home

[Freeciv-Dev] (PR#14694) use timers for last_write

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#14694) use timers for last_write
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 25 Nov 2005 19:14:04 -0800
Reply-to: bugs@xxxxxxxxxxx

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

This patch changes struct connection::last_write to be a timer struct 
rather than a time integer.

The advantage of this is that it is accurate.  Using time() is only 
accurate to within one second; if you set tcptimeout to 1, for instance, 
you may be cut immediately in some circumstances.

It also makes the math easier.

Secondly this fixes an error in the previous patch: there are two places 
where lagging connections are cut; this patch unifies them into a single 
  function call.

-jason

Index: server/sernet.c
===================================================================
--- server/sernet.c     (revision 11274)
+++ server/sernet.c     (working copy)
@@ -254,6 +254,30 @@
 }
 
 /****************************************************************************
+  If a connection lags too much this function is called and we try to cut
+  it.
+****************************************************************************/
+static void cut_lagging_connection(struct connection *pconn)
+{
+  if (game.info.tcptimeout != 0
+      && pconn->last_write
+      && conn_list_size(game.all_connections) > 1
+      && pconn->access_level != ALLOW_HACK
+      && read_timer_seconds(pconn->last_write) > game.info.tcptimeout) {
+    /* Cut the connections to players who lag too much.  This
+     * usually happens because client animation slows the client
+     * too much and it can't keep up with the server.  We don't
+     * cut HACK connections, or cut in single-player games, since
+     * it wouldn't help the game progress.  For other connections
+     * the best thing to do when they lag too much is to be
+     * disconnected and reconnect. */
+    freelog(LOG_NORMAL, "cut connection %s due to lagging player",
+           conn_description(pconn));
+    close_socket_callback(pconn);
+  }
+}
+
+/****************************************************************************
   Attempt to flush all information in the send buffers for upto 'netwait'
   seconds.
 *****************************************************************************/
@@ -307,22 +331,7 @@
            if(FD_ISSET(pconn->sock, &writefs)) {
              flush_connection_send_buffer_all(pconn);
            } else {
-             if (game.info.tcptimeout != 0
-                 && pconn->last_write != 0
-                 && conn_list_size(game.all_connections) > 1
-                 && pconn->access_level != ALLOW_HACK
-                 && time(NULL) > pconn->last_write + game.info.tcptimeout) {
-               /* Cut the connections to players who lag too much.  This
-                * usually happens because client animation slows the client
-                * too much and it can't keep up with the server.  We don't
-                * cut HACK connections, or cut in single-player games, since
-                * it wouldn't help the game progress.  For other connections
-                * the best thing to do when they lag too much is to be
-                * disconnected and reconnect. */
-               freelog(LOG_NORMAL, "cut connection %s due to lagging player",
-                       conn_description(pconn));
-               close_socket_callback(pconn);
-             }
+             cut_lagging_connection(pconn);
            }
          }
         }
@@ -710,12 +719,7 @@
          if (FD_ISSET(pconn->sock, &writefs)) {
            flush_connection_send_buffer_all(pconn);
          } else {
-           if (game.info.tcptimeout != 0 && pconn->last_write != 0
-               && (time(NULL) > pconn->last_write + game.info.tcptimeout)) {
-             freelog(LOG_NORMAL, "cut connection %s due to lagging player",
-                     conn_description(pconn));
-             close_socket_callback(pconn);
-           }
+           cut_lagging_connection(pconn);
          }
         }
       }
Index: common/connection.c
===================================================================
--- common/connection.c (revision 11270)
+++ common/connection.c (working copy)
@@ -261,7 +261,8 @@
   if (start > 0) {
     buf->ndata -= start;
     memmove(buf->data, buf->data+start, buf->ndata);
-    (void) time(&pc->last_write);
+    pc->last_write = renew_timer_start(pc->last_write,
+                                      TIMER_USER, TIMER_ACTIVE);
   }
   return 0;
 }
@@ -619,7 +620,7 @@
 {
   pconn->established = FALSE;
   pconn->used = TRUE;
-  pconn->last_write = 0;
+  pconn->last_write = NULL;
   pconn->buffer = new_socket_packet_buffer();
   pconn->send_buffer = new_socket_packet_buffer();
   pconn->statistics.bytes_send = 0;
@@ -649,6 +650,11 @@
     free_socket_packet_buffer(pconn->send_buffer);
     pconn->send_buffer = NULL;
 
+    if (pconn->last_write) {
+      free_timer(pconn->last_write);
+      pconn->last_write = NULL;
+    }
+
     free_compression_queue(pconn);
     free_packet_hashes(pconn);
   }
Index: common/connection.h
===================================================================
--- common/connection.h (revision 11270)
+++ common/connection.h (working copy)
@@ -30,6 +30,7 @@
 ***************************************************************************/
 
 #include "shared.h"            /* MAX_LEN_ADDR, bool type */
+#include "timing.h"
 
 #include "fc_types.h"
 
@@ -118,7 +119,7 @@
   bool observer;
   struct socket_packet_buffer *buffer;
   struct socket_packet_buffer *send_buffer;
-  time_t last_write;
+  struct timer *last_write;
 
   double ping_time;
   

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#14694) use timers for last_write, Jason Short <=