Can we restrict QNetworkAccessManager from consuming whole bandwidth, by restricting the download speed, as we do see such options available with almost every download manager?
Can we restrict QNetworkAccessManager from consuming whole bandwidth, by restricting the download speed, as
Share
This is not possible out of the box. But have a look at the Qt Torrent Example, especially the class
RateController(ratecontroller.h | ratecontroller.cpp). This class does almost what you want by controlling not only one but a set of connections.However, this rate controller is operating on
QTcpSockets(to be exact onPeerWireClients), so you need to change the type of the “peers” to beQIODevice, which I hope isn’t that hard, sincePeerWireClientinherits fromQTcpSocket, which itself inherits fromQIODevice:(Note that the RateController from the Torrent example controlls both upload and download, but you only need to control the download rate. So you can remove unnecessary code.)
Then you need to make requests made by your
QNetworkAccessManageruse this rate controller. This can be done by reimplementingQNetworkAccessManagerand overwriting (extending) the methodQNetworkAccessManager::createRequest, which will be called whenever a new request gets created. This method returns theQNetworkReply*(which inherits fromQIODevice*) where the download will be read from, so telling the rate controller to control this device will limit the download rate:You will not have to subclass QNetworkAccessManager if you already know the pieces of code where you actually perform requests. The methods
get()andpost()return aQNetworkReply*which you can also just add to the rate controller. (But this way, you manually do this outside of the manager, which is doesn’t fulfill the concept of information/implementation hiding, in this case the fact that downloads are rate-controlled.)