Use CEventLoop to process events until a signal is received

refs #869
This commit is contained in:
Roland Winklmeier
2017-02-17 18:10:01 +01:00
committed by Mathew Sutcliffe
parent 63f625fe4e
commit 66cf96d47f
2 changed files with 15 additions and 28 deletions

View File

@@ -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"); }

View File

@@ -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<quint16>(port), QTcpSocket::ReadOnly);
socketState = socket.state();
bool connected = (socketState == QTcpSocket::ConnectedState);
bool connected = CEventLoop::processEventsUntil(&socket, &QTcpSocket::connected, timeoutMs, [&]
{
socket.connectToHost(hostAddress, static_cast<quint16>(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;
}