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,48 +48,51 @@ void EmoteManager::reloadBTTVChannelEmotes(const QString &channelName, std::weak
printf("[EmoteManager] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName)); printf("[EmoteManager] Reload BTTV Channel Emotes for channel %s\n", qPrintable(channelName));
QString url("https://api.betterttv.net/2/channels/" + channelName); QString url("https://api.betterttv.net/2/channels/" + channelName);
util::urlFetchJSONTimeout(
url, QThread::currentThread(),
[this, channelName, _map](QJsonObject &rootNode) {
auto map = _map.lock();
if (_map.expired()) { debug::Log("Request bttv channel emotes for {}", channelName);
return;
}
map->clear(); 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();
auto emotesNode = rootNode.value("emotes").toArray(); if (_map.expired()) {
return;
}
QString linkTemplate = "https:" + rootNode.value("urlTemplate").toString(); map->clear();
std::vector<std::string> codes; auto emotesNode = rootNode.value("emotes").toArray();
for (const QJsonValue &emoteNode : emotesNode) {
QJsonObject emoteObject = emoteNode.toObject();
QString id = emoteObject.value("id").toString(); QString linkTemplate = "https:" + rootNode.value("urlTemplate").toString();
QString code = emoteObject.value("code").toString();
// emoteObject.value("imageType").toString();
QString link = linkTemplate; std::vector<std::string> codes;
link.detach(); for (const QJsonValue &emoteNode : emotesNode) {
QJsonObject emoteObject = emoteNode.toObject();
link = link.replace("{{id}}", id).replace("{{image}}", "1x"); QString id = emoteObject.value("id").toString();
QString code = emoteObject.value("code").toString();
// emoteObject.value("imageType").toString();
auto emote = QString link = linkTemplate;
this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [this, &code, &link] { link.detach();
return EmoteData(new LazyLoadedImage(*this, this->windowManager, link, 1,
code, code + "\nChannel BTTV Emote"));
});
this->bttvChannelEmotes.insert(code, emote); link = link.replace("{{id}}", id).replace("{{image}}", "1x");
map->insert(code, emote);
codes.push_back(code.toStdString());
}
this->bttvChannelEmoteCodes[channelName.toStdString()] = codes; auto emote = this->getBTTVChannelEmoteFromCaches().getOrAdd(id, [this, &code, &link] {
}, return EmoteData(new LazyLoadedImage(*this, this->windowManager, link, 1, code,
1500); code + "\nChannel BTTV Emote"));
});
this->bttvChannelEmotes.insert(code, emote);
map->insert(code, emote);
codes.push_back(code.toStdString());
}
this->bttvChannelEmoteCodes[channelName.toStdString()] = codes;
});
} }
void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map) void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_ptr<EmoteMap> _map)
@ -98,48 +101,48 @@ void EmoteManager::reloadFFZChannelEmotes(const QString &channelName, std::weak_
QString url("http://api.frankerfacez.com/v1/room/" + channelName); QString url("http://api.frankerfacez.com/v1/room/" + channelName);
util::urlFetchJSONTimeout( util::NetworkRequest req(url);
url, QThread::currentThread(), req.setCaller(QThread::currentThread());
[this, channelName, _map](QJsonObject &rootNode) { req.setTimeout(3000);
auto map = _map.lock(); req.getJSON([this, channelName, _map](QJsonObject &rootNode) {
auto map = _map.lock();
if (_map.expired()) { if (_map.expired()) {
return; return;
} }
map->clear(); map->clear();
auto setsNode = rootNode.value("sets").toObject(); auto setsNode = rootNode.value("sets").toObject();
std::vector<std::string> codes; std::vector<std::string> codes;
for (const QJsonValue &setNode : setsNode) { for (const QJsonValue &setNode : setsNode) {
auto emotesNode = setNode.toObject().value("emoticons").toArray(); auto emotesNode = setNode.toObject().value("emoticons").toArray();
for (const QJsonValue &emoteNode : emotesNode) { for (const QJsonValue &emoteNode : emotesNode) {
QJsonObject emoteObject = emoteNode.toObject(); QJsonObject emoteObject = emoteNode.toObject();
// margins // margins
int id = emoteObject.value("id").toInt(); int id = emoteObject.value("id").toInt();
QString code = emoteObject.value("name").toString(); QString code = emoteObject.value("name").toString();
QJsonObject urls = emoteObject.value("urls").toObject(); QJsonObject urls = emoteObject.value("urls").toObject();
QString url1 = "http:" + urls.value("1").toString(); QString url1 = "http:" + urls.value("1").toString();
auto emote = this->getFFZChannelEmoteFromCaches().getOrAdd(id, [this, &code, auto emote =
&url1] { this->getFFZChannelEmoteFromCaches().getOrAdd(id, [this, &code, &url1] {
return EmoteData(new LazyLoadedImage(*this, this->windowManager, url1, 1, return EmoteData(new LazyLoadedImage(*this, this->windowManager, url1, 1,
code, code + "\nGlobal FFZ Emote")); code, code + "\nGlobal FFZ Emote"));
}); });
this->ffzChannelEmotes.insert(code, emote); this->ffzChannelEmotes.insert(code, emote);
map->insert(code, emote); map->insert(code, emote);
codes.push_back(code.toStdString()); codes.push_back(code.toStdString());
}
this->ffzChannelEmoteCodes[channelName.toStdString()] = codes;
} }
},
1500); this->ffzChannelEmoteCodes[channelName.toStdString()] = codes;
}
});
} }
ConcurrentMap<QString, twitch::EmoteValue *> &EmoteManager::getTwitchEmotes() ConcurrentMap<QString, twitch::EmoteValue *> &EmoteManager::getTwitchEmotes()
@ -383,28 +386,28 @@ void EmoteManager::refreshTwitchEmotes(const std::string &roomID)
qDebug() << url; qDebug() << url;
util::urlFetchJSONTimeout( util::NetworkRequest req(url);
url, QThread::currentThread(), req.setCaller(QThread::currentThread());
[=, &emoteData](QJsonObject &root) { req.setTimeout(3000);
emoteData.emoteSets.clear(); req.getJSON([=, &emoteData](QJsonObject &root) {
emoteData.emoteCodes.clear(); emoteData.emoteSets.clear();
auto emoticonSets = root.value("emoticon_sets").toObject(); emoteData.emoteCodes.clear();
for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) { auto emoticonSets = root.value("emoticon_sets").toObject();
std::string emoteSetString = it.key().toStdString(); for (QJsonObject::iterator it = emoticonSets.begin(); it != emoticonSets.end(); ++it) {
QJsonArray emoteSetList = it.value().toArray(); std::string emoteSetString = it.key().toStdString();
QJsonArray emoteSetList = it.value().toArray();
for (QJsonValue emoteValue : emoteSetList) { for (QJsonValue emoteValue : emoteSetList) {
QJsonObject emoticon = emoteValue.toObject(); QJsonObject emoticon = emoteValue.toObject();
std::string id = emoticon["id"].toString().toStdString(); std::string id = emoticon["id"].toString().toStdString();
std::string code = emoticon["code"].toString().toStdString(); std::string code = emoticon["code"].toString().toStdString();
emoteData.emoteSets[emoteSetString].push_back({id, code}); emoteData.emoteSets[emoteSetString].push_back({id, code});
emoteData.emoteCodes.push_back(code); emoteData.emoteCodes.push_back(code);
}
} }
}
emoteData.filled = true; emoteData.filled = true;
}, });
3000);
} }
void EmoteManager::loadBTTVEmotes() void EmoteManager::loadBTTVEmotes()

View file

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

View file

@ -38,51 +38,6 @@ static QJsonObject parseJSONFromReply(QNetworkReply *reply)
return jsonDoc.object(); 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 { namespace twitch {
static void get(QString url, const QObject *caller, static void get(QString url, const QObject *caller,