diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 9738233c85..31d5a5e0e0 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -117,22 +117,22 @@ void ResourceCache::setUnusedResourceCacheSize(qint64 unusedResourcesMaxSize) { } void ResourceCache::addUnusedResource(const QSharedPointer& resource) { - if (resource->getBytesTotal() > _unusedResourcesMaxSize) { + if (resource->getBytes() > _unusedResourcesMaxSize) { // If it doesn't fit anyway, let's leave whatever is already in the cache. resource->setCache(nullptr); return; } - reserveUnusedResource(resource->getBytesTotal()); + reserveUnusedResource(resource->getBytes()); resource->setLRUKey(++_lastLRUKey); _unusedResources.insert(resource->getLRUKey(), resource); - _unusedResourcesSize += resource->getBytesTotal(); + _unusedResourcesSize += resource->getBytes(); } void ResourceCache::removeUnusedResource(const QSharedPointer& resource) { if (_unusedResources.contains(resource->getLRUKey())) { _unusedResources.remove(resource->getLRUKey()); - _unusedResourcesSize -= resource->getBytesTotal(); + _unusedResourcesSize -= resource->getBytes(); } } @@ -142,7 +142,7 @@ void ResourceCache::reserveUnusedResource(qint64 resourceSize) { // unload the oldest resource QMap >::iterator it = _unusedResources.begin(); - _unusedResourcesSize -= it.value()->getBytesTotal(); + _unusedResourcesSize -= it.value()->getBytes(); it.value()->setCache(nullptr); _unusedResources.erase(it); } @@ -399,7 +399,7 @@ void Resource::makeRequest() { connect(_request, &ResourceRequest::progress, this, &Resource::handleDownloadProgress); connect(_request, &ResourceRequest::finished, this, &Resource::handleReplyFinished); - _bytesReceived = _bytesTotal = 0; + _bytesReceived = _bytesTotal = _bytes = 0; _request->send(); } @@ -412,6 +412,8 @@ void Resource::handleDownloadProgress(uint64_t bytesReceived, uint64_t bytesTota void Resource::handleReplyFinished() { Q_ASSERT_X(_request, "Resource::handleReplyFinished", "Request should not be null while in handleReplyFinished"); + _bytes = _bytesTotal; + if (!_request || _request != sender()) { // This can happen in the edge case that a request is timed out, but a `finished` signal is emitted before it is deleted. qWarning(networking) << "Received signal Resource::handleReplyFinished from ResourceRequest that is not the current" diff --git a/libraries/networking/src/ResourceCache.h b/libraries/networking/src/ResourceCache.h index fc3d8540e6..f674c96a1e 100644 --- a/libraries/networking/src/ResourceCache.h +++ b/libraries/networking/src/ResourceCache.h @@ -181,6 +181,9 @@ public: /// For loading resources, returns the number of total bytes (<= zero if unknown). qint64 getBytesTotal() const { return _bytesTotal; } + /// For loaded resources, returns the number of actual bytes (defaults to total bytes if not explicitly set). + qint64 getBytes() const { return _bytes; } + /// For loading resources, returns the load progress. float getProgress() const { return (_bytesTotal <= 0) ? 0.0f : (float)_bytesReceived / _bytesTotal; } @@ -222,6 +225,9 @@ protected: /// This should be overridden by subclasses that need to process the data once it is downloaded. virtual void downloadFinished(const QByteArray& data) { finishedLoading(true); } + /// Called when the download is finished and processed, sets the number of actual bytes. + void setBytes(qint64 bytes) { _bytes = bytes; } + /// Called when the download is finished and processed. /// This should be called by subclasses that override downloadFinished to mark the end of processing. Q_INVOKABLE void finishedLoading(bool success); @@ -255,6 +261,7 @@ private: QTimer* _replyTimer = nullptr; qint64 _bytesReceived = 0; qint64 _bytesTotal = 0; + qint64 _bytes = 0; int _attempts = 0; };