name22

WinHTTP Download + Speed Limitation + Asynchrounos Write

Jul 31st, 2012
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 3.15 KB | None | 0 0
  1. #include <Binary.au3>
  2. #include <WinAPI.au3>
  3. #include <WinAPIEx.au3>
  4. #include <WinHTTP.au3>
  5.  
  6. ; -Author: name22(www.autoit.de)
  7.  
  8. $nSpeedLimit = 1024 * 200
  9. $iBytePerStep = 8192
  10. $iBufferSize = 1024 * 1024
  11. $iSleepTime = Round($iBytePerStep / $nSpeedLimit * 1000)
  12.  
  13. $sURL_Download = 'http://speedtest.qsc.de/10MB.qsc'
  14. $sPathDst = @DesktopDir
  15.  
  16. $vNTdll = DllOpen("ntdll.dll")
  17.  
  18. $tPrecSleep = DllStructCreate("int64 time;")
  19. $pPrecSleep = DllStructGetPtr($tPrecSleep)
  20.  
  21. $aURL_Split = _WinHttpCrackUrl($sURL_Download)
  22.  
  23. $hOpen = _WinHttpOpen()
  24. $hConnect = _WinHttpConnect($hOpen, $aURL_Split[2])
  25. $hRequest = _WinHttpOpenRequest($hConnect, "GET", $aURL_Split[6])
  26.  
  27. _WinHttpSendRequest($hRequest)
  28. _WinHttpReceiveResponse($hRequest)
  29.  
  30. $iSizeBytes = _WinHttpQueryHeaders($hRequest, $WINHTTP_QUERY_CONTENT_LENGTH)
  31.  
  32. $nT2 = TimerInit()
  33. Switch _WinHttpQueryDataAvailable($hRequest)
  34.     Case True
  35.         $hFile = _WinAPI_CreateFileEx($sPathDst & "\" & StringTrimLeft($aURL_Split[6], StringInStr($aURL_Split[6], "/", 0, -1)), $CREATE_NEW, $GENERIC_WRITE, $FILE_SHARE_READ, BitOR($FILE_ATTRIBUTE_NORMAL, $FILE_FLAG_OVERLAPPED))
  36.         $tBuffer = DllStructCreate("byte[" & $iBufferSize & "]")
  37.         $pBuffer = DllStructGetPtr($tBuffer)
  38.         $tOverlapped = _GetStruct_Overlapped(0)
  39.         $pOverlapped = DllStructGetPtr($tOverlapped)
  40.  
  41.         $iByteWritten = 0
  42.         $iOffsetFile = 0
  43.         $vData = Binary("")
  44.         $nT = TimerInit()
  45.         Do
  46.             $iSleepTemp = ($iSleepTime - TimerDiff($nT))
  47.             If $iSleepTemp > 0 Then
  48.                 DllStructSetData($tPrecSleep, "time", -10000 * $iSleepTemp)
  49.                 DllCall($vNTdll, "dword", "ZwDelayExecution", "int", 0, "ptr", $pPrecSleep)
  50.             EndIf
  51.             $nT = TimerInit()
  52.             $vData &= _WinHttpReadData($hRequest, 2, $iBytePerStep)
  53.             $iError = @error
  54.             $iBufferContent = BinaryLen($vData)
  55.  
  56.             If $iBufferContent >= $iBufferSize - $iBytePerStep Or $iError = -1 Then
  57.                 _WinAPI_GetOverlappedResult($hFile, $pOverlapped, $iByteWritten, True)
  58.  
  59.                 DllStructSetData($tBuffer, 1, $vData)
  60.                 $vData = Binary("")
  61.                 $iOffsetFile += $iByteWritten
  62.                 $tOverlapped = _GetStruct_Overlapped($iOffsetFile)
  63.                 $pOverlapped = DllStructGetPtr($tOverlapped)
  64.  
  65.                 _WinAPI_WriteFile($hFile, $pBuffer, $iBufferContent, $iByteWritten, $pOverlapped)
  66.             EndIf
  67.         Until $iError = -1
  68.  
  69.         _WinHttpCloseHandle($hRequest)
  70.         _WinHttpCloseHandle($hConnect)
  71.         _WinHttpCloseHandle($hOpen)
  72.  
  73.         MsgBox(64, "Info", "Download completed." & @CRLF & "Time: " & Int(TimerDiff($nT2)) & @CRLF & "Speed: " & Round($iSizeBytes / Int(TimerDiff($nT2))))
  74.     Case False
  75.         MsgBox(16, "Error", "No Data available.")
  76. EndSwitch
  77.  
  78. DllClose($vNTdll) ;Dll schließen
  79.  
  80. Func _GetStruct_Overlapped($iOffset, $hEvent = 0)
  81.     Local $tOffset = DllStructCreate('int64')
  82.     Local $tOffsetSplit = DllStructCreate('dword;dword', DllStructGetPtr($tOffset))
  83.     DllStructSetData($tOffset, 1, $iOffset)
  84.  
  85.     $tStruct = DllStructCreate($tagOVERLAPPED)
  86.     DllStructSetData($tStruct, "Internal", 0)
  87.     DllStructSetData($tStruct, "InternalHigh", 0)
  88.     DllStructSetData($tStruct, "Offset", DllStructGetData($tOffsetSplit, 1))
  89.     DllStructSetData($tStruct, "OffsetHigh", DllStructGetData($tOffsetSplit, 2))
  90.     DllStructSetData($tStruct, "hEvent", $hEvent)
  91.  
  92.     Return $tStruct
  93. EndFunc
Add Comment
Please, Sign In to add comment