-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from hasselmm/bugfix/core-cleanups
Various cleanups to QncCore
- Loading branch information
Showing
30 changed files
with
317 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
qnc_add_library( | ||
QncCore STATIC | ||
|
||
abstractresolver.cpp | ||
abstractresolver.h | ||
compat.h | ||
literals.cpp | ||
literals.h | ||
multicastresolver.cpp | ||
multicastresolver.h | ||
parse.cpp | ||
parse.h | ||
) | ||
|
||
target_link_libraries(QncCore PUBLIC Qt::Network) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* QtNetworkCrumbs - Some networking toys for Qt | ||
* Copyright (C) 2019-2024 Mathias Hasselmann | ||
*/ | ||
#include "abstractresolver.h" | ||
|
||
// Qt headers | ||
#include <QLoggingCategory> | ||
#include <QNetworkInterface> | ||
#include <QTimer> | ||
#include <QUdpSocket> | ||
|
||
namespace qnc::core { | ||
|
||
namespace { | ||
|
||
using namespace std::chrono_literals; | ||
|
||
Q_LOGGING_CATEGORY(lcResolver, "qnc.core.resolver") | ||
|
||
} // namespace | ||
|
||
AbstractResolver::AbstractResolver(QObject *parent) | ||
: QObject{parent} | ||
, m_timer{new QTimer{this}} | ||
{ | ||
m_timer->callOnTimeout(this, &AbstractResolver::onTimeout); | ||
QTimer::singleShot(0, this, &AbstractResolver::onTimeout); | ||
m_timer->start(15s); | ||
} | ||
|
||
void AbstractResolver::setScanInterval(std::chrono::milliseconds ms) | ||
{ | ||
if (scanIntervalAsDuration() != ms) { | ||
m_timer->setInterval(ms); | ||
emit scanIntervalChanged(scanInterval()); | ||
} | ||
} | ||
|
||
void AbstractResolver::setScanInterval(int ms) | ||
{ | ||
if (scanInterval() != ms) { | ||
m_timer->setInterval(ms); | ||
emit scanIntervalChanged(scanInterval()); | ||
} | ||
} | ||
|
||
std::chrono::milliseconds AbstractResolver::scanIntervalAsDuration() const | ||
{ | ||
return m_timer->intervalAsDuration(); | ||
} | ||
|
||
int AbstractResolver::scanInterval() const | ||
{ | ||
return m_timer->interval(); | ||
} | ||
|
||
AbstractResolver::SocketPointer | ||
AbstractResolver::socketForAddress(const QHostAddress &address) const | ||
{ | ||
return m_sockets[address]; | ||
} | ||
|
||
void AbstractResolver::scanNetworkInterfaces() | ||
{ | ||
auto newSockets = SocketTable{}; | ||
|
||
const auto &allInterfaces = QNetworkInterface::allInterfaces(); | ||
|
||
for (const auto &iface : allInterfaces) { | ||
if (!isSupportedInterface(iface)) | ||
continue; | ||
|
||
const auto addressEntries = iface.addressEntries(); | ||
for (const auto &entry : addressEntries) { | ||
if (!isSupportedAddress(entry.ip())) | ||
continue; | ||
|
||
if (const auto socket = socketForAddress(entry.ip())) { | ||
newSockets.insert(entry.ip(), socket); | ||
continue; | ||
} | ||
|
||
qCInfo(lcResolver, "Creating socket for %ls on %ls", | ||
qUtf16Printable(entry.ip().toString()), | ||
qUtf16Printable(iface.humanReadableName())); | ||
|
||
if (const auto socket = createSocket(iface, entry.ip())) | ||
newSockets.insert(entry.ip(), socket); | ||
} | ||
} | ||
|
||
std::exchange(m_sockets, newSockets); | ||
} | ||
|
||
void AbstractResolver::onTimeout() | ||
{ | ||
scanNetworkInterfaces(); | ||
submitQueries(m_sockets); | ||
} | ||
|
||
} // namespace qnc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* QtNetworkCrumbs - Some networking toys for Qt | ||
* Copyright (C) 2019-2024 Mathias Hasselmann | ||
*/ | ||
#ifndef QNCCORE_ABSTRACTRESOLVER_H | ||
#define QNCCORE_ABSTRACTRESOLVER_H | ||
|
||
#include <QAbstractSocket> | ||
#include <QHostAddress> | ||
|
||
class QNetworkInterface; | ||
class QTimer; | ||
|
||
namespace qnc::core { | ||
|
||
class AbstractResolver : public QObject | ||
{ | ||
Q_OBJECT | ||
Q_PROPERTY(int scanInterval READ scanInterval WRITE setScanInterval NOTIFY scanIntervalChanged FINAL) | ||
|
||
public: | ||
AbstractResolver(QObject *parent = nullptr); | ||
|
||
[[nodiscard]] int scanInterval() const; | ||
[[nodiscard]] std::chrono::milliseconds scanIntervalAsDuration() const; | ||
void setScanInterval(std::chrono::milliseconds ms); | ||
|
||
public slots: | ||
void setScanInterval(int ms); | ||
|
||
signals: | ||
void scanIntervalChanged(int interval); | ||
|
||
protected: | ||
using SocketPointer = std::shared_ptr<QAbstractSocket>; | ||
using SocketTable = QHash<QHostAddress, SocketPointer>; | ||
|
||
[[nodiscard]] virtual bool isSupportedInterface(const QNetworkInterface &iface) const = 0; | ||
[[nodiscard]] virtual bool isSupportedAddress(const QHostAddress &address) const = 0; | ||
|
||
[[nodiscard]] virtual SocketPointer createSocket(const QNetworkInterface &iface, | ||
const QHostAddress &address) = 0; | ||
|
||
[[nodiscard]] SocketPointer socketForAddress(const QHostAddress &address) const; | ||
|
||
virtual void submitQueries(const SocketTable &sockets) = 0; | ||
|
||
private: | ||
void onTimeout(); | ||
void scanNetworkInterfaces(); | ||
|
||
QTimer *const m_timer; | ||
SocketTable m_sockets; | ||
}; | ||
|
||
} // namespace qnc::core | ||
|
||
#endif // QNCCORE_ABSTRACTRESOLVER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* QtNetworkCrumbs - Some networking toys for Qt | ||
* Copyright (C) 2019-2024 Mathias Hasselmann | ||
*/ | ||
#ifndef QNCCORE_COMPAT_H | ||
#define QNCCORE_COMPAT_H | ||
|
||
#include <QtGlobal> | ||
|
||
#if QT_VERSION_MAJOR < 6 | ||
|
||
#define Q_IMPLICIT /* implicit */ | ||
|
||
namespace qnc::core { | ||
|
||
template <typename T> | ||
constexpr std::underlying_type_t<T> qToUnderlying(T value) noexcept | ||
{ | ||
return static_cast<std::underlying_type_t<T>>(value); | ||
} | ||
|
||
} // namespace qnc::core | ||
|
||
using qnc::core::qToUnderlying; | ||
|
||
#endif | ||
|
||
#endif // QNCCORE_COMPAT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
/* QtNetworkCrumbs - Some networking toys for Qt | ||
* Copyright (C) 2023 Mathias Hasselmann | ||
* Copyright (C) 2019-2024 Mathias Hasselmann | ||
*/ | ||
#include "qncliterals.h" | ||
#include "literals.h" | ||
|
||
namespace qnc { | ||
namespace qnc::core { | ||
static_assert('*'_L1.unicode() == 42); | ||
static_assert("constexpr"_L1.size() == 9); | ||
} // namespace qnc | ||
} // namespace qnc::core |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.