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)
if (KAreply == lastKAsent) // successful KA response
{
lastKAsent = 0;
- timeoutCount = 0;
+ lastKArecv = KAreply;
logger->log("VDR", Log::DEBUG, "Rxd correct KA reply");
}
}