Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/bearer/qnetworksession.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qnetworksession.h" | - | ||||||
41 | #include "qnetworksession_p.h" | - | ||||||
42 | #include "qbearerengine_p.h" | - | ||||||
43 | - | |||||||
44 | #include <QEventLoop> | - | ||||||
45 | #include <QMetaMethod> | - | ||||||
46 | #include <QTimer> | - | ||||||
47 | #include <QThread> | - | ||||||
48 | - | |||||||
49 | #include "qnetworkconfigmanager_p.h" | - | ||||||
50 | - | |||||||
51 | // for QNetworkSession::interface | - | ||||||
52 | #ifdef interface | - | ||||||
53 | # undef interface | - | ||||||
54 | #endif | - | ||||||
55 | - | |||||||
56 | #ifndef QT_NO_BEARERMANAGEMENT | - | ||||||
57 | - | |||||||
58 | QT_BEGIN_NAMESPACE | - | ||||||
59 | - | |||||||
60 | /*! | - | ||||||
61 | \class QNetworkSession | - | ||||||
62 | - | |||||||
63 | \brief The QNetworkSession class provides control over the system's access points | - | ||||||
64 | and enables session management for cases when multiple clients access the same access point. | - | ||||||
65 | - | |||||||
66 | \since 4.7 | - | ||||||
67 | - | |||||||
68 | \inmodule QtNetwork | - | ||||||
69 | \ingroup network | - | ||||||
70 | - | |||||||
71 | A QNetworkSession enables control over the system's network interfaces. The session's configuration | - | ||||||
72 | parameter are determined via the QNetworkConfiguration object to which it is bound. Depending on the | - | ||||||
73 | type of the session (single access point or service network) a session may be linked to one or more | - | ||||||
74 | network interfaces. By means of \l{open()}{opening} and \l{close()}{closing} of network sessions | - | ||||||
75 | a developer can start and stop the systems network interfaces. If the configuration represents | - | ||||||
76 | multiple access points (see \l QNetworkConfiguration::ServiceNetwork) more advanced features such as roaming may be supported. | - | ||||||
77 | - | |||||||
78 | QNetworkSession supports session management within the same process and depending on the platform's | - | ||||||
79 | capabilities may support out-of-process sessions. If the same | - | ||||||
80 | network configuration is used by multiple open sessions the underlying network interface is only terminated once | - | ||||||
81 | the last session has been closed. | - | ||||||
82 | - | |||||||
83 | \section1 Roaming | - | ||||||
84 | - | |||||||
85 | Applications may connect to the preferredConfigurationChanged() signal in order to | - | ||||||
86 | receive notifications when a more suitable access point becomes available. | - | ||||||
87 | In response to this signal the application must either initiate the roaming via migrate() | - | ||||||
88 | or ignore() the new access point. Once the session has roamed the | - | ||||||
89 | newConfigurationActivated() signal is emitted. The application may now test the | - | ||||||
90 | carrier and must either accept() or reject() it. The session will return to the previous | - | ||||||
91 | access point if the roaming was rejected. The subsequent state diagram depicts the required | - | ||||||
92 | state transitions. | - | ||||||
93 | - | |||||||
94 | \image roaming-states.png | - | ||||||
95 | - | |||||||
96 | Some platforms may distinguish forced roaming and application level roaming (ALR). | - | ||||||
97 | ALR implies that the application controls (via migrate(), ignore(), accept() and reject()) | - | ||||||
98 | whether a network session can roam from one access point to the next. Such control is useful | - | ||||||
99 | if the application maintains stateful socket connections and wants to control the transition from | - | ||||||
100 | one interface to the next. Forced roaming implies that the system automatically roams to the next network without | - | ||||||
101 | consulting the application. This has the advantage that the application can make use of roaming features | - | ||||||
102 | without actually being aware of it. It is expected that the application detects that the underlying | - | ||||||
103 | socket is broken and automatically reconnects via the new network link. | - | ||||||
104 | - | |||||||
105 | If the platform supports both modes of roaming, an application indicates its preference | - | ||||||
106 | by connecting to the preferredConfigurationChanged() signal. Connecting to this signal means that | - | ||||||
107 | the application wants to take control over the roaming behavior and therefore implies application | - | ||||||
108 | level roaming. If the client does not connect to the preferredConfigurationChanged(), forced roaming | - | ||||||
109 | is used. If forced roaming is not supported the network session will not roam by default. | - | ||||||
110 | - | |||||||
111 | Some applications may want to suppress any form of roaming altogether. Possible use cases may be | - | ||||||
112 | high priority downloads or remote services which cannot handle a roaming enabled client. Clients | - | ||||||
113 | can suppress roaming by connecting to the preferredConfigurationChanged() signal and answer each | - | ||||||
114 | signal emission with ignore(). | - | ||||||
115 | - | |||||||
116 | \sa QNetworkConfiguration, QNetworkConfigurationManager | - | ||||||
117 | */ | - | ||||||
118 | - | |||||||
119 | /*! | - | ||||||
120 | \enum QNetworkSession::State | - | ||||||
121 | - | |||||||
122 | This enum describes the connectivity state of the session. If the session is based on a | - | ||||||
123 | single access point configuration the state of the session is the same as the state of the | - | ||||||
124 | associated network interface. | - | ||||||
125 | - | |||||||
126 | \value Invalid The session is invalid due to an invalid configuration. This may | - | ||||||
127 | happen due to a removed access point or a configuration that was | - | ||||||
128 | invalid to begin with. | - | ||||||
129 | \value NotAvailable The session is based on a defined but not yet discovered QNetworkConfiguration | - | ||||||
130 | (see \l QNetworkConfiguration::StateFlag). | - | ||||||
131 | \value Connecting The network session is being established. | - | ||||||
132 | \value Connected The network session is connected. If the current process wishes to use this session | - | ||||||
133 | it has to register its interest by calling open(). A network session | - | ||||||
134 | is considered to be ready for socket operations if it isOpen() and connected. | - | ||||||
135 | \value Closing The network session is in the process of being shut down. | - | ||||||
136 | \value Disconnected The network session is not connected. The associated QNetworkConfiguration | - | ||||||
137 | has the state QNetworkConfiguration::Discovered. | - | ||||||
138 | \value Roaming The network session is roaming from one access point to another | - | ||||||
139 | access point. | - | ||||||
140 | */ | - | ||||||
141 | - | |||||||
142 | /*! | - | ||||||
143 | \enum QNetworkSession::SessionError | - | ||||||
144 | - | |||||||
145 | This enum describes the session errors that can occur. | - | ||||||
146 | - | |||||||
147 | \value UnknownSessionError An unidentified error occurred. | - | ||||||
148 | \value SessionAbortedError The session was aborted by the user or system. | - | ||||||
149 | \value RoamingError The session cannot roam to a new configuration. | - | ||||||
150 | \value OperationNotSupportedError The operation is not supported for current configuration. | - | ||||||
151 | \value InvalidConfigurationError The operation cannot currently be performed for the | - | ||||||
152 | current configuration. | - | ||||||
153 | */ | - | ||||||
154 | - | |||||||
155 | /*! | - | ||||||
156 | \enum QNetworkSession::UsagePolicy | - | ||||||
157 | \since 5.0 | - | ||||||
158 | - | |||||||
159 | These flags allow the system to inform the application of network usage restrictions that | - | ||||||
160 | may be in place. | - | ||||||
161 | - | |||||||
162 | \value NoPolicy No policy in force, usage is unrestricted. | - | ||||||
163 | \value NoBackgroundTrafficPolicy Background network traffic (not user initiated) should be avoided | - | ||||||
164 | for example to save battery or data charges | - | ||||||
165 | */ | - | ||||||
166 | - | |||||||
167 | /*! | - | ||||||
168 | \fn void QNetworkSession::stateChanged(QNetworkSession::State state) | - | ||||||
169 | - | |||||||
170 | This signal is emitted whenever the state of the network session changes. | - | ||||||
171 | The \a state parameter is the new state. | - | ||||||
172 | - | |||||||
173 | \sa state() | - | ||||||
174 | */ | - | ||||||
175 | - | |||||||
176 | /*! | - | ||||||
177 | \fn void QNetworkSession::error(QNetworkSession::SessionError error) | - | ||||||
178 | - | |||||||
179 | This signal is emitted after an error occurred. The \a error parameter | - | ||||||
180 | describes the error that occurred. | - | ||||||
181 | - | |||||||
182 | \sa error(), errorString() | - | ||||||
183 | */ | - | ||||||
184 | - | |||||||
185 | /*! | - | ||||||
186 | \fn void QNetworkSession::preferredConfigurationChanged(const QNetworkConfiguration &config, bool isSeamless) | - | ||||||
187 | - | |||||||
188 | This signal is emitted when the preferred configuration/access point for the | - | ||||||
189 | session changes. Only sessions which are based on service network configurations | - | ||||||
190 | may emit this signal. \a config can be used to determine access point specific | - | ||||||
191 | details such as proxy settings and \a isSeamless indicates whether roaming will | - | ||||||
192 | break the sessions IP address. | - | ||||||
193 | - | |||||||
194 | As a consequence to this signal the application must either start the roaming process | - | ||||||
195 | by calling migrate() or choose to ignore() the new access point. | - | ||||||
196 | - | |||||||
197 | If the roaming process is non-seamless the IP address will change which means that | - | ||||||
198 | a socket becomes invalid. However seamless mobility can ensure that the local IP address | - | ||||||
199 | does not change. This is achieved by using a virtual IP address which is bound to the actual | - | ||||||
200 | link address. During the roaming process the virtual address is attached to the new link | - | ||||||
201 | address. | - | ||||||
202 | - | |||||||
203 | Some platforms may support the concept of Forced Roaming and Application Level Roaming (ALR). | - | ||||||
204 | Forced roaming implies that the platform may simply roam to a new configuration without | - | ||||||
205 | consulting applications. It is up to the application to detect the link layer loss and reestablish | - | ||||||
206 | its sockets. In contrast ALR provides the opportunity to prevent the system from roaming. | - | ||||||
207 | If this session is based on a configuration that supports roaming the application can choose | - | ||||||
208 | whether it wants to be consulted (ALR use case) by connecting to this signal. For as long as this signal | - | ||||||
209 | connection remains the session remains registered as a roaming stakeholder; otherwise roaming will | - | ||||||
210 | be enforced by the platform. | - | ||||||
211 | - | |||||||
212 | \sa migrate(), ignore(), QNetworkConfiguration::isRoamingAvailable() | - | ||||||
213 | */ | - | ||||||
214 | - | |||||||
215 | /*! | - | ||||||
216 | \fn void QNetworkSession::newConfigurationActivated() | - | ||||||
217 | - | |||||||
218 | This signal is emitted once the session has roamed to the new access point. | - | ||||||
219 | The application may reopen its socket and test the suitability of the new network link. | - | ||||||
220 | Subsequently it must either accept() or reject() the new access point. | - | ||||||
221 | - | |||||||
222 | \sa accept(), reject() | - | ||||||
223 | */ | - | ||||||
224 | - | |||||||
225 | /*! | - | ||||||
226 | \fn void QNetworkSession::opened() | - | ||||||
227 | - | |||||||
228 | This signal is emitted when the network session has been opened. | - | ||||||
229 | - | |||||||
230 | The underlying network interface will not be shut down as long as the session remains open. | - | ||||||
231 | Note that this feature is dependent on \l{QNetworkConfigurationManager::SystemSessionSupport}{system wide session support}. | - | ||||||
232 | */ | - | ||||||
233 | - | |||||||
234 | /*! | - | ||||||
235 | \fn void QNetworkSession::closed() | - | ||||||
236 | - | |||||||
237 | This signal is emitted when the network session has been closed. | - | ||||||
238 | */ | - | ||||||
239 | - | |||||||
240 | /*! | - | ||||||
241 | \fn void QNetworkSession::usagePoliciesChanged(QNetworkSession::UsagePolicies usagePolicies) | - | ||||||
242 | \since 5.0 | - | ||||||
243 | - | |||||||
244 | This signal is emitted when the \a usagePolicies in force are changed by the system. | - | ||||||
245 | */ | - | ||||||
246 | - | |||||||
247 | /*! | - | ||||||
248 | Constructs a session based on \a connectionConfig with the given \a parent. | - | ||||||
249 | - | |||||||
250 | \sa QNetworkConfiguration | - | ||||||
251 | */ | - | ||||||
252 | QNetworkSession::QNetworkSession(const QNetworkConfiguration &connectionConfig, QObject *parent) | - | ||||||
253 | : QObject(parent), d(0) | - | ||||||
254 | { | - | ||||||
255 | // invalid configuration | - | ||||||
256 | if (!connectionConfig.identifier().isEmpty()) {
| 2-78 | ||||||
257 | foreachconst auto engines = qNetworkConfigurationManagerPrivate()->engines(); | - | ||||||
258 | for (QBearerEngine *engine , qNetworkConfigurationManagerPrivate()->: engines())) { | - | ||||||
259 | if (engine->hasIdentifier(connectionConfig.identifier())) {
| 8-78 | ||||||
260 | d = engine->createSessionBackend(); | - | ||||||
261 | d->q = this; | - | ||||||
262 | d->publicConfig = connectionConfig; | - | ||||||
263 | d->syncStateWithInterface(); | - | ||||||
264 | connect(d, SIGNAL(quitPendingWaitsForOpened()), this, SIGNAL(opened())); | - | ||||||
265 | connect(d, SIGNAL(error(QNetworkSession::SessionError)), | - | ||||||
266 | this, SIGNAL(error(QNetworkSession::SessionError))); | - | ||||||
267 | connect(d, SIGNAL(stateChanged(QNetworkSession::State)), | - | ||||||
268 | this, SIGNAL(stateChanged(QNetworkSession::State))); | - | ||||||
269 | connect(d, SIGNAL(closed()), this, SIGNAL(closed())); | - | ||||||
270 | connect(d, SIGNAL(preferredConfigurationChanged(QNetworkConfiguration,bool)), | - | ||||||
271 | this, SIGNAL(preferredConfigurationChanged(QNetworkConfiguration,bool))); | - | ||||||
272 | connect(d, SIGNAL(newConfigurationActivated()), | - | ||||||
273 | this, SIGNAL(newConfigurationActivated())); | - | ||||||
274 | connect(d, SIGNAL(usagePoliciesChanged(QNetworkSession::UsagePolicies)), | - | ||||||
275 | this, SIGNAL(usagePoliciesChanged(QNetworkSession::UsagePolicies))); | - | ||||||
276 | break; executed 78 times by 15 tests: break; Executed by:
| 78 | ||||||
277 | } | - | ||||||
278 | } executed 8 times by 2 tests: end of block Executed by:
| 8 | ||||||
279 | } executed 78 times by 15 tests: end of block Executed by:
| 78 | ||||||
280 | - | |||||||
281 | qRegisterMetaType<QNetworkSession::State>(); | - | ||||||
282 | qRegisterMetaType<QNetworkSession::SessionError>(); | - | ||||||
283 | qRegisterMetaType<QNetworkSession::UsagePolicies>(); | - | ||||||
284 | } executed 80 times by 15 tests: end of block Executed by:
| 80 | ||||||
285 | - | |||||||
286 | /*! | - | ||||||
287 | Frees the resources associated with the QNetworkSession object. | - | ||||||
288 | */ | - | ||||||
289 | QNetworkSession::~QNetworkSession() | - | ||||||
290 | { | - | ||||||
291 | delete d; | - | ||||||
292 | } | - | ||||||
293 | - | |||||||
294 | /*! | - | ||||||
295 | Creates an open session which increases the session counter on the underlying network interface. | - | ||||||
296 | The system will not terminate a network interface until the session reference counter reaches zero. | - | ||||||
297 | Therefore an open session allows an application to register its use of the interface. | - | ||||||
298 | - | |||||||
299 | As a result of calling open() the interface will be started if it is not connected/up yet. | - | ||||||
300 | Some platforms may not provide support for out-of-process sessions. On such platforms the session | - | ||||||
301 | counter ignores any sessions held by another process. The platform capabilities can be | - | ||||||
302 | detected via QNetworkConfigurationManager::capabilities(). | - | ||||||
303 | - | |||||||
304 | Note that this call is asynchronous. Depending on the outcome of this call the results can be enquired | - | ||||||
305 | by connecting to the stateChanged(), opened() or error() signals. | - | ||||||
306 | - | |||||||
307 | It is not a requirement to open a session in order to monitor the underlying network interface. | - | ||||||
308 | - | |||||||
309 | \sa close(), stop(), isOpen() | - | ||||||
310 | */ | - | ||||||
311 | void QNetworkSession::open() | - | ||||||
312 | { | - | ||||||
313 | if (d) | - | ||||||
314 | d->open(); | - | ||||||
315 | else | - | ||||||
316 | emit error(InvalidConfigurationError); | - | ||||||
317 | } | - | ||||||
318 | - | |||||||
319 | /*! | - | ||||||
320 | Waits until the session has been opened, up to \a msecs milliseconds. If the session has been opened, this | - | ||||||
321 | function returns \c true; otherwise it returns \c false. In the case where it returns \c false, you can call error() | - | ||||||
322 | to determine the cause of the error. | - | ||||||
323 | - | |||||||
324 | The following example waits up to one second for the session to be opened: | - | ||||||
325 | - | |||||||
326 | \code | - | ||||||
327 | session->open(); | - | ||||||
328 | if (session->waitForOpened(1000)) | - | ||||||
329 | qDebug("Open!"); | - | ||||||
330 | \endcode | - | ||||||
331 | - | |||||||
332 | If \a msecs is -1, this function will not time out. | - | ||||||
333 | - | |||||||
334 | \sa open(), error() | - | ||||||
335 | */ | - | ||||||
336 | bool QNetworkSession::waitForOpened(int msecs) | - | ||||||
337 | { | - | ||||||
338 | if (!d) | - | ||||||
339 | return false; | - | ||||||
340 | - | |||||||
341 | if (d->isOpen) | - | ||||||
342 | return true; | - | ||||||
343 | - | |||||||
344 | if (!(d->state == Connecting || d->state == Connected)) { | - | ||||||
345 | return false; | - | ||||||
346 | } | - | ||||||
347 | - | |||||||
348 | QEventLoop loop; | - | ||||||
349 | QObject::connect(d, SIGNAL(quitPendingWaitsForOpened()), &loop, SLOT(quit())); | - | ||||||
350 | QObject::connect(this, SIGNAL(error(QNetworkSession::SessionError)), &loop, SLOT(quit())); | - | ||||||
351 | - | |||||||
352 | //final call | - | ||||||
353 | if (msecs >= 0) | - | ||||||
354 | QTimer::singleShot(msecs, &loop, SLOT(quit())); | - | ||||||
355 | - | |||||||
356 | // enter the event loop and wait for opened/error/timeout | - | ||||||
357 | loop.exec(QEventLoop::ExcludeUserInputEvents | QEventLoop::WaitForMoreEvents); | - | ||||||
358 | - | |||||||
359 | return d->isOpen; | - | ||||||
360 | } | - | ||||||
361 | - | |||||||
362 | /*! | - | ||||||
363 | Decreases the session counter on the associated network configuration. If the session counter reaches zero | - | ||||||
364 | the active network interface is shut down. This also means that state() will only change from \l Connected to | - | ||||||
365 | \l Disconnected if the current session was the last open session. | - | ||||||
366 | - | |||||||
367 | If the platform does not support out-of-process sessions calling this function does not stop the | - | ||||||
368 | interface. In this case \l{stop()} has to be used to force a shut down. | - | ||||||
369 | The platform capabilities can be detected via QNetworkConfigurationManager::capabilities(). | - | ||||||
370 | - | |||||||
371 | Note that this call is asynchronous. Depending on the outcome of this call the results can be enquired | - | ||||||
372 | by connecting to the stateChanged(), opened() or error() signals. | - | ||||||
373 | - | |||||||
374 | \sa open(), stop(), isOpen() | - | ||||||
375 | */ | - | ||||||
376 | void QNetworkSession::close() | - | ||||||
377 | { | - | ||||||
378 | if (d) | - | ||||||
379 | d->close(); | - | ||||||
380 | } | - | ||||||
381 | - | |||||||
382 | /*! | - | ||||||
383 | Invalidates all open sessions against the network interface and therefore stops the | - | ||||||
384 | underlying network interface. This function always changes the session's state() flag to | - | ||||||
385 | \l Disconnected. | - | ||||||
386 | - | |||||||
387 | \sa open(), close() | - | ||||||
388 | */ | - | ||||||
389 | void QNetworkSession::stop() | - | ||||||
390 | { | - | ||||||
391 | if (d) | - | ||||||
392 | d->stop(); | - | ||||||
393 | } | - | ||||||
394 | - | |||||||
395 | /*! | - | ||||||
396 | Returns the QNetworkConfiguration that this network session object is based on. | - | ||||||
397 | - | |||||||
398 | \sa QNetworkConfiguration | - | ||||||
399 | */ | - | ||||||
400 | QNetworkConfiguration QNetworkSession::configuration() const | - | ||||||
401 | { | - | ||||||
402 | return d ? d->publicConfig : QNetworkConfiguration(); | - | ||||||
403 | } | - | ||||||
404 | - | |||||||
405 | #ifndef QT_NO_NETWORKINTERFACE | - | ||||||
406 | /*! | - | ||||||
407 | Returns the network interface that is used by this session. | - | ||||||
408 | - | |||||||
409 | This function only returns a valid QNetworkInterface when this session is \l Connected. | - | ||||||
410 | - | |||||||
411 | The returned interface may change as a result of a roaming process. | - | ||||||
412 | - | |||||||
413 | \sa state() | - | ||||||
414 | */ | - | ||||||
415 | QNetworkInterface QNetworkSession::interface() const | - | ||||||
416 | { | - | ||||||
417 | return d ? d->currentInterface() : QNetworkInterface(); | - | ||||||
418 | } | - | ||||||
419 | #endif | - | ||||||
420 | - | |||||||
421 | /*! | - | ||||||
422 | Returns \c true if this session is open. If the number of all open sessions is greater than | - | ||||||
423 | zero the underlying network interface will remain connected/up. | - | ||||||
424 | - | |||||||
425 | The session can be controlled via open() and close(). | - | ||||||
426 | */ | - | ||||||
427 | bool QNetworkSession::isOpen() const | - | ||||||
428 | { | - | ||||||
429 | return d ? d->isOpen : false; | - | ||||||
430 | } | - | ||||||
431 | - | |||||||
432 | /*! | - | ||||||
433 | Returns the state of the session. | - | ||||||
434 | - | |||||||
435 | If the session is based on a single access point configuration the state of the | - | ||||||
436 | session is the same as the state of the associated network interface. Therefore | - | ||||||
437 | a network session object can be used to monitor network interfaces. | - | ||||||
438 | - | |||||||
439 | A \l QNetworkConfiguration::ServiceNetwork based session summarizes the state of all its children | - | ||||||
440 | and therefore returns the \l Connected state if at least one of the service network's | - | ||||||
441 | \l {QNetworkConfiguration::children()}{children()} configurations is active. | - | ||||||
442 | - | |||||||
443 | Note that it is not required to hold an open session in order to obtain the network interface state. | - | ||||||
444 | A connected but closed session may be used to monitor network interfaces whereas an open and connected | - | ||||||
445 | session object may prevent the network interface from being shut down. | - | ||||||
446 | - | |||||||
447 | \sa error(), stateChanged() | - | ||||||
448 | */ | - | ||||||
449 | QNetworkSession::State QNetworkSession::state() const | - | ||||||
450 | { | - | ||||||
451 | return d ? d->state : QNetworkSession::Invalid; | - | ||||||
452 | } | - | ||||||
453 | - | |||||||
454 | /*! | - | ||||||
455 | Returns the type of error that last occurred. | - | ||||||
456 | - | |||||||
457 | \sa state(), errorString() | - | ||||||
458 | */ | - | ||||||
459 | QNetworkSession::SessionError QNetworkSession::error() const | - | ||||||
460 | { | - | ||||||
461 | return d ? d->error() : InvalidConfigurationError; | - | ||||||
462 | } | - | ||||||
463 | - | |||||||
464 | /*! | - | ||||||
465 | Returns a human-readable description of the last device error that | - | ||||||
466 | occurred. | - | ||||||
467 | - | |||||||
468 | \sa error() | - | ||||||
469 | */ | - | ||||||
470 | QString QNetworkSession::errorString() const | - | ||||||
471 | { | - | ||||||
472 | return d ? d->errorString() : tr("Invalid configuration."); | - | ||||||
473 | } | - | ||||||
474 | - | |||||||
475 | /*! | - | ||||||
476 | Returns the value for property \a key. | - | ||||||
477 | - | |||||||
478 | A network session can have properties attached which may describe the session in more details. | - | ||||||
479 | This function can be used to gain access to those properties. | - | ||||||
480 | - | |||||||
481 | The following property keys are guaranteed to be specified on all platforms: | - | ||||||
482 | - | |||||||
483 | \table 80% | - | ||||||
484 | \header | - | ||||||
485 | \li Key \li Description | - | ||||||
486 | \row | - | ||||||
487 | \li ActiveConfiguration | - | ||||||
488 | \li If the session \l isOpen() this property returns the identifier of the | - | ||||||
489 | QNetworkConfiguration that is used by this session; otherwise an empty string. | - | ||||||
490 | - | |||||||
491 | The main purpose of this key is to determine which Internet access point is used | - | ||||||
492 | if the session is based on a \l{QNetworkConfiguration::ServiceNetwork}{ServiceNetwork}. | - | ||||||
493 | The following code snippet highlights the difference: | - | ||||||
494 | \code | - | ||||||
495 | QNetworkConfigurationManager mgr; | - | ||||||
496 | QNetworkConfiguration ap = mgr.defaultConfiguration(); | - | ||||||
497 | QNetworkSession *session = new QNetworkSession(ap); | - | ||||||
498 | ... //code activates session | - | ||||||
499 | - | |||||||
500 | QString ident = session->sessionProperty("ActiveConfiguration").toString(); | - | ||||||
501 | if ( ap.type() == QNetworkConfiguration::ServiceNetwork ) { | - | ||||||
502 | Q_ASSERT( ap.identifier() != ident ); | - | ||||||
503 | Q_ASSERT( ap.children().contains( mgr.configurationFromIdentifier(ident) ) ); | - | ||||||
504 | } else if ( ap.type() == QNetworkConfiguration::InternetAccessPoint ) { | - | ||||||
505 | Q_ASSERT( ap.identifier() == ident ); | - | ||||||
506 | } | - | ||||||
507 | \endcode | - | ||||||
508 | \row | - | ||||||
509 | \li UserChoiceConfiguration | - | ||||||
510 | \li If the session \l isOpen() and is bound to a QNetworkConfiguration of type | - | ||||||
511 | UserChoice, this property returns the identifier of the QNetworkConfiguration that the | - | ||||||
512 | configuration resolved to when \l open() was called; otherwise an empty string. | - | ||||||
513 | - | |||||||
514 | The purpose of this key is to determine the real QNetworkConfiguration that the | - | ||||||
515 | session is using. This key is different from \e ActiveConfiguration in that | - | ||||||
516 | this key may return an identifier for either a | - | ||||||
517 | \l {QNetworkConfiguration::ServiceNetwork}{service network} or a | - | ||||||
518 | \l {QNetworkConfiguration::InternetAccessPoint}{Internet access points} configurations, | - | ||||||
519 | whereas \e ActiveConfiguration always returns identifiers to | - | ||||||
520 | \l {QNetworkConfiguration::InternetAccessPoint}{Internet access points} configurations. | - | ||||||
521 | \row | - | ||||||
522 | \li ConnectInBackground | - | ||||||
523 | \li Setting this property to \e true before calling \l open() implies that the connection attempt | - | ||||||
524 | is made but if no connection can be established, the user is not connsulted and asked to select | - | ||||||
525 | a suitable connection. This property is not set by default and support for it depends on the platform. | - | ||||||
526 | - | |||||||
527 | \row | - | ||||||
528 | \li AutoCloseSessionTimeout | - | ||||||
529 | \li If the session requires polling to keep its state up to date, this property holds | - | ||||||
530 | the timeout in milliseconds before the session will automatically close. If the | - | ||||||
531 | value of this property is -1 the session will not automatically close. This property | - | ||||||
532 | is set to -1 by default. | - | ||||||
533 | - | |||||||
534 | The purpose of this property is to minimize resource use on platforms that use | - | ||||||
535 | polling to update the state of the session. Applications can set the value of this | - | ||||||
536 | property to the desired timeout before the session is closed. In response to the | - | ||||||
537 | closed() signal the network session should be deleted to ensure that all polling is | - | ||||||
538 | stopped. The session can then be recreated once it is required again. This property | - | ||||||
539 | has no effect for sessions that do not require polling. | - | ||||||
540 | \endtable | - | ||||||
541 | */ | - | ||||||
542 | QVariant QNetworkSession::sessionProperty(const QString &key) const | - | ||||||
543 | { | - | ||||||
544 | if (!d || !d->publicConfig.isValid()) | - | ||||||
545 | return QVariant(); | - | ||||||
546 | - | |||||||
547 | if (key == QLatin1String("ActiveConfiguration")) | - | ||||||
548 | return d->isOpen ? d->activeConfig.identifier() : QString(); | - | ||||||
549 | - | |||||||
550 | if (key == QLatin1String("UserChoiceConfiguration")) { | - | ||||||
551 | if (!d->isOpen || d->publicConfig.type() != QNetworkConfiguration::UserChoice) | - | ||||||
552 | return QString(); | - | ||||||
553 | - | |||||||
554 | if (d->serviceConfig.isValid()) | - | ||||||
555 | return d->serviceConfig.identifier(); | - | ||||||
556 | else | - | ||||||
557 | return d->activeConfig.identifier(); | - | ||||||
558 | } | - | ||||||
559 | - | |||||||
560 | return d->sessionProperty(key); | - | ||||||
561 | } | - | ||||||
562 | - | |||||||
563 | /*! | - | ||||||
564 | Sets the property \a value on the session. The property is identified using | - | ||||||
565 | \a key. Removing an already set property can be achieved by passing an | - | ||||||
566 | invalid QVariant. | - | ||||||
567 | - | |||||||
568 | Note that the \e UserChoiceConfiguration and \e ActiveConfiguration | - | ||||||
569 | properties are read only and cannot be changed using this method. | - | ||||||
570 | */ | - | ||||||
571 | void QNetworkSession::setSessionProperty(const QString &key, const QVariant &value) | - | ||||||
572 | { | - | ||||||
573 | if (!d) | - | ||||||
574 | return; | - | ||||||
575 | - | |||||||
576 | if (key == QLatin1String("ActiveConfiguration") || | - | ||||||
577 | key == QLatin1String("UserChoiceConfiguration")) { | - | ||||||
578 | return; | - | ||||||
579 | } | - | ||||||
580 | - | |||||||
581 | d->setSessionProperty(key, value); | - | ||||||
582 | } | - | ||||||
583 | - | |||||||
584 | /*! | - | ||||||
585 | Instructs the session to roam to the new access point. The old access point remains active | - | ||||||
586 | until the application calls accept(). | - | ||||||
587 | - | |||||||
588 | The newConfigurationActivated() signal is emitted once roaming has been completed. | - | ||||||
589 | - | |||||||
590 | \sa accept() | - | ||||||
591 | */ | - | ||||||
592 | void QNetworkSession::migrate() | - | ||||||
593 | { | - | ||||||
594 | if (d) | - | ||||||
595 | d->migrate(); | - | ||||||
596 | } | - | ||||||
597 | - | |||||||
598 | /*! | - | ||||||
599 | This function indicates that the application does not wish to roam the session. | - | ||||||
600 | - | |||||||
601 | \sa migrate() | - | ||||||
602 | */ | - | ||||||
603 | void QNetworkSession::ignore() | - | ||||||
604 | { | - | ||||||
605 | if (d) | - | ||||||
606 | d->ignore(); | - | ||||||
607 | } | - | ||||||
608 | - | |||||||
609 | /*! | - | ||||||
610 | Instructs the session to permanently accept the new access point. Once this function | - | ||||||
611 | has been called the session may not return to the old access point. | - | ||||||
612 | - | |||||||
613 | The old access point may be closed in the process if there are no other network sessions for it. | - | ||||||
614 | Therefore any open socket that still uses the old access point | - | ||||||
615 | may become unusable and should be closed before completing the migration. | - | ||||||
616 | */ | - | ||||||
617 | void QNetworkSession::accept() | - | ||||||
618 | { | - | ||||||
619 | if (d) | - | ||||||
620 | d->accept(); | - | ||||||
621 | } | - | ||||||
622 | - | |||||||
623 | /*! | - | ||||||
624 | The new access point is not suitable for the application. By calling this function the | - | ||||||
625 | session returns to the previous access point/configuration. This action may invalidate | - | ||||||
626 | any socket that has been created via the not desired access point. | - | ||||||
627 | - | |||||||
628 | \sa accept() | - | ||||||
629 | */ | - | ||||||
630 | void QNetworkSession::reject() | - | ||||||
631 | { | - | ||||||
632 | if (d) | - | ||||||
633 | d->reject(); | - | ||||||
634 | } | - | ||||||
635 | - | |||||||
636 | - | |||||||
637 | /*! | - | ||||||
638 | Returns the amount of data sent in bytes; otherwise 0. | - | ||||||
639 | - | |||||||
640 | This field value includes the usage across all open network | - | ||||||
641 | sessions which use the same network interface. | - | ||||||
642 | - | |||||||
643 | If the session is based on a service network configuration the number of | - | ||||||
644 | sent bytes across all active member configurations are returned. | - | ||||||
645 | - | |||||||
646 | This function may not always be supported on all platforms and returns 0. | - | ||||||
647 | The platform capability can be detected via QNetworkConfigurationManager::DataStatistics. | - | ||||||
648 | - | |||||||
649 | \note On some platforms this function may run the main event loop. | - | ||||||
650 | */ | - | ||||||
651 | quint64 QNetworkSession::bytesWritten() const | - | ||||||
652 | { | - | ||||||
653 | return d ? d->bytesWritten() : Q_UINT64_C(0); | - | ||||||
654 | } | - | ||||||
655 | - | |||||||
656 | /*! | - | ||||||
657 | Returns the amount of data received in bytes; otherwise 0. | - | ||||||
658 | - | |||||||
659 | This field value includes the usage across all open network | - | ||||||
660 | sessions which use the same network interface. | - | ||||||
661 | - | |||||||
662 | If the session is based on a service network configuration the number of | - | ||||||
663 | sent bytes across all active member configurations are returned. | - | ||||||
664 | - | |||||||
665 | This function may not always be supported on all platforms and returns 0. | - | ||||||
666 | The platform capability can be detected via QNetworkConfigurationManager::DataStatistics. | - | ||||||
667 | - | |||||||
668 | \note On some platforms this function may run the main event loop. | - | ||||||
669 | */ | - | ||||||
670 | quint64 QNetworkSession::bytesReceived() const | - | ||||||
671 | { | - | ||||||
672 | return d ? d->bytesReceived() : Q_UINT64_C(0); | - | ||||||
673 | } | - | ||||||
674 | - | |||||||
675 | /*! | - | ||||||
676 | Returns the number of seconds that the session has been active. | - | ||||||
677 | */ | - | ||||||
678 | quint64 QNetworkSession::activeTime() const | - | ||||||
679 | { | - | ||||||
680 | return d ? d->activeTime() : Q_UINT64_C(0); | - | ||||||
681 | } | - | ||||||
682 | - | |||||||
683 | /*! | - | ||||||
684 | Returns the network usage policies currently in force by the system. | - | ||||||
685 | */ | - | ||||||
686 | QNetworkSession::UsagePolicies QNetworkSession::usagePolicies() const | - | ||||||
687 | { | - | ||||||
688 | return d ? d->usagePolicies() : QNetworkSession::NoPolicy; | - | ||||||
689 | } | - | ||||||
690 | - | |||||||
691 | /*! | - | ||||||
692 | \internal | - | ||||||
693 | Change usage policies for unit testing. | - | ||||||
694 | In normal use, the policies are published by the bearer plugin | - | ||||||
695 | */ | - | ||||||
696 | void QNetworkSessionPrivate::setUsagePolicies(QNetworkSession &session, QNetworkSession::UsagePolicies policies) | - | ||||||
697 | { | - | ||||||
698 | if (!session.d) | - | ||||||
699 | return; | - | ||||||
700 | session.d->setUsagePolicies(policies); | - | ||||||
701 | } | - | ||||||
702 | - | |||||||
703 | /*! | - | ||||||
704 | \internal | - | ||||||
705 | - | |||||||
706 | This function is required to detect whether the client wants to control | - | ||||||
707 | the roaming process. If he connects to preferredConfigurationChanged() signal | - | ||||||
708 | he intends to influence it. Otherwise QNetworkSession always roams | - | ||||||
709 | without registering this session as a stakeholder in the roaming process. | - | ||||||
710 | - | |||||||
711 | For more details check the Forced vs ALR roaming section in the QNetworkSession | - | ||||||
712 | class description. | - | ||||||
713 | */ | - | ||||||
714 | void QNetworkSession::connectNotify(const QMetaMethod &signal) | - | ||||||
715 | { | - | ||||||
716 | QObject::connectNotify(signal); | - | ||||||
717 | - | |||||||
718 | if (!d) | - | ||||||
719 | return; | - | ||||||
720 | - | |||||||
721 | //check for preferredConfigurationChanged() signal connect notification | - | ||||||
722 | //This is not required on all platforms | - | ||||||
723 | static const QMetaMethod preferredConfigurationChangedSignal = | - | ||||||
724 | QMetaMethod::fromSignal(&QNetworkSession::preferredConfigurationChanged); | - | ||||||
725 | if (signal == preferredConfigurationChangedSignal) | - | ||||||
726 | d->setALREnabled(true); | - | ||||||
727 | } | - | ||||||
728 | - | |||||||
729 | /*! | - | ||||||
730 | \internal | - | ||||||
731 | - | |||||||
732 | This function is called when the client disconnects from the | - | ||||||
733 | preferredConfigurationChanged() signal. | - | ||||||
734 | - | |||||||
735 | \sa connectNotify() | - | ||||||
736 | */ | - | ||||||
737 | void QNetworkSession::disconnectNotify(const QMetaMethod &signal) | - | ||||||
738 | { | - | ||||||
739 | QObject::disconnectNotify(signal); | - | ||||||
740 | - | |||||||
741 | if (!d) | - | ||||||
742 | return; | - | ||||||
743 | - | |||||||
744 | //check for preferredConfigurationChanged() signal disconnect notification | - | ||||||
745 | //This is not required on all platforms | - | ||||||
746 | static const QMetaMethod preferredConfigurationChangedSignal = | - | ||||||
747 | QMetaMethod::fromSignal(&QNetworkSession::preferredConfigurationChanged); | - | ||||||
748 | if (signal == preferredConfigurationChangedSignal) | - | ||||||
749 | d->setALREnabled(false); | - | ||||||
750 | } | - | ||||||
751 | - | |||||||
752 | #include "moc_qnetworksession.cpp" | - | ||||||
753 | - | |||||||
754 | QT_END_NAMESPACE | - | ||||||
755 | - | |||||||
756 | #endif // QT_NO_BEARERMANAGEMENT | - | ||||||
Source code | Switch to Preprocessed file |