PDA

View Full Version : QNetworkAccessManager bandwidth throttling



bluefly
10th April 2009, 22:33
Hi,

I'm using the QNetworkAccessManager to manage some downloads from websites. I'd also like to limit the download rate.
I came upon this wonderful article http://doc.trolltech.com/qq/qq17-ratecontrol.html that implements bandwidth throttling but unfortunately (for me) it does so by subclassing the QTcpSocket. I don't have that kind of control in the QNetworkAccessManager class.

Does anyone have a suggestion how I would go about implementing (possibly integrating/using the source code in the article) bandwidth throttling in the QNetworkAccessManager?

wysota
12th April 2009, 00:16
I don't think you can do that. The only virtual method in this class is QNetworkAccessManager::createRequest but this won't help you. The only thing you could do is to implement your own backend for QNetworkAccessManager but then it would be like doing everything from scratch.

bluefly
12th April 2009, 09:50
I don't think you can do that. The only virtual method in this class is QNetworkAccessManager::createRequest but this won't help you. The only thing you could do is to implement your own backend for QNetworkAccessManager but then it would be like doing everything from scratch.

Thank you for your reply.
Do you have any other suggestion in mind other than implementing everything from scratch? Maybe I could copy the QNetworkAccessManager code and change some code there?

faldzip
12th April 2009, 12:12
Maybe I could copy the QNetworkAccessManager code and change some code there?
You can change QNetworkAccessManager in your Qt sources as it is open source. Copying the code would be complicated because of "pimpl" - private implementation. Most of Qt classes (QNetworkAccessManager also) uses a lot of stuff which is not easily available for you when you write your program with Qt or using it is a bit dangerous as in those private impl headers stands:


//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of the Network Access API. This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

So I would rather try to understand the QNetworkAccessManager code and modify it in it's source code.

bluefly
12th April 2009, 15:08
You can change QNetworkAccessManager in your Qt sources as it is open source. Copying the code would be complicated because of "pimpl" - private implementation. Most of Qt classes (QNetworkAccessManager also) uses a lot of stuff which is not easily available for you when you write your program with Qt or using it is a bit dangerous as in those private impl headers stands:


//
// W A R N I N G
// -------------
//
// This file is not part of the Qt API. It exists for the convenience
// of the Network Access API. This header file may change from
// version to version without notice, or even be removed.
//
// We mean it.
//

So I would rather try to understand the QNetworkAccessManager code and modify it in it's source code.

actually I tried to take a look at QNetworkAccessManager code and think of changing it but it looks so complicated... and all those private classes make it impossible! even if I do dig more into it I highly doubt there's a place I could just replace "QTcpSocket" with "MySocket" and it will work just like that...
so the way I see it now the best option is not to use QNetworkAccessManager but instead work around the class proposed in the article and extend it.

bluefly
12th April 2009, 15:58
I just had a nice idea (well it's a dirty hack, but still):

1. Create a Qt Library with class that uses QNetworkAccessManager (this will replace QNetworkAccessManager for me).
2. Change QTcpSocket code in Qt to include the changes in the article.
3. Do the other changes that are mentioned in the article in the appropriate place.
4. Build & Link to Qt statically.
5. Restore old Qt code.
6. Use the library in my code instead of QNetworkAccessManager.

The main idea is that I'm guessing somewhere QNetworkAccessManager uses QTcpSocket, so if I change QTcpSocket (rather than subclass it) I will get the desired result. I need the separate library since I don't want to change QTcpSocket in my "original" Qt, only where I need bandwidth throttling.
The potential side effect is that QNetworkAccessManager might be using QTcpSocket in places where the modified one will break it.

wysota
14th April 2009, 20:49
Thank you for your reply.
Do you have any other suggestion in mind other than implementing everything from scratch? Maybe I could copy the QNetworkAccessManager code and change some code there?

You can use qFindChild() to access the tcp socket that handles your request and use event filters on it or connect to it to change its behaviour. But I don't know if that will be sufficient for you. It might be easier to reimplement createRequest() to have your own backend for QNetworkAccessManager.