Move timeout logic to NetworkRequest

This commit is contained in:
Rasmus Karlsson 2017-10-27 22:04:05 +02:00
parent 41ec892bf8
commit 2de98dc1f8
3 changed files with 109 additions and 125 deletions

View file

@ -48,9 +48,14 @@ void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, std::weak
printf("[EmoteManager] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName));
QString url("https://api.betterttv.net/2/channels/" + channelName);
util::urlFetchJSONTimeout(
url, QThread::currentThread(),
[this, channelName, _map](QJsonObject &rootNode) {
debug::Log("Request bttv channel emotes for {}", channelName);
util::NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.setTimeout(3000);
req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
debug::Log("Got bttv channel emotes for {}", channelName);
auto map = _map.lock();
if (_map.expired()) {
@ -76,10 +81,9 @@ void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, std::weak
link = link.replace("{{id}}", id).replace("{{image}}", "1x");
auto emote =
this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [this, &code, &link] {
return EmoteData(new LazyLoadedImage(*this, this->windowManager, link, 1,
code, code + "\nChannel BTTV Emote"));
auto emote = this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [this, &code, &link] {
return EmoteData(new LazyLoadedImage(*this, this->windowManager, link, 1, code,
code + "\nChannel BTTV Emote"));
});
this->bttvChannelEmotes.insert(code, emote);
@ -88,8 +92,7 @@ void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, std::weak
}
this->bttvChannelEmoteCodes[channelName.toStdString()] = codes;
},
1500);
});
}
void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map)
@ -98,9 +101,10 @@ void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_
QString url("http://api.frankerfacez.com/v1/room/" + channelName);
util::urlFetchJSONTimeout(
url, QThread::currentThread(),
[this, channelName, _map](QJsonObject &rootNode) {
util::NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.setTimeout(3000);
req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
auto map = _map.lock();
if (_map.expired()) {
@ -125,8 +129,8 @@ void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_
QJsonObject urls = emoteObject.value("urls").toObject();
QString url1 = "http:" + urls.value("1").toString();
auto emote = this->getFFZChannelEmoteFromCaches().getOrAdd(id, [this, &code,
&url1] {
auto emote =
this->getFFZChannelEmoteFromCaches().getOrAdd(id, [this, &code, &url1] {
return EmoteData(new LazyLoadedImage(*this, this->windowManager, url1, 1,
code, code + "\nGlobal FFZ Emote"));
});
@ -138,8 +142,7 @@ void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_
this->ffzChannelEmoteCodes[channelName.toStdString()] = codes;
}
},
1500);
});
}
ConcurrentMap<QString, twitch::EmoteValue *> &EmoteManager::getTwitchEmotes()
@ -383,9 +386,10 @@ void EmoteManager::refreshTwitchEmotes(const std::string &roomID)
qDebug() << url;
util::urlFetchJSONTimeout(
url, QThread::currentThread(),
[=, &emoteData](QJsonObject &root) {
util::NetworkRequest req(url);
req.setCaller(QThread::currentThread());
req.setTimeout(3000);
req.getJSON([=, &emoteData](QJsonObject &root) {
emoteData.emoteSets.clear();
emoteData.emoteCodes.clear();
auto emoticonSets = root.value("emoticon_sets").toObject();
@ -403,8 +407,7 @@ void EmoteManager::refreshTwitchEmotes(const std::string &roomID)
}
emoteData.filled = true;
},
3000);
});
}
void EmoteManager::loadBTTVEmotes()

View file

@ -1,10 +1,13 @@
#pragma once
#include "debug/log.hpp"
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QThread>
#include <QTimer>
#include <QUrl>
namespace chatterino {
@ -32,6 +35,7 @@ class NetworkRequest
QNetworkRequest request;
const QObject *caller = nullptr;
std::function<void(QNetworkReply *)> onReplyCreated;
int timeoutMS = -1;
} data;
public:
@ -67,9 +71,19 @@ public:
this->data.request.setRawHeader(headerName, value);
}
void setTimeout(int ms)
{
this->data.timeoutMS = ms;
}
template <typename FinishedCallback>
void get(FinishedCallback onFinished)
{
QTimer *timer = nullptr;
if (this->data.timeoutMS > 0) {
timer = new QTimer;
}
NetworkRequester requester;
NetworkWorker *worker = new NetworkWorker;
@ -83,11 +97,23 @@ public:
});
}
if (timer != nullptr) {
timer->start(this->data.timeoutMS);
}
QObject::connect(
&requester, &NetworkRequester::requestUrl, worker,
[ data = std::move(this->data), worker, onFinished{std::move(onFinished)} ]() {
[ timer, data = std::move(this->data), worker, onFinished{std::move(onFinished)} ]() {
QNetworkReply *reply = NetworkManager::NaM.get(data.request);
if (timer != nullptr) {
QObject::connect(timer, &QTimer::timeout, worker, [reply, timer]() {
debug::Log("Aborted!");
reply->abort();
timer->deleteLater();
});
}
if (data.onReplyCreated) {
data.onReplyCreated(reply);
}

View file

@ -38,51 +38,6 @@ static QJsonObject parseJSONFromReply(QNetworkReply *reply)
return jsonDoc.object();
}
static void urlFetchTimeout(const QString &url, const QObject *caller,
std::function<void(QNetworkReply *)> successCallback, int timeoutMs)
{
QTimer *timer = new QTimer;
timer->setSingleShot(true);
QEventLoop *loop = new QEventLoop;
util::NetworkRequest req(url);
req.setCaller(loop);
req.setOnReplyCreated([loop, timer](QNetworkReply *reply) {
QObject::connect(timer, &QTimer::timeout, loop, [=]() {
QObject::disconnect(reply, &QNetworkReply::finished, loop, &QEventLoop::quit);
reply->abort();
reply->deleteLater();
});
});
req.get([=](QNetworkReply *reply) {
if (reply->error() == QNetworkReply::NetworkError::NoError) {
successCallback(reply);
}
reply->deleteLater();
loop->quit();
});
QObject::connect(timer, SIGNAL(timeout()), loop, SLOT(quit()));
timer->start(timeoutMs);
loop->exec();
delete timer;
delete loop;
}
static void urlFetchJSONTimeout(const QString &url, const QObject *caller,
std::function<void(QJsonObject &)> successCallback, int timeoutMs)
{
urlFetchTimeout(url, caller,
[=](QNetworkReply *reply) {
auto node = parseJSONFromReply(reply);
successCallback(node);
},
timeoutMs);
}
namespace twitch {
static void get(QString url, const QObject *caller,