QEventLoop 和 QNAM 的 QTimer 超时问题
我创建了自己的 HTTP 类,它利用 QNAM 并提供发送 HTTP 请求的方法。它使用 QEventLoop 进行同步,使用 QTimer 进行超时。
我的解决方案遇到了一些问题。在某些 Symbian 平台上,我的 QTimer 信号超时太快(例如,当超时为 30 秒时,1 秒后)。如果我的 HTTP Post 播放负载很大或者我通过 GET 下载文件(请求需要一些时间才能完成),通常会发生这种情况。我想指出的是,相同的代码在某些设备(S60 第三版)上运行良好,但另一方面,某些设备(第五版)几乎总是出现此错误。
下面是一个代码片段:
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();
}
}
就我个人而言,我认为以下原因之一可能会导致问题:
- 信号
- 重新进入本地事件循环会扰乱我多次使用相同 QNAM 的 (同一会话期间的多个请求)。这是必需的,因为如果我破坏 QNAM,我的会话就会在服务器端中断。
有谁能够看到一些可能导致此行为的错误吗?
平台:Symbian S60 第 3/5 版
工具:Nokia Qt SDK
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也正是遇到这样的问题。使用 local 方法 QEventLoop 会产生奇怪的结果,例如阻塞某些要处理的事件(然后循环永远不会退出),或者像解释的那样,导致 QTimer 在超时之前触发得太快(然后循环太早退出)。
使用在循环的父对象的构造函数中初始化一次的循环实例字段似乎可以解决问题。
我使用的是 Qt 4.6.3 和 Symbian S60/5th Edition。
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.