[Qt-interest] qtconcurrent::run() virtual member methods?
Daniel Bowen
qtmailinglist1 at bowensite.com
Wed Jun 10 02:31:30 CEST 2009
I was just contemplating making a request to add a timeout for
QNetworkRequest/Reply to Qt itself. What I'd like to see is at least one
timeout value, but ideally 2 new timeouts - a "connect timeout" and a
"read/write timeout". Then, if a timeout is encountered, the reply's error
could be QNetworkReply::TimeoutError (which is already defined). Connect
timeout would be how long before giving up on connecting to the resource,
and read/write timeout is how long to go before giving up on being able to
complete a read (on a get) or write (on a put or post). It could be done
using QNetworkRequest::Attribute - QNetworkRequest::ConnectTimeout and
QNetworkRequest::ReadWriteTimeout. Each of them could be a QVariant::Int.
If the QVariant is empty, or the value is -1, it could mean no timeout.
What's the best way to make that request formally?
In the meantime, here's one way to deal with timeouts. I've attached two
files - NetworkRequestTimeout.h and NetworkRequestTimeout.cpp. To use it,
you'd do something like this:
QNetworkRequest networkRequest;
// ... (Fill out the request)
QEventLoop eventLoop;
QNetworkReply* reply = networkManager.get(networkRequest);
QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
// Request timeout
NetworkRequestTimeout timeoutHandler(reply);
timeoutHandler.Start(30 * 1000); // Have a 30-second timeout
eventLoop.exec();
// "completed" will be true if the request completed, and false if it
timed out.
bool completed = timeoutHandler.Stop();
if(completed)
{
QByteArray contents = reply->readAll();
qDebug(contents.constData());
}
-Daniel
From: qt-interest-bounces at trolltech.com
[mailto:qt-interest-bounces at trolltech.com] On Behalf Of Liam Staskawicz
Sent: Tuesday, June 09, 2009 3:46 PM
To: qt-interest at trolltech.com
Subject: [Qt-interest] qtconcurrent::run() virtual member methods?
Hi - I was taking the opportunity of adding timeout functionality to the
QNetworkAccessManager api to experiment with QtConcurrent a bit. Since
there doesn't seem to be a built-in way to specify a timeout per request, I
thought I'd run each request in a thread via QtConcurrent and call
waitForReadyRead with my appropriate timeout value. Code looks roughly
like:
QNetworkReply* reply = netAccess.get(QNetworkRequest(url));
QFuture<bool> future = QtConcurrent::run( reply,
&QNetworkReply::waitForReadyRead, REQUEST_TIMEOUT );
but I'm getting the following compile error:
error: no matching function for call to 'run(QNetworkReply*&, bool
(QIODevice::*)(int), int)'
just for fun, I tried passing a method defined explicitly by QNetworkReply -
::manager() - and that seemed to compile happily. Any chance this has
something to do with the fact that waitForReadyRead is provided by
QIODevice? The doc for QtConcurrent::run() is mostly based on a few
examples so I wasn't able to glean too much more from it.
Secondarily, any other better timeout strategies for QNetworkAccessManager
would also be welcome!
Thanks,
Liam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090609/d058ae2c/attachment.html
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: NetworkRequestTimeout.h
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090609/d058ae2c/attachment.h
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: NetworkRequestTimeout.cpp
Url: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090609/d058ae2c/attachment.pl
More information about the Qt-interest-old
mailing list