Skip to content

Commit

Permalink
Fixed a crash when no internet connection (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
foldynl committed Feb 22, 2025
1 parent 1665a12 commit 748ef0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
27 changes: 19 additions & 8 deletions core/PropConditions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ void PropConditions::updateDxTrends()
FCT_IDENTIFICATION;

dxTrendResult.clear();
dxTrendTimeoutTimer.stop();
dxTrendPendingConnections.clear(); // pending connections will be ignored.

for ( const QString& continent : Data::getContinentList() )
dxTrendPendingConnections << nam->get(prepareRequest(QUrl(DXC_TRENDS + QString("/%0/15").arg(continent))));
Expand Down Expand Up @@ -209,9 +211,9 @@ void PropConditions::processReply(QNetworkReply* reply)
emit mufMapUpdated();
}
}
else if ( replyURL.toString().contains(DXC_TRENDS) )
else if ( dxTrendPendingConnections.contains(reply) )
{
dxTrendPendingConnections.removeAll(reply);
dxTrendPendingConnections.remove(reply);

QJsonDocument doc = QJsonDocument::fromJson(data);
const QString &requestContinent = replyURL.path().section('/', -2, -2);
Expand Down Expand Up @@ -247,7 +249,7 @@ void PropConditions::processReply(QNetworkReply* reply)
else
{
qCDebug(runtime) << "HTTP Status Code" << replyStatusCode;
dxTrendPendingConnections.removeAll(reply);
dxTrendPendingConnections.remove(reply);
repeateRequest(reply->url());
reply->deleteLater();
}
Expand All @@ -267,7 +269,10 @@ void PropConditions::repeateRequest(const QUrl &url)
QTimer::singleShot(1000 * RESEND_BASE_INTERVAL * failedRequests[url], this, [this, url]()
{
qCDebug(runtime) << "Resending request" << url.toString();
nam->get(prepareRequest(url));
QNetworkReply *req = nam->get(prepareRequest(url));
if ( url.toString().contains(DXC_TRENDS))
dxTrendPendingConnections << req;

});
}
else
Expand All @@ -291,18 +296,24 @@ void PropConditions::dxTrendTimeout()

dxTrendTimeoutTimer.stop();

for ( auto it = dxTrendPendingConnections.begin(); it != dxTrendPendingConnections.end(); ++it )
for ( auto it = dxTrendPendingConnections.begin(); it != dxTrendPendingConnections.end(); )
{
(*it)->abort();
(*it)->deleteLater();
QNetworkReply* reply = *it;
it = dxTrendPendingConnections.erase(it);
if ( reply )
{
reply->abort();
reply->deleteLater();
}
}

dxTrendPendingConnections.clear();
dxTrendResult.clear();
emit dxTrendFinalized(dxTrendResult); // emit empty result
}

PropConditions::~PropConditions()
{
dxTrendTimeoutTimer.stop();
dxTrendTimeout();
nam->deleteLater();
}
Expand Down
4 changes: 2 additions & 2 deletions core/PropConditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <QObject>
#include <QDateTime>
#include <QVector>
#include <QSet>
#include <QTimer>

class QNetworkAccessManager;
Expand Down Expand Up @@ -104,7 +104,7 @@ public slots:
GenericValueMap<double> auroraMap;
GenericValueMap<double> mufMap;
QHash<QUrl, int> failedRequests;
QList<QNetworkReply *> dxTrendPendingConnections;
QSet<QNetworkReply *> dxTrendPendingConnections;
QTimer dxTrendTimeoutTimer;
QHash<QString, QHash<QString, QHash<QString, int>>> dxTrendResult;
QByteArray agentString;
Expand Down

0 comments on commit 748ef0d

Please sign in to comment.