mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 14:08:51 +02:00
Move SendAssetTask to separate file
This commit is contained in:
parent
34f7aa74f3
commit
d66c521200
5 changed files with 125 additions and 82 deletions
|
@ -21,74 +21,16 @@
|
||||||
#include <QRunnable>
|
#include <QRunnable>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
#include <NodeType.h>
|
#include "NetworkLogging.h"
|
||||||
|
#include "NodeType.h"
|
||||||
|
#include "SendAssetTask.h"
|
||||||
|
|
||||||
const QString ASSET_SERVER_LOGGING_TARGET_NAME = "asset-server";
|
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) :
|
AssetServer::AssetServer(NLPacket& packet) :
|
||||||
ThreadedAssignment(packet),
|
ThreadedAssignment(packet),
|
||||||
_taskPool(this) {
|
_taskPool(this)
|
||||||
|
{
|
||||||
|
|
||||||
// Most of the work will be I/O bound, reading from disk and constructing packet objects,
|
// 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.
|
// so the ideal is greater than the number of cores on the system.
|
||||||
|
|
|
@ -40,23 +40,8 @@ private:
|
||||||
QThreadPool _taskPool;
|
QThreadPool _taskPool;
|
||||||
};
|
};
|
||||||
|
|
||||||
class SendAssetTask : public QRunnable {
|
inline void writeError(NLPacketList* packetList, AssetServerError error) {
|
||||||
public:
|
packetList->writePrimitive(error);
|
||||||
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
|
#endif
|
||||||
|
|
73
assignment-client/src/assets/SendAssetTask.cpp
Normal file
73
assignment-client/src/assets/SendAssetTask.cpp
Normal 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);
|
||||||
|
}
|
41
assignment-client/src/assets/SendAssetTask.h
Normal file
41
assignment-client/src/assets/SendAssetTask.h
Normal 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
|
|
@ -11,6 +11,8 @@
|
||||||
#ifndef hifi_AssetUtils_h
|
#ifndef hifi_AssetUtils_h
|
||||||
#define hifi_AssetUtils_h
|
#define hifi_AssetUtils_h
|
||||||
|
|
||||||
|
#include "NLPacketList.h"
|
||||||
|
|
||||||
using MessageID = uint32_t;
|
using MessageID = uint32_t;
|
||||||
using DataOffset = int64_t;
|
using DataOffset = int64_t;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue