使用Qt无线网络模块时在Windows下可能网络波动(WIFI打开的情况下)

18 篇文章 2 订阅

使用WIFI的时候,Qt的Wifi模块默认每隔10s会检测WIFI模块的运行情况,在Windows下这样的检测会导致网络波动。

解决方案是将Qt的默认10s改成-1s也就是不检测能显著消除网络波动。

设置环境变量以解决问题:

qputenv("QT_BEARER_POLL_TIMEOUT", QByteArray::number(-1));



Qt QNetworkAccessManager causing latency spikes on wifi

I don’t post a lot from my other life, the programming personality, most of my posts come from my adventures in virtualization. I have a bit more fun writing about virtualization than I do writing about programming, plus the number of coding blogs out there is enormous. But I’ve been troubleshooting an extremely hard to pin down issue with one of my applications (WhatPulse), where there would be a latency spike every 10 seconds (or less), only when primarily connected on wifi on Mac OS 10.12 (lower versions don’t seem affected). I’ve been pulling my hair on this because there was nothing to be found on it. Hence this contribution to the interwebs.

Pinging to your default gateway IP address looked like this:

After digging a bit further and looking at the files being opened by the app, I discovered that the Mac OS network settings files were being opened during these latency spikes.

Is it me?

WhatPulse counts and graphs network traffic, so it has a pcap integration to get a copy of the headers of each network packet to analyse. Due to this feature, I naturally thought the latency spikes were related to my programming. After removing all modules of the application and basically leaving a GUI without any data services attached to it, the issue was still happening. So, removed more code. And then some more. The spikes were still happening. After the code carnage, I decided to rule out the application itself and went looking on the OS. Thus far only Mac OS 10.12 users had reported issues and I didn’t have a Windows instance connected wifi-only, so had to rely on other peoples tests.

Posts on the Apple forums mentioned some issues with similar symptoms, so I submitted a bug report to Apple. No response of course.

After a while and some more code carnage, I got to the point where I removed the last QNetworkAccessManager class from being instantiated and something wonderful happened; the latency spikes stopped.

No, no it isn’t.

I could now focus the search on QNetworkAccessManager and these Qt bug reports reports turned up:

Cause

It appears as the culprit is here: qtbase / src / network / bearer / qnetworkconfigmanager_p.cpp

This looks simple and harmless enough; just check for configuration changes on network connectivity. It’s supposed to be able to carry over network connections create in Qt to new connections if the connectivity changes. But here’s the thing; it basically rescans and reconnects your network connectivity when you’re on wifi. Hence the latency spikes, it’s a reconnection not bad enough to drop network packets, but bad enough to cause a latency spike and affect real-time network applications (like games).

Work around

The Qt bugs are still open (oldest one is from July 2014, so it’s probably never going to be fixed?!?), so there’s no permanent fix. I also didn’t have the motivation or time to dive into the Qt code itself to look for a fix. BUT, we can work around it. The timer that triggers the reconnects looks for the environment variable QT_BEARER_POLL_TIMEOUT as its timer interval. So we can set that environment variable to fix the latency spikes. I simply put this in mymain():

After doing so, the ping output looks much better:

I hope this saves someone else the time I’ve put in to troubleshoot this.



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值