Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
// Qt-Security score:significant reason:default
4
5
#
ifndef
QHOSTINFO_P_H
6
#
define
QHOSTINFO_P_H
7
8
//
9
// W A R N I N G
10
// -------------
11
//
12
// This file is not part of the Qt API. It exists for the convenience
13
// of the QHostInfo class. This header file may change from
14
// version to version without notice, or even be removed.
15
//
16
// We mean it.
17
//
18
19
#
include
<
QtNetwork
/
private
/
qtnetworkglobal_p
.
h
>
20
#
include
"QtCore/qcoreapplication.h"
21
#
include
"private/qcoreapplication_p.h"
22
#
include
"private/qmetaobject_p.h"
23
#
include
"QtNetwork/qhostinfo.h"
24
#
include
"QtCore/qmutex.h"
25
#
include
"QtCore/qwaitcondition.h"
26
#
include
"QtCore/qobject.h"
27
#
include
"QtCore/qpointer.h"
28
#
include
"QtCore/qthread.h"
29
#
if
QT_CONFIG
(
thread
)
30
#
include
"QtCore/qthreadpool.h"
31
#
endif
32
#
include
"QtCore/qrunnable.h"
33
#
include
"QtCore/qlist.h"
34
#
include
"QtCore/qqueue.h"
35
#
include
<
QElapsedTimer
>
36
#
include
<
QCache
>
37
38
#
include
<
atomic
>
39
#
include
<
memory
>
40
41
QT_BEGIN_NAMESPACE
42
43
44
class
QHostInfoResult :
public
QObject
45
{
46
Q_OBJECT
47
public
:
48
explicit
QHostInfoResult(
const
QObject *receiver, QtPrivate::SlotObjUniquePtr slot);
49
~QHostInfoResult() override;
50
51
void
postResultsReady(
const
QHostInfo &info);
52
53
Q_SIGNALS:
54
void
resultsReady(
const
QHostInfo &info);
55
56
private
Q_SLOTS:
57
void
finalizePostResultsReady(
const
QHostInfo &info);
58
59
private
:
60
QHostInfoResult(QHostInfoResult *other)
61
: receiver(other->receiver.get() != other ? other->receiver.get() :
this
),
62
slotObj{std::move(other->slotObj)}
63
{
64
// cleanup if the application terminates before results are delivered
65
connect(QCoreApplication::instance(), &QCoreApplication::aboutToQuit,
66
this
, &QObject::deleteLater);
67
// maintain thread affinity
68
moveToThread(other->thread());
69
}
70
71
// receiver is either a QObject provided by the user,
72
// or it's set to `this` (to emulate the behavior of the contextless connect())
73
QPointer<
const
QObject> receiver =
nullptr
;
74
QtPrivate::SlotObjUniquePtr slotObj;
75
};
76
77
class
QHostInfoAgent
78
{
79
public
:
80
static
QHostInfo
fromName
(
const
QString &hostName);
81
static
QHostInfo
lookup
(
const
QString &hostName);
82
static
QHostInfo
reverseLookup
(
const
QHostAddress &address);
83
};
84
85
class
QHostInfoPrivate
86
{
87
public
:
88
inline
QHostInfoPrivate
()
89
:
err
(
QHostInfo
::
NoError
),
90
errorStr
(
QLatin1StringView
(
QT_TRANSLATE_NOOP
(
"QHostInfo"
,
"Unknown error"
))),
91
lookupId
(0)
92
{
93
}
94
static
int
lookupHostImpl(
const
QString &name,
95
const
QObject *receiver,
96
QtPrivate::QSlotObjectBase *slotObj,
97
const
char
*member);
98
99
QHostInfo
::
HostInfoError
err
;
100
QString
errorStr
;
101
QList
<
QHostAddress
>
addrs
;
102
QString
hostName
;
103
int
lookupId
;
104
};
105
106
// These functions are outside of the QHostInfo class and strictly internal.
107
// Do NOT use them outside of QAbstractSocket.
108
QHostInfo Q_NETWORK_EXPORT qt_qhostinfo_lookup(
const
QString &name, QObject *receiver,
const
char
*member,
bool
*valid,
int
*id);
109
void
Q_AUTOTEST_EXPORT qt_qhostinfo_clear_cache();
110
void
Q_AUTOTEST_EXPORT qt_qhostinfo_enable_cache(
bool
e);
111
void
Q_AUTOTEST_EXPORT qt_qhostinfo_cache_inject(
const
QString &hostname,
const
QHostInfo &resolution);
112
113
class
QHostInfoCache
114
{
115
public
:
116
#
if
defined
(
QT_BUILD_INTERNAL
)
||
QT_CONFIG
(
hostinfocache
)
117
// unit tests require the ability to manipulate the cache
118
QHostInfoCache
();
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
); }
129
private
:
130
std
::
atomic
<
bool
>
enabled
;
131
struct
QHostInfoCacheElement
{
132
QHostInfo
info
;
133
QElapsedTimer
age
;
134
};
135
QCache
<
QString
,
QHostInfoCacheElement
>
cache
;
136
QMutex
mutex
;
137
#
else
138
QHostInfo
get
(
const
QString &,
bool
*valid) { *valid =
false
;
return
QHostInfo(); }
139
void
put
(
const
QString &,
const
QHostInfo &) { }
140
void
clear
() {}
141
bool
isEnabled
() {
return
false
; }
142
void
setEnabled
(
bool
) { }
143
#
endif
144
};
145
146
// the following classes are used for the (normal) case: We use multiple threads to lookup DNS
147
148
class
QHostInfoRunnable
:
public
QRunnable
149
{
150
public
:
151
explicit
QHostInfoRunnable(
const
QString &hn,
int
i,
const
QObject *receiver,
152
QtPrivate::SlotObjUniquePtr slotObj);
153
~
QHostInfoRunnable
()
override
;
154
155
void
run
()
override
;
156
157
QString
toBeLookedUp
;
158
int
id
;
159
std
::
unique_ptr
<
QHostInfoResult
>
resultEmitter
;
160
};
161
162
163
class
QHostInfoLookupManager
164
{
165
public
:
166
QHostInfoLookupManager
();
167
~
QHostInfoLookupManager
();
168
169
void
clear
();
170
171
// called from QHostInfo
172
void
scheduleLookup
(
QHostInfoRunnable
*r);
173
void
abortLookup
(
int
id);
174
175
// called from QHostInfoRunnable
176
void
lookupFinished
(
QHostInfoRunnable
*r);
177
bool
wasAborted
(
int
id);
178
179
#
if
__has_attribute
(
no_unique_address
)
&&
__cplusplus
>=
201803L
180
[[
no_unique_address
]]
181
#
endif
182
QHostInfoCache
cache
;
183
184
friend
class
QHostInfoRunnable
;
185
protected
:
186
#
if
QT_CONFIG
(
thread
)
187
QList
<
QHostInfoRunnable
*>
currentLookups
;
// in progress
188
QList
<
QHostInfoRunnable
*>
postponedLookups
;
// postponed because in progress for same host
189
#
endif
190
QQueue
<
QHostInfoRunnable
*>
scheduledLookups
;
// not yet started
191
QList
<
QHostInfoRunnable
*>
finishedLookups
;
// recently finished
192
QList
<
int
>
abortedLookups
;
// ids of aborted lookups
193
194
#
if
QT_CONFIG
(
thread
)
195
QThreadPool
threadPool
;
196
#
endif
197
QMutex
mutex
;
198
199
bool
wasDeleted
;
200
201
private
:
202
void
rescheduleWithMutexHeld();
203
};
204
205
QT_END_NAMESPACE
206
207
#
endif
// QHOSTINFO_P_H
QHostInfoAgent
Definition
qhostinfo_p.h:78
QHostInfoAgent::lookup
static QHostInfo lookup(const QString &hostName)
Definition
qhostinfo.cpp:395
QHostInfoAgent::reverseLookup
static QHostInfo reverseLookup(const QHostAddress &address)
Definition
qhostinfo.cpp:359
QHostInfoAgent::fromName
static QHostInfo fromName(const QString &hostName)
Definition
qhostinfo_unix.cpp:79
QHostInfoCache
Definition
qhostinfo_p.h:114
QHostInfoCache::clear
void clear()
Definition
qhostinfo_p.h:140
QHostInfoCache::setEnabled
void setEnabled(bool)
Definition
qhostinfo_p.h:142
QHostInfoCache::get
QHostInfo get(const QString &, bool *valid)
Definition
qhostinfo_p.h:138
QHostInfoCache::isEnabled
bool isEnabled()
Definition
qhostinfo_p.h:141
QHostInfoCache::put
void put(const QString &, const QHostInfo &)
Definition
qhostinfo_p.h:139
QHostInfoLookupManager
Definition
qhostinfo_p.h:164
QHostInfoLookupManager::~QHostInfoLookupManager
~QHostInfoLookupManager()
Definition
qhostinfo.cpp:910
QHostInfoLookupManager::scheduledLookups
QQueue< QHostInfoRunnable * > scheduledLookups
Definition
qhostinfo_p.h:190
QHostInfoLookupManager::wasAborted
bool wasAborted(int id)
Definition
qhostinfo.cpp:1044
QHostInfoLookupManager::mutex
QMutex mutex
Definition
qhostinfo_p.h:197
QHostInfoLookupManager::lookupFinished
void lookupFinished(QHostInfoRunnable *r)
Definition
qhostinfo.cpp:1055
QHostInfoLookupManager::scheduleLookup
void scheduleLookup(QHostInfoRunnable *r)
Definition
qhostinfo.cpp:999
QHostInfoLookupManager::clear
void clear()
Definition
qhostinfo.cpp:920
QHostInfoLookupManager::abortLookup
void abortLookup(int id)
Definition
qhostinfo.cpp:1011
QHostInfoLookupManager::abortedLookups
QList< int > abortedLookups
Definition
qhostinfo_p.h:192
QHostInfoLookupManager::cache
QHostInfoCache cache
Definition
qhostinfo_p.h:182
QHostInfoLookupManager::finishedLookups
QList< QHostInfoRunnable * > finishedLookups
Definition
qhostinfo_p.h:191
QHostInfoLookupManager::wasDeleted
bool wasDeleted
Definition
qhostinfo_p.h:199
QHostInfoLookupManager::QHostInfoLookupManager
QHostInfoLookupManager()
Definition
qhostinfo.cpp:900
QHostInfoPrivate
Definition
qhostinfo_p.h:86
QHostInfoPrivate::lookupId
int lookupId
Definition
qhostinfo_p.h:103
QHostInfoPrivate::QHostInfoPrivate
QHostInfoPrivate()
Definition
qhostinfo_p.h:88
QHostInfoPrivate::addrs
QList< QHostAddress > addrs
Definition
qhostinfo_p.h:101
QHostInfoPrivate::hostName
QString hostName
Definition
qhostinfo_p.h:102
QHostInfoPrivate::errorStr
QString errorStr
Definition
qhostinfo_p.h:100
QHostInfoRunnable
Definition
qhostinfo_p.h:149
QHostInfoRunnable::run
void run() override
Implement this pure virtual function in your subclass.
Definition
qhostinfo.cpp:844
QHostInfoRunnable::id
int id
Definition
qhostinfo_p.h:158
QHostInfoRunnable::resultEmitter
std::unique_ptr< QHostInfoResult > resultEmitter
Definition
qhostinfo_p.h:159
QHostInfoRunnable::toBeLookedUp
QString toBeLookedUp
Definition
qhostinfo_p.h:157
QHostInfoRunnable::~QHostInfoRunnable
~QHostInfoRunnable() override
Definition
qhostinfo.cpp:836
QHostInfo
The QHostInfo class provides static functions for host name lookups.
Definition
qhostinfo.h:22
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
__has_attribute
#define __has_attribute(x)
Definition
qcompilerdetection.h:450
qt_qhostinfo_clear_cache
void qt_qhostinfo_clear_cache()
Definition
qhostinfo.cpp:1091
nextId
static int nextId()
Definition
qhostinfo.cpp:193
qt_qhostinfo_lookup
QHostInfo qt_qhostinfo_lookup(const QString &name, QObject *receiver, const char *member, bool *valid, int *id)
Definition
qhostinfo.cpp:1070
qtbase
src
network
kernel
qhostinfo_p.h
Generated on
for Qt by
1.18.0