mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-21 12:55:31 +08:00
refs #883, added utility function ping
This commit is contained in:
committed by
Mathew Sutcliffe
parent
1619a5ceb3
commit
92100c85f7
@@ -16,6 +16,7 @@
|
|||||||
#include <QHostAddress>
|
#include <QHostAddress>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
|
#include <QProcess>
|
||||||
#include <QNetworkAddressEntry>
|
#include <QNetworkAddressEntry>
|
||||||
#include <QNetworkInterface>
|
#include <QNetworkInterface>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
@@ -28,6 +29,7 @@
|
|||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
#include <QUrlQuery>
|
#include <QUrlQuery>
|
||||||
|
#include <QEventLoop>
|
||||||
#include <QVariant>
|
#include <QVariant>
|
||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
@@ -49,32 +51,43 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
// http://stackoverflow.com/questions/2475266/verfiying-the-network-connection-using-qt-4-4
|
// http://stackoverflow.com/questions/2475266/verfiying-the-network-connection-using-qt-4-4
|
||||||
const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
|
const QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
|
||||||
bool result = false;
|
for (const QNetworkInterface &interface : interfaces)
|
||||||
|
|
||||||
for (int i = 0; i < interfaces.count(); i++)
|
|
||||||
{
|
{
|
||||||
QNetworkInterface interface = interfaces.at(i);
|
|
||||||
|
|
||||||
// details of connection
|
// details of connection
|
||||||
if (withDebugOutput) qDebug() << "name:" << interface.name() << endl << "ip addresses:" << endl << "mac:" << interface.hardwareAddress() << endl;
|
if (withDebugOutput) qDebug() << "name:" << interface.name() << endl << "ip addresses:" << endl << "mac:" << interface.hardwareAddress() << endl;
|
||||||
if (interface.flags().testFlag(QNetworkInterface::IsUp) && !interface.flags().testFlag(QNetworkInterface::IsLoopBack))
|
if (interface.flags().testFlag(QNetworkInterface::IsUp) && !interface.flags().testFlag(QNetworkInterface::IsLoopBack))
|
||||||
{
|
{
|
||||||
// this loop is important
|
// this loop is important to check if there are addresses
|
||||||
for (int j = 0; j < interface.addressEntries().count(); j++)
|
for (const QNetworkAddressEntry &entry : interface.addressEntries())
|
||||||
{
|
{
|
||||||
if (withDebugOutput) qDebug() << interface.addressEntries().at(j).ip().toString() << " / " << interface.addressEntries().at(j).netmask().toString() << endl;
|
if (withDebugOutput) qDebug() << entry.ip().toString() << " / " << entry.netmask().toString() << endl;
|
||||||
|
|
||||||
// we have an interface that is up, and has an ip address, therefore the link is present
|
// we have an interface that is up, and has an ip address, therefore the link is present
|
||||||
// we will only enable this check on first positive, all later results are incorrect
|
// we will only enable this check on first positive, all later results are incorrect
|
||||||
if (!result)
|
return true;
|
||||||
{
|
|
||||||
result = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CNetworkUtils::canPing(const QString &hostAddress)
|
||||||
|
{
|
||||||
|
if (hostAddress.isEmpty()) { return false; }
|
||||||
|
QStringList params;
|
||||||
|
if (CBuildConfig::isRunningOnWindowsNtPlatform())
|
||||||
|
{
|
||||||
|
params << "-n" << "1";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// all UNIX alike
|
||||||
|
params << "-c" << "1";
|
||||||
|
}
|
||||||
|
params << hostAddress;
|
||||||
|
|
||||||
|
const int exitCode = QProcess::execute("ping", params);
|
||||||
|
return exitCode == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList CNetworkUtils::getKnownLocalIpV4Addresses()
|
QStringList CNetworkUtils::getKnownLocalIpV4Addresses()
|
||||||
@@ -93,6 +106,7 @@ namespace BlackMisc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ips.sort();
|
||||||
return ips;
|
return ips;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,6 +54,10 @@ namespace BlackMisc
|
|||||||
//! \param withDebugOutput enables some debugging output
|
//! \param withDebugOutput enables some debugging output
|
||||||
static bool hasConnectedInterface(bool withDebugOutput = false);
|
static bool hasConnectedInterface(bool withDebugOutput = false);
|
||||||
|
|
||||||
|
//! Can ping the address?
|
||||||
|
//! \note uses OS ping
|
||||||
|
static bool canPing(const QString &hostAddress);
|
||||||
|
|
||||||
//! Can connect?
|
//! Can connect?
|
||||||
//! \param hostAddress 130.4.20.3, or myserver.com
|
//! \param hostAddress 130.4.20.3, or myserver.com
|
||||||
//! \param port 80, 1234
|
//! \param port 80, 1234
|
||||||
|
|||||||
Reference in New Issue
Block a user