Qt限制带宽 [英] bandwidth throttling with Qt

查看:131
本文介绍了Qt限制带宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QNetworkAccessManager从Web下载文件,它为任务提供了简单的API.但是我希望为该类添加一个下载速率限制,因此所有http答复都不会超过该限制(我认为没有理由限制请求).

I'm using the QNetworkAccessManager to download files from the web, it provides an easy API to the task. But I wish to add a download rate limit to the class, so all http replies won't exceed that limit (I see no reason to limit the requests).

我在Google上搜索了一下,并在此处找到了有趣的帖子.但是它是QTcpSocket的子类,并使用管理一组套接字的单独类提供对带宽的控制.尽管这很好,但我想继续使用QNetworkAcessManager类.

I've googled abit and found an interesting post here. But what it does is subclass QTcpSocket and gives control over the bandwidth using a separate class that manages a set of sockets. While this is nice I want to continue using the QNetworkAcessManager class.

我看到的方式:

  1. 子类QNetworkAccessManager并以某种方式集成子类QTcpSocket(这可能涉及子类化更多类,即QHttp *类).
  2. 使用本文提供的类,并围绕这些类构建我自己的QNetworkAccessManager.
  3. QNetworkAccessManager允许使用代理.我可以编写一个伪代理类,其中将包含带宽限制逻辑.但这对我来说似乎是一个丑陋的入侵.

虽然前两个选项是可能的,但我想知道是否有更简单的方法来做到这一点?如果不是,您会建议哪一个?

While the first 2 options are possible, I was wondering if there's an easier way to do this? If not which one would you suggest?

推荐答案

我最终使用了本文和QHttp类.在使用QHttp进行获取/发布请求之前,我创建了一个RcTcpSocket,将其添加到我的RateController中并使用QHttp :: setSocket(QTcpSocket *).我仍然没有找到继续使用QNetworkAccessManager的解决方案,但这已经足够接近并且效果很好.

I ended up using the RcTcpSocket and RateController from this article with the QHttp class. Prior to making get/post requests with the QHttp I create a RcTcpSocket, add it to my RateController and use QHttp::setSocket(QTcpSocket*). I still didn't find a solution to keep using QNetworkAccessManager but this is close enough and works very good.

这篇关于Qt限制带宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆