diff --git a/src/corelib/io/qiodevice.cpp b/src/corelib/io/qiodevice.cpp index f1c42c9bb5..35911934df 100644 --- a/src/corelib/io/qiodevice.cpp +++ b/src/corelib/io/qiodevice.cpp @@ -1667,6 +1667,23 @@ QString QIODevice::errorString() const \sa read(), write() */ +/*! + \internal + \fn int qt_subtract_from_timeout(int timeout, int elapsed) + + Reduces the \a timeout by \a elapsed, taking into account that -1 is a + special value for timeouts. +*/ + +int qt_subtract_from_timeout(int timeout, int elapsed) +{ + if (timeout == -1) + return -1; + + timeout = timeout - elapsed; + return timeout < 0 ? 0 : timeout; +} + #if !defined(QT_NO_DEBUG_STREAM) QDebug operator<<(QDebug debug, QIODevice::OpenMode modes) diff --git a/src/corelib/io/qiodevice_p.h b/src/corelib/io/qiodevice_p.h index d764cb0fbb..ddd222e9a8 100644 --- a/src/corelib/io/qiodevice_p.h +++ b/src/corelib/io/qiodevice_p.h @@ -60,6 +60,8 @@ QT_BEGIN_NAMESPACE #define QIODEVICE_BUFFERSIZE Q_INT64_C(16384) #endif +Q_CORE_EXPORT int qt_subtract_from_timeout(int timeout, int elapsed); + // This is QIODevice's read buffer, optimized for read(), isEmpty() and getChar() class QIODevicePrivateLinearBuffer { diff --git a/src/corelib/io/qprocess.cpp b/src/corelib/io/qprocess.cpp index 94912ad616..baba9a0f9e 100644 --- a/src/corelib/io/qprocess.cpp +++ b/src/corelib/io/qprocess.cpp @@ -1793,8 +1793,7 @@ bool QProcess::waitForBytesWritten(int msecs) bool started = waitForStarted(msecs); if (!started) return false; - if (msecs != -1) - msecs -= stopWatch.elapsed(); + msecs = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); } return d->waitForBytesWritten(msecs); @@ -1830,8 +1829,7 @@ bool QProcess::waitForFinished(int msecs) bool started = waitForStarted(msecs); if (!started) return false; - if (msecs != -1) - msecs -= stopWatch.elapsed(); + msecs = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); } return d->waitForFinished(msecs); diff --git a/src/corelib/io/qprocess_unix.cpp b/src/corelib/io/qprocess_unix.cpp index cc154cfbc5..1c1c058c7a 100644 --- a/src/corelib/io/qprocess_unix.cpp +++ b/src/corelib/io/qprocess_unix.cpp @@ -1019,19 +1019,6 @@ void QProcessPrivate::killProcess() ::kill(pid_t(pid), SIGKILL); } -/* - Returns the difference between msecs and elapsed. If msecs is -1, - however, -1 is returned. -*/ -static int qt_timeout_value(int msecs, int elapsed) -{ - if (msecs == -1) - return -1; - - int timeout = msecs - elapsed; - return timeout < 0 ? 0 : timeout; -} - bool QProcessPrivate::waitForStarted(int msecs) { Q_Q(QProcess); @@ -1106,7 +1093,7 @@ bool QProcessPrivate::waitForReadyRead(int msecs) if (!stdinChannel.buffer.isEmpty() && stdinChannel.pipe[1] != -1) add_fd(nfds, stdinChannel.pipe[1], &fdwrite); - int timeout = qt_timeout_value(msecs, stopWatch.elapsed()); + int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); #ifdef Q_OS_BLACKBERRY int ret = bb_select(notifiers, nfds + 1, &fdread, &fdwrite, timeout); #else @@ -1187,7 +1174,7 @@ bool QProcessPrivate::waitForBytesWritten(int msecs) if (!stdinChannel.buffer.isEmpty() && stdinChannel.pipe[1] != -1) add_fd(nfds, stdinChannel.pipe[1], &fdwrite); - int timeout = qt_timeout_value(msecs, stopWatch.elapsed()); + int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); #ifdef Q_OS_BLACKBERRY int ret = bb_select(notifiers, nfds + 1, &fdread, &fdwrite, timeout); #else @@ -1262,7 +1249,7 @@ bool QProcessPrivate::waitForFinished(int msecs) if (!stdinChannel.buffer.isEmpty() && stdinChannel.pipe[1] != -1) add_fd(nfds, stdinChannel.pipe[1], &fdwrite); - int timeout = qt_timeout_value(msecs, stopWatch.elapsed()); + int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); #ifdef Q_OS_BLACKBERRY int ret = bb_select(notifiers, nfds + 1, &fdread, &fdwrite, timeout); #else diff --git a/src/corelib/io/qwinoverlappedionotifier.cpp b/src/corelib/io/qwinoverlappedionotifier.cpp index de0e205d26..7a005430ea 100644 --- a/src/corelib/io/qwinoverlappedionotifier.cpp +++ b/src/corelib/io/qwinoverlappedionotifier.cpp @@ -41,6 +41,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE @@ -332,11 +333,9 @@ bool QWinOverlappedIoNotifier::waitForNotified(int msecs, OVERLAPPED *overlapped return false; if (triggeredOverlapped == overlapped) return true; - if (msecs != -1) { - t = msecs - stopWatch.elapsed(); - if (t < 0) - return false; - } + msecs = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); + if (t == 0) + return false; } } diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index 5a1ad40b90..26667715be 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -1976,20 +1976,6 @@ QVariant QAbstractSocket::socketOption(QAbstractSocket::SocketOption option) return QVariant(ret); } - -/* - Returns the difference between msecs and elapsed. If msecs is -1, - however, -1 is returned. -*/ -static int qt_timeout_value(int msecs, int elapsed) -{ - if (msecs == -1) - return -1; - - int timeout = msecs - elapsed; - return timeout < 0 ? 0 : timeout; -} - /*! Waits until the socket is connected, up to \a msecs milliseconds. If the connection has been established, this @@ -2067,7 +2053,7 @@ bool QAbstractSocket::waitForConnected(int msecs) int attempt = 1; #endif while (state() == ConnectingState && (msecs == -1 || stopWatch.elapsed() < msecs)) { - int timeout = qt_timeout_value(msecs, stopWatch.elapsed()); + int timeout = qt_subtract_from_timeout(msecs, stopWatch.elapsed()); if (msecs != -1 && timeout > QT_CONNECT_TIMEOUT) timeout = QT_CONNECT_TIMEOUT; #if defined (QABSTRACTSOCKET_DEBUG) @@ -2146,7 +2132,7 @@ bool QAbstractSocket::waitForReadyRead(int msecs) bool readyToRead = false; bool readyToWrite = false; if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, true, !d->writeBuffer.isEmpty(), - qt_timeout_value(msecs, stopWatch.elapsed()))) { + qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { d->socketError = d->socketEngine->error(); setErrorString(d->socketEngine->errorString()); #if defined (QABSTRACTSOCKET_DEBUG) @@ -2169,7 +2155,7 @@ bool QAbstractSocket::waitForReadyRead(int msecs) if (state() != ConnectedState) return false; - } while (msecs == -1 || qt_timeout_value(msecs, stopWatch.elapsed()) > 0); + } while (msecs == -1 || qt_subtract_from_timeout(msecs, stopWatch.elapsed()) > 0); return false; } @@ -2218,7 +2204,7 @@ bool QAbstractSocket::waitForBytesWritten(int msecs) bool readyToRead = false; bool readyToWrite = false; if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, true, !d->writeBuffer.isEmpty(), - qt_timeout_value(msecs, stopWatch.elapsed()))) { + qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { d->socketError = d->socketEngine->error(); setErrorString(d->socketEngine->errorString()); #if defined (QABSTRACTSOCKET_DEBUG) @@ -2300,7 +2286,7 @@ bool QAbstractSocket::waitForDisconnected(int msecs) bool readyToWrite = false; if (!d->socketEngine->waitForReadOrWrite(&readyToRead, &readyToWrite, state() == ConnectedState, !d->writeBuffer.isEmpty(), - qt_timeout_value(msecs, stopWatch.elapsed()))) { + qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { d->socketError = d->socketEngine->error(); setErrorString(d->socketEngine->errorString()); #if defined (QABSTRACTSOCKET_DEBUG) diff --git a/src/network/socket/qhttpsocketengine.cpp b/src/network/socket/qhttpsocketengine.cpp index b929ee088e..7b601db79d 100644 --- a/src/network/socket/qhttpsocketengine.cpp +++ b/src/network/socket/qhttpsocketengine.cpp @@ -36,6 +36,7 @@ #include "qhostaddress.h" #include "qurl.h" #include "private/qhttpnetworkreply_p.h" +#include "private/qiodevice_p.h" #include "qelapsedtimer.h" #include "qnetworkinterface.h" @@ -330,19 +331,6 @@ bool QHttpSocketEngine::setOption(SocketOption option, int value) return false; } -/* - Returns the difference between msecs and elapsed. If msecs is -1, - however, -1 is returned. -*/ -static int qt_timeout_value(int msecs, int elapsed) -{ - if (msecs == -1) - return -1; - - int timeout = msecs - elapsed; - return timeout < 0 ? 0 : timeout; -} - bool QHttpSocketEngine::waitForRead(int msecs, bool *timedOut) { Q_D(const QHttpSocketEngine); @@ -355,7 +343,7 @@ bool QHttpSocketEngine::waitForRead(int msecs, bool *timedOut) // Wait for more data if nothing is available. if (!d->socket->bytesAvailable()) { - if (!d->socket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { + if (!d->socket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { if (d->socket->state() == QAbstractSocket::UnconnectedState) return true; setError(d->socket->error(), d->socket->errorString()); @@ -367,7 +355,7 @@ bool QHttpSocketEngine::waitForRead(int msecs, bool *timedOut) // If we're not connected yet, wait until we are, or until an error // occurs. - while (d->state != Connected && d->socket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { + while (d->state != Connected && d->socket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { // Loop while the protocol handshake is taking place. } @@ -403,7 +391,7 @@ bool QHttpSocketEngine::waitForWrite(int msecs, bool *timedOut) // If we're not connected yet, wait until we are, and until bytes have // been received (i.e., the socket has connected, we have sent the // greeting, and then received the response). - while (d->state != Connected && d->socket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { + while (d->state != Connected && d->socket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { // Loop while the protocol handshake is taking place. } diff --git a/src/network/socket/qsocks5socketengine.cpp b/src/network/socket/qsocks5socketengine.cpp index 1634352333..60f1c2fa50 100644 --- a/src/network/socket/qsocks5socketengine.cpp +++ b/src/network/socket/qsocks5socketengine.cpp @@ -47,6 +47,7 @@ #include "qcoreapplication.h" #include "qurl.h" #include "qauthenticator.h" +#include "private/qiodevice_p.h" #include #include @@ -269,19 +270,6 @@ static int qt_socks5_get_host_address_and_port(const QByteArray &buf, QHostAddre return ret; } -/* - Returns the difference between msecs and elapsed. If msecs is -1, - however, -1 is returned. -*/ -static int qt_timeout_value(int msecs, int elapsed) -{ - if (msecs == -1) - return -1; - - int timeout = msecs - elapsed; - return timeout < 0 ? 0 : timeout; -} - struct QSocks5Data { QTcpSocket *controlSocket; @@ -1396,7 +1384,7 @@ bool QSocks5SocketEngine::bind(const QHostAddress &addr, quint16 port) dummy.setProxy(QNetworkProxy::NoProxy); if (!dummy.bind() || writeDatagram(0,0, d->data->controlSocket->localAddress(), dummy.localPort()) != 0 - || !dummy.waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed())) + || !dummy.waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed())) || dummy.readDatagram(0,0, &d->localAddress, &d->localPort) != 0) { QSOCKS5_DEBUG << "udp actual address and port lookup failed"; setState(QAbstractSocket::UnconnectedState); @@ -1484,7 +1472,7 @@ void QSocks5SocketEngine::close() QElapsedTimer stopWatch; stopWatch.start(); while (!d->data->controlSocket->bytesToWrite()) { - if (!d->data->controlSocket->waitForBytesWritten(qt_timeout_value(msecs, stopWatch.elapsed()))) + if (!d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) break; } } @@ -1740,7 +1728,7 @@ bool QSocks5SocketEnginePrivate::waitForConnected(int msecs, bool *timedOut) stopWatch.start(); while (socks5State != wantedState) { - if (!data->controlSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { + if (!data->controlSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { if (data->controlSocket->state() == QAbstractSocket::UnconnectedState) return true; @@ -1774,7 +1762,7 @@ bool QSocks5SocketEngine::waitForRead(int msecs, bool *timedOut) if (d->mode == QSocks5SocketEnginePrivate::ConnectMode || d->mode == QSocks5SocketEnginePrivate::BindMode) { while (!d->readNotificationActivated) { - if (!d->data->controlSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { + if (!d->data->controlSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { if (d->data->controlSocket->state() == QAbstractSocket::UnconnectedState) return true; @@ -1787,7 +1775,7 @@ bool QSocks5SocketEngine::waitForRead(int msecs, bool *timedOut) #ifndef QT_NO_UDPSOCKET } else { while (!d->readNotificationActivated) { - if (!d->udpData->udpSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { + if (!d->udpData->udpSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { setError(d->udpData->udpSocket->error(), d->udpData->udpSocket->errorString()); if (timedOut && d->udpData->udpSocket->error() == QAbstractSocket::SocketTimeoutError) *timedOut = true; @@ -1824,11 +1812,11 @@ bool QSocks5SocketEngine::waitForWrite(int msecs, bool *timedOut) // flush any bytes we may still have buffered in the time that we have left if (d->data->controlSocket->bytesToWrite()) - d->data->controlSocket->waitForBytesWritten(qt_timeout_value(msecs, stopWatch.elapsed())); + d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); while ((msecs == -1 || stopWatch.elapsed() < msecs) && d->data->controlSocket->state() == QAbstractSocket::ConnectedState && d->data->controlSocket->bytesToWrite() >= MaxWriteBufferSize) - d->data->controlSocket->waitForBytesWritten(qt_timeout_value(msecs, stopWatch.elapsed())); + d->data->controlSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); return d->data->controlSocket->bytesToWrite() < MaxWriteBufferSize; } diff --git a/src/network/ssl/qsslsocket.cpp b/src/network/ssl/qsslsocket.cpp index c0c15fb5b1..39cd562bc4 100644 --- a/src/network/ssl/qsslsocket.cpp +++ b/src/network/ssl/qsslsocket.cpp @@ -301,19 +301,6 @@ QT_BEGIN_NAMESPACE -/* - Returns the difference between msecs and elapsed. If msecs is -1, - however, -1 is returned. -*/ -static int qt_timeout_value(int msecs, int elapsed) -{ - if (msecs == -1) - return -1; - - int timeout = msecs - elapsed; - return timeout < 0 ? 0 : timeout; -} - class QSslSocketGlobalData { public: @@ -1518,7 +1505,7 @@ bool QSslSocket::waitForEncrypted(int msecs) startClientEncryption(); // Loop, waiting until the connection has been encrypted or an error // occurs. - if (!d->plainSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) + if (!d->plainSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) return false; } return d->connectionEncrypted; @@ -1562,7 +1549,7 @@ bool QSslSocket::waitForReadyRead(int msecs) // test readyReadEmitted first because either operation above // (waitForEncrypted or transmit) may have set it while (!readyReadEmitted && - d->plainSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) { + d->plainSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) { } d->readyReadEmittedPointer = previousReadyReadEmittedPointer; @@ -1593,7 +1580,7 @@ bool QSslSocket::waitForBytesWritten(int msecs) d->transmit(); } - return d->plainSocket->waitForBytesWritten(qt_timeout_value(msecs, stopWatch.elapsed())); + return d->plainSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); } /*! @@ -1626,7 +1613,7 @@ bool QSslSocket::waitForDisconnected(int msecs) if (!waitForEncrypted(msecs)) return false; } - bool retVal = d->plainSocket->waitForDisconnected(qt_timeout_value(msecs, stopWatch.elapsed())); + bool retVal = d->plainSocket->waitForDisconnected(qt_subtract_from_timeout(msecs, stopWatch.elapsed())); if (!retVal) { setSocketState(d->plainSocket->state()); setSocketError(d->plainSocket->error()); diff --git a/tests/auto/other/networkselftest/networkselftest.pro b/tests/auto/other/networkselftest/networkselftest.pro index c8b870128d..22208e02fb 100644 --- a/tests/auto/other/networkselftest/networkselftest.pro +++ b/tests/auto/other/networkselftest/networkselftest.pro @@ -2,7 +2,7 @@ CONFIG += testcase TARGET = tst_networkselftest SOURCES += tst_networkselftest.cpp -QT = core network testlib +QT = core core-private network testlib win32:CONFIG += insignificant_test # QTBUG-27571 DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 diff --git a/tests/auto/other/networkselftest/tst_networkselftest.cpp b/tests/auto/other/networkselftest/tst_networkselftest.cpp index 36c9027aca..5472b74c07 100644 --- a/tests/auto/other/networkselftest/tst_networkselftest.cpp +++ b/tests/auto/other/networkselftest/tst_networkselftest.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #ifndef QT_NO_BEARERMANAGEMENT #include @@ -171,10 +172,11 @@ static bool doSocketRead(QTcpSocket *socket, int minBytesAvailable, int timeout forever { if (socket->bytesAvailable() >= minBytesAvailable) return true; + timeout = qt_subtract_from_timeout(timeout, timer.elapsed()); if (socket->state() == QAbstractSocket::UnconnectedState - || timer.elapsed() >= timeout) + || timeout == 0) return false; - if (!socket->waitForReadyRead(timeout - timer.elapsed())) + if (!socket->waitForReadyRead(timeout)) return false; } } @@ -202,10 +204,11 @@ static bool doSocketFlush(QTcpSocket *socket, int timeout = 4000) #endif ) return true; + timeout = qt_subtract_from_timeout(timeout, timer.elapsed()); if (socket->state() == QAbstractSocket::UnconnectedState - || timer.elapsed() >= timeout) + || timeout == 0) return false; - if (!socket->waitForBytesWritten(timeout - timer.elapsed())) + if (!socket->waitForBytesWritten(timeout)) return false; } }