Originally Posted By: westor
This is actually an Scripts & Popups function and not Developers stuff but it would be nice to show us the code to help you further because now we cannot imagine where is the problem exactly.

Oh sorry, my mistake. Maybe a moderator will move the topic to the right section.
In the meanwhile, i made some more attempts and sometimes the download reaches 100% and the self update process ends succesfully, so this confusing me even more.

About the code...
i create a first socket on a small php file to check if a new update is available (it returns version number and script.ini size to make an integrity check later), then, if that's the case, i create another socket to download the .ini file, this way (some code not related to the problem stripped for convenience and readability):
Code:
alias startupdate {
    ; Open a dialog with a progress bar
    .write -c $script $+ .tmp
    .sockclose updatemyscript
    .sockopen updatemyscript my.website.url 80
}



on *:SOCKOPEN:updatemyscript:{
  if ($sockerr) {
    ; Report error
    .return
  }
  else {
    .sockwrite -n $sockname GET /subfolder/myscript.ini HTTP/1.0
    .sockwrite -n $sockname Host: my.website.url
    .sockwrite -n $sockname $crlf
  }
}



on *:SOCKREAD:updatemyscript:{
  if ($sockerr) {
    ; Report error
    .return
  }
  else {
    .sockread -fn &mybinvar
    if ($bvar(&mybinvar,$calc($bvar(&mybinvar,0) - 1),1) == 0) {
      .bwrite $script $+ .tmp -1 $calc($bvar(&mybinvar,0) - 2) &mybinvar
      .bwrite $script $+ .tmp -1 -1 $crlf
    }
    else {
      .bwrite $script $+ .tmp -1 $bvar(&mybinvar,0) &mybinvar
    }
    .var %downloaded.update.size $file($script $+ .tmp).size
    .var %perc.update $round($calc(%downloaded.update.size * 100 / %xdcc.newver.bytes),0)
    ; Update progress bar in a dialog
  }
}


on *:SOCKCLOSE:updatemyscript:{
  if ($sockerr) {
    ; Report error
    .return
  }
  else {
    ; Update progress bar in a dialog to ensure it reaches 100% (cause of approximation in "on SOCKREAD" $calc operation of %perc.update)
    ; Now strip headers by removing all lines before [script] line
    .var %i 1
    .var %script.first.line $chr(91) $+ script $+ $chr(93)
    while (%i < $lines($script $+ .tmp)) {
      if ($read($script $+ .tmp,n,1) != %script.first.line) {
        .write -dl1 $script $+ .tmp
      }
      else {
        .break
      }
      .inc %i
    }
    ; Close the dialog containing progress bar
    ; Now lets check if the file size matches
    if ($file($script $+ .tmp).size == %myscript.bytes) {
      ; Everything went ok, go on with other stuff
    }
    else {
      ; ERROR! Something went wrong and the download got interrupted
    }
  }
}

Last edited by Fabius; 29/11/15 03:14 PM.