From c1a82ad60e1b6737fe39c0d6422bbf8c0b608a37 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Thu, 30 Mar 2017 00:50:53 +0100 Subject: [PATCH] Fixed undescriptive error message generated by CNetworkUtils::canConnect. --- src/blackcore/context/contextnetworkimpl.cpp | 2 +- src/blackmisc/network/networkutils.cpp | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/blackcore/context/contextnetworkimpl.cpp b/src/blackcore/context/contextnetworkimpl.cpp index a328b0432..77b88b1b0 100644 --- a/src/blackcore/context/contextnetworkimpl.cpp +++ b/src/blackcore/context/contextnetworkimpl.cpp @@ -164,7 +164,7 @@ namespace BlackCore { return CStatusMessage({ CLogCategory::validation() }, CStatusMessage::SeverityError, "Invalid ICAO data for own aircraft"); } - else if (!CNetworkUtils::canConnect(server, msg, 2000)) + else if (!CNetworkUtils::canConnect(server, msg, 5000)) { return CStatusMessage({ CLogCategory::validation() }, CStatusMessage::SeverityError, msg); } diff --git a/src/blackmisc/network/networkutils.cpp b/src/blackmisc/network/networkutils.cpp index 81fd242c4..c89e2c08a 100644 --- a/src/blackmisc/network/networkutils.cpp +++ b/src/blackmisc/network/networkutils.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -88,20 +89,26 @@ namespace BlackMisc bool CNetworkUtils::canConnect(const QString &hostAddress, int port, QString &message, int timeoutMs) { QTcpSocket socket; - bool connected = CEventLoop::processEventsUntil(&socket, &QTcpSocket::connected, timeoutMs, [&] + QSignalMapper mapper; + QObject::connect(&socket, &QTcpSocket::connected, &mapper, QOverload<>::of(&QSignalMapper::map)); + QObject::connect(&socket, QOverload::of(&QTcpSocket::error), &mapper, QOverload<>::of(&QSignalMapper::map)); + mapper.setMapping(&socket, 0); + const bool timedOut = !CEventLoop::processEventsUntil(&mapper, QOverload::of(&QSignalMapper::mapped), timeoutMs, [&] { socket.connectToHost(hostAddress, static_cast(port)); }); - if (!connected) + if (socket.state() != QTcpSocket::ConnectedState) { - message = QObject::tr("Connection failed : %1", "BlackMisc").arg(socket.errorString()); + const QString error = timedOut ? QObject::tr("Timed out", "BlackMisc") : socket.errorString(); + message = QObject::tr("Connection failed : %1", "BlackMisc").arg(error); + return false; } else { message = QObject::tr("OK, connected", "BlackMisc"); + return true; } - return connected; } bool CNetworkUtils::canConnect(const Network::CServer &server, QString &message, int timeoutMs)