Currently I use TIdHTTP
in Delphi 11 to automatically download updates of my software. My installer is relatively big (about 100 MB) and the download takes a couple of minutes or even more.
Supposing I have the same installation file on different servers, is there a way to use all these servers to improve the download speed, something like Torrent does?
Torrent works by downloading separate pieces of a file from multiple sources in parallel, and then putting the pieces together into the final file.
You can do that with
TIdHTTP
too, if all of the servers (or even just 1 server) support the HTTPRange
request header. So, for example, you could download the file in 1KB chunks by downloading byte ranges0-1023
,1024-2047
,2048-3071
, and so on until the final chunk.If your server(s) support that, then you can do the following:
TIdHTTP
.TFileStream
1 to the final file, requesting and sharing read/write access/rights, and then seek it to the desired start offset for the piece in the file.TFileStream
, setting theTIdHTTP.Request.Range
property to'bytes=<start>-<end>'
, wherestart
is the starting offset, andend
is the ending offset, of the piece in the file.1 UPDATE: Oh wait, I forgot that
TIdHTTP
(more specifically,TIdIOHandler.ReadStream()
) resizes the givenTStream
to the size of the data being downloaded, if that size is reported by the server (which it would be in this situation). You DON'T want that to happen when you have already presized the target file ahead of time, otherwise it will get truncated/corrupted if you download multipleTFileStream
s into the same file. So, you can't use a standardTFileStream
here. What you could do, though, is derive a new class fromTFileStream
and override its virtualSetSize()
methods to do nothing. That should work, I think.Alternatively:
TIdHTTP
.TFileStream
(a standardTFileStream
is fine here) to a separate temp file, requesting write-only access and sharing no rights, and do not seek it.TFileStream
, settingTIdHTTP.Request.Range
as described above.