mirror of
https://github.com/lubosz/overte.git
synced 2025-04-24 16:43:33 +02:00
Merge branch 'protocol' of github.com:birarda/hifi into atp
This commit is contained in:
commit
0b712eef55
7 changed files with 79 additions and 71 deletions
|
@ -114,7 +114,7 @@ void AssetServer::handleAssetGetInfo(QSharedPointer<NLPacket> packet, SharedNode
|
|||
replyPacket->writePrimitive(AssetServerError::NO_ERROR);
|
||||
replyPacket->writePrimitive(fileInfo.size());
|
||||
} else {
|
||||
qDebug() << "Asset not found: " << assetHash;
|
||||
qDebug() << "Asset not found: " << QString(hexHash);
|
||||
replyPacket->writePrimitive(AssetServerError::ASSET_NOT_FOUND);
|
||||
}
|
||||
|
||||
|
|
|
@ -49,11 +49,12 @@ AssetRequest* AssetClient::createRequest(const QString& hash, const QString& ext
|
|||
auto nodeList = DependencyManager::get<NodeList>();
|
||||
SharedNodePointer assetServer = nodeList->soloNodeOfType(NodeType::AssetServer);
|
||||
|
||||
if (assetServer) {
|
||||
return new AssetRequest(this, hash, extension);
|
||||
if (!assetServer) {
|
||||
qDebug() << "No Asset Server";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
return new AssetRequest(this, hash, extension);
|
||||
}
|
||||
|
||||
AssetUpload* AssetClient::createUpload(const QString& filename) {
|
||||
|
@ -152,7 +153,7 @@ void AssetClient::handleAssetGetInfoReply(QSharedPointer<NLPacket> packet, Share
|
|||
|
||||
if (_pendingInfoRequests.contains(messageID)) {
|
||||
auto callback = _pendingInfoRequests.take(messageID);
|
||||
callback(error == NO_ERROR, info);
|
||||
callback(error, info);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -181,7 +182,7 @@ void AssetClient::handleAssetGetReply(QSharedPointer<NLPacketList> packetList, S
|
|||
|
||||
if (_pendingRequests.contains(messageID)) {
|
||||
auto callback = _pendingRequests.take(messageID);
|
||||
callback(error == NO_ERROR, data);
|
||||
callback(error, data);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -234,6 +235,6 @@ void AssetClient::handleAssetUploadReply(QSharedPointer<NLPacket> packet, Shared
|
|||
|
||||
if (_pendingUploads.contains(messageID)) {
|
||||
auto callback = _pendingUploads.take(messageID);
|
||||
callback(error == NO_ERROR, hashString);
|
||||
callback(error, hashString);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,9 +29,9 @@ struct AssetInfo {
|
|||
int64_t size;
|
||||
};
|
||||
|
||||
using ReceivedAssetCallback = std::function<void(bool result, const QByteArray& data)>;
|
||||
using GetInfoCallback = std::function<void(bool result, AssetInfo info)>;
|
||||
using UploadResultCallback = std::function<void(bool result, const QString& hash)>;
|
||||
using ReceivedAssetCallback = std::function<void(AssetServerError error, const QByteArray& data)>;
|
||||
using GetInfoCallback = std::function<void(AssetServerError error, AssetInfo info)>;
|
||||
using UploadResultCallback = std::function<void(AssetServerError error, const QString& hash)>;
|
||||
|
||||
class AssetClient : public QObject, public Dependency {
|
||||
Q_OBJECT
|
||||
|
|
|
@ -16,9 +16,9 @@
|
|||
#include <QThread>
|
||||
|
||||
#include "AssetClient.h"
|
||||
#include "NetworkLogging.h"
|
||||
#include "NodeList.h"
|
||||
|
||||
|
||||
AssetRequest::AssetRequest(QObject* parent, const QString& hash, const QString& extension) :
|
||||
QObject(parent),
|
||||
_hash(hash),
|
||||
|
@ -33,45 +33,48 @@ void AssetRequest::start() {
|
|||
return;
|
||||
}
|
||||
|
||||
if (_state == NOT_STARTED) {
|
||||
_state = WAITING_FOR_INFO;
|
||||
|
||||
auto assetClient = DependencyManager::get<AssetClient>();
|
||||
assetClient->getAssetInfo(_hash, _extension, [this](bool success, AssetInfo info) {
|
||||
_info = info;
|
||||
_data.resize(info.size);
|
||||
const DataOffset CHUNK_SIZE = 1024000000;
|
||||
|
||||
qDebug() << "Got size of " << _hash << " : " << info.size << " bytes";
|
||||
|
||||
// Round up
|
||||
int numChunks = (info.size + CHUNK_SIZE - 1) / CHUNK_SIZE;
|
||||
auto assetClient = DependencyManager::get<AssetClient>();
|
||||
for (int i = 0; i < numChunks; ++i) {
|
||||
++_numPendingRequests;
|
||||
auto start = i * CHUNK_SIZE;
|
||||
auto end = std::min((i + 1) * CHUNK_SIZE, info.size);
|
||||
|
||||
assetClient->getAsset(_hash, _extension, start, end, [this, start, end](bool success, const QByteArray& data) {
|
||||
Q_ASSERT(data.size() == (end - start));
|
||||
|
||||
if (success) {
|
||||
_result = Success;
|
||||
memcpy((_data.data() + start), data.constData(), end - start);
|
||||
_totalReceived += data.size();
|
||||
emit progress(_totalReceived, _info.size);
|
||||
} else {
|
||||
_result = Error;
|
||||
qDebug() << "Got error retrieving asset";
|
||||
}
|
||||
|
||||
--_numPendingRequests;
|
||||
if (_numPendingRequests == 0) {
|
||||
_state = FINISHED;
|
||||
emit finished(this);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
if (_state != NOT_STARTED) {
|
||||
qCWarning(networking) << "AssetRequest already started.";
|
||||
return;
|
||||
}
|
||||
|
||||
_state = WAITING_FOR_INFO;
|
||||
|
||||
auto assetClient = DependencyManager::get<AssetClient>();
|
||||
assetClient->getAssetInfo(_hash, _extension, [this](AssetServerError error, AssetInfo info) {
|
||||
_info = info;
|
||||
_error = error;
|
||||
|
||||
if (_error != NO_ERROR) {
|
||||
qCDebug(networking) << "Got error retrieving asset info for" << _hash;
|
||||
_state = FINISHED;
|
||||
emit finished(this);
|
||||
return;
|
||||
}
|
||||
|
||||
_state = WAITING_FOR_DATA;
|
||||
_data.resize(info.size);
|
||||
|
||||
qCDebug(networking) << "Got size of " << _hash << " : " << info.size << " bytes";
|
||||
|
||||
int start = 0, end = _info.size;
|
||||
|
||||
auto assetClient = DependencyManager::get<AssetClient>();
|
||||
assetClient->getAsset(_hash, _extension, start, end, [this, start, end](AssetServerError error,
|
||||
const QByteArray& data) {
|
||||
Q_ASSERT(data.size() == (end - start));
|
||||
|
||||
_error = error;
|
||||
if (_error == NO_ERROR) {
|
||||
memcpy(_data.data() + start, data.constData(), data.size());
|
||||
_totalReceived += data.size();
|
||||
emit progress(_totalReceived, _info.size);
|
||||
} else {
|
||||
qCDebug(networking) << "Got error retrieving asset" << _hash;
|
||||
}
|
||||
|
||||
_state = FINISHED;
|
||||
emit finished(this);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -30,26 +30,21 @@ public:
|
|||
FINISHED
|
||||
};
|
||||
|
||||
enum Result {
|
||||
Success = 0,
|
||||
Timeout,
|
||||
NotFound,
|
||||
Error,
|
||||
};
|
||||
|
||||
AssetRequest(QObject* parent, const QString& hash, const QString& extension);
|
||||
|
||||
Q_INVOKABLE void start();
|
||||
|
||||
const QByteArray& getData() { return _data; }
|
||||
const QByteArray& getData() const { return _data; }
|
||||
State getState() const { return _state; }
|
||||
AssetServerError getError() const { return _error; }
|
||||
|
||||
signals:
|
||||
void finished(AssetRequest*);
|
||||
void finished(AssetRequest* thisRequest);
|
||||
void progress(qint64 totalReceived, qint64 total);
|
||||
|
||||
private:
|
||||
State _state = NOT_STARTED;
|
||||
Result _result;
|
||||
AssetServerError _error;
|
||||
AssetInfo _info;
|
||||
uint64_t _totalReceived { 0 };
|
||||
QString _hash;
|
||||
|
|
|
@ -29,16 +29,24 @@ void AssetResourceRequest::doSend() {
|
|||
|
||||
connect(request, &AssetRequest::progress, this, &AssetResourceRequest::progress);
|
||||
QObject::connect(request, &AssetRequest::finished, [this](AssetRequest* req) mutable {
|
||||
if (_state != InProgress) return;
|
||||
_state = Finished;
|
||||
if (true) {
|
||||
_data = req->getData();
|
||||
_result = ResourceRequest::Success;
|
||||
emit finished();
|
||||
} else {
|
||||
_result = ResourceRequest::Error;
|
||||
emit finished();
|
||||
Q_ASSERT(_state == InProgress);
|
||||
Q_ASSERT(req->getState() == AssetRequest::FINISHED);
|
||||
|
||||
switch (req->getError()) {
|
||||
case NO_ERROR:
|
||||
_data = req->getData();
|
||||
_result = Success;
|
||||
break;
|
||||
case ASSET_NOT_FOUND:
|
||||
_result = NotFound;
|
||||
break;
|
||||
default:
|
||||
_result = Error;
|
||||
break;
|
||||
}
|
||||
|
||||
_state = Finished;
|
||||
emit finished();
|
||||
});
|
||||
|
||||
request->start();
|
||||
|
|
|
@ -323,10 +323,11 @@ void Resource::attemptRequest() {
|
|||
}
|
||||
|
||||
void Resource::finishedLoading(bool success) {
|
||||
qDebug() << "Finished loading: " << _url;
|
||||
if (success) {
|
||||
qDebug() << "Finished loading:" << _url;
|
||||
_loaded = true;
|
||||
} else {
|
||||
qDebug() << "Failed to load:" << _url;
|
||||
_failedToLoad = true;
|
||||
}
|
||||
_loadPriorities.clear();
|
||||
|
|
Loading…
Reference in a new issue