开发者

QTimer timeout problems with QEventLoop and QNAM

开发者 https://www.devze.com 2023-01-17 20:34 出处:网络
I have created my own HTTP class that utilizes QNAM and provides means for sending HTTP requests. It uses QEventLoop for synchronization and QTimer for timeouts.

I have created my own HTTP class that utilizes QNAM and provides means for sending HTTP requests. It uses QEventLoop for synchronization and QTimer for timeouts.

I'm facing few problems with my solution. On certain Symbian platforms my QTimer signals timeout too fast (e.g. like after 1 sec when timeout is 30 secs). This happends usually if my HTTP Post playload is large or if I'm downloading a file via GET (request takes some time to complete). I want to note that same code works fine on certain devices (S60 3rd ed.) but on the other hand some devices (5th edition) get this error almost all the time.

Here is a code snippet:

MyHttp::MyHttp(QObject *parent) : QObject(parent)
{
    m_Timer.setSingleShot(true);
    connect(&m_Manager, SIGNAL(finished(QNetworkReply*)), SLOT(OnFinished(QNetworkReply*)));
    connect(&m_Timer, SIGNAL(timeout()), SLOT(OnTimeout()));
}


void MyHttp::Post(const QString &data)
{
    m_RetCode = 0;
    QNetworkRequest request(url);
    m_Reply = m_Manager.post(request, data.toAscii());  // QPointer<QNetworkReply> m_Reply

    m_Timer.start(30*1000); 
    m_EventLoop.exec(); // Synchronization point
}


void MyHttp::OnFinished(QNetworkReply * reply)
{
    // Handle response / Timeout / Errors

    reply->deleteLater(); // Use deleteLater() as adviced in the documentation
    StopWaiting();
}


void MyHttp::StopWaiting()
{
    m_Timer.stop();
    m_EventLoop.exit();
}

void MyHttp::OnTimeout()
{
    m_RetCode = TIMEOUT; // #define TIMEOUT 50000

    if(m_Reply.isNull() == false)
    {
        // Abort reply
        m_Reply->abort();
    }
}

Personally I think that one of the following might cause the problem:

  • re-entering local event loop messes up the signals
  • I'm utilizing same QNAM multiple times (several request during same session). This is required because if I destroy the QNAM my开发者_开发技巧 session goes down on the server side.

Is anyone able to see some errors that might cause this behavior?

Platform: Symbian S60 3rd/5th edition

Tools: Nokia Qt SDK


I've exactly that sort of problems as well. Using local to the method QEventLoop produces strange results, like blocking some events to be processed (and then loop never exits), or like explained, provoking QTimer to fire too fast before timeout (and then loop exits too early). Using an instance field for the loop initialized once in the constructor of parent object of the loop, seems to solve the problem. I'm on Qt 4.6.3 and Symbian S60/5th Edition.

0

精彩评论

暂无评论...
验证码 换一张
取 消