Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implementing a download manager that supports resuming

I intend on writing a small download manager in C++ that supports resuming (and multiple connections per download).

From the info I gathered so far, when sending the http request I need to add a header field with a key of "Range" and the value "bytes=startoff-endoff". Then the server returns a http response with the data between those offsets.

So roughly what I have in mind is to split the file to the number of allowed connections per file and send a http request per splitted part with the appropriate "Range". So if I have a 4mb file and 4 allowed connections, I'd split the file to 4 and have 4 http requests going, each with the appropriate "Range" field. Implementing the resume feature would involve remembering which offsets are already downloaded and simply not request those.

  • Is this the right way to do this?
  • What if the web server doesn't support resuming? (my guess is it will ignore the "Range" and just send the entire file)
  • When sending the http requests, should I specify in the range the entire splitted size? Or maybe ask smaller pieces, say 1024k per request?
  • When reading the data, should I write it immediately to the file or do some kind of buffering? I guess it could be wasteful to write small chunks.
  • Should I use a memory mapped file? If I remember correctly, it's recommended for frequent reads rather than writes (I could be wrong). Is it memory wise? What if I have several downloads simultaneously?
  • If I'm not using a memory mapped file, should I open the file per allowed connection? Or when needing to write to the file simply seek? (if I did use a memory mapped file this would be really easy, since I could simply have several pointers).

Note: I'll probably be using Qt, but this is a general question so I left code out of it.

like image 673
Idan K Avatar asked Apr 04 '09 16:04

Idan K


4 Answers

Regarding the request/response:

for a Range-d request, you could get three different responses:

206 Partial Content - resuming supported and possible; check Content-Range header for size/range of response
200 OK - byte ranges ("resuming") not supported, whole resource ("file") follows
416 Requested Range Not Satisfiable - incorrect range (past EOF etc.)

Content-Range usu. looks like this: Content-Range: bytes 21010-47000/47022, that is bytes start-end/total.

Check the HTTP spec for details, esp. sections 14.5, 14.16 and 14.35

like image 90
Piskvor left the building Avatar answered Nov 15 '22 21:11

Piskvor left the building


I am not an expert on C++, however, I had once done a .net application which needed similar functionality (download scheduling, resume support, prioritizing downloads)

i used microsoft bits (Background Intelligent Transfer Service) component - which has been developed in c. windows update uses BITS too. I went for this solution because I don't think I am a good enough a programmer to write something of this level myself ;-)

Although I am not sure if you can get the code of BITS - I do think you should just have a look at its documentation which might help you understand how they implemented it, the architecture, interfaces, etc.

Here it is - http://msdn.microsoft.com/en-us/library/aa362708(VS.85).aspx

like image 30
Raj Avatar answered Nov 15 '22 21:11

Raj


I can't answer all your questions, but here is my take on two of them.

Chunk size

There are two things you should consider about chunk size:

  1. The smaller they are the more overhead you get form sending the HTTP request.
  2. With larger chunks you run the risk of re-downloading the same data twice, if one download fails.

I'd recommend you go with smaller chunks of data. You'll have to do some test to see what size is best for your purpose though.

In memory vs. files

You should write the data chunks to in memory buffer, and then when it is full write it to the disk. If you are going to download large files, it can be troublesome for your users, if they run out of RAM. If I remember correctly the IIS stores requests smaller than 256kb in memory, anything larger will be written to the disk, you may want to consider a simmilar approach.

like image 35
Jesper Fyhr Knudsen Avatar answered Nov 15 '22 21:11

Jesper Fyhr Knudsen


Besides keeping track of what were the offsets marking the beginning of your segments and each segment length (unless you want to compute that upon resume, which would involve sort the offset list and calculate the distance between two of them) you will want to check the Accept-Ranges header of the HTTP response sent by the server to make sure it supports the usage of the Range header. The best way to specify the range is "Range: bytes=START_BYTE-END_BYTE" and the range you request includes both START_BYTE and byte END_BYTE, thus consisting of (END_BYTE-START_BYTE)+1 bytes.

Requesting micro chunks is something I'd advise against as you might be blacklisted by a firewall rule to block HTTP flood. In general, I'd suggest you don't make chunks smaller than 1MB and don't make more than 10 chunks. Depending on what control you plan to have on your download, if you've got socket-level control you can consider writing only once every 32K at least, or writing data asynchronously.

I couldn't comment on the MMF idea, but if the downloaded file is large that's not going to be a good idea as you'll eat up a lot of RAM and eventually even cause the system to swap, which is not efficient.

About handling the chunks, you could just create several files - one per segment, optionally preallocate the disk space filling up the file with as many \x00 as the size of the chunk (preallocating might save you sometime while you write during the download, but will make starting the download slower), and then finally just write all of the chunks sequentially into the final file.

One thing you should beware of is that several servers have a max. concurrent connections limit, and you don't get to know it in advance, so you should be prepared to handle http errors/timeouts and to change the size of the chunks or to create a queue of the chunks in case you created more chunks than max. connections.

like image 30
em70 Avatar answered Nov 15 '22 22:11

em70