Qt 6.x
The Qt SDK
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:
47 : receiver(receiver), slotObj(slotObj),
48 withContextObject(slotObj && receiver)
49 {
50 if (receiver)
51 moveToThread(receiver->thread());
52 }
53
54 void postResultsReady(const QHostInfo &info);
55
58
59protected:
60 bool event(QEvent *event) override;
61
62private:
64 : receiver(other->receiver), slotObj(other->slotObj),
65 withContextObject(other->withContextObject)
66 {
67 // cleanup if the application terminates before results are delivered
70 // maintain thread affinity
71 moveToThread(other->thread());
72 }
73
74 QPointer<const QObject> receiver = nullptr;
75 QtPrivate::QSlotObjectBase *slotObj = nullptr;
76 const bool withContextObject = false;
77};
78
80{
81public:
82 static QHostInfo fromName(const QString &hostName);
83 static QHostInfo lookup(const QString &hostName);
85};
86
88{
89public:
92 errorStr(QLatin1StringView(QT_TRANSLATE_NOOP("QHostInfo", "Unknown error"))),
93 lookupId(0)
94 {
95 }
96 static int lookupHostImpl(const QString &name,
97 const QObject *receiver,
99 const char *member);
100
106};
107
108// These functions are outside of the QHostInfo class and strictly internal.
109// Do NOT use them outside of QAbstractSocket.
110QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id);
113void Q_AUTOTEST_EXPORT qt_qhostinfo_cache_inject(const QString &hostname, const QHostInfo &resolution);
114
116{
117public:
119 const int max_age; // seconds
120
121 QHostInfo get(const QString &name, bool *valid);
122 void put(const QString &name, const QHostInfo &info);
123 void clear();
124
125 bool isEnabled() { return enabled.load(std::memory_order_relaxed); }
126 // this function is currently only used for the auto tests
127 // and not usable by public API
128 void setEnabled(bool e) { enabled.store(e, std::memory_order_relaxed); }
129private:
130 std::atomic<bool> enabled;
131 struct QHostInfoCacheElement {
133 QElapsedTimer age;
134 };
136 QMutex mutex;
137};
138
139// the following classes are used for the (normal) case: We use multiple threads to lookup DNS
140
142{
143public:
144 QHostInfoRunnable(const QString &hn, int i, const QObject *receiver,
146 void run() override;
147
149 int id;
151};
152
153
155{
156public:
159
160 void clear();
161
162 // called from QHostInfo
164 void abortLookup(int id);
165
166 // called from QHostInfoRunnable
168 bool wasAborted(int id);
169
171
172 friend class QHostInfoRunnable;
173protected:
174#if QT_CONFIG(thread)
175 QList<QHostInfoRunnable*> currentLookups; // in progress
176 QList<QHostInfoRunnable*> postponedLookups; // postponed because in progress for same host
177#endif
180 QList<int> abortedLookups; // ids of aborted lookups
181
182#if QT_CONFIG(thread)
183 QThreadPool threadPool;
184#endif
186
188
189private:
190 void rescheduleWithMutexHeld();
191};
192
194
195#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
\inmodule QtCore
Definition qcoreevent.h:45
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)
void abortLookup(int id)
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
QHostInfo::HostInfoError err
void resultsReady(const QHostInfo &info)
void postResultsReady(const QHostInfo &info)
Definition qhostinfo.cpp:86
QHostInfoResult(const QObject *receiver, QtPrivate::QSlotObjectBase *slotObj)
Definition qhostinfo_p.h:46
void run() override
Implement this pure virtual function in your subclass.
QHostInfoResult resultEmitter
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:21
Definition qlist.h:74
\inmodule QtCore
Definition qmutex.h:285
\inmodule QtCore
Definition qobject.h:90
void moveToThread(QThread *thread)
Changes the thread affinity for this object and its children.
Definition qobject.cpp:1606
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2823
QThread * thread() const
Returns the thread in which the object lives.
Definition qobject.cpp:1561
void deleteLater()
\threadsafe
Definition qobject.cpp:2352
\inmodule QtCore
Definition qpointer.h:18
\inmodule QtCore
Definition qqueue.h:14
\inmodule QtCore
Definition qrunnable.h:18
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qthreadpool.h:20
double e
Combined button and popup list for selecting options.
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
struct _cl_event * event
GLuint GLuint64EXT address
@ NoError
Definition main.cpp:34
#define Q_AUTOTEST_EXPORT
#define Q_OBJECT
#define Q_SIGNALS
#define QT_TRANSLATE_NOOP(scope, x)
QFileInfo info(fileName)
[8]
QSharedPointer< T > other(t)
[5]