From 2ea364310081a6edcd67f4e60f524e8c29705aab Mon Sep 17 00:00:00 2001 From: pajlada Date: Sat, 7 Jul 2018 15:50:05 +0200 Subject: [PATCH] Fix timer stuff (#580) * Add and sort Network files to chatterino project file --- chatterino.pro | 11 +++++---- src/common/NetworkRequest.cpp | 12 +++++----- src/common/NetworkRequest.hpp | 2 +- src/common/NetworkTimer.cpp | 39 +++++++++++++++++++++++++++++++ src/common/NetworkTimer.hpp | 43 ++++++++--------------------------- 5 files changed, 62 insertions(+), 45 deletions(-) create mode 100644 src/common/NetworkTimer.cpp diff --git a/chatterino.pro b/chatterino.pro index 0716cd70..22554da5 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -102,10 +102,11 @@ SOURCES += \ src/common/Channel.cpp \ src/common/CompletionModel.cpp \ src/common/Emotemap.cpp \ - src/common/NetworkManager.cpp \ - src/common/NetworkResult.cpp \ src/common/NetworkData.cpp \ + src/common/NetworkManager.cpp \ src/common/NetworkRequest.cpp \ + src/common/NetworkResult.cpp \ + src/common/NetworkTimer.cpp \ src/controllers/accounts/Account.cpp \ src/controllers/accounts/AccountController.cpp \ src/controllers/accounts/AccountModel.cpp \ @@ -241,11 +242,13 @@ HEADERS += \ src/common/FlagsEnum.hpp \ src/common/LockedObject.hpp \ src/common/MutexValue.hpp \ - src/common/NetworkManager.hpp \ - src/common/NetworkResult.hpp \ + src/common/NetworkCommon.hpp \ src/common/NetworkData.hpp \ + src/common/NetworkManager.hpp \ src/common/NetworkRequest.hpp \ src/common/NetworkRequester.hpp \ + src/common/NetworkResult.hpp \ + src/common/NetworkTimer.hpp \ src/common/NetworkWorker.hpp \ src/common/NullablePtr.hpp \ src/common/Property.hpp \ diff --git a/src/common/NetworkRequest.cpp b/src/common/NetworkRequest.cpp index 68a91a77..0311c09e 100644 --- a/src/common/NetworkRequest.cpp +++ b/src/common/NetworkRequest.cpp @@ -105,7 +105,6 @@ void NetworkRequest::execute() // Get requests try to load from cache, then perform the request if (this->data->useQuickLoadCache_) { if (this->tryLoadCachedFile()) { - Log("Loaded from cache"); // Successfully loaded from cache return; } @@ -167,8 +166,7 @@ void NetworkRequest::doRequest() this->timer->start(); - auto onUrlRequested = [data = std::move(this->data), timer = std::move(this->timer), - worker]() mutable { + auto onUrlRequested = [data = this->data, timer = this->timer, worker]() mutable { QNetworkReply *reply = nullptr; switch (data->requestType_) { case NetworkRequestType::Get: { @@ -205,7 +203,8 @@ void NetworkRequest::doRequest() bool directAction = (data->caller_ == nullptr); - auto handleReply = [data = std::move(data), timer = std::move(timer), reply]() mutable { + auto handleReply = [data, timer, reply]() mutable { + // TODO(pajlada): A reply was received, kill the timeout timer if (reply->error() != QNetworkReply::NetworkError::NoError) { if (data->onError_) { data->onError_(reply->error()); @@ -225,7 +224,8 @@ void NetworkRequest::doRequest() }; if (data->caller_ != nullptr) { - QObject::connect(worker, &NetworkWorker::doneUrl, data->caller_, std::move(handleReply)); + QObject::connect(worker, &NetworkWorker::doneUrl, data->caller_, + std::move(handleReply)); QObject::connect(reply, &QNetworkReply::finished, worker, [worker]() mutable { emit worker->doneUrl(); @@ -241,7 +241,7 @@ void NetworkRequest::doRequest() } }; - QObject::connect(&requester, &NetworkRequester::requestUrl, worker, std::move(onUrlRequested)); + QObject::connect(&requester, &NetworkRequester::requestUrl, worker, onUrlRequested); emit requester.requestUrl(); } diff --git a/src/common/NetworkRequest.hpp b/src/common/NetworkRequest.hpp index cac47a6b..e2fd2e7e 100644 --- a/src/common/NetworkRequest.hpp +++ b/src/common/NetworkRequest.hpp @@ -18,7 +18,7 @@ class NetworkRequest // Timer that tracks the timeout // By default, there's no explicit timeout for the request // to enable the timer, the "setTimeout" function needs to be called before execute is called - std::unique_ptr timer; + std::shared_ptr timer; // The NetworkRequest destructor will assert if executed_ hasn't been set to true before dying bool executed_ = false; diff --git a/src/common/NetworkTimer.cpp b/src/common/NetworkTimer.cpp new file mode 100644 index 00000000..522fdd31 --- /dev/null +++ b/src/common/NetworkTimer.cpp @@ -0,0 +1,39 @@ +#include "common/NetworkTimer.hpp" + +#include "common/NetworkWorker.hpp" + +#include + +#include + +namespace chatterino { + +void NetworkTimer::start() +{ + if (this->timeoutMS_ <= 0) { + return; + } + + this->timer_ = new QTimer; + this->timer_->start(this->timeoutMS_); + QObject::connect(this->timer_, &QTimer::timeout, [timer = this->timer_] { + timer->deleteLater(); // + }); + + this->started_ = true; +} + +bool NetworkTimer::isStarted() const +{ + return this->started_; +} + +void NetworkTimer::onTimeout(NetworkWorker *worker, std::function cb) const +{ + assert(this->timer_ != nullptr); + assert(worker != nullptr); + + QObject::connect(this->timer_, &QTimer::timeout, worker, cb); +} + +} // namespace chatterino diff --git a/src/common/NetworkTimer.hpp b/src/common/NetworkTimer.hpp index a4765f2e..fc2e25ab 100644 --- a/src/common/NetworkTimer.hpp +++ b/src/common/NetworkTimer.hpp @@ -1,18 +1,16 @@ #pragma once -#include "common/NetworkWorker.hpp" - -#include - -#include #include -#include + +class QTimer; namespace chatterino { +class NetworkWorker; + class NetworkTimer { - std::unique_ptr timer_; + QTimer *timer_ = nullptr; bool started_{}; @@ -20,10 +18,7 @@ public: int timeoutMS_ = -1; NetworkTimer() = default; - ~NetworkTimer() - { - this->timer_.release(); - } + ~NetworkTimer() = default; NetworkTimer(const NetworkTimer &other) = delete; NetworkTimer &operator=(const NetworkTimer &other) = delete; @@ -31,31 +26,11 @@ public: NetworkTimer(NetworkTimer &&other) = default; NetworkTimer &operator=(NetworkTimer &&other) = default; - void start() - { - if (this->timeoutMS_ <= 0) { - return; - } + void start(); - this->timer_ = std::make_unique(); - this->timer_->start(this->timeoutMS_); + void onTimeout(NetworkWorker *worker, std::function cb) const; - this->started_ = true; - } - - bool isStarted() const - { - return this->started_; - } - - void onTimeout(NetworkWorker *worker, std::function cb) const - { - if (!this->timer_) { - return; - } - - QObject::connect(this->timer_.get(), &QTimer::timeout, worker, cb); - } + bool isStarted() const; }; } // namespace chatterino