NetworkController.cpp
134 lines
| 4.7 KiB
| text/x-c
|
CppLexer
r313 | #include "Network/NetworkController.h" | |||
#include <QMutex> | ||||
r356 | #include <QNetworkAccessManager> | |||
#include <QNetworkReply> | ||||
r359 | #include <QNetworkRequest> | |||
r389 | #include <QReadWriteLock> | |||
r313 | #include <QThread> | |||
r359 | #include <unordered_map> | |||
r313 | Q_LOGGING_CATEGORY(LOG_NetworkController, "NetworkController") | |||
struct NetworkController::NetworkControllerPrivate { | ||||
r359 | explicit NetworkControllerPrivate(NetworkController *parent) : m_WorkingMutex{} {} | |||
r510 | ||||
void lockRead() { m_Lock.lockForRead(); } | ||||
void lockWrite() { m_Lock.lockForWrite(); } | ||||
void unlock() { m_Lock.unlock(); } | ||||
r313 | QMutex m_WorkingMutex; | |||
r356 | ||||
r389 | QReadWriteLock m_Lock; | |||
r359 | std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToVariableId; | |||
r356 | std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr}; | |||
r313 | }; | |||
NetworkController::NetworkController(QObject *parent) | ||||
: QObject(parent), impl{spimpl::make_unique_impl<NetworkControllerPrivate>(this)} | ||||
{ | ||||
r356 | } | |||
r359 | void NetworkController::onProcessRequested(const QNetworkRequest &request, QUuid identifier, | |||
std::function<void(QNetworkReply *, QUuid)> callback) | ||||
r356 | { | |||
r389 | qCDebug(LOG_NetworkController()) << tr("NetworkController registered") | |||
r392 | << QThread::currentThread()->objectName(); | |||
auto reply = impl->m_AccessManager->get(request); | ||||
r359 | ||||
// Store the couple reply id | ||||
r389 | impl->lockWrite(); | |||
r359 | impl->m_NetworkReplyToVariableId[reply] = identifier; | |||
r389 | impl->unlock(); | |||
r359 | ||||
auto onReplyFinished = [reply, this, identifier, callback]() { | ||||
r389 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished") | |||
<< QThread::currentThread() << reply; | ||||
impl->lockRead(); | ||||
r359 | auto it = impl->m_NetworkReplyToVariableId.find(reply); | |||
r389 | impl->unlock(); | |||
r359 | if (it != impl->m_NetworkReplyToVariableId.cend()) { | |||
r389 | impl->lockWrite(); | |||
impl->m_NetworkReplyToVariableId.erase(reply); | ||||
impl->unlock(); | ||||
// Deletes reply | ||||
r359 | callback(reply, identifier); | |||
r389 | reply->deleteLater(); | |||
emit this->replyDownloadProgress(identifier, 0); | ||||
r359 | } | |||
r389 | ||||
qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished END") | ||||
<< QThread::currentThread() << reply; | ||||
r359 | }; | |||
r369 | auto onReplyProgress = [reply, this](qint64 bytesRead, qint64 totalBytes) { | |||
r356 | ||||
r369 | double progress = (bytesRead * 100.0) / totalBytes; | |||
r389 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress | |||
<< QThread::currentThread() << reply; | ||||
impl->lockRead(); | ||||
r359 | auto it = impl->m_NetworkReplyToVariableId.find(reply); | |||
r389 | impl->unlock(); | |||
r359 | if (it != impl->m_NetworkReplyToVariableId.cend()) { | |||
r369 | emit this->replyDownloadProgress(it->second, progress); | |||
r359 | } | |||
r389 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END") | |||
<< QThread::currentThread() << reply; | ||||
r359 | }; | |||
r356 | ||||
r359 | ||||
connect(reply, &QNetworkReply::finished, this, onReplyFinished); | ||||
r369 | connect(reply, &QNetworkReply::downloadProgress, this, onReplyProgress); | |||
r392 | qCDebug(LOG_NetworkController()) << tr("NetworkController registered END") | |||
<< QThread::currentThread()->objectName() << reply; | ||||
r313 | } | |||
void NetworkController::initialize() | ||||
{ | ||||
qCDebug(LOG_NetworkController()) << tr("NetworkController init") << QThread::currentThread(); | ||||
impl->m_WorkingMutex.lock(); | ||||
r359 | impl->m_AccessManager = std::make_unique<QNetworkAccessManager>(); | |||
r392 | ||||
auto onReplyErrors = [this](QNetworkReply *reply, const QList<QSslError> &errors) { | ||||
qCCritical(LOG_NetworkController()) << tr("NetworkAcessManager errors: ") << errors; | ||||
}; | ||||
connect(impl->m_AccessManager.get(), &QNetworkAccessManager::sslErrors, this, onReplyErrors); | ||||
r313 | qCDebug(LOG_NetworkController()) << tr("NetworkController init END"); | |||
} | ||||
void NetworkController::finalize() | ||||
{ | ||||
impl->m_WorkingMutex.unlock(); | ||||
} | ||||
r359 | void NetworkController::onReplyCanceled(QUuid identifier) | |||
{ | ||||
auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; | ||||
r389 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled") | |||
<< QThread::currentThread(); | ||||
r359 | ||||
r389 | impl->lockRead(); | |||
r359 | auto end = impl->m_NetworkReplyToVariableId.cend(); | |||
auto it = std::find_if(impl->m_NetworkReplyToVariableId.cbegin(), end, findReply); | ||||
r389 | impl->unlock(); | |||
r359 | if (it != end) { | |||
it->first->abort(); | ||||
} | ||||
r389 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END") | |||
<< QThread::currentThread(); | ||||
r359 | } | |||
r313 | void NetworkController::waitForFinish() | |||
{ | ||||
QMutexLocker locker{&impl->m_WorkingMutex}; | ||||
} | ||||