From 993217491fccb0465175d8baedbb369d8f2dfce1 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 28 Aug 2015 19:43:37 +0200 Subject: [PATCH 01/11] Retry to load only on timeout --- libraries/networking/src/AssetClient.cpp | 7 ++- .../networking/src/FileResourceRequest.cpp | 16 +++-- .../networking/src/HTTPResourceRequest.cpp | 58 +++++++++---------- .../networking/src/HTTPResourceRequest.h | 3 +- libraries/networking/src/ResourceCache.cpp | 1 - libraries/script-engine/src/BatchLoader.cpp | 5 +- 6 files changed, 44 insertions(+), 46 deletions(-) diff --git a/libraries/networking/src/AssetClient.cpp b/libraries/networking/src/AssetClient.cpp index 4d228fc6bd..4bbc9f740a 100644 --- a/libraries/networking/src/AssetClient.cpp +++ b/libraries/networking/src/AssetClient.cpp @@ -70,11 +70,12 @@ AssetUpload* AssetClient::createUpload(const QString& filename) { auto nodeList = DependencyManager::get(); SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer); - if (assetServer) { - return new AssetUpload(this, filename); + if (!assetServer) { + qDebug() << "No Asset Server"; + return nullptr; } - return nullptr; + return new AssetUpload(this, filename); } bool AssetClient::getAsset(const QString& hash, const QString& extension, DataOffset start, DataOffset end, diff --git a/libraries/networking/src/FileResourceRequest.cpp b/libraries/networking/src/FileResourceRequest.cpp index ffaaa0d193..dd2f6c8478 100644 --- a/libraries/networking/src/FileResourceRequest.cpp +++ b/libraries/networking/src/FileResourceRequest.cpp @@ -18,17 +18,15 @@ void FileResourceRequest::doSend() { QFile file(filename); - _state = Finished; - if (file.exists()) { - if (file.open(QFile::ReadOnly)) { - _data = file.readAll(); - _result = ResourceRequest::Success; - } else { - _result = ResourceRequest::AccessDenied; - } + if (!file.exists()) { + _result = NotFound; + } else if (file.open(QFile::ReadOnly)) { + _data = file.readAll(); + _result = Success; } else { - _result = ResourceRequest::NotFound; + _result = AccessDenied; } + _state = Finished; emit finished(); } diff --git a/libraries/networking/src/HTTPResourceRequest.cpp b/libraries/networking/src/HTTPResourceRequest.cpp index b122369c30..fbfcabb53d 100644 --- a/libraries/networking/src/HTTPResourceRequest.cpp +++ b/libraries/networking/src/HTTPResourceRequest.cpp @@ -54,43 +54,41 @@ void HTTPResourceRequest::onRequestFinished() { Q_ASSERT(_state == InProgress); Q_ASSERT(_reply); - _state = Finished; - - auto error = _reply->error(); - if (error == QNetworkReply::NoError) { - _data = _reply->readAll(); - _loadedFromCache = _reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool(); - _result = ResourceRequest::Success; - emit finished(); - } else if (error == QNetworkReply::TimeoutError) { - _result = ResourceRequest::Timeout; - emit finished(); - } else { - _result = ResourceRequest::Error; - emit finished(); + _sendTimer.stop(); + + switch(_reply->error()) { + case QNetworkReply::NoError: + _data = _reply->readAll(); + _loadedFromCache = _reply->attribute(QNetworkRequest::SourceIsFromCacheAttribute).toBool(); + _result = Success; + break; + case QNetworkReply::TimeoutError: + _result = Timeout; + break; + default: + _result = Error; + break; } - - _reply->deleteLater(); - _reply = nullptr; + + _state = Finished; + emit finished(); } void HTTPResourceRequest::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { - if (_state == InProgress) { - // We've received data, so reset the timer - _sendTimer.start(); - } + Q_ASSERT(_state == InProgress); + + // We've received data, so reset the timer + _sendTimer.start(); emit progress(bytesReceived, bytesTotal); } void HTTPResourceRequest::onTimeout() { - Q_ASSERT(_state != NotStarted); - - if (_state == InProgress) { - qCDebug(networking) << "Timed out loading " << _url; - _reply->abort(); - _state = Finished; - _result = Timeout; - emit finished(); - } + Q_ASSERT(_state == InProgress); + + qCDebug(networking) << "Timed out loading " << _url; + _reply->abort(); + _result = Timeout; + _state = Finished; + emit finished(); } diff --git a/libraries/networking/src/HTTPResourceRequest.h b/libraries/networking/src/HTTPResourceRequest.h index 09c94314d6..f0d3bb82d9 100644 --- a/libraries/networking/src/HTTPResourceRequest.h +++ b/libraries/networking/src/HTTPResourceRequest.h @@ -21,9 +21,8 @@ class HTTPResourceRequest : public ResourceRequest { Q_OBJECT public: - ~HTTPResourceRequest(); - HTTPResourceRequest(QObject* parent, const QUrl& url) : ResourceRequest(parent, url) { } + ~HTTPResourceRequest(); protected: virtual void doSend() override; diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index ea8c6c74c2..01d9d804d2 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -423,7 +423,6 @@ void Resource::handleReplyFinished() { } void Resource::downloadFinished(const QByteArray& data) { - ; } uint qHash(const QPointer& value, uint seed) { diff --git a/libraries/script-engine/src/BatchLoader.cpp b/libraries/script-engine/src/BatchLoader.cpp index dae8c8e739..46ef05e65a 100644 --- a/libraries/script-engine/src/BatchLoader.cpp +++ b/libraries/script-engine/src/BatchLoader.cpp @@ -34,9 +34,11 @@ void BatchLoader::start() { } _started = true; - for (QUrl url : _urls) { + for (const auto& url : _urls) { auto request = ResourceManager::createResourceRequest(this, url); if (!request) { + _data.insert(url, QString()); + qCDebug(scriptengine) << "Could not load" << url; continue; } connect(request, &ResourceRequest::finished, this, [=]() { @@ -44,6 +46,7 @@ void BatchLoader::start() { _data.insert(url, request->getData()); } else { _data.insert(url, QString()); + qCDebug(scriptengine) << "Could not load" << url; } request->deleteLater(); checkFinished(); From a448eb9109c9762ecb8770b9314bc8d3264cd279 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 28 Aug 2015 20:26:51 +0200 Subject: [PATCH 02/11] Fix Asset Upload popup --- libraries/networking/src/AssetUpload.cpp | 29 ++++++++++---------- libraries/networking/src/ResourceManager.cpp | 2 +- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/libraries/networking/src/AssetUpload.cpp b/libraries/networking/src/AssetUpload.cpp index 7f69f83e97..33d1dcc33a 100644 --- a/libraries/networking/src/AssetUpload.cpp +++ b/libraries/networking/src/AssetUpload.cpp @@ -41,21 +41,22 @@ void AssetUpload::start() { // ask the AssetClient to upload the asset and emit the proper signals from the passed callback auto assetClient = DependencyManager::get(); - assetClient->uploadAsset(data, _extension, [this](bool success, const QString& hash){ - if (success) { - // successful upload - emit finished with a point to ourselves and the resulting hash - _result = Success; - - emit finished(this, hash); - } else { - // error during upload - emit finished with an empty hash - // callers can get the error from this object - - // TODO: get the actual error from the callback - _result = PermissionDenied; - - emit finished(this, hash); + assetClient->uploadAsset(data, _extension, [this](AssetServerError error, const QString& hash){ + switch (error) { + case NO_ERROR: + _result = Success; + break; + case ASSET_TOO_LARGE: + _result = TooLarge; + break; + case PERMISSION_DENIED: + _result = PermissionDenied; + break; + default: + _result = ErrorLoadingFile; + break; } + emit finished(this, hash); }); } else { // we couldn't open the file - set the error result diff --git a/libraries/networking/src/ResourceManager.cpp b/libraries/networking/src/ResourceManager.cpp index ded3dfe222..74891487a4 100644 --- a/libraries/networking/src/ResourceManager.cpp +++ b/libraries/networking/src/ResourceManager.cpp @@ -27,7 +27,7 @@ ResourceRequest* ResourceManager::createResourceRequest(QObject* parent, const Q return new AssetResourceRequest(parent, url); } - qDebug() << "Failed to load: " << url.url(); + qDebug() << "Unknow scheme (" << scheme << ") for URL: " << url.url(); return nullptr; } From 729fd965390f844715d0e96096e8734c2be601d4 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 28 Aug 2015 22:08:30 +0200 Subject: [PATCH 03/11] Fix bug where _requestLimit is stuck at 0 --- libraries/networking/src/ResourceCache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 01d9d804d2..18024000b3 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -402,7 +402,7 @@ void Resource::handleReplyFinished() { const int MAX_ATTEMPTS = 8; const int BASE_DELAY_MS = 1000; if (++_attempts < MAX_ATTEMPTS) { - QTimer::singleShot(BASE_DELAY_MS * (int)pow(2.0, _attempts), this, SLOT(attemptRequest())); + QTimer::singleShot(BASE_DELAY_MS * (int)pow(2.0, _attempts), this, &Resource::makeRequest); retry = true; break; } From bcf3fceadcad3d1c55efc5018c00706fbbc4eda3 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Fri, 28 Aug 2015 22:10:54 +0200 Subject: [PATCH 04/11] Typo --- libraries/networking/src/ResourceManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/networking/src/ResourceManager.cpp b/libraries/networking/src/ResourceManager.cpp index 74891487a4..c721419c7e 100644 --- a/libraries/networking/src/ResourceManager.cpp +++ b/libraries/networking/src/ResourceManager.cpp @@ -27,7 +27,7 @@ ResourceRequest* ResourceManager::createResourceRequest(QObject* parent, const Q return new AssetResourceRequest(parent, url); } - qDebug() << "Unknow scheme (" << scheme << ") for URL: " << url.url(); + qDebug() << "Unknown scheme (" << scheme << ") for URL: " << url.url(); return nullptr; } From 15e97978275b9ba4eff141738128ed2dbc8ccab5 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 31 Aug 2015 22:12:42 +0200 Subject: [PATCH 05/11] CR --- libraries/networking/src/AssetClient.cpp | 4 +- .../networking/src/FileResourceRequest.cpp | 15 +++--- .../networking/src/HTTPResourceRequest.cpp | 3 ++ libraries/networking/src/ResourceCache.cpp | 48 +++++++------------ 4 files changed, 30 insertions(+), 40 deletions(-) diff --git a/libraries/networking/src/AssetClient.cpp b/libraries/networking/src/AssetClient.cpp index 4bbc9f740a..68b7c5ad76 100644 --- a/libraries/networking/src/AssetClient.cpp +++ b/libraries/networking/src/AssetClient.cpp @@ -50,7 +50,7 @@ AssetRequest* AssetClient::createRequest(const QString& hash, const QString& ext SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer); if (!assetServer) { - qDebug() << "No Asset Server"; + qDebug().nospace() << "Could not request " << hash << "." << extension << " since you are not currently connected to an asset-server."; return nullptr; } @@ -71,7 +71,7 @@ AssetUpload* AssetClient::createUpload(const QString& filename) { SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer); if (!assetServer) { - qDebug() << "No Asset Server"; + qDebug() << "Could not upload" << filename << "since you are not currently connected to an asset-server."; return nullptr; } diff --git a/libraries/networking/src/FileResourceRequest.cpp b/libraries/networking/src/FileResourceRequest.cpp index dd2f6c8478..9e0320f6b7 100644 --- a/libraries/networking/src/FileResourceRequest.cpp +++ b/libraries/networking/src/FileResourceRequest.cpp @@ -17,14 +17,15 @@ void FileResourceRequest::doSend() { QString filename = _url.toLocalFile(); QFile file(filename); - - if (!file.exists()) { - _result = NotFound; - } else if (file.open(QFile::ReadOnly)) { - _data = file.readAll(); - _result = Success; + if (file.exists()) { + if (file.open(QFile::ReadOnly)) { + _data = file.readAll(); + _result = ResourceRequest::Success; + } else { + _result = ResourceRequest::AccessDenied; + } } else { - _result = AccessDenied; + _result = ResourceRequest::NotFound; } _state = Finished; diff --git a/libraries/networking/src/HTTPResourceRequest.cpp b/libraries/networking/src/HTTPResourceRequest.cpp index fbfcabb53d..f48c7d9132 100644 --- a/libraries/networking/src/HTTPResourceRequest.cpp +++ b/libraries/networking/src/HTTPResourceRequest.cpp @@ -69,6 +69,9 @@ void HTTPResourceRequest::onRequestFinished() { _result = Error; break; } + _reply->disconnect(this); + _reply->deleteLater(); + _reply = nullptr; _state = Finished; emit finished(); diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 18024000b3..1e0e5f745b 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -158,14 +158,12 @@ void ResourceCache::attemptRequest(Resource* resource) { // Disable request limiting for ATP if (resource->getURL().scheme() != URL_SCHEME_ATP) { if (_requestLimit <= 0) { - qDebug() << "REQUEST LIMIT REACHED (" << _requestLimit << "), queueing: " << resource->getURL(); // wait until a slot becomes available sharedItems->_pendingRequests.append(resource); return; } - qDebug() << "-- Decreasing limit for : " << resource->getURL(); - _requestLimit--; + --_requestLimit; } sharedItems->_loadingRequests.append(resource); @@ -176,8 +174,7 @@ void ResourceCache::requestCompleted(Resource* resource) { auto sharedItems = DependencyManager::get(); sharedItems->_loadingRequests.removeOne(resource); if (resource->getURL().scheme() != URL_SCHEME_ATP) { - qDebug() << "++ Increasing limit after finished: " << resource->getURL(); - _requestLimit++; + ++_requestLimit; } // look for the highest priority pending request @@ -367,33 +364,22 @@ void Resource::handleDownloadProgress(uint64_t bytesReceived, uint64_t bytesTota void Resource::handleReplyFinished() { Q_ASSERT(_request); - + + ResourceCache::requestCompleted(this); + auto result = _request->getResult(); if (result == ResourceRequest::Success) { _data = _request->getData(); qDebug() << "Request finished for " << _url << ", " << _activeUrl; - - _request->disconnect(this); - _request->deleteLater(); - _request = nullptr; - - ResourceCache::requestCompleted(this); - + + finishedLoading(false); emit loaded(_data); - downloadFinished(_data); } else { - _request->disconnect(this); - _request->deleteLater(); - _request = nullptr; - if (result == ResourceRequest::Result::Timeout) { qDebug() << "Timed out loading" << _url << "received" << _bytesReceived << "total" << _bytesTotal; - } else { - qDebug() << "Error loading " << _url; } - bool retry = false; switch (result) { case ResourceRequest::Result::Timeout: case ResourceRequest::Result::ServerUnavailable: @@ -402,24 +388,24 @@ void Resource::handleReplyFinished() { const int MAX_ATTEMPTS = 8; const int BASE_DELAY_MS = 1000; if (++_attempts < MAX_ATTEMPTS) { - QTimer::singleShot(BASE_DELAY_MS * (int)pow(2.0, _attempts), this, &Resource::makeRequest); - retry = true; + QTimer::singleShot(BASE_DELAY_MS * (int)pow(2.0, _attempts), this, SLOT(attemptRequest())); break; } // fall through to final failure } - default: + default: { + qDebug() << "Error loading " << _url; + auto error = (result == ResourceRequest::Timeout) ? QNetworkReply::TimeoutError : QNetworkReply::UnknownNetworkError; + emit failed(error); finishedLoading(false); break; - } - - auto error = result == ResourceRequest::Timeout ? QNetworkReply::TimeoutError : QNetworkReply::UnknownNetworkError; - - if (!retry) { - emit failed(error); - ResourceCache::requestCompleted(this); + } } } + + _request->disconnect(this); + _request->deleteLater(); + _request = nullptr; } void Resource::downloadFinished(const QByteArray& data) { From 9a9c703a78f26e2beda1a6d330bf814fd5b644f0 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 31 Aug 2015 22:31:24 +0200 Subject: [PATCH 06/11] Some code cleanup --- libraries/networking/src/ResourceCache.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 1e0e5f745b..47568c22a6 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -372,30 +372,29 @@ void Resource::handleReplyFinished() { _data = _request->getData(); qDebug() << "Request finished for " << _url << ", " << _activeUrl; - finishedLoading(false); + finishedLoading(true); emit loaded(_data); downloadFinished(_data); } else { - if (result == ResourceRequest::Result::Timeout) { - qDebug() << "Timed out loading" << _url << "received" << _bytesReceived << "total" << _bytesTotal; - } - switch (result) { case ResourceRequest::Result::Timeout: + qDebug() << "Timed out loading" << _url << "received" << _bytesReceived << "total" << _bytesTotal; + // Fall through to other cases case ResourceRequest::Result::ServerUnavailable: case ResourceRequest::Result::Error: { // retry with increasing delays const int MAX_ATTEMPTS = 8; const int BASE_DELAY_MS = 1000; if (++_attempts < MAX_ATTEMPTS) { - QTimer::singleShot(BASE_DELAY_MS * (int)pow(2.0, _attempts), this, SLOT(attemptRequest())); + QTimer::singleShot(BASE_DELAY_MS * (int)pow(2.0, _attempts), this, &Resource::attemptRequest); break; } // fall through to final failure } default: { qDebug() << "Error loading " << _url; - auto error = (result == ResourceRequest::Timeout) ? QNetworkReply::TimeoutError : QNetworkReply::UnknownNetworkError; + auto error = (result == ResourceRequest::Timeout) ? QNetworkReply::TimeoutError + : QNetworkReply::UnknownNetworkError; emit failed(error); finishedLoading(false); break; From 8f74935e96d359945cef2a83ab3ec8dcb902453f Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 31 Aug 2015 22:34:16 +0200 Subject: [PATCH 07/11] Don't reload on error --- libraries/networking/src/ResourceCache.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 47568c22a6..cea48e7419 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -380,13 +380,14 @@ void Resource::handleReplyFinished() { case ResourceRequest::Result::Timeout: qDebug() << "Timed out loading" << _url << "received" << _bytesReceived << "total" << _bytesTotal; // Fall through to other cases - case ResourceRequest::Result::ServerUnavailable: - case ResourceRequest::Result::Error: { + case ResourceRequest::Result::ServerUnavailable: { // retry with increasing delays const int MAX_ATTEMPTS = 8; const int BASE_DELAY_MS = 1000; if (++_attempts < MAX_ATTEMPTS) { - QTimer::singleShot(BASE_DELAY_MS * (int)pow(2.0, _attempts), this, &Resource::attemptRequest); + auto waitTime = BASE_DELAY_MS * (int)pow(2.0, _attempts); + qDebug() << "Retrying to load the asset in " << waitTime; + QTimer::singleShot(waitTime, this, &Resource::attemptRequest); break; } // fall through to final failure From dc7d7ef4442bc0a4ec1b14b56d5e10d88da577df Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 31 Aug 2015 15:17:40 -0600 Subject: [PATCH 08/11] make SendAssetTask be consistent with new UploadAssetTask --- assignment-client/src/assets/AssetServer.cpp | 48 ++++++++++--------- .../src/assets/SendAssetTask.cpp | 32 +++++++++---- assignment-client/src/assets/SendAssetTask.h | 18 ++++--- .../src/assets/UploadAssetTask.cpp | 19 -------- 4 files changed, 61 insertions(+), 56 deletions(-) diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index c046b39f66..62f1b877b5 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -125,39 +125,43 @@ void AssetServer::handleAssetGetInfo(QSharedPointer packet, SharedNode } void AssetServer::handleAssetGet(QSharedPointer packet, SharedNodePointer senderNode) { - MessageID messageID; - QByteArray assetHash; - uint8_t extensionLength; - DataOffset start; - DataOffset end; - auto minSize = qint64(sizeof(messageID) + SHA256_HASH_LENGTH + sizeof(extensionLength) + sizeof(start) + sizeof(end)); + auto minSize = qint64(sizeof(MessageID) + SHA256_HASH_LENGTH + sizeof(uint8_t) + sizeof(DataOffset) + sizeof(DataOffset)); + if (packet->getPayloadSize() < minSize) { qDebug() << "ERROR bad file request"; return; } - packet->readPrimitive(&messageID); - assetHash = packet->read(SHA256_HASH_LENGTH); - packet->readPrimitive(&extensionLength); - QByteArray extension = packet->read(extensionLength); - packet->readPrimitive(&start); - packet->readPrimitive(&end); - - QByteArray hexHash = assetHash.toHex(); - - qDebug() << "Received a request for the file (" << messageID << "): " << hexHash << " from " << start << " to " << end; - // Queue task - QString filePath = _resourcesDirectory.filePath(QString(hexHash) + "." + QString(extension)); - auto task = new SendAssetTask(messageID, assetHash, filePath, start, end, senderNode); + auto task = new SendAssetTask(packet, senderNode, _resourcesDirectory); _taskPool.start(task); } void AssetServer::handleAssetUpload(QSharedPointer packetList, SharedNodePointer senderNode) { - qDebug() << "Starting an UploadAssetTask for upload from" << uuidStringWithoutCurlyBraces(senderNode->getUUID()); - auto task = new UploadAssetTask(packetList, senderNode, _resourcesDirectory); - _taskPool.start(task); + if (senderNode->getCanRez()) { + qDebug() << "Starting an UploadAssetTask for upload from" << uuidStringWithoutCurlyBraces(senderNode->getUUID()); + + auto task = new UploadAssetTask(packetList, senderNode, _resourcesDirectory); + _taskPool.start(task); + } else { + // this is a node the domain told us is not allowed to rez entities + // for now this also means it isn't allowed to add assets + // so return a packet with error that indicates that + + auto permissionErrorPacket = NLPacket::create(PacketType::AssetUploadReply, sizeof(MessageID) + sizeof(AssetServerError)); + + MessageID messageID; + packetList->readPrimitive(&messageID); + + // write the message ID and a permission denied error + permissionErrorPacket->writePrimitive(messageID); + permissionErrorPacket->writePrimitive(AssetServerError::PERMISSION_DENIED); + + // send off the packet + auto nodeList = DependencyManager::get(); + nodeList->sendPacket(std::move(permissionErrorPacket), *senderNode); + } } diff --git a/assignment-client/src/assets/SendAssetTask.cpp b/assignment-client/src/assets/SendAssetTask.cpp index 91782b95ad..c4c7bf2503 100644 --- a/assignment-client/src/assets/SendAssetTask.cpp +++ b/assignment-client/src/assets/SendAssetTask.cpp @@ -18,23 +18,35 @@ #include #include #include +#include #include "AssetUtils.h" -SendAssetTask::SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, - const SharedNodePointer& sendToNode) : +SendAssetTask::SendAssetTask(QSharedPointer packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir) : QRunnable(), - _messageID(messageID), - _assetHash(assetHash), - _filePath(filePath), - _start(start), - _end(end), - _sendToNode(sendToNode) + _packet(packet), + _senderNode(sendToNode), + _resourcesDir(resourcesDir) { + } void SendAssetTask::run() { + MessageID messageID; + uint8_t extensionLength; + DataOffset start, end; + + _packet->readPrimitive(&messageID); + QByteArray assetHash = _packet->read(SHA256_HASH_LENGTH); + _packet->readPrimitive(&extensionLength); + QByteArray extension = _packet->read(extensionLength); + _packet->readPrimitive(&start); + _packet->readPrimitive(&end); + QString hexHash = _assetHash.toHex(); + + qDebug() << "Received a request for the file (" << messageID << "): " << hexHash << " from " << start << " to " << end; + qDebug() << "Starting task to send asset: " << hexHash << " for messageID " << _messageID; auto replyPacketList = std::unique_ptr(new NLPacketList(PacketType::AssetGetReply, QByteArray(), true, true)); @@ -45,6 +57,8 @@ void SendAssetTask::run() { if (_end <= _start) { writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); } else { + QString filePath = _resourcesDir.filePath(QString(hexHash) + "." + QString(extension)); + QFile file { _filePath }; if (file.open(QIODevice::ReadOnly)) { @@ -67,5 +81,5 @@ void SendAssetTask::run() { } auto nodeList = DependencyManager::get(); - nodeList->sendPacketList(std::move(replyPacketList), *_sendToNode); + nodeList->sendPacketList(std::move(replyPacketList), *_senderNode); } diff --git a/assignment-client/src/assets/SendAssetTask.h b/assignment-client/src/assets/SendAssetTask.h index 477f42c1a3..1dc6c8dca2 100644 --- a/assignment-client/src/assets/SendAssetTask.h +++ b/assignment-client/src/assets/SendAssetTask.h @@ -12,28 +12,34 @@ #ifndef hifi_SendAssetTask_h #define hifi_SendAssetTask_h -#include -#include -#include +#include +#include +#include +#include #include "AssetUtils.h" #include "AssetServer.h" #include "Node.h" +namespace udt { + class Packet; +} + class SendAssetTask : public QRunnable { public: - SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, - const SharedNodePointer& sendToNode); + SendAssetTask(QSharedPointer packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir); void run(); private: + QSharedPointer _packet; MessageID _messageID; QByteArray _assetHash; QString _filePath; DataOffset _start; DataOffset _end; - SharedNodePointer _sendToNode; + SharedNodePointer _senderNode; + QDir _resourcesDir; }; #endif diff --git a/assignment-client/src/assets/UploadAssetTask.cpp b/assignment-client/src/assets/UploadAssetTask.cpp index 66fdc80e90..d4991f9554 100644 --- a/assignment-client/src/assets/UploadAssetTask.cpp +++ b/assignment-client/src/assets/UploadAssetTask.cpp @@ -37,25 +37,6 @@ void UploadAssetTask::run() { MessageID messageID; buffer.read(reinterpret_cast(&messageID), sizeof(messageID)); - if (!_senderNode->getCanRez()) { - // this is a node the domain told us is not allowed to rez entities - // for now this also means it isn't allowed to add assets - // so return a packet with error that indicates that - - auto permissionErrorPacket = NLPacket::create(PacketType::AssetUploadReply, sizeof(MessageID) + sizeof(AssetServerError)); - - // write the message ID and a permission denied error - permissionErrorPacket->writePrimitive(messageID); - permissionErrorPacket->writePrimitive(AssetServerError::PERMISSION_DENIED); - - // send off the packet - auto nodeList = DependencyManager::get(); - nodeList->sendPacket(std::move(permissionErrorPacket), *_senderNode); - - // return so we're not attempting to handle upload - return; - } - uint8_t extensionLength; buffer.read(reinterpret_cast(&extensionLength), sizeof(extensionLength)); From 3f5394281b24d48e24c5116111039a98443a50d7 Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 31 Aug 2015 23:20:07 +0200 Subject: [PATCH 09/11] Fix HTTPS timeout issue --- libraries/networking/src/AssetResourceRequest.cpp | 1 - libraries/networking/src/HTTPResourceRequest.cpp | 12 +++++++----- libraries/networking/src/ResourceCache.cpp | 8 +++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/libraries/networking/src/AssetResourceRequest.cpp b/libraries/networking/src/AssetResourceRequest.cpp index b98560152a..d27151ac53 100644 --- a/libraries/networking/src/AssetResourceRequest.cpp +++ b/libraries/networking/src/AssetResourceRequest.cpp @@ -68,6 +68,5 @@ void AssetResourceRequest::doSend() { } void AssetResourceRequest::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { - qDebug() << "Got asset data: " << bytesReceived << " / " << bytesTotal; emit progress(bytesReceived, bytesTotal); } diff --git a/libraries/networking/src/HTTPResourceRequest.cpp b/libraries/networking/src/HTTPResourceRequest.cpp index f48c7d9132..fd650df348 100644 --- a/libraries/networking/src/HTTPResourceRequest.cpp +++ b/libraries/networking/src/HTTPResourceRequest.cpp @@ -40,12 +40,12 @@ void HTTPResourceRequest::doSend() { } _reply = networkAccessManager.get(networkRequest); - + connect(_reply, &QNetworkReply::finished, this, &HTTPResourceRequest::onRequestFinished); + connect(_reply, &QNetworkReply::downloadProgress, this, &HTTPResourceRequest::onDownloadProgress); + connect(&_sendTimer, &QTimer::timeout, this, &HTTPResourceRequest::onTimeout); static const int TIMEOUT_MS = 10000; - - connect(&_sendTimer, &QTimer::timeout, this, &HTTPResourceRequest::onTimeout); _sendTimer.setSingleShot(true); _sendTimer.start(TIMEOUT_MS); } @@ -88,9 +88,11 @@ void HTTPResourceRequest::onDownloadProgress(qint64 bytesReceived, qint64 bytesT void HTTPResourceRequest::onTimeout() { Q_ASSERT(_state == InProgress); - - qCDebug(networking) << "Timed out loading " << _url; + _reply->disconnect(this); _reply->abort(); + _reply->deleteLater(); + _reply = nullptr; + _result = Timeout; _state = Finished; emit finished(); diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index cea48e7419..1b1dae5beb 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -377,16 +377,18 @@ void Resource::handleReplyFinished() { downloadFinished(_data); } else { switch (result) { - case ResourceRequest::Result::Timeout: + case ResourceRequest::Result::Timeout: { qDebug() << "Timed out loading" << _url << "received" << _bytesReceived << "total" << _bytesTotal; // Fall through to other cases + } case ResourceRequest::Result::ServerUnavailable: { // retry with increasing delays const int MAX_ATTEMPTS = 8; const int BASE_DELAY_MS = 1000; - if (++_attempts < MAX_ATTEMPTS) { + if (_attempts++ < MAX_ATTEMPTS) { auto waitTime = BASE_DELAY_MS * (int)pow(2.0, _attempts); - qDebug() << "Retrying to load the asset in " << waitTime; + qDebug().nospace() << "Retrying to load the asset in " << waitTime + << ", attempt " << _attempts << " of " << MAX_ATTEMPTS; QTimer::singleShot(waitTime, this, &Resource::attemptRequest); break; } From 0de4c02e8d46bf02222181f2c084df0b2661657f Mon Sep 17 00:00:00 2001 From: Atlante45 Date: Mon, 31 Aug 2015 23:24:14 +0200 Subject: [PATCH 10/11] Add time unit in debug --- libraries/networking/src/ResourceCache.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/networking/src/ResourceCache.cpp b/libraries/networking/src/ResourceCache.cpp index 1b1dae5beb..18135cf6df 100644 --- a/libraries/networking/src/ResourceCache.cpp +++ b/libraries/networking/src/ResourceCache.cpp @@ -388,7 +388,7 @@ void Resource::handleReplyFinished() { if (_attempts++ < MAX_ATTEMPTS) { auto waitTime = BASE_DELAY_MS * (int)pow(2.0, _attempts); qDebug().nospace() << "Retrying to load the asset in " << waitTime - << ", attempt " << _attempts << " of " << MAX_ATTEMPTS; + << "ms, attempt " << _attempts << " of " << MAX_ATTEMPTS; QTimer::singleShot(waitTime, this, &Resource::attemptRequest); break; } From cb903a8d06471c9744ec0ed4076da65de657d0ef Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 31 Aug 2015 15:30:18 -0600 Subject: [PATCH 11/11] fix the new send asset task --- .../src/assets/SendAssetTask.cpp | 24 +++++++++---------- assignment-client/src/assets/SendAssetTask.h | 13 +++------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/assignment-client/src/assets/SendAssetTask.cpp b/assignment-client/src/assets/SendAssetTask.cpp index c4c7bf2503..8131bafaf3 100644 --- a/assignment-client/src/assets/SendAssetTask.cpp +++ b/assignment-client/src/assets/SendAssetTask.cpp @@ -22,7 +22,7 @@ #include "AssetUtils.h" -SendAssetTask::SendAssetTask(QSharedPointer packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir) : +SendAssetTask::SendAssetTask(QSharedPointer packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir) : QRunnable(), _packet(packet), _senderNode(sendToNode), @@ -43,31 +43,31 @@ void SendAssetTask::run() { _packet->readPrimitive(&start); _packet->readPrimitive(&end); - QString hexHash = _assetHash.toHex(); + QString hexHash = assetHash.toHex(); qDebug() << "Received a request for the file (" << messageID << "): " << hexHash << " from " << start << " to " << end; - qDebug() << "Starting task to send asset: " << hexHash << " for messageID " << _messageID; + qDebug() << "Starting task to send asset: " << hexHash << " for messageID " << messageID; auto replyPacketList = std::unique_ptr(new NLPacketList(PacketType::AssetGetReply, QByteArray(), true, true)); - replyPacketList->write(_assetHash); + replyPacketList->write(assetHash); - replyPacketList->writePrimitive(_messageID); + replyPacketList->writePrimitive(messageID); - if (_end <= _start) { + if (end <= start) { writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); } else { QString filePath = _resourcesDir.filePath(QString(hexHash) + "." + QString(extension)); - QFile file { _filePath }; + QFile file { filePath }; if (file.open(QIODevice::ReadOnly)) { - if (file.size() < _end) { + if (file.size() < end) { writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); - qCDebug(networking) << "Bad byte range: " << hexHash << " " << _start << ":" << _end; + qCDebug(networking) << "Bad byte range: " << hexHash << " " << start << ":" << end; } else { - auto size = _end - _start; - file.seek(_start); + auto size = end - start; + file.seek(start); replyPacketList->writePrimitive(AssetServerError::NO_ERROR); replyPacketList->writePrimitive(size); replyPacketList->write(file.read(size)); @@ -75,7 +75,7 @@ void SendAssetTask::run() { } file.close(); } else { - qCDebug(networking) << "Asset not found: " << _filePath << "(" << hexHash << ")"; + qCDebug(networking) << "Asset not found: " << filePath << "(" << hexHash << ")"; writeError(replyPacketList.get(), AssetServerError::ASSET_NOT_FOUND); } } diff --git a/assignment-client/src/assets/SendAssetTask.h b/assignment-client/src/assets/SendAssetTask.h index 1dc6c8dca2..4e1a35b3fe 100644 --- a/assignment-client/src/assets/SendAssetTask.h +++ b/assignment-client/src/assets/SendAssetTask.h @@ -21,23 +21,16 @@ #include "AssetServer.h" #include "Node.h" -namespace udt { - class Packet; -} +class NLPacket; class SendAssetTask : public QRunnable { public: - SendAssetTask(QSharedPointer packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir); + SendAssetTask(QSharedPointer packet, const SharedNodePointer& sendToNode, const QDir& resourcesDir); void run(); private: - QSharedPointer _packet; - MessageID _messageID; - QByteArray _assetHash; - QString _filePath; - DataOffset _start; - DataOffset _end; + QSharedPointer _packet; SharedNodePointer _senderNode; QDir _resourcesDir; };