Move SendAssetTask to separate file

This commit is contained in:
Ryan Huffman 2015-08-26 15:37:23 -07:00
parent 34f7aa74f3
commit d66c521200
5 changed files with 125 additions and 82 deletions

View file

@ -21,74 +21,16 @@
#include <QRunnable>
#include <QString>
#include <NodeType.h>
#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<NLPacketList>(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>();
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.

View file

@ -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

View file

@ -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 <QFile>
#include <DependencyManager.h>
#include <NetworkLogging.h>
#include <NLPacket.h>
#include <NLPacketList.h>
#include <NodeList.h>
#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<NLPacketList>(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>();
nodeList->sendPacketList(std::move(replyPacketList), *_sendToNode);
}

View file

@ -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 <QByteArray>
#include <QString>
#include <QRunnable>
#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

View file

@ -11,6 +11,8 @@
#ifndef hifi_AssetUtils_h
#define hifi_AssetUtils_h
#include "NLPacketList.h"
using MessageID = uint32_t;
using DataOffset = int64_t;