Results 1 to 6 of 6

Thread: Synchronous Http (Good reason)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: Synchronous Http (Good reason)

    Yes, this is what I thought... First, I tried creating and exec()-ing QEventLoop. It worked, but I did not like it, partially because I was not sure how exactly it worked and how expensive it was... The QT docs for QEventLoop are not very good I am afraid

    I think that the solution I have right now is slightly better (it does not interfere with the main even loop), but I may be wrong. Here it is (maybe someone else will find it helpful, the timeout parameter is currently ignored):

    //
    // fetches the file data over http and writes it to the device; returns true if succeeds and false if an error is encountered or the http request is timed-out.
    //
    bool HttpFileFetcher::FetchFile(const QString* url, QIODevice* device, UINT32 timeout) {
    bool rval = IS_TRUE(url != NULL && device != NULL);
    if (rval) {
    QUrl qUrl(*url);

    rval = IS_TRUE(qUrl.isValid());
    if (rval) {
    setHost(qUrl.host());
    requestId_ = get(*url, device);

    while(currentId() != 0) {
    qApp->processEvents();
    }

    rval = !error_;
    }
    }

    return rval;
    }

    //
    // called when the http request is done.
    //
    void HttpFileFetcher::RequestFinishedSlot(int requestId, bool error) {
    if (requestId_ == requestId) {
    if (error) {
    error_ = error;
    Log(MessageFilter::ALERT1) << L"failed to fetch an http resource: " << errorString().toAscii().data() << Log::endl;
    }
    }
    }

    //
    // fetches the file data over http and writes it to the device; returns true if succeeds and false if an error is encountered or the http request is timed-out.
    //
    bool HttpFileFetcher::Fetch(const QString* url, QIODevice* device, UINT32 timeout) {
    HttpFileFetcher fileFetcher;
    return fileFetcher.FetchFile(url, device, timeout);
    }

    //
    // fetches the file data over http and writes it to the buffer; returns true if succeeds and false if an error is encountered or the http request is timed-out.
    //
    bool HttpFileFetcher::Fetch(const QString* url, QByteArray* buffer, UINT32 timeout) {
    bool rval = IS_TRUE(url != NULL && buffer != NULL);
    if (rval) {
    QBuffer device(buffer);
    device.open(QIODevice::WriteOnly);

    rval = IS_TRUE(device.isOpen());
    if (rval) {
    HttpFileFetcher fileFetcher;
    rval = fileFetcher.FetchFile(url, &device, timeout);
    }
    }

    return rval;
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Synchronous Http (Good reason)

    Quote Originally Posted by umbrella View Post
    Yes, this is what I thought... First, I tried creating and exec()-ing QEventLoop. It worked, but I did not like it, partially because I was not sure how exactly it worked and how expensive it was...
    It's the same loop Qt uses for its application loop. I mean similar, not the same instance.

    The QT docs for QEventLoop are not very good I am afraid
    Because it is very rarely used, so there is no point in documenting it too much.

    I think that the solution I have right now is slightly better (it does not interfere with the main even loop), but I may be wrong. Here it is (maybe someone else will find it helpful, the timeout parameter is currently ignored):
    It's not better. It's almost the same, just that any optimizations to the loop performed by QEventLoop (if there are any) are not used by your code.

  3. #3
    Join Date
    Apr 2008
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    1

    Default Re: Synchronous Http (Good reason)

    It's not better. It's almost the same, just that any optimizations to the loop performed by QEventLoop (if there are any) are not used by your code.[/QUOTE]

    So how exactly does this work, doesn't another executing QEventLoop block the main QT event loop? Or QT manages to jump from executing one loop to another?

    Thanks.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Synchronous Http (Good reason)

    Quote Originally Posted by umbrella View Post
    So how exactly does this work, doesn't another executing QEventLoop block the main QT event loop?
    It does. That's the whole point.

    Or QT manages to jump from executing one loop to another?
    Look at how modal dialogs (ones triggered using QDialog::exec()) work - they use the same mechanism, they have an internal event loop.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.