attempt to implement urlFetch with a timeout

This commit is contained in:
Rasmus Karlsson 2017-06-26 15:53:43 +02:00
parent 1d8795ef1b
commit c2e67e4b90
9 changed files with 130 additions and 15 deletions

3
.gitmodules vendored
View file

@ -7,3 +7,6 @@
[submodule "lib/humanize"]
path = lib/humanize
url = https://github.com/pajlada/humanize.git
[submodule "lib/signals"]
path = lib/signals
url = https://github.com/pajlada/signals.git

View file

@ -92,7 +92,8 @@ SOURCES += \
src/accountmanager.cpp \
src/twitch/twitchuser.cpp \
src/ircaccount.cpp \
src/widgets/accountpopup.cpp
src/widgets/accountpopup.cpp \
src/messagefactory.cpp
HEADERS += \
src/asyncexec.hpp \
@ -150,7 +151,9 @@ HEADERS += \
src/twitch/twitchuser.hpp \
src/ircaccount.hpp \
src/widgets/accountpopup.hpp \
src/util/distancebetweenpoints.hpp
src/util/distancebetweenpoints.hpp \
src/messagefactory.hpp \
src/widgets/basewidget.hpp
PRECOMPILED_HEADER =
@ -197,4 +200,5 @@ werr {
# External dependencies
include(dependencies/rapidjson.pri)
include(dependencies/settings.pri)
include(dependencies/signals.pri)
include(dependencies/humanize.pri)

View file

@ -4,6 +4,3 @@ SOURCES += \
$$PWD/../lib/settings/src/settings/settingmanager.cpp
INCLUDEPATH += $$PWD/../lib/settings/include/
# signals
INCLUDEPATH += $$PWD/../lib/settings/external/signals/include

2
dependencies/signals.pri vendored Normal file
View file

@ -0,0 +1,2 @@
# signals
INCLUDEPATH += $$PWD/../lib/signals/include/

1
lib/signals Submodule

@ -0,0 +1 @@
Subproject commit 2c9c92b971f4a1313eeb4d9daf8ea59565d3c691

6
src/messagefactory.cpp Normal file
View file

@ -0,0 +1,6 @@
#include "messagefactory.hpp"
MessageFactory::MessageFactory()
{
}

11
src/messagefactory.hpp Normal file
View file

@ -0,0 +1,11 @@
#ifndef MESSAGEFACTORY_HPP
#define MESSAGEFACTORY_HPP
class MessageFactory
{
public:
MessageFactory();
};
#endif // MESSAGEFACTORY_HPP

View file

@ -1,5 +1,6 @@
#pragma once
#include <QEventLoop>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
@ -8,15 +9,22 @@
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QString>
#include <QTimer>
#include <functional>
namespace chatterino {
namespace util {
static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback)
static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> successCallback,
QNetworkAccessManager *manager = nullptr)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
bool customManager = true;
if (manager == nullptr) {
manager = new QNetworkAccessManager();
customManager = false;
}
QUrl requestUrl(url);
QNetworkRequest request(requestUrl);
@ -39,13 +47,17 @@ static void urlFetch(const QString &url, std::function<void(QNetworkReply &)> su
}
reply->deleteLater();
if (!customManager) {
manager->deleteLater();
}
});
}
static void urlJsonFetch(const QString &url, std::function<void(QJsonObject &)> successCallback)
static void urlFetchJSON(const QString &url, std::function<void(QJsonObject &)> successCallback,
QNetworkAccessManager *manager = nullptr)
{
urlFetch(url, [=](QNetworkReply &reply) {
urlFetch(url,
[=](QNetworkReply &reply) {
QByteArray data = reply.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
@ -56,7 +68,82 @@ static void urlJsonFetch(const QString &url, std::function<void(QJsonObject &)>
QJsonObject rootNode = jsonDoc.object();
successCallback(rootNode);
},
manager);
}
static void urlFetchTimeout(const QString &url,
std::function<void(QNetworkReply &)> successCallback, int timeoutMs,
QNetworkAccessManager *manager = nullptr)
{
bool customManager = true;
if (manager == nullptr) {
manager = new QNetworkAccessManager();
customManager = false;
}
QUrl requestUrl(url);
QNetworkRequest request(requestUrl);
QNetworkReply *reply = manager->get(request);
QTimer timer;
timer.setSingleShot(true);
QEventLoop loop;
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
QObject::connect(reply, &QNetworkReply::finished, [=] {
/* uncomment to follow redirects
QVariant replyStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
if (replyStatus >= 300 && replyStatus <= 304) {
QString newUrl =
reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString();
urlFetch(newUrl, successCallback);
return;
}
*/
if (reply->error() == QNetworkReply::NetworkError::NoError) {
successCallback(*reply);
}
reply->deleteLater();
if (!customManager) {
manager->deleteLater();
}
});
timer.start(timeoutMs);
loop.exec();
if (!timer.isActive()) {
qDebug() << "TIMED OUT";
QObject::disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
reply->abort();
} else {
// qDebug() << "XDDD HEHEHE";
}
}
static void urlFetchJSONTimeout(const QString &url,
std::function<void(QJsonObject &)> successCallback, int timeoutMs,
QNetworkAccessManager *manager = nullptr)
{
urlFetchTimeout(url,
[=](QNetworkReply &reply) {
QByteArray data = reply.readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
if (jsonDoc.isNull()) {
return;
}
QJsonObject rootNode = jsonDoc.object();
successCallback(rootNode);
},
timeoutMs, manager);
}
} // namespace util

View file

@ -0,0 +1,4 @@
#ifndef BASEWIDGET_HPP
#define BASEWIDGET_HPP
#endif // BASEWIDGET_HPP