Jump to content

checks and retries a connection


ripdad
 Share

Recommended Posts

checks and retries a connection x times - plus extras

;checks and retries a connection x times - plus extras
;some scriplets i've put together plus some of my own
;not a total Inet errors solution - but it greatly helps
;only works if website supports file size return
;by ripdad, feb 20, 2010
#include <Inet.au3>
;
Global $ck_count = 0; init loop counter
Check_Connect()
;
Func Check_Connect(); checks if there is indeed a connection
    $ck_count = $ck_count + 1; advance loop counter
    $CkConnect1 = InetGetSize("http://www.somesite.com/somefile.txt")
    Sleep(5000)
    If Not $CkConnect1 <> 0 Then; if no response then
        $CkConnect2 = Ping("http://www.somesite.com", 250); second check
        Sleep(2000)
        If Not $CkConnect2 Then; if still no response then
            Connection_Error(); go sleep for awhile
        EndIf
    EndIf
EndFunc
;
Download_File()
;
Func Download_File()
    Local $hdownload
    $GetFile = "http://www.somesite.com/somefile.txt"
    $SourceSize = InetGetSize($GetFile)
    $hdownload = InetGet($GetFile, @DesktopDir & '\somefile.txt', 1, 1)
    ;
    Do
        Sleep(1000)
        TrayTip("Downloading File   -   " & Int(InetGetInfo($hdownload, 0) / $SourceSize * 100) & '%' & "  Done", $GetFile, 10, 5000)
    Until InetGetInfo($hdownload, 2)
    ;
    If InetGetInfo($hdownload, 0) < $SourceSize Then; checks if connection was lost during download with size compare
        InetClose($hdownload)
        TrayTip("", "", 5); closes traytip
        Connection_Error(); go sleep for awhile
    EndIf
EndFunc
;
Func Connection_Error()
    If $ck_count = 5 Then MsgBox(16, "No Connection", "Retried " & $ck_count & " Times With No Success - Exiting", 10); possible logging here
    If $ck_count = 5 Then Exit;  escape with loop count. change $ck_count = 5 to your preference
    MsgBox(16, "Connection Error", "Will Try Again in 5 Minutes" & @CRLF & @CRLF & "Retry Number: " & $ck_count, 10)
    Sleep(300000);  wait 5 minutes - change sleep value to your preference
    Check_Connect(); try connection again
EndFunc
;

* oops, added \ in: $hdownload = InetGet($GetFile, @DesktopDir & 'somefile.txt', 1, 1)

Edited by ripdad

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...