Ignore:
Timestamp:
Feb 11, 2010, 11:19:06 PM (15 years ago)
Author:
Dmitry A. Kuminov
Message:

trunk: Merged in qt 4.6.1 sources.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/network/socket/qabstractsocket.cpp

    r2 r561  
    22**
    33** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
    4 ** Contact: Qt Software Information (qt-info@nokia.com)
     4** All rights reserved.
     5** Contact: Nokia Corporation (qt-info@nokia.com)
    56**
    67** This file is part of the QtNetwork module of the Qt Toolkit.
     
    2122** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    2223**
    23 ** In addition, as a special exception, Nokia gives you certain
    24 ** additional rights. These rights are described in the Nokia Qt LGPL
    25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
    26 ** package.
     24** In addition, as a special exception, Nokia gives you certain additional
     25** rights.  These rights are described in the Nokia Qt LGPL Exception
     26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
    2727**
    2828** GNU General Public License Usage
     
    3434** met: http://www.gnu.org/copyleft/gpl.html.
    3535**
    36 ** If you are unsure which license is appropriate for your use, please
    37 ** contact the sales department at qt-sales@nokia.com.
     36** If you have questions regarding the use of this file, please contact
     37** Nokia at qt-info@nokia.com.
    3838** $QT_END_LICENSE$
    3939**
     
    4949
    5050    \reentrant
    51     \ingroup io
     51    \ingroup network
    5252    \inmodule QtNetwork
    5353
     
    100100    write buffer size. You can monitor its size by listening to this
    101101    signal.
    102    
     102
    103103    The readyRead() signal is emitted every time a new chunk of data
    104104    has arrived. bytesAvailable() then returns the number of bytes
     
    161161    is available before attempting to read it using operator>>().
    162162
    163     \sa QFtp, QHttp, QTcpServer
     163    \sa QFtp, QNetworkAccessManager, QTcpServer
    164164*/
    165165
     
    200200
    201201    QAbstractSocket::SocketError is not a registered metatype, so for queued
    202     connections, you will have to register it with Q_REGISTER_METATYPE.
    203 
    204     \sa error(), errorString()
     202    connections, you will have to register it with Q_DECLARE_METATYPE() and
     203    qRegisterMetaType().
     204
     205    \sa error(), errorString(), {Creating Custom Qt Types}
    205206*/
    206207
     
    212213
    213214    QAbstractSocket::SocketState is not a registered metatype, so for queued
    214     connections, you will have to register it with Q_REGISTER_METATYPE.
    215 
    216     \sa state()
     215    connections, you will have to register it with Q_REGISTER_METATYPE() and
     216    qRegisterMetaType().
     217
     218    \sa state(), {Creating Custom Qt Types}
    217219*/
    218220
     
    331333*/
    332334
     335/*!
     336    \enum QAbstractSocket::SocketOption
     337    \since 4.6
     338
     339    This enum represents the options that can be set on a socket.
     340    If desired, they can be set after having received the connected() signal from
     341    the socket or after having received a new socket from a QTcpServer.
     342
     343    \value LowDelayOption Try to optimize the socket for low latency. For a QTcpSocket
     344    this would set the TCP_NODELAY option and disable Nagle's algorithm. Set this to 1
     345    to enable.
     346    \value KeepAliveOption Set this to 1 to enable the SO_KEEPALIVE socket option
     347
     348    \sa QAbstractSocket::setSocketOption(), QAbstractSocket::socketOption()
     349*/
     350
    333351#include "qabstractsocket.h"
    334352#include "qabstractsocket_p.h"
     
    436454      socketEngine(0),
    437455      cachedSocketDescriptor(-1),
     456#ifdef Q_OS_LINUX
     457      addToBytesAvailable(0),
     458#endif
    438459      readBufferMaxSize(0),
    439460      readBuffer(QABSTRACTSOCKET_BUFFERSIZE),
     
    442463      blockingTimeout(30000),
    443464      connectTimer(0),
     465      disconnectTimer(0),
    444466      connectTimeElapsed(0),
    445467      hostLookupId(-1),
     468      socketType(QAbstractSocket::UnknownSocketType),
    446469      state(QAbstractSocket::UnconnectedState),
    447470      socketError(QAbstractSocket::UnknownSocketError)
     
    476499        cachedSocketDescriptor = -1;
    477500    }
    478     if (connectTimer) {
     501    if (connectTimer)
    479502        connectTimer->stop();
    480     }
     503    if (disconnectTimer)
     504        disconnectTimer->stop();
    481505}
    482506
     
    648672    if (socketEngine) {
    649673#if defined (Q_OS_WIN)
    650         if (!writeBuffer.isEmpty())
    651             socketEngine->setWriteNotificationEnabled(true);
     674        if (!writeBuffer.isEmpty())
     675            socketEngine->setWriteNotificationEnabled(true);
    652676#else
    653         if (writeBuffer.isEmpty())
    654             socketEngine->setWriteNotificationEnabled(false);
     677        if (writeBuffer.isEmpty() && socketEngine->bytesToWrite() == 0)
     678            socketEngine->setWriteNotificationEnabled(false);
    655679#endif
    656680    }
     
    689713{
    690714    Q_Q(QAbstractSocket);
    691     if (!socketEngine || !socketEngine->isValid() || writeBuffer.isEmpty()) {
     715    if (!socketEngine || !socketEngine->isValid() || (writeBuffer.isEmpty()
     716        && socketEngine->bytesToWrite() == 0)) {
    692717#if defined (QABSTRACTSOCKET_DEBUG)
    693718    qDebug("QAbstractSocketPrivate::flush() nothing to do: valid ? %s, writeBuffer.isEmpty() ? %s",
    694719           socketEngine->isValid() ? "yes" : "no", writeBuffer.isEmpty() ? "yes" : "no");
    695720#endif
     721
     722        // this covers the case when the buffer was empty, but we had to wait for the socket engine to finish
     723        if (state == QAbstractSocket::ClosingState)
     724            q->disconnectFromHost();
     725
    696726        return false;
    697727    }
     
    730760    }
    731761
    732     if (writeBuffer.isEmpty() && socketEngine && socketEngine->isWriteNotificationEnabled())
     762    if (writeBuffer.isEmpty() && socketEngine && socketEngine->isWriteNotificationEnabled()
     763        && !socketEngine->bytesToWrite())
    733764        socketEngine->setWriteNotificationEnabled(false);
    734765    if (state == QAbstractSocket::ClosingState)
     
    851882        s += addresses.at(i).toString();
    852883    }
    853     s += "}";
     884    s += '}';
    854885    qDebug("QAbstractSocketPrivate::_q_startConnecting(hostInfo == %s)", s.toLatin1().constData());
    855886#endif
     
    10521083    if (socketEngine)
    10531084        socketEngine->setWriteNotificationEnabled(false);
     1085
    10541086    connectTimer->stop();
    10551087
     
    10621094    } else {
    10631095        _q_connectToNextAddress();
     1096    }
     1097}
     1098
     1099void QAbstractSocketPrivate::_q_forceDisconnect()
     1100{
     1101    Q_Q(QAbstractSocket);
     1102    if (socketEngine && socketEngine->isValid() && state == QAbstractSocket::ClosingState) {
     1103        socketEngine->close();
     1104        q->disconnectFromHost();
    10641105    }
    10651106}
     
    11281169/*! \internal
    11291170
    1130     Sets up the the internal state after the connection has succeeded.
     1171    Sets up the internal state after the connection has succeeded.
    11311172*/
    11321173void QAbstractSocketPrivate::fetchConnectionParameters()
     
    12321273    \a hostName may be an IP address in string form (e.g.,
    12331274    "43.195.83.32"), or it may be a host name (e.g.,
    1234     "qtsoftware.com"). QAbstractSocket will do a lookup only if
     1275    "example.com"). QAbstractSocket will do a lookup only if
    12351276    required. \a port is in native byte order.
    12361277
     
    12641305#endif
    12651306
    1266     if (d->state == ConnectedState || d->state == ConnectingState) {
    1267         qWarning("QAbstractSocket::connectToHost() called when already connecting/connected to \"%s\"", qPrintable(hostName));
     1307    if (d->state == ConnectedState || d->state == ConnectingState
     1308        || d->state == ClosingState || d->state == HostLookupState) {
     1309        qWarning("QAbstractSocket::connectToHost() called when already looking up or connecting/connected to \"%s\"", qPrintable(hostName));
    12681310        return;
    12691311    }
     
    15471589}
    15481590
     1591/*!
     1592    \since 4.6
     1593    Sets the given \a option to the value described by \a value.
     1594
     1595    \sa socketOption()
     1596*/
     1597void QAbstractSocket::setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value)
     1598{
     1599#ifndef QT_NO_OPENSSL
     1600    if (QSslSocket *sslSocket = qobject_cast<QSslSocket*>(this)) {
     1601        sslSocket->setSocketOption(option, value);
     1602        return;
     1603    }
     1604#endif
     1605
     1606    if (!d_func()->socketEngine)
     1607        return;
     1608
     1609    switch (option) {
     1610        case LowDelayOption:
     1611            d_func()->socketEngine->setOption(QAbstractSocketEngine::LowDelayOption, value.toInt());
     1612            break;
     1613
     1614        case KeepAliveOption:
     1615            d_func()->socketEngine->setOption(QAbstractSocketEngine::KeepAliveOption, value.toInt());
     1616            break;
     1617    }
     1618}
     1619
     1620/*!
     1621    \since 4.6
     1622    Returns the value of the \a option option.
     1623
     1624    \sa setSocketOption()
     1625*/
     1626QVariant QAbstractSocket::socketOption(QAbstractSocket::SocketOption option)
     1627{
     1628#ifndef QT_NO_OPENSSL
     1629    if (QSslSocket *sslSocket = qobject_cast<QSslSocket*>(this)) {
     1630        return sslSocket->socketOption(option);
     1631    }
     1632#endif
     1633
     1634    if (!d_func()->socketEngine)
     1635        return QVariant();
     1636
     1637    int ret = -1;
     1638    switch (option) {
     1639        case LowDelayOption:
     1640            ret = d_func()->socketEngine->option(QAbstractSocketEngine::LowDelayOption);
     1641            break;
     1642
     1643        case KeepAliveOption:
     1644            ret = d_func()->socketEngine->option(QAbstractSocketEngine::KeepAliveOption);
     1645            break;
     1646    }
     1647    if (ret == -1)
     1648        return QVariant();
     1649    else
     1650        return QVariant(ret);
     1651}
     1652
     1653
    15491654/*
    15501655   Returns the difference between msecs and elapsed. If msecs is -1,
     
    16571762
    16581763/*!
    1659     This function blocks until data is available for reading and the
     1764    This function blocks until new data is available for reading and the
    16601765    \l{QIODevice::}{readyRead()} signal has been emitted. The function
    16611766    will timeout after \a msecs milliseconds; the default timeout is
     
    16631768
    16641769    The function returns true if the readyRead() signal is emitted and
    1665     there is data available for reading; otherwise it returns false
     1770    there is new data available for reading; otherwise it returns false
    16661771    (if an error occurred or the operation timed out).
    16671772
     
    18651970
    18661971/*!
    1867     Aborts the current connection and resets the socket. Unlike
    1868     disconnectFromHost(), this function immediately closes the socket, discarding
    1869     any pending data in the write buffer.
     1972    Aborts the current connection and resets the socket. Unlike disconnectFromHost(),
     1973    this function immediately closes the socket, discarding any pending data in the
     1974    write buffer.
    18701975
    18711976    \sa disconnectFromHost(), close()
     
    21642269
    21652270/*!
    2166     Disconnects the socket's connection with the host.
     2271    Closes the I/O device for the socket, disconnects the socket's connection with the
     2272    host, closes the socket, and resets the name, address, port number and underlying
     2273    socket descriptor.
     2274
     2275    See QIODevice::close() for a description of the actions that occur when an I/O
     2276    device is closed.
    21672277
    21682278    \sa abort()
     
    22562366
    22572367        // Wait for pending data to be written.
    2258         if (d->socketEngine && d->socketEngine->isValid() && d->writeBuffer.size() > 0) {
     2368        if (d->socketEngine && d->socketEngine->isValid() && (d->writeBuffer.size() > 0
     2369            || d->socketEngine->bytesToWrite() > 0)) {
     2370            // hack: when we are waiting for the socket engine to write bytes (only
     2371            // possible when using Socks5 or HTTP socket engine), then close
     2372            // anyway after 2 seconds. This is to prevent a timeout on Mac, where we
     2373            // sometimes just did not get the write notifier from the underlying
     2374            // CFSocket and no progress was made.
     2375            if (d->writeBuffer.size() == 0 && d->socketEngine->bytesToWrite() > 0) {
     2376                if (!d->disconnectTimer) {
     2377                    d->disconnectTimer = new QTimer(this);
     2378                    connect(d->disconnectTimer, SIGNAL(timeout()), this,
     2379                            SLOT(_q_forceDisconnect()), Qt::DirectConnection);
     2380                }
     2381                if (!d->disconnectTimer->isActive())
     2382                    d->disconnectTimer->start(2000);
     2383            }
    22592384            d->socketEngine->setWriteNotificationEnabled(true);
    22602385
     
    24572582
    24582583#ifdef QT3_SUPPORT
    2459 /*! \enum QAbstractSocket::Error
     2584/*!
     2585    \enum QAbstractSocket::Error
    24602586    \compat
    24612587
     
    25892715        break;
    25902716    default:
    2591         debug << "QAbstractSocket::SocketError(" << int(error) << ")";
     2717        debug << "QAbstractSocket::SocketError(" << int(error) << ')';
    25922718        break;
    25932719    }
     
    26202746        break;
    26212747    default:
    2622         debug << "QAbstractSocket::SocketState(" << int(state) << ")";
     2748        debug << "QAbstractSocket::SocketState(" << int(state) << ')';
    26232749        break;
    26242750    }
Note: See TracChangeset for help on using the changeset viewer.