summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
Diffstat (limited to 'src/network')
-rw-r--r--src/network/access/qnetworkaccessmanager.cpp21
-rw-r--r--src/network/access/qnetworkaccessmanager.h1
-rw-r--r--src/network/access/qnetworkaccessmanager_p.h2
-rw-r--r--src/network/bearer/qbearerengine.cpp38
-rw-r--r--src/network/bearer/qbearerengine_p.h3
-rw-r--r--src/network/bearer/qnetworkconfigmanager.cpp77
-rw-r--r--src/network/bearer/qnetworkconfigmanager_p.cpp90
-rw-r--r--src/network/bearer/qnetworkconfigmanager_p.h15
-rw-r--r--src/network/bearer/qnetworksession.cpp14
9 files changed, 234 insertions, 27 deletions
diff --git a/src/network/access/qnetworkaccessmanager.cpp b/src/network/access/qnetworkaccessmanager.cpp
index 5876ee2..7bb1399 100644
--- a/src/network/access/qnetworkaccessmanager.cpp
+++ b/src/network/access/qnetworkaccessmanager.cpp
@@ -880,11 +880,16 @@ QNetworkReply *QNetworkAccessManager::createRequest(QNetworkAccessManager::Opera
return new QDisabledNetworkReply(this, req, op);
}
- if (d->initializeSession && !d->networkSession) {
+ if (!d->networkSession && (d->initializeSession || !d->networkConfiguration.isEmpty())) {
QNetworkConfigurationManager manager;
- d->createSession(manager.defaultConfiguration());
+ if (d->networkConfiguration.isEmpty())
+ d->createSession(manager.defaultConfiguration());
+ else
+ d->createSession(manager.configurationFromIdentifier(d->networkConfiguration));
d->initializeSession = false;
+ } else if (d->networkSession) {
+ d->networkSession->setSessionProperty(QLatin1String("AutoCloseSessionTimeout"), -1);
}
QNetworkRequest request = req;
@@ -943,6 +948,9 @@ void QNetworkAccessManagerPrivate::_q_replyFinished()
QNetworkReply *reply = qobject_cast<QNetworkReply *>(q->sender());
if (reply)
emit q->finished(reply);
+
+ if (networkSession && q->findChildren<QNetworkReply *>().count() == 1)
+ networkSession->setSessionProperty(QLatin1String("AutoCloseSessionTimeout"), 120000);
}
void QNetworkAccessManagerPrivate::_q_replySslErrors(const QList<QSslError> &errors)
@@ -1210,6 +1218,7 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co
networkSession = new QNetworkSession(config, q);
QObject::connect(networkSession, SIGNAL(opened()), q, SIGNAL(networkSessionOnline()));
+ QObject::connect(networkSession, SIGNAL(closed()), q, SLOT(_q_networkSessionClosed()));
QObject::connect(networkSession, SIGNAL(newConfigurationActivated()),
q, SLOT(_q_networkSessionNewConfigurationActivated()));
QObject::connect(networkSession,
@@ -1218,6 +1227,14 @@ void QNetworkAccessManagerPrivate::createSession(const QNetworkConfiguration &co
SLOT(_q_networkSessionPreferredConfigurationChanged(QNetworkConfiguration,bool)));
}
+void QNetworkAccessManagerPrivate::_q_networkSessionClosed()
+{
+ networkConfiguration = networkSession->configuration().identifier();
+
+ delete networkSession;
+ networkSession = 0;
+}
+
void QNetworkAccessManagerPrivate::_q_networkSessionNewConfigurationActivated()
{
Q_Q(QNetworkAccessManager);
diff --git a/src/network/access/qnetworkaccessmanager.h b/src/network/access/qnetworkaccessmanager.h
index 252dfd0..694a54f 100644
--- a/src/network/access/qnetworkaccessmanager.h
+++ b/src/network/access/qnetworkaccessmanager.h
@@ -139,6 +139,7 @@ private:
Q_DECLARE_PRIVATE(QNetworkAccessManager)
Q_PRIVATE_SLOT(d_func(), void _q_replyFinished())
Q_PRIVATE_SLOT(d_func(), void _q_replySslErrors(QList<QSslError>))
+ Q_PRIVATE_SLOT(d_func(), void _q_networkSessionClosed())
Q_PRIVATE_SLOT(d_func(), void _q_networkSessionNewConfigurationActivated())
Q_PRIVATE_SLOT(d_func(), void _q_networkSessionPreferredConfigurationChanged(QNetworkConfiguration,bool))
};
diff --git a/src/network/access/qnetworkaccessmanager_p.h b/src/network/access/qnetworkaccessmanager_p.h
index 8d772f0..0140268 100644
--- a/src/network/access/qnetworkaccessmanager_p.h
+++ b/src/network/access/qnetworkaccessmanager_p.h
@@ -105,6 +105,7 @@ public:
void createSession(const QNetworkConfiguration &config);
+ void _q_networkSessionClosed();
void _q_networkSessionNewConfigurationActivated();
void _q_networkSessionPreferredConfigurationChanged(const QNetworkConfiguration &config,
bool isSeamless);
@@ -121,6 +122,7 @@ public:
#endif
QNetworkSession *networkSession;
+ QString networkConfiguration;
bool networkAccessEnabled;
bool initializeSession;
diff --git a/src/network/bearer/qbearerengine.cpp b/src/network/bearer/qbearerengine.cpp
index eb851cc..c42e2d2 100644
--- a/src/network/bearer/qbearerengine.cpp
+++ b/src/network/bearer/qbearerengine.cpp
@@ -70,6 +70,44 @@ QBearerEngine::~QBearerEngine()
}
}
+bool QBearerEngine::requiresPolling() const
+{
+ return false;
+}
+
+/*
+ Returns true if configurations are in use; otherwise returns false.
+
+ If configurations are in use and requiresPolling() returns true, polling will be enabled for
+ this engine.
+*/
+bool QBearerEngine::configurationsInUse() const
+{
+ QHash<QString, QNetworkConfigurationPrivatePointer>::ConstIterator it;
+ QHash<QString, QNetworkConfigurationPrivatePointer>::ConstIterator end;
+
+ QMutexLocker locker(&mutex);
+
+ for (it = accessPointConfigurations.begin(),
+ end = accessPointConfigurations.end(); it != end; ++it) {
+ if (it.value()->ref > 1)
+ return true;
+ }
+
+ for (it = snapConfigurations.begin(), end = snapConfigurations.end(); it != end; ++it) {
+ if (it.value()->ref > 1)
+ return true;
+ }
+
+ for (it = userChoiceConfigurations.begin(),
+ end = userChoiceConfigurations.end(); it != end; ++it) {
+ if (it.value()->ref > 1)
+ return true;
+ }
+
+ return false;
+}
+
#include "moc_qbearerengine_p.cpp"
QT_END_NAMESPACE
diff --git a/src/network/bearer/qbearerengine_p.h b/src/network/bearer/qbearerengine_p.h
index 5e12b0f..028c174 100644
--- a/src/network/bearer/qbearerengine_p.h
+++ b/src/network/bearer/qbearerengine_p.h
@@ -89,6 +89,9 @@ public:
virtual QNetworkConfigurationPrivatePointer defaultConfiguration() = 0;
+ virtual bool requiresPolling() const;
+ bool configurationsInUse() const;
+
Q_SIGNALS:
void configurationAdded(QNetworkConfigurationPrivatePointer config);
void configurationRemoved(QNetworkConfigurationPrivatePointer config);
diff --git a/src/network/bearer/qnetworkconfigmanager.cpp b/src/network/bearer/qnetworkconfigmanager.cpp
index 9ff197b..b631cf1 100644
--- a/src/network/bearer/qnetworkconfigmanager.cpp
+++ b/src/network/bearer/qnetworkconfigmanager.cpp
@@ -174,7 +174,8 @@ QNetworkConfigurationManagerPrivate *qNetworkConfigurationManagerPrivate()
QNetworkConfigurationManager::QNetworkConfigurationManager( QObject* parent )
: QObject(parent)
{
- QNetworkConfigurationManagerPrivate* priv = connManager();
+ QNetworkConfigurationManagerPrivate *priv = connManager();
+
connect(priv, SIGNAL(configurationAdded(QNetworkConfiguration)),
this, SIGNAL(configurationAdded(QNetworkConfiguration)));
connect(priv, SIGNAL(configurationRemoved(QNetworkConfiguration)),
@@ -185,6 +186,8 @@ QNetworkConfigurationManager::QNetworkConfigurationManager( QObject* parent )
this, SIGNAL(onlineStateChanged(bool)));
connect(priv, SIGNAL(configurationChanged(QNetworkConfiguration)),
this, SIGNAL(configurationChanged(QNetworkConfiguration)));
+
+ priv->enablePolling();
}
/*!
@@ -192,6 +195,9 @@ QNetworkConfigurationManager::QNetworkConfigurationManager( QObject* parent )
*/
QNetworkConfigurationManager::~QNetworkConfigurationManager()
{
+ QNetworkConfigurationManagerPrivate *priv = connManager();
+
+ priv->disablePolling();
}
@@ -221,7 +227,7 @@ QNetworkConfiguration QNetworkConfigurationManager::defaultConfiguration() const
// Engines don't have a default configuration.
// Return first active snap
- QNetworkConfigurationPrivatePointer firstDiscovered;
+ QNetworkConfigurationPrivatePointer defaultConfiguration;
foreach (QBearerEngine *engine, conPriv->engines()) {
QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator it;
@@ -236,22 +242,35 @@ QNetworkConfiguration QNetworkConfigurationManager::defaultConfiguration() const
QNetworkConfiguration config;
config.d = it.value();
return config;
- } else if ((it.value()->state & QNetworkConfiguration::Discovered) ==
- QNetworkConfiguration::Discovered) {
- firstDiscovered = it.value();
+ } else if (!defaultConfiguration) {
+ if ((it.value()->state & QNetworkConfiguration::Discovered) ==
+ QNetworkConfiguration::Discovered) {
+ defaultConfiguration = it.value();
+ }
}
}
}
// No Active SNAPs return first Discovered SNAP.
- if (firstDiscovered) {
+ if (defaultConfiguration) {
QNetworkConfiguration config;
- config.d = firstDiscovered;
+ config.d = defaultConfiguration;
return config;
}
- // No Active or Discovered SNAPs, do same for InternetAccessPoints.
- firstDiscovered.reset();
+ /*
+ No Active or Discovered SNAPs, find the perferred access point.
+ The following priority order is used:
+
+ 1. Active Ethernet
+ 2. Active WLAN
+ 3. Active Other
+ 4. Discovered Ethernet
+ 5. Discovered WLAN
+ 6. Discovered Other
+ */
+
+ defaultConfiguration.reset();
foreach (QBearerEngine *engine, conPriv->engines()) {
QHash<QString, QNetworkConfigurationPrivatePointer>::Iterator it;
@@ -261,22 +280,42 @@ QNetworkConfiguration QNetworkConfigurationManager::defaultConfiguration() const
for (it = engine->accessPointConfigurations.begin(),
end = engine->accessPointConfigurations.end(); it != end; ++it) {
- if ((it.value()->state & QNetworkConfiguration::Active) ==
- QNetworkConfiguration::Active) {
- QNetworkConfiguration config;
- config.d = it.value();
- return config;
- } else if ((it.value()->state & QNetworkConfiguration::Discovered) ==
- QNetworkConfiguration::Discovered) {
- firstDiscovered = it.value();
+
+ if ((it.value()->state & QNetworkConfiguration::Discovered) ==
+ QNetworkConfiguration::Discovered) {
+ if (!defaultConfiguration) {
+ defaultConfiguration = it.value();
+ } else {
+ if (defaultConfiguration->state == it.value()->state) {
+ if (defaultConfiguration->bearerName() == QLatin1String("Ethernet")) {
+ // do nothing
+ } else if (defaultConfiguration->bearerName() == QLatin1String("WLAN")) {
+ // ethernet beats wlan
+ if (it.value()->bearerName() == QLatin1String("Ethernet"))
+ defaultConfiguration = it.value();
+ } else {
+ // ethernet and wlan beats other
+ if (it.value()->bearerName() == QLatin1String("Ethernet") ||
+ it.value()->bearerName() == QLatin1String("WLAN")) {
+ defaultConfiguration = it.value();
+ }
+ }
+ } else {
+ // active beats discovered
+ if ((defaultConfiguration->state & QNetworkConfiguration::Active) !=
+ QNetworkConfiguration::Active) {
+ defaultConfiguration = it.value();
+ }
+ }
+ }
}
}
}
// No Active InternetAccessPoint return first Discovered InternetAccessPoint.
- if (firstDiscovered) {
+ if (defaultConfiguration) {
QNetworkConfiguration config;
- config.d = firstDiscovered;
+ config.d = defaultConfiguration;
return config;
}
diff --git a/src/network/bearer/qnetworkconfigmanager_p.cpp b/src/network/bearer/qnetworkconfigmanager_p.cpp
index 1ac10c5..9740424 100644
--- a/src/network/bearer/qnetworkconfigmanager_p.cpp
+++ b/src/network/bearer/qnetworkconfigmanager_p.cpp
@@ -55,7 +55,7 @@ Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
(QBearerEngineFactoryInterface_iid, QLatin1String("/bearer")))
QNetworkConfigurationManagerPrivate::QNetworkConfigurationManagerPrivate()
-: capFlags(0), mutex(QMutex::Recursive), firstUpdate(true)
+: capFlags(0), mutex(QMutex::Recursive), pollTimer(0), forcedPolling(0), firstUpdate(true)
{
updateConfigurations();
@@ -82,7 +82,7 @@ void QNetworkConfigurationManagerPrivate::configurationAdded(QNetworkConfigurati
}
if (ptr->state == QNetworkConfiguration::Active) {
- onlineConfigurations.insert(ptr);
+ onlineConfigurations.insert(ptr->id);
if (!firstUpdate && onlineConfigurations.count() == 1)
emit onlineStateChanged(true);
}
@@ -100,7 +100,7 @@ void QNetworkConfigurationManagerPrivate::configurationRemoved(QNetworkConfigura
emit configurationRemoved(item);
}
- onlineConfigurations.remove(ptr);
+ onlineConfigurations.remove(ptr->id);
if (!firstUpdate && onlineConfigurations.isEmpty())
emit onlineStateChanged(false);
}
@@ -118,9 +118,9 @@ void QNetworkConfigurationManagerPrivate::configurationChanged(QNetworkConfigura
bool previous = !onlineConfigurations.isEmpty();
if (ptr->state == QNetworkConfiguration::Active)
- onlineConfigurations.insert(ptr);
+ onlineConfigurations.insert(ptr->id);
else
- onlineConfigurations.remove(ptr);
+ onlineConfigurations.remove(ptr->id);
bool online = !onlineConfigurations.isEmpty();
@@ -133,6 +133,9 @@ void QNetworkConfigurationManagerPrivate::updateConfigurations()
QMutexLocker locker(&mutex);
if (firstUpdate) {
+ if (sender())
+ return;
+
updating = false;
QFactoryLoader *l = loader();
@@ -161,6 +164,8 @@ void QNetworkConfigurationManagerPrivate::updateConfigurations()
this, SLOT(configurationChanged(QNetworkConfigurationPrivatePointer)));
capFlags |= engine->capabilities();
+
+ engine->requestUpdate();
}
}
@@ -180,6 +185,15 @@ void QNetworkConfigurationManagerPrivate::updateConfigurations()
emit configurationUpdateComplete();
}
+ if (engine && !pollingEngines.isEmpty()) {
+ int index = sessionEngines.indexOf(engine);
+ if (index >= 0)
+ pollingEngines.remove(index);
+
+ if (pollingEngines.isEmpty())
+ startPolling();
+ }
+
if (firstUpdate)
firstUpdate = false;
}
@@ -208,4 +222,70 @@ QList<QBearerEngine *> QNetworkConfigurationManagerPrivate::engines()
return sessionEngines;
}
+void QNetworkConfigurationManagerPrivate::startPolling()
+{
+ QMutexLocker locker(&mutex);
+
+ bool pollingRequired = false;
+
+ if (forcedPolling > 0) {
+ foreach (QBearerEngine *engine, sessionEngines) {
+ if (engine->requiresPolling()) {
+ pollingRequired = true;
+ break;
+ }
+ }
+ }
+
+ if (!pollingRequired) {
+ foreach (QBearerEngine *engine, sessionEngines) {
+ if (engine->configurationsInUse()) {
+ pollingRequired = true;
+ break;
+ }
+ }
+ }
+
+ if (pollingRequired) {
+ if (!pollTimer) {
+ pollTimer = new QTimer(this);
+ pollTimer->setInterval(10000);
+ pollTimer->setSingleShot(true);
+ connect(pollTimer, SIGNAL(timeout()), this, SLOT(pollEngines()));
+ }
+
+ pollTimer->start();
+ }
+}
+
+void QNetworkConfigurationManagerPrivate::pollEngines()
+{
+ QMutexLocker locker(&mutex);
+
+ for (int i = 0; i < sessionEngines.count(); ++i) {
+ if ((forcedPolling && sessionEngines.at(i)->requiresPolling()) ||
+ sessionEngines.at(i)->configurationsInUse()) {
+ pollingEngines.insert(i);
+ sessionEngines.at(i)->requestUpdate();
+ }
+ }
+}
+
+void QNetworkConfigurationManagerPrivate::enablePolling()
+{
+ QMutexLocker locker(&mutex);
+
+ ++forcedPolling;
+
+ if (forcedPolling == 1)
+ startPolling();
+}
+
+void QNetworkConfigurationManagerPrivate::disablePolling()
+{
+ QMutexLocker locker(&mutex);
+
+ --forcedPolling;
+}
+
QT_END_NAMESPACE
diff --git a/src/network/bearer/qnetworkconfigmanager_p.h b/src/network/bearer/qnetworkconfigmanager_p.h
index c7e988e..ac8518c 100644
--- a/src/network/bearer/qnetworkconfigmanager_p.h
+++ b/src/network/bearer/qnetworkconfigmanager_p.h
@@ -61,6 +61,7 @@
QT_BEGIN_NAMESPACE
class QBearerEngine;
+class QTimer;
class Q_NETWORK_EXPORT QNetworkConfigurationManagerPrivate : public QObject
{
@@ -77,6 +78,11 @@ public:
QList<QBearerEngine *> engines();
+ void startPolling();
+
+ void enablePolling();
+ void disablePolling();
+
public slots:
void updateConfigurations();
@@ -92,19 +98,26 @@ Q_SIGNALS:
private:
QMutex mutex;
+ QTimer *pollTimer;
+
QList<QBearerEngine *> sessionEngines;
- QSet<QNetworkConfigurationPrivatePointer> onlineConfigurations;
+ QSet<QString> onlineConfigurations;
QSet<int> updatingEngines;
bool updating;
+ QSet<int> pollingEngines;
+ int forcedPolling;
+
bool firstUpdate;
private Q_SLOTS:
void configurationAdded(QNetworkConfigurationPrivatePointer ptr);
void configurationRemoved(QNetworkConfigurationPrivatePointer ptr);
void configurationChanged(QNetworkConfigurationPrivatePointer ptr);
+
+ void pollEngines();
};
Q_NETWORK_EXPORT QNetworkConfigurationManagerPrivate *qNetworkConfigurationManagerPrivate();
diff --git a/src/network/bearer/qnetworksession.cpp b/src/network/bearer/qnetworksession.cpp
index 047c8d3..cf9f4b2 100644
--- a/src/network/bearer/qnetworksession.cpp
+++ b/src/network/bearer/qnetworksession.cpp
@@ -492,6 +492,20 @@ QString QNetworkSession::errorString() const
\o Setting this property to \i true before calling \l open() implies that the connection attempt
is made but if no connection can be established, the user is not connsulted and asked to select
a suitable connection. This property is not set by default and support for it depends on the platform.
+
+ \row
+ \o AutoCloseSessionTimeout
+ \o If the session requires polling to keep its state up to date, this property holds
+ the timeout in milliseconds before the session will automatically close. If the
+ value of this property is -1 the session will not automatically close. This property
+ is set to -1 by default.
+
+ The purpose of this property is to minimize resource use on platforms that use
+ polling to update the state of the session. Applications can set the value of this
+ property to the desired timeout before the session is closed. In response to the
+ closed() signal the network session should be deleted to ensure that all polling is
+ stopped. The session can then be recreated once it is required again. This property
+ has no effect for sessions that do not require polling.
\endtable
*/
QVariant QNetworkSession::sessionProperty(const QString& key) const