src/network/socket/qabstractsocket.cpp
changeset 7 f7bc934e204c
parent 3 41300fa6a67c
--- a/src/network/socket/qabstractsocket.cpp	Tue Feb 02 00:43:10 2010 +0200
+++ b/src/network/socket/qabstractsocket.cpp	Wed Mar 31 11:06:36 2010 +0300
@@ -1,6 +1,6 @@
 /****************************************************************************
 **
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
 ** All rights reserved.
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
@@ -155,6 +155,9 @@
     See the \l network/fortuneclient and \l network/blockingfortuneclient
     examples for an overview of both approaches.
 
+    \note We discourage the use of the blocking functions together
+    with signals. One of the two possibilities should be used.
+
     QAbstractSocket can be used with QTextStream and QDataStream's
     stream operators (operator<<() and operator>>()). There is one
     issue to be aware of, though: You must make sure that enough data
@@ -169,6 +172,10 @@
     This signal is emitted after connectToHost() has been called and
     the host lookup has succeeded.
 
+    \note Since Qt 4.6.3 QAbstractSocket may emit hostFound()
+    directly from the connectToHost() call since a DNS result could have been
+    cached.
+
     \sa connected()
 */
 
@@ -178,6 +185,10 @@
     This signal is emitted after connectToHost() has been called and
     a connection has been successfully established.
 
+    \note On some operating systems the connected() signal may
+    be directly emitted from the connectToHost() call for connections
+    to the localhost.
+
     \sa connectToHost(), disconnected()
 */
 
@@ -351,6 +362,8 @@
 #include "qabstractsocket.h"
 #include "qabstractsocket_p.h"
 
+#include "private/qhostinfo_p.h"
+
 #include <qabstracteventdispatcher.h>
 #include <qdatetime.h>
 #include <qhostaddress.h>
@@ -520,13 +533,13 @@
     Q_Q(QAbstractSocket);
 #if defined (QABSTRACTSOCKET_DEBUG)
     QString typeStr;
-    if (q->socketType() == QAbstractSocket::TcpSocket) typeStr = "TcpSocket";
-    else if (q->socketType() == QAbstractSocket::UdpSocket) typeStr = "UdpSocket";
-    else typeStr = "UnknownSocketType";
+    if (q->socketType() == QAbstractSocket::TcpSocket) typeStr = QLatin1String("TcpSocket");
+    else if (q->socketType() == QAbstractSocket::UdpSocket) typeStr = QLatin1String("UdpSocket");
+    else typeStr = QLatin1String("UnknownSocketType");
     QString protocolStr;
-    if (protocol == QAbstractSocket::IPv4Protocol) protocolStr = "IPv4Protocol";
-    else if (protocol == QAbstractSocket::IPv6Protocol) protocolStr = "IPv6Protocol";
-    else protocolStr = "UnknownNetworkLayerProtocol";
+    if (protocol == QAbstractSocket::IPv4Protocol) protocolStr = QLatin1String("IPv4Protocol");
+    else if (protocol == QAbstractSocket::IPv6Protocol) protocolStr = QLatin1String("IPv6Protocol");
+    else protocolStr = QLatin1String("UnknownNetworkLayerProtocol");
 #endif
 
     resetSocketLayer();
@@ -873,15 +886,19 @@
     if (state != QAbstractSocket::HostLookupState)
         return;
 
+    if (hostLookupId != -1 && hostLookupId != hostInfo.lookupId()) {
+        qWarning("QAbstractSocketPrivate::_q_startConnecting() received hostInfo for wrong lookup ID %d expected %d", hostInfo.lookupId(), hostLookupId);
+    }
+
     addresses = hostInfo.addresses();
 
 #if defined(QABSTRACTSOCKET_DEBUG)
-    QString s = "{";
+    QString s = QLatin1String("{");
     for (int i = 0; i < addresses.count(); ++i) {
-        if (i != 0) s += ", ";
+        if (i != 0) s += QLatin1String(", ");
         s += addresses.at(i).toString();
     }
-    s += '}';
+    s += QLatin1Char('}');
     qDebug("QAbstractSocketPrivate::_q_startConnecting(hostInfo == %s)", s.toLatin1().constData());
 #endif
 
@@ -1362,8 +1379,20 @@
         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)
@@ -1678,9 +1707,12 @@
 
     If msecs is -1, this function will not time out.
 
-    Note: This function may wait slightly longer than \a msecs,
+    \note This function may wait slightly longer than \a msecs,
     depending on the time it takes to complete the host lookup.
 
+    \note Multiple calls to this functions do not accumulate the time.
+    If the function times out, the connecting process will be aborted.
+
     \sa connectToHost(), connected()
 */
 bool QAbstractSocket::waitForConnected(int msecs)
@@ -1718,7 +1750,7 @@
         d->_q_startConnecting(QHostInfo::fromName(d->hostName));
     }
     if (state() == UnconnectedState)
-        return false;
+        return false; // connect not im progress anymore!
 
     bool timedOut = true;
 #if defined (QABSTRACTSOCKET_DEBUG)