diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index 2e594715c..e9c3dd93d 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -20,6 +20,7 @@ #include "blackmisc/datacache.h" #include "blackmisc/dbusserver.h" #include "blackmisc/directoryutils.h" +#include "blackmisc/eventloop.h" #include "blackmisc/filelogger.h" #include "blackmisc/logcategory.h" #include "blackmisc/logcategorylist.h" @@ -349,13 +350,10 @@ namespace BlackCore CStatusMessageList CApplication::waitForSetup() { if (!this->m_setupReader) { return CStatusMessage(this).error("No setup reader"); } - if (!this->m_setupReader->isSetupAvailable()) + CEventLoop::processEventsUntil(this, &CApplication::setupHandlingCompleted, 5000, [this] { - QEventLoop eventLoop; - QTimer::singleShot(5000, &eventLoop, &QEventLoop::quit); - connect(this, &CApplication::setupHandlingCompleted, &eventLoop, &QEventLoop::quit); - eventLoop.exec(); - } + return this->m_setupReader->isSetupAvailable(); + }); // setup handling completed with success or failure, or we run into time out if (this->m_setupReader->isSetupAvailable()) { return CStatusMessage(this).info("Setup available"); } diff --git a/src/blackmisc/network/networkutils.cpp b/src/blackmisc/network/networkutils.cpp index 10892e963..81fd242c4 100644 --- a/src/blackmisc/network/networkutils.cpp +++ b/src/blackmisc/network/networkutils.cpp @@ -7,6 +7,7 @@ * contained in the LICENSE file. */ +#include "blackmisc/eventloop.h" #include "blackmisc/network/networkutils.h" #include "blackmisc/network/server.h" #include "blackconfig/buildconfig.h" @@ -86,32 +87,20 @@ namespace BlackMisc bool CNetworkUtils::canConnect(const QString &hostAddress, int port, QString &message, int timeoutMs) { - // KB: I have had an issue with QTcpSocket. It was stuck in HostLookupState and did - // only recover after a reboot for no obvious reason. - // Currently trying the ping alternative - bool ping = CNetworkUtils::canPing(hostAddress); - if (ping) { return true; } - - // http://qt-project.org/forums/viewthread/9346 - // socket.waitForConnected() unrelaiable under Windows, see Qt docu QTcpSocket socket; - QTcpSocket::SocketState socketState; - socket.connectToHost(hostAddress, static_cast(port), QTcpSocket::ReadOnly); - socketState = socket.state(); - bool connected = (socketState == QTcpSocket::ConnectedState); + bool connected = CEventLoop::processEventsUntil(&socket, &QTcpSocket::connected, timeoutMs, [&] + { + socket.connectToHost(hostAddress, static_cast(port)); + }); + if (!connected) { - QEventLoop eventLoop; - QObject::connect(&socket, &QTcpSocket::connected, &eventLoop, &QEventLoop::quit); - QTimer::singleShot(timeoutMs, &eventLoop, &QEventLoop::quit); - eventLoop.exec(); + message = QObject::tr("Connection failed : %1", "BlackMisc").arg(socket.errorString()); + } + else + { + message = QObject::tr("OK, connected", "BlackMisc"); } - socketState = socket.state(); - connected = (socketState == QTcpSocket::ConnectedState); - message = connected ? - QObject::tr("OK, connected", "BlackMisc") : - QObject::tr("Connection failed : '%1'", "BlackMisc").arg(socket.errorString()); - socket.close(); return connected; }