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..8131bafaf3 100644 --- a/assignment-client/src/assets/SendAssetTask.cpp +++ b/assignment-client/src/assets/SendAssetTask.cpp @@ -18,42 +18,56 @@ #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() { - QString hexHash = _assetHash.toHex(); - qDebug() << "Starting task to send asset: " << hexHash << " for messageID " << _messageID; + 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)); - 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 { - QFile file { _filePath }; + QString filePath = _resourcesDir.filePath(QString(hexHash) + "." + QString(extension)); + + 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)); @@ -61,11 +75,11 @@ 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); } } 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..4e1a35b3fe 100644 --- a/assignment-client/src/assets/SendAssetTask.h +++ b/assignment-client/src/assets/SendAssetTask.h @@ -12,28 +12,27 @@ #ifndef hifi_SendAssetTask_h #define hifi_SendAssetTask_h -#include -#include -#include +#include +#include +#include +#include #include "AssetUtils.h" #include "AssetServer.h" #include "Node.h" +class NLPacket; + 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: - MessageID _messageID; - QByteArray _assetHash; - QString _filePath; - DataOffset _start; - DataOffset _end; - SharedNodePointer _sendToNode; + QSharedPointer _packet; + 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)); diff --git a/libraries/networking/src/AssetClient.cpp b/libraries/networking/src/AssetClient.cpp index 2d89a7259f..65806f3e96 100644 --- a/libraries/networking/src/AssetClient.cpp +++ b/libraries/networking/src/AssetClient.cpp @@ -55,7 +55,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; } @@ -75,11 +75,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() << "Could not upload" << filename << "since you are not currently connected to an 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/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/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/FileResourceRequest.cpp b/libraries/networking/src/FileResourceRequest.cpp index ffaaa0d193..9e0320f6b7 100644 --- a/libraries/networking/src/FileResourceRequest.cpp +++ b/libraries/networking/src/FileResourceRequest.cpp @@ -17,8 +17,6 @@ void FileResourceRequest::doSend() { QString filename = _url.toLocalFile(); QFile file(filename); - - _state = Finished; if (file.exists()) { if (file.open(QFile::ReadOnly)) { _data = file.readAll(); @@ -30,5 +28,6 @@ void FileResourceRequest::doSend() { _result = ResourceRequest::NotFound; } + _state = Finished; emit finished(); } diff --git a/libraries/networking/src/HTTPResourceRequest.cpp b/libraries/networking/src/HTTPResourceRequest.cpp index b122369c30..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); } @@ -54,43 +54,46 @@ 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->disconnect(this); _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); + _reply->disconnect(this); + _reply->abort(); + _reply->deleteLater(); + _reply = nullptr; + + _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..18135cf6df 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,63 +364,53 @@ 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(true); 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: - case ResourceRequest::Result::Error: { + 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) { - QTimer::singleShot(BASE_DELAY_MS * (int)pow(2.0, _attempts), this, SLOT(attemptRequest())); - retry = true; + if (_attempts++ < MAX_ATTEMPTS) { + auto waitTime = BASE_DELAY_MS * (int)pow(2.0, _attempts); + qDebug().nospace() << "Retrying to load the asset in " << waitTime + << "ms, attempt " << _attempts << " of " << MAX_ATTEMPTS; + QTimer::singleShot(waitTime, this, &Resource::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) { - ; } uint qHash(const QPointer& value, uint seed) { diff --git a/libraries/networking/src/ResourceManager.cpp b/libraries/networking/src/ResourceManager.cpp index ded3dfe222..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() << "Failed to load: " << url.url(); + qDebug() << "Unknown scheme (" << scheme << ") for URL: " << url.url(); return nullptr; } 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();