From 1734e2ff50d3321ffcccca58f7fb7026f1988cc9 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Sun, 1 Jun 2008 15:30:14 +0000 Subject: [PATCH] Client side timeouts --- vdr.cc | 107 +++++++++++++++++++++++++++------------------------------ 1 file changed, 51 insertions(+), 56 deletions(-) diff --git a/vdr.cc b/vdr.cc index 0ca6a88..e612c9d 100644 --- 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"); } } -- 2.39.5