@@ -21,7 +21,7 struct NetworkController::NetworkControllerPrivate { | |||||
21 | QMutex m_WorkingMutex; |
|
21 | QMutex m_WorkingMutex; | |
22 |
|
22 | |||
23 | QReadWriteLock m_Lock; |
|
23 | QReadWriteLock m_Lock; | |
24 |
std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyTo |
|
24 | std::unordered_map<QNetworkReply *, QUuid> m_NetworkReplyToId; | |
25 | std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr}; |
|
25 | std::unique_ptr<QNetworkAccessManager> m_AccessManager{nullptr}; | |
26 | }; |
|
26 | }; | |
27 |
|
27 | |||
@@ -40,7 +40,8 void NetworkController::onProcessRequested(std::shared_ptr<QNetworkRequest> requ | |||||
40 |
|
40 | |||
41 | // Store the couple reply id |
|
41 | // Store the couple reply id | |
42 | impl->lockWrite(); |
|
42 | impl->lockWrite(); | |
43 |
impl->m_NetworkReplyTo |
|
43 | impl->m_NetworkReplyToId[reply] = identifier; | |
|
44 | qCDebug(LOG_NetworkController()) << tr("Store for reply: ") << identifier; | |||
44 | impl->unlock(); |
|
45 | impl->unlock(); | |
45 |
|
46 | |||
46 | auto onReplyFinished = [request, reply, this, identifier, callback]() { |
|
47 | auto onReplyFinished = [request, reply, this, identifier, callback]() { | |
@@ -48,11 +49,13 void NetworkController::onProcessRequested(std::shared_ptr<QNetworkRequest> requ | |||||
48 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished") |
|
49 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyFinished") | |
49 | << QThread::currentThread() << request.get() << reply; |
|
50 | << QThread::currentThread() << request.get() << reply; | |
50 | impl->lockRead(); |
|
51 | impl->lockRead(); | |
51 |
auto it = impl->m_NetworkReplyTo |
|
52 | auto it = impl->m_NetworkReplyToId.find(reply); | |
52 | impl->unlock(); |
|
53 | impl->unlock(); | |
53 |
if (it != impl->m_NetworkReplyTo |
|
54 | if (it != impl->m_NetworkReplyToId.cend()) { | |
54 | impl->lockWrite(); |
|
55 | impl->lockWrite(); | |
55 | impl->m_NetworkReplyToVariableId.erase(reply); |
|
56 | qCDebug(LOG_NetworkController()) << tr("Remove for reply: ") | |
|
57 | << impl->m_NetworkReplyToId[reply]; | |||
|
58 | impl->m_NetworkReplyToId.erase(reply); | |||
56 | impl->unlock(); |
|
59 | impl->unlock(); | |
57 | // Deletes reply |
|
60 | // Deletes reply | |
58 | callback(reply, identifier); |
|
61 | callback(reply, identifier); | |
@@ -65,17 +68,21 void NetworkController::onProcessRequested(std::shared_ptr<QNetworkRequest> requ | |||||
65 |
|
68 | |||
66 | auto onReplyProgress = [reply, request, this](qint64 bytesRead, qint64 totalBytes) { |
|
69 | auto onReplyProgress = [reply, request, this](qint64 bytesRead, qint64 totalBytes) { | |
67 |
|
70 | |||
|
71 | // NOTE: a totalbytes of 0 can happened when a request has been aborted | |||
|
72 | if (totalBytes > 0) { | |||
68 | double progress = (bytesRead * 100.0) / totalBytes; |
|
73 | double progress = (bytesRead * 100.0) / totalBytes; | |
69 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress |
|
74 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress") << progress | |
70 |
<< QThread::currentThread() << request.get() << reply |
|
75 | << QThread::currentThread() << request.get() << reply | |
|
76 | << bytesRead << totalBytes; | |||
71 | impl->lockRead(); |
|
77 | impl->lockRead(); | |
72 |
auto it = impl->m_NetworkReplyTo |
|
78 | auto it = impl->m_NetworkReplyToId.find(reply); | |
73 | impl->unlock(); |
|
79 | impl->unlock(); | |
74 |
if (it != impl->m_NetworkReplyTo |
|
80 | if (it != impl->m_NetworkReplyToId.cend()) { | |
75 | emit this->replyDownloadProgress(it->second, request, progress); |
|
81 | emit this->replyDownloadProgress(it->second, request, progress); | |
76 | } |
|
82 | } | |
77 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END") |
|
83 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyProgress END") | |
78 | << QThread::currentThread() << reply; |
|
84 | << QThread::currentThread() << reply; | |
|
85 | } | |||
79 | }; |
|
86 | }; | |
80 |
|
87 | |||
81 |
|
88 | |||
@@ -113,14 +120,16 void NetworkController::onReplyCanceled(QUuid identifier) | |||||
113 | { |
|
120 | { | |
114 | auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; |
|
121 | auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; | |
115 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled") |
|
122 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled") | |
116 |
|
|
123 | << QThread::currentThread() << identifier; | |
117 |
|
124 | |||
118 |
|
125 | |||
119 | impl->lockRead(); |
|
126 | impl->lockRead(); | |
120 |
auto end = impl->m_NetworkReplyTo |
|
127 | auto end = impl->m_NetworkReplyToId.cend(); | |
121 |
auto it = std::find_if(impl->m_NetworkReplyTo |
|
128 | auto it = std::find_if(impl->m_NetworkReplyToId.cbegin(), end, findReply); | |
122 | impl->unlock(); |
|
129 | impl->unlock(); | |
123 | if (it != end) { |
|
130 | if (it != end) { | |
|
131 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled ABORT DONE") | |||
|
132 | << QThread::currentThread() << identifier; | |||
124 | it->first->abort(); |
|
133 | it->first->abort(); | |
125 | } |
|
134 | } | |
126 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END") |
|
135 | qCDebug(LOG_NetworkController()) << tr("NetworkController onReplyCanceled END") |
General Comments 0
You need to be logged in to leave comments.
Login now