From d66c5212009270b447fa925b94266f81d4307869 Mon Sep 17 00:00:00 2001 From: Ryan Huffman Date: Wed, 26 Aug 2015 15:37:23 -0700 Subject: [PATCH] Move SendAssetTask to separate file --- assignment-client/src/assets/AssetServer.cpp | 70 ++---------------- assignment-client/src/assets/AssetServer.h | 21 +----- .../src/assets/SendAssetTask.cpp | 73 +++++++++++++++++++ assignment-client/src/assets/SendAssetTask.h | 41 +++++++++++ libraries/networking/src/AssetUtils.h | 2 + 5 files changed, 125 insertions(+), 82 deletions(-) create mode 100644 assignment-client/src/assets/SendAssetTask.cpp create mode 100644 assignment-client/src/assets/SendAssetTask.h diff --git a/assignment-client/src/assets/AssetServer.cpp b/assignment-client/src/assets/AssetServer.cpp index 697b4b21ec..f8f3aa8d4c 100644 --- a/assignment-client/src/assets/AssetServer.cpp +++ b/assignment-client/src/assets/AssetServer.cpp @@ -21,74 +21,16 @@ #include #include -#include +#include "NetworkLogging.h" +#include "NodeType.h" +#include "SendAssetTask.h" const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server"; -void writeError(NLPacketList* packetList, AssetServerError error) { - packetList->writePrimitive(error); -} - -SendAssetTask::SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, - const SharedNodePointer& sendToNode) : - QRunnable(), - _messageID(messageID), - _assetHash(assetHash), - _filePath(filePath), - _start(start), - _end(end), - _sendToNode(sendToNode) -{ -} - -void SendAssetTask::run() { - qDebug() << "Starting task to send asset: " << _assetHash << " for messageID " << _messageID; - auto replyPacketList = std::unique_ptr(new NLPacketList(PacketType::AssetGetReply, QByteArray(), true, true)); - - replyPacketList->write(_assetHash, HASH_HEX_LENGTH); - - replyPacketList->writePrimitive(_messageID); - - const int64_t MAX_LENGTH = 4294967296; - - if (_end <= _start) { - writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); - } else if (_end - _start > MAX_LENGTH) { - writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); - } else { - QFile file { _filePath }; - qDebug() << "Opening file: " << QString(QFileInfo(_assetHash).fileName()); - - if (file.open(QIODevice::ReadOnly)) { - if (file.size() < _end) { - writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); - } else { - auto size = _end - _start; - file.seek(_start); - replyPacketList->writePrimitive(AssetServerError::NO_ERROR); - replyPacketList->writePrimitive(size); - while (file.pos() < file.size()) { - static const int chunkSize = 20000; - QByteArray data = file.read(chunkSize); - replyPacketList->write(data, chunkSize); - } - qDebug() << "Done reading"; - } - file.close(); - } else { - qDebug() << "Asset not found"; - writeError(replyPacketList.get(), AssetServerError::ASSET_NOT_FOUND); - } - } - - qDebug() << "Sending asset"; - auto nodeList = DependencyManager::get(); - nodeList->sendPacketList(std::move(replyPacketList), *_sendToNode); -} - AssetServer::AssetServer(NLPacket& packet) : - ThreadedAssignment(packet), - _taskPool(this) { + ThreadedAssignment(packet), + _taskPool(this) +{ // Most of the work will be I/O bound, reading from disk and constructing packet objects, // so the ideal is greater than the number of cores on the system. diff --git a/assignment-client/src/assets/AssetServer.h b/assignment-client/src/assets/AssetServer.h index 485614d984..7a9cbd36ef 100644 --- a/assignment-client/src/assets/AssetServer.h +++ b/assignment-client/src/assets/AssetServer.h @@ -40,23 +40,8 @@ private: QThreadPool _taskPool; }; -class SendAssetTask : public QRunnable { -public: - SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, - const SharedNodePointer& sendToNode); - - void run(); - -signals: - void finished(); - -private: - MessageID _messageID; - QByteArray _assetHash; - QString _filePath; - DataOffset _start; - DataOffset _end; - SharedNodePointer _sendToNode; -}; +inline void writeError(NLPacketList* packetList, AssetServerError error) { + packetList->writePrimitive(error); +} #endif diff --git a/assignment-client/src/assets/SendAssetTask.cpp b/assignment-client/src/assets/SendAssetTask.cpp new file mode 100644 index 0000000000..559331342f --- /dev/null +++ b/assignment-client/src/assets/SendAssetTask.cpp @@ -0,0 +1,73 @@ +// +// SendAssetTask.cpp +// +// Created by Ryan Huffman on 2015/08/26 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "SendAssetTask.h" + +#include + +#include +#include +#include +#include +#include + +#include "AssetUtils.h" + +SendAssetTask::SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, + const SharedNodePointer& sendToNode) : + QRunnable(), + _messageID(messageID), + _assetHash(assetHash), + _filePath(filePath), + _start(start), + _end(end), + _sendToNode(sendToNode) +{ +} + +void SendAssetTask::run() { + qDebug() << "Starting task to send asset: " << _assetHash << " for messageID " << _messageID; + auto replyPacketList = std::unique_ptr(new NLPacketList(PacketType::AssetGetReply, QByteArray(), true, true)); + + replyPacketList->write(_assetHash, HASH_HEX_LENGTH); + + replyPacketList->writePrimitive(_messageID); + + const int64_t MAX_LENGTH = 4294967296; + + if (_end <= _start) { + writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); + } else if (_end - _start > MAX_LENGTH) { + writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); + } else { + QFile file { _filePath }; + + if (file.open(QIODevice::ReadOnly)) { + if (file.size() < _end) { + writeError(replyPacketList.get(), AssetServerError::INVALID_BYTE_RANGE); + qCDebug(networking) << "Bad byte range: " << _assetHash << " " << _start << ":" << _end; + } else { + auto size = _end - _start; + file.seek(_start); + replyPacketList->writePrimitive(AssetServerError::NO_ERROR); + replyPacketList->writePrimitive(size); + replyPacketList->write(file.read(size)); + } + file.close(); + qCDebug(networking) << "Sending asset: " << _assetHash; + } else { + qCDebug(networking) << "Asset not found: " << _assetHash; + writeError(replyPacketList.get(), AssetServerError::ASSET_NOT_FOUND); + } + } + + auto nodeList = DependencyManager::get(); + nodeList->sendPacketList(std::move(replyPacketList), *_sendToNode); +} diff --git a/assignment-client/src/assets/SendAssetTask.h b/assignment-client/src/assets/SendAssetTask.h new file mode 100644 index 0000000000..7dd3616f83 --- /dev/null +++ b/assignment-client/src/assets/SendAssetTask.h @@ -0,0 +1,41 @@ +// +// SendAssetTask.h +// +// Created by Ryan Huffman on 2015/08/26 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_SendAssetTask_h +#define hifi_SendAssetTask_h + +#include +#include +#include + +#include "AssetUtils.h" +#include "AssetServer.h" +#include "Node.h" + +class SendAssetTask : public QRunnable { +public: + SendAssetTask(MessageID messageID, const QByteArray& assetHash, QString filePath, DataOffset start, DataOffset end, + const SharedNodePointer& sendToNode); + + void run(); + +signals: + void finished(); + +private: + MessageID _messageID; + QByteArray _assetHash; + QString _filePath; + DataOffset _start; + DataOffset _end; + SharedNodePointer _sendToNode; +}; + +#endif diff --git a/libraries/networking/src/AssetUtils.h b/libraries/networking/src/AssetUtils.h index 904fa71da2..c23b54de5d 100644 --- a/libraries/networking/src/AssetUtils.h +++ b/libraries/networking/src/AssetUtils.h @@ -11,6 +11,8 @@ #ifndef hifi_AssetUtils_h #define hifi_AssetUtils_h +#include "NLPacketList.h" + using MessageID = uint32_t; using DataOffset = int64_t;