Refactored NetworkRequest and misc

This commit is contained in:
fourtf 2018-07-06 17:56:11 +02:00
parent 477d4751f7
commit 3993708164
8 changed files with 244 additions and 243 deletions

View file

@ -22,10 +22,10 @@ Channel::Channel(const QString &_name, Type _type)
, completionModel(this->name) , completionModel(this->name)
, type_(_type) , type_(_type)
{ {
QObject::connect(&this->clearCompletionModelTimer, &QTimer::timeout, [this]() { QObject::connect(&this->clearCompletionModelTimer_, &QTimer::timeout, [this]() {
this->completionModel.ClearExpiredStrings(); // this->completionModel.clearExpiredStrings(); //
}); });
this->clearCompletionModelTimer.start(60 * 1000); this->clearCompletionModelTimer_.start(60 * 1000);
} }
Channel::~Channel() Channel::~Channel()

View file

@ -213,7 +213,7 @@ void CompletionModel::clearExpiredStrings()
for (auto it = this->emotes_.begin(); it != this->emotes_.end();) { for (auto it = this->emotes_.begin(); it != this->emotes_.end();) {
const auto &taggedString = *it; const auto &taggedString = *it;
if (taggedString.HasExpired(now)) { if (taggedString.isExpired(now)) {
// Log("String {} expired", taggedString.str); // Log("String {} expired", taggedString.str);
it = this->emotes_.erase(it); it = this->emotes_.erase(it);
} else { } else {

View file

@ -14,12 +14,12 @@ struct EmoteData {
bool isValid() const; bool isValid() const;
Image *getImage(float scale) const; Image *getImage(float scale) const;
// Link to the emote page i.e. https://www.frankerfacez.com/emoticon/144722-pajaCringe
QString pageLink;
Image *image1x = nullptr; Image *image1x = nullptr;
Image *image2x = nullptr; Image *image2x = nullptr;
Image *image3x = nullptr; Image *image3x = nullptr;
// Link to the emote page i.e. https://www.frankerfacez.com/emoticon/144722-pajaCringe
QString pageLink;
}; };
using EmoteMap = ConcurrentMap<QString, EmoteData>; using EmoteMap = ConcurrentMap<QString, EmoteData>;

View file

@ -10,29 +10,29 @@ class LockedObject
public: public:
LockedObject &operator=(const LockedObject<Type> &other) LockedObject &operator=(const LockedObject<Type> &other)
{ {
this->mutex.lock(); this->mutex_.lock();
this->data = other.getValue(); this->data = other.getValue();
this->mutex.unlock(); this->mutex_.unlock();
return *this; return *this;
} }
LockedObject &operator=(const Type &other) LockedObject &operator=(const Type &other)
{ {
this->mutex.lock(); this->mutex_.lock();
this->data = other; this->data = other;
this->mutex.unlock(); this->mutex_.unlock();
return *this; return *this;
} }
private: private:
Type value; Type value_;
std::mutex mutex; std::mutex mutex_;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -8,32 +8,33 @@ namespace chatterino {
template <typename T> template <typename T>
class MutexValue : boost::noncopyable class MutexValue : boost::noncopyable
{ {
mutable std::mutex mutex;
T value;
public: public:
MutexValue() MutexValue()
{ {
} }
MutexValue(T &&val) MutexValue(T &&val)
: value(val) : value_(val)
{ {
} }
T get() const T get() const
{ {
std::lock_guard<std::mutex> guard(this->mutex); std::lock_guard<std::mutex> guard(this->mutex_);
return this->value; return this->value_;
} }
void set(const T &val) void set(const T &val)
{ {
std::lock_guard<std::mutex> guard(this->mutex); std::lock_guard<std::mutex> guard(this->mutex_);
this->value = val; this->value_ = val;
} }
private:
mutable std::mutex mutex_;
T value_;
}; };
} // namespace chatterino } // namespace chatterino

View file

@ -14,22 +14,6 @@
namespace chatterino { namespace chatterino {
static QJsonObject parseJSONFromReplyxD(QNetworkReply *reply)
{
if (reply->error() != QNetworkReply::NetworkError::NoError) {
return QJsonObject();
}
QByteArray data = reply->readAll();
QJsonDocument jsonDoc(QJsonDocument::fromJson(data));
if (jsonDoc.isNull()) {
return QJsonObject();
}
return jsonDoc.object();
}
class NetworkManager : public QObject class NetworkManager : public QObject
{ {
Q_OBJECT Q_OBJECT

View file

@ -19,6 +19,48 @@ NetworkRequest::NetworkRequest(const QString &url)
this->data.request.setUrl(QUrl(url)); this->data.request.setUrl(QUrl(url));
} }
void NetworkRequest::setRequestType(RequestType newRequestType)
{
this->data.requestType = newRequestType;
}
void NetworkRequest::setCaller(const QObject *_caller)
{
this->data.caller = _caller;
}
void NetworkRequest::setOnReplyCreated(std::function<void(QNetworkReply *)> f)
{
this->data.onReplyCreated = f;
}
void NetworkRequest::setRawHeader(const char *headerName, const char *value)
{
this->data.request.setRawHeader(headerName, value);
}
void NetworkRequest::setRawHeader(const char *headerName, const QByteArray &value)
{
this->data.request.setRawHeader(headerName, value);
}
void NetworkRequest::setRawHeader(const char *headerName, const QString &value)
{
this->data.request.setRawHeader(headerName, value.toUtf8());
}
void NetworkRequest::setTimeout(int ms)
{
this->data.timeoutMS = ms;
}
void NetworkRequest::makeAuthorizedV5(const QString &clientID, const QString &oauthToken)
{
this->setRawHeader("Client-ID", clientID);
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
this->setRawHeader("Authorization", "OAuth " + oauthToken);
}
void NetworkRequest::setUseQuickLoadCache(bool value) void NetworkRequest::setUseQuickLoadCache(bool value)
{ {
this->data.useQuickLoadCache = value; this->data.useQuickLoadCache = value;
@ -39,4 +81,170 @@ void NetworkRequest::Data::writeToCache(const QByteArray &bytes)
} }
} }
void NetworkRequest::execute()
{
switch (this->data.requestType) {
case GetRequest: {
this->executeGet();
} break;
case PutRequest: {
this->executePut();
} break;
case DeleteRequest: {
this->executeDelete();
} break;
default: {
Log("[Execute] Unhandled request type {}", (int)this->data.requestType);
} break;
}
}
void NetworkRequest::useCache()
{
if (this->data.useQuickLoadCache) {
auto app = getApp();
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data.getHash());
if (cachedFile.exists()) {
if (cachedFile.open(QIODevice::ReadOnly)) {
QByteArray bytes = cachedFile.readAll();
// qDebug() << "Loaded cached resource" << this->data.request.url();
auto document = parseJSONFromData2(bytes);
bool success = false;
if (!document.IsNull()) {
success = this->data.onSuccess(document);
}
cachedFile.close();
if (!success) {
// The images were not successfully loaded from the file
// XXX: Invalidate the cache file so we don't attempt to load it again next
// time
}
}
}
}
}
void NetworkRequest::doRequest()
{
QTimer *timer = nullptr;
if (this->data.timeoutMS > 0) {
timer = new QTimer;
}
NetworkRequester requester;
NetworkWorker *worker = new NetworkWorker;
worker->moveToThread(&NetworkManager::workerThread);
if (this->data.caller != nullptr) {
QObject::connect(worker, &NetworkWorker::doneUrl,
this->data.caller, [data = this->data](auto reply) mutable {
if (reply->error() != QNetworkReply::NetworkError::NoError) {
if (data.onError) {
data.onError(reply->error());
}
return;
}
QByteArray readBytes = reply->readAll();
QByteArray bytes;
bytes.setRawData(readBytes.data(), readBytes.size());
data.writeToCache(bytes);
data.onSuccess(parseJSONFromData2(bytes));
reply->deleteLater();
});
}
if (timer != nullptr) {
timer->start(this->data.timeoutMS);
}
QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
[ timer, data = std::move(this->data), worker ]() {
QNetworkReply *reply = nullptr;
switch (data.requestType) {
case GetRequest: {
reply = NetworkManager::NaM.get(data.request);
} break;
case PutRequest: {
reply = NetworkManager::NaM.put(data.request, data.payload);
} break;
case DeleteRequest: {
reply = NetworkManager::NaM.deleteResource(data.request);
} break;
}
if (reply == nullptr) {
Log("Unhandled request type {}", (int)data.requestType);
return;
}
if (timer != nullptr) {
QObject::connect(timer, &QTimer::timeout, worker,
[reply, timer, data]() {
Log("Aborted!");
reply->abort();
timer->deleteLater();
data.onError(-2);
});
}
if (data.onReplyCreated) {
data.onReplyCreated(reply);
}
QObject::connect(reply, &QNetworkReply::finished, worker,
[ data = std::move(data), worker, reply ]() mutable {
if (data.caller == nullptr) {
QByteArray bytes = reply->readAll();
data.writeToCache(bytes);
if (data.onSuccess) {
data.onSuccess(parseJSONFromData2(bytes));
} else {
qWarning() << "data.onSuccess not found";
}
reply->deleteLater();
} else {
emit worker->doneUrl(reply);
}
delete worker;
});
});
emit requester.requestUrl();
}
void NetworkRequest::executeGet()
{
this->useCache();
this->doRequest();
}
void NetworkRequest::executePut()
{
this->doRequest();
}
void NetworkRequest::executeDelete()
{
this->doRequest();
}
} // namespace chatterino } // namespace chatterino

View file

@ -115,10 +115,7 @@ public:
explicit NetworkRequest(const std::string &url); explicit NetworkRequest(const std::string &url);
explicit NetworkRequest(const QString &url); explicit NetworkRequest(const QString &url);
void setRequestType(RequestType newRequestType) void setRequestType(RequestType newRequestType);
{
this->data.requestType = newRequestType;
}
template <typename Func> template <typename Func>
void onError(Func cb) void onError(Func cb)
@ -138,43 +135,13 @@ public:
} }
void setUseQuickLoadCache(bool value); void setUseQuickLoadCache(bool value);
void setCaller(const QObject *_caller);
void setCaller(const QObject *_caller) void setOnReplyCreated(std::function<void(QNetworkReply *)> f);
{ void setRawHeader(const char *headerName, const char *value);
this->data.caller = _caller; void setRawHeader(const char *headerName, const QByteArray &value);
} void setRawHeader(const char *headerName, const QString &value);
void setTimeout(int ms);
void setOnReplyCreated(std::function<void(QNetworkReply *)> f) void makeAuthorizedV5(const QString &clientID, const QString &oauthToken);
{
this->data.onReplyCreated = f;
}
void setRawHeader(const char *headerName, const char *value)
{
this->data.request.setRawHeader(headerName, value);
}
void setRawHeader(const char *headerName, const QByteArray &value)
{
this->data.request.setRawHeader(headerName, value);
}
void setRawHeader(const char *headerName, const QString &value)
{
this->data.request.setRawHeader(headerName, value.toUtf8());
}
void setTimeout(int ms)
{
this->data.timeoutMS = ms;
}
void makeAuthorizedV5(const QString &clientID, const QString &oauthToken)
{
this->setRawHeader("Client-ID", clientID);
this->setRawHeader("Accept", "application/vnd.twitchtv.v5+json");
this->setRawHeader("Authorization", "OAuth " + oauthToken);
}
template <typename FinishedCallback> template <typename FinishedCallback>
void get(FinishedCallback onFinished) void get(FinishedCallback onFinished)
@ -300,173 +267,14 @@ public:
}); });
} }
void execute() void execute();
{
switch (this->data.requestType) {
case GetRequest: {
this->executeGet();
} break;
case PutRequest: {
this->executePut();
} break;
case DeleteRequest: {
this->executeDelete();
} break;
default: {
Log("[Execute] Unhandled request type {}", (int)this->data.requestType);
} break;
}
}
private: private:
void useCache() void useCache();
{ void doRequest();
if (this->data.useQuickLoadCache) { void executeGet();
auto app = getApp(); void executePut();
void executeDelete();
QFile cachedFile(app->paths->cacheDirectory + "/" + this->data.getHash());
if (cachedFile.exists()) {
if (cachedFile.open(QIODevice::ReadOnly)) {
QByteArray bytes = cachedFile.readAll();
// qDebug() << "Loaded cached resource" << this->data.request.url();
auto document = parseJSONFromData2(bytes);
bool success = false;
if (!document.IsNull()) {
success = this->data.onSuccess(document);
}
cachedFile.close();
if (!success) {
// The images were not successfully loaded from the file
// XXX: Invalidate the cache file so we don't attempt to load it again next
// time
}
}
}
}
}
void doRequest()
{
QTimer *timer = nullptr;
if (this->data.timeoutMS > 0) {
timer = new QTimer;
}
NetworkRequester requester;
NetworkWorker *worker = new NetworkWorker;
worker->moveToThread(&NetworkManager::workerThread);
if (this->data.caller != nullptr) {
QObject::connect(worker, &NetworkWorker::doneUrl,
this->data.caller, [data = this->data](auto reply) mutable {
if (reply->error() != QNetworkReply::NetworkError::NoError) {
if (data.onError) {
data.onError(reply->error());
}
return;
}
QByteArray readBytes = reply->readAll();
QByteArray bytes;
bytes.setRawData(readBytes.data(), readBytes.size());
data.writeToCache(bytes);
data.onSuccess(parseJSONFromData2(bytes));
reply->deleteLater();
});
}
if (timer != nullptr) {
timer->start(this->data.timeoutMS);
}
QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
[ timer, data = std::move(this->data), worker ]() {
QNetworkReply *reply = nullptr;
switch (data.requestType) {
case GetRequest: {
reply = NetworkManager::NaM.get(data.request);
} break;
case PutRequest: {
reply = NetworkManager::NaM.put(data.request, data.payload);
} break;
case DeleteRequest: {
reply = NetworkManager::NaM.deleteResource(data.request);
} break;
}
if (reply == nullptr) {
Log("Unhandled request type {}", (int)data.requestType);
return;
}
if (timer != nullptr) {
QObject::connect(timer, &QTimer::timeout, worker,
[reply, timer, data]() {
Log("Aborted!");
reply->abort();
timer->deleteLater();
data.onError(-2);
});
}
if (data.onReplyCreated) {
data.onReplyCreated(reply);
}
QObject::connect(reply, &QNetworkReply::finished, worker,
[ data = std::move(data), worker, reply ]() mutable {
if (data.caller == nullptr) {
QByteArray bytes = reply->readAll();
data.writeToCache(bytes);
if (data.onSuccess) {
data.onSuccess(parseJSONFromData2(bytes));
} else {
qWarning() << "data.onSuccess not found";
}
reply->deleteLater();
} else {
emit worker->doneUrl(reply);
}
delete worker;
});
});
emit requester.requestUrl();
}
void executeGet()
{
this->useCache();
this->doRequest();
}
void executePut()
{
this->doRequest();
}
void executeDelete()
{
this->doRequest();
}
}; };
} // namespace chatterino } // namespace chatterino