#include <WinAPI.au3>
TCPStartup()

Global Const $iChunk_Size = 2 * 1024 * 1024
Global Const $iConnections = 4
Global $aDownload_Global[1][$iConnections + 1][11] = [[["SOCKET", "X", "Y", "SIZE", " DATA", "STATE", "HEAD", "URL", "FULLSIZE", "NEXT CHUNK POS", "File"]]]


_Download_Ex_Add("http://pg4.clipfish.de/media/7a/643465967863d520d7cf297fc284fe7a.flv", @ScriptDir & "\test.flv")

AdlibRegister("_Download_Ex")

While 1
	Sleep(100)
WEnd



Func _Download_Ex()
	For $d = 0 To UBound($aDownload_Global) -1
		For $c = 1 To $iConnections
			If $aDownload_Global[$d][$c][5] = 0 And $aDownload_Global[$d][0][9] < $aDownload_Global[$d][0][8] Then
				$aDownload_Global[$d][$c][1] = $aDownload_Global[$d][0][9]
				$aDownload_Global[$d][$c][2] = $aDownload_Global[$d][0][9] + $iChunk_Size -1
				$aDownload_Global[$d][$c][4] = Binary("") ;DATA
				$aDownload_Global[$d][$c][5] = 1
				$aDownload_Global[$d][$c][6] = 0

				If $aDownload_Global[$d][$c][2] + $iChunk_Size >= $aDownload_Global[$d][0][8] Then $aDownload_Global[$d][$c][2] = $aDownload_Global[$d][$c][8] -1
				$aDownload_Global[$d][$c][3] = $aDownload_Global[$d][$c][2] - $aDownload_Global[$d][$c][1] +1

				_Download_Ex_Request_Chunk($d, $c)
				$aDownload_Global[$d][0][9] = $aDownload_Global[$d][$c][2] + 1
			EndIf

			If $aDownload_Global[$d][$c][5] = 1 Then
				_Download_Ex_Recv($d, $c)
			EndIf

			If $aDownload_Global[$d][$c][5] = 2 Then
				Local $tBuffer = DllStructCreate("byte[" & BinaryLen($aDownload_Global[$d][$c][4]) & "]")
				DllStructSetData($tBuffer, 1, $aDownload_Global[$d][$c][4])
				Local $nBytes
				_WinAPI_SetFilePointer($aDownload_Global[$d][0][10], $aDownload_Global[$d][$c][1])
				_WinAPI_WriteFile($aDownload_Global[$d][0][10], DllStructGetPtr($tBuffer), $aDownload_Global[$d][$c][3], $nBytes)
				TCPCloseSocket($aDownload_Global[$d][$c][0])
				$tBuffer = ''
				$aDownload_Global[$d][$c][5] = 0
			EndIf
			If _Download_Ex_Complete($d) Then MsgBox(0, "", "DOWNLOAD DONE")
		Next
	Next
EndFunc

Func _Download_Ex_Recv($iDownload_Nr, $iChunk_Nr)
	Local $sRecv = TCPRecv($aDownload_Global[$iDownload_Nr][$iChunk_Nr][0], $aDownload_Global[$iDownload_Nr][$iChunk_Nr][3], 1)

	If @error <> 0 Then
		If BinaryLen($aDownload_Global[$iDownload_Nr][$iChunk_Nr][4]) < $aDownload_Global[$iDownload_Nr][$iChunk_Nr][3] Then
			TCPCloseSocket($aDownload_Global[$iDownload_Nr][$iChunk_Nr][0])
			$aDownload_Global[$iDownload_Nr][$iChunk_Nr][4] = Binary("")
			_Download_Ex_Request_Chunk($iDownload_Nr, $iChunk_Nr)
		Else
			$aDownload_Global[$iDownload_Nr][$iChunk_Nr][5] = 2
		EndIf
		Return
	EndIf

	If Not ($sRecv == "") Then
		If $aDownload_Global[$iDownload_Nr][$iChunk_Nr][6] = 0 Then
			Local $sRecv_2 = Binary("")
			For $b = 0 To BinaryLen($sRecv)
				$Bit = BinaryMid($sRecv, $b, 1)
				$sRecv_2 &= $Bit
				If StringRight($sRecv_2, 8) = Binary("0D0A0D0A") Then
					$sRecv_2 = Binary("")
					$aDownload_Global[$iDownload_Nr][$iChunk_Nr][6] = 1
				EndIf
			Next
			$sRecv = $sRecv_2
		EndIf
		$aDownload_Global[$iDownload_Nr][$iChunk_Nr][4] &= $sRecv
	EndIf
EndFunc


Func _Download_Ex_Request_Chunk($iDownload_Nr, $iChunk_Nr)
	$aUrl_Split = _Url_Split($aDownload_Global[$iDownload_Nr][0][7])

	Local $sHead = "GET " & $aUrl_Split[2] & " HTTP/1.1" & @CRLF & _
				   "Range: bytes=" & $aDownload_Global[$iDownload_Nr][$iChunk_Nr][1] & "-" & $aDownload_Global[$iDownload_Nr][$iChunk_Nr][2] & @CRLF & _
				   "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.2.8) Gecko/20100722 Ant.com Toolbar 2.0.1 Firefox/3.6.8" & @CRLF & _
				   "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" & @CRLF & _
				   "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3" & @CRLF & _
				   "Accept-Encoding: gzip,deflate" & @CRLF & _
				   "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" & @CRLF & _
				   "Keep-Alive: 115" & @CRLF & _
				   "Connection: keep-alive" & @CRLF & _
				   "Host: " & $aUrl_Split[1] & @CRLF & _
				   "Pragma: no-cache" & @CRLF & @CRLF


	$aDownload_Global[$iDownload_Nr][$iChunk_Nr][0] = TCPConnect(TCPNameToIP($aUrl_Split[1]), 80)
	$iBytesSend = TCPSend($aDownload_Global[$iDownload_Nr][$iChunk_Nr][0], $sHead)

	Return $iBytesSend
EndFunc

Func _Download_Ex_Add($sUrl, $sFile)
	ReDim $aDownload_Global[UBound($aDownload_Global) +1][$iConnections +1][11]

	$aDownload_Global[UBound($aDownload_Global) -1][0][10] = _WinAPI_CreateFile($sFile, 3, 4, 2)
	$aDownload_Global[UBound($aDownload_Global) -1][0][9] = 0
	$aDownload_Global[UBound($aDownload_Global) -1][0][8] = InetGetSize($sUrl, 1)
	$aDownload_Global[UBound($aDownload_Global) -1][0][7] = $sUrl

	For $i = 1 To $iConnections
		$aDownload_Global[UBound($aDownload_Global) -1][0][5] = 0
	Next
EndFunc

Func _Download_Ex_Complete($iDownload_Nr)
	Local $iMatch
	For $i = 1 To $iConnections
		If $aDownload_Global[$iDownload_Nr][$i][5] = 2 Then $iMatch += 1
	Next

	If $iMatch = $iConnections Then Return True
	Return False
EndFunc

Func _Url_Split($sUrl)
	Local $sTmp = $sUrl
	If StringLeft($sUrl, 7) <> "http://" And StringLeft($sUrl, 8) <> "https://" Then $sUrl = "http://" & $sUrl

	If StringRight($sUrl, 1) <> "/" Then $sUrl &= "/"


	$aHost = StringRegExp($sUrl, "http?://(.*?)/", 3)

	If @error Then Return SetError(1, @extended, -1)
	$sHost = $aHost[0]

	If $sHost = "" Then Return SetError(2, @extended, -1)

	$sUrl = StringTrimLeft($sUrl, StringInStr($sUrl, "/", -1, 3)-1)
	If $sUrl = "" Then Return SetError(3, @extended, -1)

	If StringLen($sUrl) > 1 Then $sUrl = StringLeft($sUrl, StringLen($sUrl) -1)

	Local $aSplit[3] = [$sTmp, $sHost, $sUrl]
	Return $aSplit
EndFunc