mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 16:56:53 +08:00
refs #205, renamed to CNetworkUtils as more methods for IP-addresses are introduced, added CNetworkUtils::getKnownIpAddresses()
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
#include "vatsimbookingreader.h"
|
||||
#include "vatsimdatafilereader.h"
|
||||
|
||||
#include "blackmisc/networkchecks.h"
|
||||
#include "blackmisc/networkutils.h"
|
||||
#include "blackmisc/avatcstationlist.h"
|
||||
|
||||
#include <QtXml/QDomElement>
|
||||
@@ -128,7 +128,7 @@ namespace BlackCore
|
||||
else
|
||||
{
|
||||
QString msg;
|
||||
if (CNetworkChecks::canConnect(currentServer, msg, 2000))
|
||||
if (CNetworkUtils::canConnect(currentServer, msg, 2000))
|
||||
{
|
||||
INetwork::LoginMode mode = static_cast<INetwork::LoginMode>(loginMode);
|
||||
this->m_ownAircraft.setPilot(currentServer.getUser()); // still needed?
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "networkchecks.h"
|
||||
#include "networkutils.h"
|
||||
#include <QtNetwork/QNetworkInterface>
|
||||
#include <QtNetwork/QTcpSocket>
|
||||
#include <QCoreApplication>
|
||||
@@ -15,7 +15,7 @@ namespace BlackMisc
|
||||
/*
|
||||
* Connected interface?
|
||||
*/
|
||||
bool CNetworkChecks::hasConnectedInterface(bool withDebugOutput)
|
||||
bool CNetworkUtils::hasConnectedInterface(bool withDebugOutput)
|
||||
{
|
||||
// http://stackoverflow.com/questions/2475266/verfiying-the-network-connection-using-qt-4-4
|
||||
QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
|
||||
@@ -24,12 +24,11 @@ namespace BlackMisc
|
||||
for (int i = 0; i < interfaces.count(); i++)
|
||||
{
|
||||
QNetworkInterface iface = interfaces.at(i);
|
||||
|
||||
// details of connection
|
||||
if (withDebugOutput) qDebug() << "name:" << iface.name() << endl << "ip addresses:" << endl << "mac:" << iface.hardwareAddress() << endl;
|
||||
if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack))
|
||||
{
|
||||
|
||||
// details of connection
|
||||
if (withDebugOutput) qDebug() << "name:" << iface.name() << endl << "ip addresses:" << endl << "mac:" << iface.hardwareAddress() << endl;
|
||||
|
||||
// this loop is important
|
||||
for (int j = 0; j < iface.addressEntries().count(); j++)
|
||||
{
|
||||
@@ -48,12 +47,31 @@ namespace BlackMisc
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* my IP
|
||||
*/
|
||||
QStringList CNetworkUtils::getKnownIpAddresses()
|
||||
{
|
||||
QStringList ips;
|
||||
if (!CNetworkUtils::hasConnectedInterface(false)) return ips;
|
||||
foreach(const QHostAddress & address, QNetworkInterface::allAddresses())
|
||||
{
|
||||
if (address.isLoopback() || address.isNull()) continue;
|
||||
if (address.protocol() == QAbstractSocket::IPv4Protocol && address != QHostAddress(QHostAddress::LocalHost))
|
||||
{
|
||||
QString a = address.toString();
|
||||
if (CNetworkUtils::isValidIPv4Address(a)) ips.append(a);
|
||||
}
|
||||
}
|
||||
return ips;
|
||||
}
|
||||
|
||||
/*
|
||||
* Can connect to IP/port?
|
||||
*/
|
||||
bool CNetworkChecks::canConnect(const QString &hostAddress, quint16 port, QString &message, int timeoutMs)
|
||||
bool CNetworkUtils::canConnect(const QString &hostAddress, quint16 port, QString &message, int timeoutMs)
|
||||
{
|
||||
if (!CNetworkChecks::hasConnectedInterface(false))
|
||||
if (!CNetworkUtils::hasConnectedInterface(false))
|
||||
{
|
||||
message = QObject::tr("No connected network interface", "BlackMisc");
|
||||
return false;
|
||||
@@ -80,15 +98,15 @@ namespace BlackMisc
|
||||
/*
|
||||
* Can connect server?
|
||||
*/
|
||||
bool CNetworkChecks::canConnect(const Network::CServer &server, QString &message, int timeoutMs)
|
||||
bool CNetworkUtils::canConnect(const Network::CServer &server, QString &message, int timeoutMs)
|
||||
{
|
||||
return CNetworkChecks::canConnect(server.getAddress(), server.getPort(), message, timeoutMs);
|
||||
return CNetworkUtils::canConnect(server.getAddress(), server.getPort(), message, timeoutMs);
|
||||
}
|
||||
|
||||
/*
|
||||
* Valid IPv4 address
|
||||
*/
|
||||
bool CNetworkChecks::isValidIPv4Address(const QString &candidate)
|
||||
bool CNetworkUtils::isValidIPv4Address(const QString &candidate)
|
||||
{
|
||||
QHostAddress address(candidate);
|
||||
return (QAbstractSocket::IPv4Protocol == address.protocol());
|
||||
@@ -97,7 +115,7 @@ namespace BlackMisc
|
||||
/*
|
||||
* Valid IPv6 address
|
||||
*/
|
||||
bool CNetworkChecks::isValidIPv6Address(const QString &candidate)
|
||||
bool CNetworkUtils::isValidIPv6Address(const QString &candidate)
|
||||
{
|
||||
QHostAddress address(candidate);
|
||||
return (QAbstractSocket::IPv6Protocol == address.protocol());
|
||||
@@ -106,7 +124,7 @@ namespace BlackMisc
|
||||
/*
|
||||
* Valid port?
|
||||
*/
|
||||
bool CNetworkChecks::isValidPort(const QString &port)
|
||||
bool CNetworkUtils::isValidPort(const QString &port)
|
||||
{
|
||||
bool success;
|
||||
int p = port.toInt(&success);
|
||||
@@ -3,25 +3,23 @@
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#ifndef BLACKMISC_NETWORKCHECKS_H
|
||||
#define BLACKMISC_NETWORKCHECKS_H
|
||||
#ifndef BLACKMISC_NETWORKUTILS_H
|
||||
#define BLACKMISC_NETWORKUTILS_H
|
||||
|
||||
#include "nwserver.h"
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
/*!
|
||||
* \brief Utilities checking whether a network connection can be established
|
||||
* \brief Utilities, e.g. checking whether a network connection can be established
|
||||
*/
|
||||
class CNetworkChecks
|
||||
class CNetworkUtils
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
* \brief Constructor
|
||||
*/
|
||||
CNetworkChecks() {}
|
||||
// Constructor
|
||||
CNetworkUtils() {}
|
||||
|
||||
public:
|
||||
/*!
|
||||
@@ -50,13 +48,16 @@ namespace BlackMisc
|
||||
*/
|
||||
static bool canConnect(const BlackMisc::Network::CServer &server, QString &message, int timeoutMs = 1500);
|
||||
|
||||
//! \brief Valid IPv4 address
|
||||
//! Find out my IPv4 address, empty if not possible
|
||||
static QStringList getKnownIpAddresses();
|
||||
|
||||
//! Valid IPv4 address
|
||||
static bool isValidIPv4Address(const QString &candidate);
|
||||
|
||||
//! \brief Valid IPv6 address
|
||||
//! Valid IPv6 address
|
||||
static bool isValidIPv6Address(const QString &candidate);
|
||||
|
||||
//! \brief Valid port
|
||||
//! Valid port
|
||||
static bool isValidPort(const QString &port);
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user