From 127fb62b372386f8bd916df3c73d341a962f37c6 Mon Sep 17 00:00:00 2001 From: Chris Tallon Date: Fri, 17 Sep 2021 16:52:29 +0100 Subject: [PATCH] Improve connection failure handling --- tcp.cc | 14 ++++++++++++-- vconnect.cc | 18 +++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/tcp.cc b/tcp.cc index 38d5700..ddf955b 100644 --- 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); diff --git a/vconnect.cc b/vconnect.cc index d416358..2929934 100644 --- a/vconnect.cc +++ b/vconnect.cc @@ -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 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 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"); -- 2.39.5