I am trying to add automatic updating capabilities to the application that I am developing. I based this functionality on the Qt HTTP Example (and based on that, I mean that I accurately copied this example and then went from there). It downloads the ZIP file and then extracts its contents to fix the application.
Sometimes when downloading, the connection fails and the download stops. To be a little more user friendly, I decided that I would add the ability to automatically restart the bootloader, where it will try to restart the download once if it fails.
Here are the highlights of my code: the method names correspond to the method names in the example:
void Autopatcher::httpReadyRead()
{
if (file) {
QByteArray qba = reply->readAll();
bytesWritten += qba.size();
file->write(qba);
}
}
void Autopatcher::startRequest(QUrl url)
{
if (doResume) {
QNetworkRequest req(url);
QByteArray rangeHeaderValue = "bytes=" + QByteArray::number(bytesWritten) + "-";
req.setRawHeader("Range",rangeHeaderValue);
reply = qnam.get(req);
} else {
reply = qnam.get(QNetworkRequest(url));
}
}
void Autopatcher::fileGetError(QNetworkReply::NetworkError error) {
httpRequestAborted = true;
}
void Autopatcher::httpFinished() {
if (reply->error()) {
if (!retried) {
doResume=true;
QTimer::singleShot(5000,this,SLOT(downloadFile()));
}
else {
if (file) {
file->close();
file->remove();
delete file;
file = 0;
}
}
} else {
if (file) {
file->close();
delete file;
file = 0;
}
doPatch();
}
reply->deleteLater();
reply = 0;
}
, , . ZIP . , , , ZIP 7-zip, (7-zip - " ).
, - " ", , HTTP Range. , , , httpReadyRead. , .
Sysinternals TCPView TCP- . , , , !