From daace2df3eb80f1b62f38151f5cc3f3b355927e7 Mon Sep 17 00:00:00 2001 From: Markus Goetz Date: Tue, 2 Mar 2010 10:39:54 +0100 Subject: QAbstractSocket: Use new faster DNS function Use qt_qhostinfo_lookup which avoids the event loop when the DNS result is already cached. Reviewed-by: Thiago --- src/network/socket/qabstractsocket.cpp | 18 ++++++++++++++++-- tests/auto/qsslsocket/qsslsocket.pro | 2 ++ tests/auto/qsslsocket/tst_qsslsocket.cpp | 6 +++++- tests/auto/qtcpsocket/qtcpsocket.pro | 3 +++ tests/auto/qtcpsocket/tst_qtcpsocket.cpp | 13 +++++++++++-- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index 275c436..d990448 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -354,6 +354,8 @@ #include "qabstractsocket.h" #include "qabstractsocket_p.h" +#include "private/qhostinfo_p.h" + #include #include #include @@ -1369,8 +1371,20 @@ void QAbstractSocket::connectToHostImplementation(const QString &hostName, quint return; #endif } else { - if (d->threadData->eventDispatcher) - d->hostLookupId = QHostInfo::lookupHost(hostName, this, SLOT(_q_startConnecting(QHostInfo))); + if (d->threadData->eventDispatcher) { + // this internal API for QHostInfo either immediatly gives us the desired + // QHostInfo from cache or later calls the _q_startConnecting slot. + bool immediateResultValid = false; + QHostInfo hostInfo = qt_qhostinfo_lookup(hostName, + this, + SLOT(_q_startConnecting(QHostInfo)), + &immediateResultValid, + &d->hostLookupId); + if (immediateResultValid) { + d->hostLookupId = -1; + d->_q_startConnecting(hostInfo); + } + } } #if defined(QABSTRACTSOCKET_DEBUG) diff --git a/tests/auto/qsslsocket/qsslsocket.pro b/tests/auto/qsslsocket/qsslsocket.pro index 147175e..541b2d9 100644 --- a/tests/auto/qsslsocket/qsslsocket.pro +++ b/tests/auto/qsslsocket/qsslsocket.pro @@ -32,3 +32,5 @@ wince* { } else { DEFINES += SRCDIR=\\\"$$PWD/\\\" } + +requires(contains(QT_CONFIG,private_tests)) diff --git a/tests/auto/qsslsocket/tst_qsslsocket.cpp b/tests/auto/qsslsocket/tst_qsslsocket.cpp index abd3237..ad2b50d 100644 --- a/tests/auto/qsslsocket/tst_qsslsocket.cpp +++ b/tests/auto/qsslsocket/tst_qsslsocket.cpp @@ -54,6 +54,8 @@ #include #include +#include "private/qhostinfo_p.h" + #include "../network-settings.h" Q_DECLARE_METATYPE(QAbstractSocket::SocketState) @@ -288,6 +290,8 @@ void tst_QSslSocket::init() } QNetworkProxy::setApplicationProxy(proxy); } + + qt_qhostinfo_clear_cache(); } void tst_QSslSocket::cleanup() @@ -477,7 +481,7 @@ void tst_QSslSocket::simpleConnectWithIgnore() // Start connecting socket.connectToHost(QtNetworkSettings::serverName(), 993); - QCOMPARE(socket.state(), QAbstractSocket::HostLookupState); + QVERIFY(socket.state() != QAbstractSocket::UnconnectedState); // something must be in progress enterLoop(10); // Start handshake diff --git a/tests/auto/qtcpsocket/qtcpsocket.pro b/tests/auto/qtcpsocket/qtcpsocket.pro index 4bbec23..3d4eba3 100644 --- a/tests/auto/qtcpsocket/qtcpsocket.pro +++ b/tests/auto/qtcpsocket/qtcpsocket.pro @@ -3,3 +3,6 @@ TEMPLATE = subdirs !wince*: SUBDIRS = test stressTest wince*|symbian*|vxworks* : SUBDIRS = test + + +requires(contains(QT_CONFIG,private_tests)) diff --git a/tests/auto/qtcpsocket/tst_qtcpsocket.cpp b/tests/auto/qtcpsocket/tst_qtcpsocket.cpp index e638e287..ef7ff83 100644 --- a/tests/auto/qtcpsocket/tst_qtcpsocket.cpp +++ b/tests/auto/qtcpsocket/tst_qtcpsocket.cpp @@ -92,6 +92,8 @@ #include #endif +#include "private/qhostinfo_p.h" + #include "../network-settings.h" Q_DECLARE_METATYPE(QAbstractSocket::SocketError) @@ -315,6 +317,8 @@ void tst_QTcpSocket::init() } QNetworkProxy::setApplicationProxy(proxy); } + + qt_qhostinfo_clear_cache(); } QTcpSocket *tst_QTcpSocket::newSocket() const @@ -1059,7 +1063,9 @@ void tst_QTcpSocket::disconnectWhileLookingUp() // just connect and disconnect, then make sure nothing weird happened QTcpSocket *socket = newSocket(); socket->connectToHost(QtNetworkSettings::serverName(), 21); - QVERIFY(socket->state() == QAbstractSocket::HostLookupState); + + // check that connect is in progress + QVERIFY(socket->state() != QAbstractSocket::UnconnectedState); QFETCH(bool, doClose); if (doClose) { @@ -1665,7 +1671,10 @@ void tst_QTcpSocket::waitForConnectedInHostLookupSlot() connect(tmpSocket, SIGNAL(hostFound()), this, SLOT(hostLookupSlot())); tmpSocket->connectToHost(QtNetworkSettings::serverName(), 143); - loop.exec(); + // only execute the loop if not already connected + if (tmpSocket->state() != QAbstractSocket::ConnectedState) + loop.exec(); + QCOMPARE(timerSpy.count(), 0); delete tmpSocket; -- cgit v0.12