]> git.vomp.tv Git - vompclient.git/commitdiff
Improve connection failure handling
authorChris Tallon <chris@vomp.tv>
Fri, 17 Sep 2021 15:52:29 +0000 (16:52 +0100)
committerChris Tallon <chris@vomp.tv>
Fri, 17 Sep 2021 15:52:29 +0000 (16:52 +0100)
tcp.cc
vconnect.cc

diff --git a/tcp.cc b/tcp.cc
index 38d570032b6d9f1664ec6955d7255416f6f08d42..ddf955b6f934f169e248ab8ba1f3294a4aeae771 100644 (file)
--- a/tcp.cc
+++ b/tcp.cc
@@ -183,6 +183,7 @@ bool TCP::waitForConnect(int connectResult)
 
   fd_set readfds;
   FD_ZERO(&readfds);
+  FD_SET(sockfd, &readfds);
 
 
 #ifdef WIN32
@@ -193,7 +194,7 @@ bool TCP::waitForConnect(int connectResult)
 #endif
 
   struct timeval tv;
-  tv.tv_sec = 20; // Allow 5s for a connect
+  tv.tv_sec = 10; // Allow 10s for a connect
   tv.tv_usec = 0;
 
   int selectResult = select(maxfd + 1, &readfds, &writefds, NULL, &tv);
@@ -209,12 +210,21 @@ bool TCP::waitForConnect(int connectResult)
     return false;
   }
 
-  if ((selectResult == 1) || FD_ISSET(sockfd, &writefds))
+  logger->trace(TAG, "waitForConnect(): selectResult = {}", selectResult);
+
+  if ((selectResult == 1) && FD_ISSET(sockfd, &writefds))
   {
     logger->info(TAG, "Connected");
     connected = true;
     return true;
   }
+  else if ((selectResult == 2) && FD_ISSET(sockfd, &readfds) && FD_ISSET(sockfd, &writefds))
+  {
+    CLOSESOCKET(sockfd);
+    sockfd = -1;
+    logger->crit(TAG, "Detected connection failed");
+    return false;
+  }
   else
   {
     CLOSESOCKET(sockfd);
index d416358711af1c08462355b4e9f97e97e6545b91..292993464f1c432f884e10ba16347e2940fd58df 100644 (file)
@@ -251,8 +251,11 @@ bool VConnect::attemptConnect(const VDRServer* vdrServer)
     setOneLiner(tr("Connection failed"));
     draw();
     boxstack->update(this);
-    if (threadReqQuit) return false;
-    MILLISLEEP(3000); // FIXME wait on cond?
+    {
+      std::unique_lock<std::mutex> ul(threadMutex);
+      if (threadReqQuit) return false;
+      threadCond.wait_for(ul, std::chrono::milliseconds(3000), [this]{ return threadReqQuit; });
+    }
     return false;
   }
 
@@ -279,14 +282,19 @@ bool VConnect::attemptConnect(const VDRServer* vdrServer)
 
     draw();
     boxstack->update(this);
-    if (threadReqQuit) return false;
-    MILLISLEEP(3000); // FIXME wait on cond?
+
+    {
+      std::unique_lock<std::mutex> ul(threadMutex);
+      if (threadReqQuit) return false;
+      threadCond.wait_for(ul, std::chrono::milliseconds(3000), [this]{ return threadReqQuit; });
+    }
+
     return false;
   }
 
   logger->debug(TAG, "VDR login ok");
 
-  Config::getInstance()->set("subtitles", "default", subtitles);
+  Config::getInstance()->set("subtitles", "default", subtitles); // FIXME move this directly into vdr.cc ?
   Wol::getInstance()->setWakeUpIP(vdrServer->ip.c_str());
 
   logger->info(TAG, "Send VDR connected message");