views:

549

answers:

8

hi,

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.

+4  A: 

hey 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. coz i dont think i am not a good enough a programmer to write something of this level ;-)

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

hope you find this useful :-)

Raj
thanks for the link, i'll look into it :)
Idan K
BITS is a nice (and relatively unknown) Windows feature; however, be prepared to fallback to something if it's not available/disabled.
Piskvor
A: 

It seems to me you would want to limit the size per download chunk. Large chunks could force you to repeat download of data if the connection aborted close to the end of the data part. Specially an issue with slower connections.

You could save the data you already received and only request those you didn't get - e.g., if a 100 MB chunk aborts at 95 MB, next request will be for the last 5 MB instead of the whole chunk.
Piskvor
+1  A: 

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.

emaster70
+3  A: 

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.

Arkain
+2  A: 

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

Piskvor
A: 

Hi daniel, I know i should email you about this but there is no contact info about you here, thats why i am writing here.

So basically i was about to ask a question how to start writing a download manager, and came across your question while searching if someone also ask this question before.

So is there anyway i can be in contact with you? Thanks.

itsaboutcode
my email is blueflyy at gmail.com
Idan K
A: 

for the pause resume support look at this simple example Simple download manager in Qt with puase/ resume support

Buzz
+2  A: 

Not really an answer to the original questions, but another thing worth mentioning is that a resumable downloader should also check the last modified date on a resource before trying to grab the next chunk of something that may have changed.

Rob Cooney
A very good point indeed!
Piskvor