Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qhostinfo_p.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QHOSTINFO_P_H
5#define QHOSTINFO_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists for the convenience
12// of the QHostInfo class. This header file may change from
13// version to version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtNetwork/private/qtnetworkglobal_p.h>
19#include "QtCore/qcoreapplication.h"
20#include "private/qcoreapplication_p.h"
21#include "private/qmetaobject_p.h"
22#include "QtNetwork/qhostinfo.h"
23#include "QtCore/qmutex.h"
24#include "QtCore/qwaitcondition.h"
25#include "QtCore/qobject.h"
26#include "QtCore/qpointer.h"
27#include "QtCore/qthread.h"
28#if QT_CONFIG(thread)
29#include "QtCore/qthreadpool.h"
30#endif
31#include "QtCore/qrunnable.h"
32#include "QtCore/qlist.h"
33#include "QtCore/qqueue.h"
34#include <QElapsedTimer>
35#include <QCache>
36
37#include <atomic>
38
40
41
43{
45public:
46 explicit QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot);
47 ~QHostInfoResult() override;
48
49 void postResultsReady(const QHostInfo &info);
50
53
54private Q_SLOTS:
55 void finalizePostResultsReady(const QHostInfo &info);
56
57private:
59 : receiver(other->receiver.get() != other ? other->receiver.get() : this),
60 slotObj{std::move(other->slotObj)}
61 {
62 // cleanup if the application terminates before results are delivered
65 // maintain thread affinity
66 moveToThread(other->thread());
67 }
68
69 // receiver is either a QObject provided by the user,
70 // or it's set to `this` (to emulate the behavior of the contextless connect())
71 QPointer<const QObject> receiver = nullptr;
73};
74
76{
77public:
78 static QHostInfo fromName(const QString &hostName);
79 static QHostInfo lookup(const QString &hostName);
81};
82
84{
85public:
88 errorStr(QLatin1StringView(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error"))),
89 lookupId(0)
90 {
91 }
92 static int lookupHostImpl(const QString &name,
93 const QObject *receiver,
95 const char *member);
96
99 QList<QHostAddress> addrs;
102};
103
104// These functions are outside of the QHostInfo class and strictly internal.
105// Do NOT use them outside of QAbstractSocket.
106QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id);
109void Q_AUTOTEST_EXPORT qt_qhostinfo_cache_inject(const QString &hostname, const QHostInfo &resolution);
110
112{
113public:
115 const int max_age; // seconds
116
117 QHostInfo get(const QString &name, bool *valid);
118 void put(const QString &name, const QHostInfo &info);
119 void clear();
120
121 bool isEnabled() { return enabled.load(std::memory_order_relaxed); }
122 // this function is currently only used for the auto tests
123 // and not usable by public API
124 void setEnabled(bool e) { enabled.store(e, std::memory_order_relaxed); }
125private:
126 std::atomic<bool> enabled;
127 struct QHostInfoCacheElement {
128 QHostInfo info;
129 QElapsedTimer age;
130 };
131 QCache<QString,QHostInfoCacheElement> cache;
132 QMutex mutex;
133};
134
135// the following classes are used for the (normal) case: We use multiple threads to lookup DNS
136
138{
139public:
140 explicit QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
143
144 void run() override;
145
147 int id;
149};
150
151
153{
154public:
157
158 void clear();
159
160 // called from QHostInfo
162 void abortLookup(int id);
163
164 // called from QHostInfoRunnable
166 bool wasAborted(int id);
167
169
170 friend class QHostInfoRunnable;
171protected:
172#if QT_CONFIG(thread)
173 QList<QHostInfoRunnable*> currentLookups; // in progress
174 QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
175#endif
176 QQueue<QHostInfoRunnable*> scheduledLookups; // not yet started
177 QList<QHostInfoRunnable*> finishedLookups; // recently finished
178 QList<int> abortedLookups; // ids of aborted lookups
179
180#if QT_CONFIG(thread)
181 QThreadPool threadPool;
182#endif
184
186
187private:
188 void rescheduleWithMutexHeld();
189};
190
192
193#endif // QHOSTINFO_P_H
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
void aboutToQuit(QPrivateSignal)
This signal is emitted when the application is about to quit the main event loop, e....
\inmodule QtCore
The QHostAddress class provides an IP address.
static QHostInfo lookup(const QString &hostName)
static QHostInfo reverseLookup(const QHostAddress &address)
static QHostInfo fromName(const QString &hostName)
void setEnabled(bool e)
QHostInfo get(const QString &name, bool *valid)
const int max_age
void put(const QString &name, const QHostInfo &info)
QQueue< QHostInfoRunnable * > scheduledLookups
void lookupFinished(QHostInfoRunnable *r)
void scheduleLookup(QHostInfoRunnable *r)
QList< int > abortedLookups
QHostInfoCache cache
QList< QHostInfoRunnable * > finishedLookups
static int lookupHostImpl(const QString &name, const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj, const char *member)
QList< QHostAddress > addrs
Definition qhostinfo_p.h:99
QHostInfo::HostInfoError err
Definition qhostinfo_p.h:97
void resultsReady(const QHostInfo &info)
QHostInfoResult(const QObject *receiver, QtPrivate::SlotObjUniquePtr slot)
Definition qhostinfo.cpp:75
void postResultsReady(const QHostInfo &info)
Definition qhostinfo.cpp:96
~QHostInfoResult() override
void run() override
Implement this pure virtual function in your subclass.
QHostInfoRunnable(const QString &hn, int i, const QObject *receiver, QtPrivate::SlotObjUniquePtr slotObj)
QHostInfoResult resultEmitter
~QHostInfoRunnable() override
The QHostInfo class provides static functions for host name lookups.
Definition qhostinfo.h:19
HostInfoError
This enum describes the various errors that can occur when trying to resolve a host name.
Definition qhostinfo.h:22
\inmodule QtCore
Definition qmutex.h:281
\inmodule QtCore
Definition qobject.h:103
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
bool moveToThread(QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
Changes the thread affinity for this object and its children and returns true on success.
Definition qobject.cpp:1643
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
\inmodule QtCore
Definition qrunnable.h:18
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qthreadpool.h:22
#define this
Definition dialogs.cpp:9
Combined button and popup list for selecting options.
std::unique_ptr< QSlotObjectBase, QSlotObjectBase::Deleter > SlotObjUniquePtr
static QDBusError::ErrorType get(const char *name)
void Q_AUTOTEST_EXPORT qt_qhostinfo_clear_cache()
void Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(bool e)
QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id)
void Q_AUTOTEST_EXPORT qt_qhostinfo_cache_inject(const QString &hostname, const QHostInfo &resolution)
GLboolean r
[2]
GLenum GLenum GLsizei const GLuint GLboolean enabled
GLuint name
GLuint GLuint64EXT address
@ NoError
Definition main.cpp:34
#define Q_AUTOTEST_EXPORT
#define Q_OBJECT
#define Q_SLOTS
#define Q_SIGNALS
#define QT_TRANSLATE_NOOP(scope, x)
QSharedPointer< T > other(t)
[5]
QHostInfo info
[0]