From ed0c0070f9b05c647019270dfc42073d071c830a Mon Sep 17 00:00:00 2001 From: Daniel Teske Date: Mon, 8 Dec 2014 15:29:19 +0100 Subject: Introduce qt_subtract_from_timeout to reduce code duplication. The same qt_timeout_value function was copied 5 times in qtbase's code, so provide a common implementation in QIoDevice that can be used by everyone. This commit also corrects the remaining time calculation in QProcess::waitForBytesWritten and QProcess::waitForFinished by using this new function. For QProcess::waitForFinished, if the process started within almost exactly the timeout time passed to waitForFinished, msecs - stopWatch.elapsed() would be -1, which is a special value. Change-Id: I7b76ee6bae695eafdd02e3db03e2ff1e23a7f40c Reviewed-by: Oswald Buddenhagen --- src/network/socket/qhttpsocketengine.cpp | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'src/network/socket/qhttpsocketengine.cpp') diff --git a/src/network/socket/qhttpsocketengine.cpp b/src/network/socket/qhttpsocketengine.cpp index b929ee088e4..7b601db79d1 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. } -- cgit v1.2.3