Refactored host connectivity check into a separate function. Also decrease the number of remaining connection attempts *only* if the connection failed.

This commit is contained in:
LoRd_MuldeR 2014-02-02 21:37:22 +01:00
parent ec9caf7c25
commit 14baf8d52d
3 changed files with 40 additions and 18 deletions

View File

@ -35,7 +35,7 @@
#define VER_LAMEXP_MINOR_LO 9
#define VER_LAMEXP_TYPE Final
#define VER_LAMEXP_PATCH 1
#define VER_LAMEXP_BUILD 1525
#define VER_LAMEXP_BUILD 1526
#define VER_LAMEXP_CONFG 1524
///////////////////////////////////////////////////////////////////////////////

View File

@ -253,7 +253,7 @@ void UpdateCheckThread::checkForUpdates(void)
// ----- Test Internet Connection ----- //
int connectionScore = 0;
int maxConnectTries = 2 * MIN_CONNSCORE;
int maxConnectTries = (3 * MIN_CONNSCORE) / 2;
log("Checking internet connection...");
setStatus(UpdateStatus_CheckingConnection);
@ -279,29 +279,21 @@ void UpdateCheckThread::checkForUpdates(void)
lamexp_seed_rand();
while(!(hostList.isEmpty() || (connectionScore >= MIN_CONNSCORE) || (--maxConnectTries < 0)))
while(!(hostList.isEmpty() || (connectionScore >= MIN_CONNSCORE) || (maxConnectTries < 1)))
{
QString currentHost = hostList.takeAt(lamexp_rand() % hostList.count());
log("", "Testing host:", currentHost);
QString outFile = QString("%1/%2.htm").arg(lamexp_temp_folder2(), lamexp_rand_str());
bool httpOk = false;
if(getFile(currentHost, outFile, 0, &httpOk))
switch(tryContactHost(hostList.takeAt(lamexp_rand() % hostList.count())))
{
connectionScore++;
setProgress(qBound(1, connectionScore + 1, MIN_CONNSCORE + 1));
lamexp_sleep(64);
case 01: connectionScore += 1; break;
case 02: connectionScore += 2; break;
default: maxConnectTries -= 1; break;
}
if(httpOk)
{
connectionScore++;
setProgress(qBound(1, connectionScore + 1, MIN_CONNSCORE + 1));
lamexp_sleep(64);
}
QFile::remove(outFile);
setProgress(qBound(1, connectionScore + 1, MIN_CONNSCORE + 1));
lamexp_sleep(64);
}
if(connectionScore < MIN_CONNSCORE)
{
log("", "Connectivity test has failed: Internet connection appears to be broken!");
setProgress(m_maxProgress);
setStatus(UpdateStatus_ErrorConnectionTestFailed);
return;
@ -440,6 +432,35 @@ void UpdateCheckThread::log(const QString &str1, const QString &str2, const QStr
if(!str4.isNull()) emit messageLogged(str4);
}
int UpdateCheckThread::tryContactHost(const QString &url)
{
int result = -1; bool httpOkay = false;
const QString outFile = QString("%1/%2.htm").arg(lamexp_temp_folder2(), lamexp_rand_str());
log("", "Testing host:", url);
if(getFile(url, outFile, 0, &httpOkay))
{
log("Connection to host was established successfully.");
result = 2;
}
else
{
if(httpOkay)
{
log("Connection to host timed out after HTTP OK was received.");
result = 1;
}
else
{
log("Connection failed: The host could not be reached!");
result = 0;
}
}
QFile::remove(outFile);
return result;
}
bool UpdateCheckThread::tryUpdateMirror(UpdateInfo *updateInfo, const QString &url)
{
bool success = false;

View File

@ -102,6 +102,7 @@ private:
inline void log(const QString &str1, const QString &str2 = QString(), const QString &str3 = QString(), const QString &str4 = QString());
bool getFile(const QString &url, const QString &outFile, unsigned int maxRedir = 5, bool *httpOk = NULL);
int tryContactHost(const QString &url);
bool tryUpdateMirror(UpdateInfo *updateInfo, const QString &url);
bool checkSignature(const QString &file, const QString &signature);
bool parseVersionInfo(const QString &file, UpdateInfo *updateInfo);