Using QNetworkReply::setReadBufferSize() to control download rate of GET requests.
-
I'm curious that the following sentence means in practice:
bq. Unlike QAbstractSocket::setReadBufferSize(), QNetworkReply cannot guarantee precision in the read buffer size. That is, bytesAvailable() can return more than size.
I'm trying to create an application that downloads files and control the download rate.
To control the download rate i've attempted to first set the readbuffer size to 4096 bytes(arbitrary low number). And then connect the qnetworkreply::readyread signal to a slot that first checks if there according to the download rate are any more bytes that can be downloaded and if there is then read from the reply and if there is not wait untill the next tick replenishes the number of bytes availiable.
@reply = controller->nam()->get(req);
reply->setReadBufferSize(READ_BUFFER_SIZE);
connect(reply,SIGNAL(readyRead()),this,SLOT(networkHasBytes()));@The networkHasBytes slot goes something like:
@qint64 bytesToRead = reply->bytesAvailable();
if(bytesToRead > controller->avaliableBytes())
qint64 written = device->write(reply->read(bytesToRead));
else
waiting = true;@The controller->avaliableBytes() returns the number of bytes that the rate controller says is free for the next tick.
And at the beginning of each tick the networkHasBytes slot is called again for each download that is waiting(waiting == true) and reads whatever bytes are availiable.What is bothering me is twofold.
First, reply->bytesAvailiable() always returns a much larger number then i set the readbuffer too suggesting that the qnetworkaccessmanager is buffering somewhere else aswell and that reducing the buffersize and then controlling the reading is not really reducing the rate.
Second, that occasionally the download finishes with the QNetworkReply::ProtocolFailure error. "Data Corrupted" is the errorString().
I have no clue whats causing this but i suspect it has something to do with the 'wierd?' buffer behaviour i'm seeing and that is causing me to suspect thatbq. QNetworkReply cannot guarantee precision in the read buffer size.
makes this way of trying to throttle the download rate unworkable.
Any suggestions as to what is causing the "data corrupted" error?
Or what it actually means? For instance is one buffer overwriting another buffer or might it be the webserver that is causing the error?