]> git.vomp.tv Git - vompclient.git/commitdiff
Client side timeouts
authorChris Tallon <chris@vomp.tv>
Sun, 1 Jun 2008 15:30:14 +0000 (15:30 +0000)
committerChris Tallon <chris@vomp.tv>
Sun, 1 Jun 2008 15:30:14 +0000 (15:30 +0000)
vdr.cc

diff --git a/vdr.cc b/vdr.cc
index 0ca6a887125a3da4f4f6ee62e09221d886dbfd2d..e612c9d3e93f570ffbcf118a103e0de8657f4d1b 100644 (file)
--- a/vdr.cc
+++ b/vdr.cc
@@ -198,78 +198,73 @@ void VDR::threadMethod()
 
   VDR_ResponsePacket* vresp;
   
-  int timeoutCount = 0;
-  ULONG lastKAsent = 0;
   ULONG timeNow = 0;
-  
+  ULONG lastKAsent = 0;
+  ULONG lastKArecv = time(NULL);
+  int readSuccess;
+
   while(1) 
-  {  
-    if (!tcp->readData((UCHAR*)&channelID, sizeof(ULONG)))  // 2s timeout atm
-    {
-      ++timeoutCount;
+  {
+    timeNow = time(NULL);
     
-      // Error or timeout.
-
-      /* This is the simple version of timeout system until I work out how to make it better
-         e.g. different timeout lengths for different requests, a decent keepalive system
-         running in parallel etc etc.
-        
-         Ignore the stream channel
-         Start with lastPacketReceived = 0
-         When a packet comes in, set lastPacketReceived = time(NULL)
-         loop
-         {
-           if last packet is over 20s ago
-           {
-             if lastKAsent is less than 20s ago, continue
-             
-             if lastKAsent = 0, send a KA, continue
-             
-             if lastKAsent is over 20s ago, connection is dead, kill it
-           }
-         }         
-      */
-
-      logger->log("VDR", Log::DEBUG, "Net read timeout %i", timeoutCount);
+    readSuccess = tcp->readData((UCHAR*)&channelID, sizeof(ULONG));  // 2s timeout atm
 
+    if (!readSuccess)
+    {
+      logger->log("VDR", Log::DEBUG, "Net read timeout");
       if (!tcp->isConnected()) { connectionDied(); return; } // return to stop this thread
+    }
       
-  //    timeoutCount = 0; // disable it for now
+    // Error or timeout.
+
+    /* This is the simple version of timeout system until I work out how to make it better
+       e.g. different timeout lengths for different requests, a decent keepalive system
+       running in parallel etc etc.
       
-      if (timeoutCount >= 3) // x2 for seconds
+       Ignore the stream channel
+       Start with lastPacketReceived = 0
+       When a packet comes in, set lastPacketReceived = time(NULL)
+       loop
+       {
+         if last packet is over 20s ago
+         {
+           if lastKAsent is less than 20s ago, continue
+           
+           if lastKAsent = 0, send a KA, continue
+           
+           if lastKAsent is over 20s ago, connection is dead, kill it
+         }
+       }         
+    */
+
+    if (!lastKAsent) // have not sent a KA
+    {
+      if (lastKArecv < (timeNow - 5))
       {
-        timeNow = (ULONG)time(NULL);
-      
-        if (lastKAsent > (timeNow - 6)) continue;
-        
-        if (lastKAsent == 0)
+        logger->log("VDR", Log::DEBUG, "Sending KA packet");
+        if (!sendKA(timeNow))
         {
-          logger->log("VDR", Log::DEBUG, "Sending KA packet");
-          if (!sendKA(timeNow))
-          {
-            logger->log("VDR", Log::DEBUG, "Could not send KA, calling connectionDied");
-            connectionDied();
-            return;
-          }
-          lastKAsent = timeNow;
-          continue;
-        }
-        
-        if (lastKAsent <= (timeNow - 6))
-        {
-          logger->log("VDR", Log::DEBUG, "lastKA over 20s ago, calling connectionDied");
+          logger->log("VDR", Log::DEBUG, "Could not send KA, calling connectionDied");
           connectionDied();
           return;
         }
+        lastKAsent = timeNow;
       }
-     
-      continue;      
     }
+    else
+    {
+      if (lastKAsent <= (timeNow - 10))
+      {
+        logger->log("VDR", Log::DEBUG, "lastKA over 10s ago, calling connectionDied");
+        connectionDied();
+        return;
+      }    
+    }
+
+    if (!readSuccess) continue; // no data was read but the connection is ok.
     
     // Data was read
             
-    timeoutCount = 0;
-    
     channelID = ntohl(channelID);
    
     if (channelID == CHANNEL_REQUEST_RESPONSE)
@@ -335,7 +330,7 @@ void VDR::threadMethod()
       if (KAreply == lastKAsent) // successful KA response
       {
         lastKAsent = 0;
-        timeoutCount = 0;
+        lastKArecv = KAreply;
         logger->log("VDR", Log::DEBUG, "Rxd correct KA reply");
       }
     }