Sign in to follow this  
Followers 0
AdmiralAlkex

Simultaneous downloads with InetGet

1 post in this topic

I recommend to use 2 downloads at a time OR split the downloads between multiple servers, because there's some "2 connections per server" limit somewhere.

Blocking=

#include-once

#cs
#include "Array.au3"

Global $asFiles[5][2] = [["http://dl.dropbox.com/u/18344147/Opera_1110_int_Setup.exe", @ScriptDir & "\Opera Desktop 11.10.exe"], ["http://dl.dropbox.com/u/18344147/Opera_Mobile_11_Win7.exe", @ScriptDir & "\Opera Mobile for Desktop.exe"], ["http://something.something/something.something", @ScriptDir & "\Poop.exe"], ["http://dl.dropbox.com/u/18344147/autoit-v3.3.6.1-setup.exe", @ScriptDir & "\AutoIt 3.3.6.1.exe"], ["http://dl.dropbox.com/u/18344147/SciTE4AutoIt3%202010.02.28.exe", @ScriptDir & "\SciTE4AutoIt.exe"]]
$aiResults = _MultiDownload($asFiles, 2)
_ArrayDisplay($aiResults)
#ce

Func _MultiDownload($asFiles, $iSimultaneous)
    If IsArray($asFiles) = 0 Then Return SetError(1, 0, 0)   ;$asFiles must be array
    If UBound($asFiles, 0) <> 2 Then Return SetError(2, 0, 0)   ;a 2-dimensional array
    If IsInt($iSimultaneous) And $iSimultaneous < 1 Then Return SetError(3, 0, 0)   ;make sure $iSimultaneous is a number above 0

    Local $ahInet[$iSimultaneous][UBound($asFiles)], $afResults[UBound($asFiles)]
    $iSimultaneous -= 1   ;cheat so we don't need UBound() later in the For-loops ;)

    ;For temporary stuff
    Local $avInet, $iBusy
    ;For temporary end

    While 1
        Sleep(10)

        For $iX = 0 To $iSimultaneous
            If $ahInet[$iX][0] <> "" Then
                $avInet = InetGetInfo($ahInet[$iX][0])
                If $avInet[2] Then
                    $afResults[$ahInet[$iX][1]] = $avInet[3]
                    InetClose($ahInet[$iX][0])
                    $ahInet[$iX][0] = ""
                EndIf
            EndIf
        Next

        For $iX = $iSimultaneous To 0 Step -1
            If $ahInet[$iX][0] <> "" Then ContinueLoop
            If IsArray($asFiles) = 0 Then ExitLoop

            $ahInet[$iX][1] = UBound($asFiles) -1
            $asFile = __ArrayPop2D($asFiles)
            $ahInet[$iX][0] = InetGet($asFile[0], $asFile[1], 1, 1)
        Next

        $iBusy = 0
        For $iX = 0 To $iSimultaneous
            If $ahInet[$iX][0] <> "" Then $iBusy += 1
        Next
        If $iBusy < 1 Then ExitLoop
    WEnd
    Return $afResults
EndFunc

;Based on _ArrayPop() from the official Array.au3
Func __ArrayPop2D(ByRef $avArray)
    Local $iUBound = UBound($avArray) - 1
    Local $asLastVal[2] = [$avArray[$iUBound][0], $avArray[$iUBound][1]]

    ; Remove last item
    If Not $iUBound Then
        $avArray = ""
    Else
        ReDim $avArray[$iUBound][2]
    EndIf

    ; Return last item
    Return $asLastVal
EndFunc   ;==>_ArrayPop

Hacked to adlib=

#include-once

Global $__MultiDownloadInet
Global $__MultiDownloadFiles
Global $__MultiDownloadResults

#cs
Global $asFiles[5][2] = [["http://dl.dropbox.com/u/18344147/Opera_1110_int_Setup.exe", @ScriptDir & "\Opera Desktop 11.10.exe"], ["http://dl.dropbox.com/u/18344147/Opera_Mobile_11_Win7.exe", @ScriptDir & "\Opera Mobile for Desktop.exe"], ["http://something.something/something.something", @ScriptDir & "\Poop.exe"], ["http://dl.dropbox.com/u/18344147/autoit-v3.3.6.1-setup.exe", @ScriptDir & "\AutoIt 3.3.6.1.exe"], ["http://dl.dropbox.com/u/18344147/SciTE4AutoIt3%202010.02.28.exe", @ScriptDir & "\SciTE4AutoIt.exe"]]
_CW(_MultiDownloadAdlib($asFiles, 2))
_CW(_MultiDownloadAdlib($asFiles, 2))

Do
    Sleep(100)
    For $iX = 0 To UBound($asFiles, 1) -1
        _CW($__MultiDownloadResults[$iX][0] & ": " & $__MultiDownloadResults[$iX][1])
    Next
Until IsArray($__MultiDownloadInet) = 0

Func _CW($Whatever1, $Whatever2 = @ScriptLineNumber, $Whatever3 = @error, $Whatever4 = @extended)       ;Copied from CodecControl 2.2.9.au3
    Static Local $iExist = FileExists(@ScriptDir & "\Log.txt")
    Local $sText = "(" & $Whatever2 & ") :" & " /Time:" & @YDAY & ":" & @HOUR & ":" & @MIN & ":" & @SEC & ":" & @MSEC & " /Current@Error:" & $Whatever3 & " /Current@Extended:" & $Whatever4 & " /VarGetType:" & VarGetType($Whatever1) & " /Value:" & $Whatever1 & @CRLF
    If $iExist <> 0 Then
        FileWrite(@ScriptDir & "\Log.txt", $sText)
    Else
        ConsoleWrite($sText)
    EndIf
EndFunc
#ce

Func _MultiDownloadAdlib($asFiles, $iSimultaneous, $iAdlibTime = 50)
    If IsArray($asFiles) = 0 Then Return SetError(1, 0, 0)   ;$asFiles must be array
    If UBound($asFiles, 0) <> 2 Then Return SetError(2, 0, 0)   ;a 2-dimensional array
    If IsInt($iSimultaneous) And $iSimultaneous < 1 Then Return SetError(3, 0, 0)   ;make sure $iSimultaneous is a number above 0
    If IsArray($__MultiDownloadInet) Then Return SetError(4, 0, 0)   ;There's already one adlib going on, error and return to prevent clash

    Global $__MultiDownloadInet[$iSimultaneous][UBound($asFiles)], $__MultiDownloadResults[UBound($asFiles)][3]
    Global $__MultiDownloadFiles = $asFiles

    AdlibRegister("__MultiDownloadAdlibInternal", $iAdlibTime)
EndFunc

Func __MultiDownloadAdlibInternal()
    Local $avInet, $iBusy

    For $iX = 0 To UBound($__MultiDownloadInet, 1) -1
        If $__MultiDownloadInet[$iX][0] <> "" Then
            $avInet = InetGetInfo($__MultiDownloadInet[$iX][0])
            $__MultiDownloadResults[$__MultiDownloadInet[$iX][1]][1] = $avInet[0] / $avInet[1] * 100
            If $avInet[2] Then
                $__MultiDownloadResults[$__MultiDownloadInet[$iX][1]][2] = $avInet[3]
                InetClose($__MultiDownloadInet[$iX][0])
                $__MultiDownloadInet[$iX][0] = ""
            EndIf
        EndIf
    Next

    For $iX = UBound($__MultiDownloadInet, 1) -1 To 0 Step -1
        If $__MultiDownloadInet[$iX][0] <> "" Then ContinueLoop
        If IsArray($__MultiDownloadFiles) = 0 Then ExitLoop

        $__MultiDownloadInet[$iX][1] = UBound($__MultiDownloadFiles) -1
        $asFile = __ArrayPop2D($__MultiDownloadFiles)
        $__MultiDownloadInet[$iX][0] = InetGet($asFile[0], $asFile[1], 1, 1)
        $__MultiDownloadResults[$__MultiDownloadInet[$iX][1]][0] = $asFile[0]
    Next

    $iBusy = 0
    For $iX = 0 To UBound($__MultiDownloadInet, 1) -1
        If $__MultiDownloadInet[$iX][0] <> "" Then $iBusy += 1
    Next
    If $iBusy < 1 Then
        AdlibUnRegister("__MultiDownloadAdlibInternal")
        Global $__MultiDownloadInet = ""
    EndIf
EndFunc

;Based on _ArrayPop() from the official Array.au3
Func __ArrayPop2D(ByRef $avArray)
    Local $iUBound = UBound($avArray) - 1
    Local $asLastVal[2] = [$avArray[$iUBound][0], $avArray[$iUBound][1]]

    ; Remove last item
    If Not $iUBound Then
        $avArray = ""
    Else
        ReDim $avArray[$iUBound][2]
    EndIf

    ; Return last item
    Return $asLastVal
EndFunc   ;==>_ArrayPop

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0