mirror of
https://github.com/overte-org/overte.git
synced 2025-08-10 11:13:09 +02:00
Factored out bits common to resource caches, added global limit on number of
resources being requested at any one time.
This commit is contained in:
parent
e91d47174b
commit
29f7954d20
10 changed files with 390 additions and 297 deletions
|
@ -58,6 +58,7 @@
|
||||||
#include <PacketHeaders.h>
|
#include <PacketHeaders.h>
|
||||||
#include <ParticlesScriptingInterface.h>
|
#include <ParticlesScriptingInterface.h>
|
||||||
#include <PerfStat.h>
|
#include <PerfStat.h>
|
||||||
|
#include <ResourceCache.h>
|
||||||
#include <UUID.h>
|
#include <UUID.h>
|
||||||
#include <VoxelSceneStats.h>
|
#include <VoxelSceneStats.h>
|
||||||
|
|
||||||
|
@ -275,6 +276,8 @@ Application::Application(int& argc, char** argv, timeval &startup_time) :
|
||||||
cache->setCacheDirectory(!cachePath.isEmpty() ? cachePath : "interfaceCache");
|
cache->setCacheDirectory(!cachePath.isEmpty() ? cachePath : "interfaceCache");
|
||||||
_networkAccessManager->setCache(cache);
|
_networkAccessManager->setCache(cache);
|
||||||
|
|
||||||
|
ResourceCache::setNetworkAccessManager(_networkAccessManager);
|
||||||
|
|
||||||
_window->setCentralWidget(_glWidget);
|
_window->setCentralWidget(_glWidget);
|
||||||
|
|
||||||
restoreSizeAndPosition();
|
restoreSizeAndPosition();
|
||||||
|
|
|
@ -35,9 +35,6 @@ void MetavoxelSystem::init() {
|
||||||
_program.link();
|
_program.link();
|
||||||
|
|
||||||
_pointScaleLocation = _program.uniformLocation("pointScale");
|
_pointScaleLocation = _program.uniformLocation("pointScale");
|
||||||
|
|
||||||
// let the script cache know to use our common access manager
|
|
||||||
ScriptCache::getInstance()->setNetworkAccessManager(Application::getInstance()->getNetworkAccessManager());
|
|
||||||
}
|
}
|
||||||
_buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
|
_buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
|
||||||
_buffer.create();
|
_buffer.create();
|
||||||
|
|
|
@ -8,7 +8,6 @@
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QTimer>
|
|
||||||
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
#include "GeometryCache.h"
|
#include "GeometryCache.h"
|
||||||
|
@ -287,53 +286,25 @@ void GeometryCache::renderGrid(int xDivisions, int yDivisions) {
|
||||||
}
|
}
|
||||||
|
|
||||||
QSharedPointer<NetworkGeometry> GeometryCache::getGeometry(const QUrl& url, const QUrl& fallback, bool delayLoad) {
|
QSharedPointer<NetworkGeometry> GeometryCache::getGeometry(const QUrl& url, const QUrl& fallback, bool delayLoad) {
|
||||||
if (!url.isValid() && fallback.isValid()) {
|
return getResource(url, fallback, delayLoad).staticCast<NetworkGeometry>();
|
||||||
return getGeometry(fallback, QUrl(), delayLoad);
|
}
|
||||||
}
|
|
||||||
QSharedPointer<NetworkGeometry> geometry = _networkGeometry.value(url);
|
QSharedPointer<Resource> GeometryCache::createResource(const QUrl& url,
|
||||||
if (geometry.isNull()) {
|
const QSharedPointer<Resource>& fallback, bool delayLoad, void* extra) {
|
||||||
geometry = QSharedPointer<NetworkGeometry>(new NetworkGeometry(url, fallback.isValid() ?
|
|
||||||
getGeometry(fallback, QUrl(), true) : QSharedPointer<NetworkGeometry>(), delayLoad));
|
QSharedPointer<NetworkGeometry> geometry(new NetworkGeometry(url, fallback.staticCast<NetworkGeometry>(), delayLoad));
|
||||||
geometry->setLODParent(geometry);
|
geometry->setLODParent(geometry);
|
||||||
_networkGeometry.insert(url, geometry);
|
return geometry.staticCast<Resource>();
|
||||||
}
|
|
||||||
return geometry;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const float NetworkGeometry::NO_HYSTERESIS = -1.0f;
|
const float NetworkGeometry::NO_HYSTERESIS = -1.0f;
|
||||||
|
|
||||||
NetworkGeometry::NetworkGeometry(const QUrl& url, const QSharedPointer<NetworkGeometry>& fallback, bool delayLoad,
|
NetworkGeometry::NetworkGeometry(const QUrl& url, const QSharedPointer<NetworkGeometry>& fallback, bool delayLoad,
|
||||||
const QVariantHash& mapping, const QUrl& textureBase) :
|
const QVariantHash& mapping, const QUrl& textureBase) :
|
||||||
_request(url),
|
Resource(url, delayLoad),
|
||||||
_reply(NULL),
|
|
||||||
_mapping(mapping),
|
_mapping(mapping),
|
||||||
_textureBase(textureBase.isValid() ? textureBase : url),
|
_textureBase(textureBase.isValid() ? textureBase : url),
|
||||||
_fallback(fallback),
|
_fallback(fallback) {
|
||||||
_startedLoading(false),
|
|
||||||
_failedToLoad(false),
|
|
||||||
_attempts(0) {
|
|
||||||
|
|
||||||
if (!url.isValid()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
|
|
||||||
|
|
||||||
// start loading immediately unless instructed otherwise
|
|
||||||
if (!delayLoad) {
|
|
||||||
makeRequest();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
NetworkGeometry::~NetworkGeometry() {
|
|
||||||
if (_reply != NULL) {
|
|
||||||
delete _reply;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkGeometry::ensureLoading() {
|
|
||||||
if (!_startedLoading) {
|
|
||||||
makeRequest();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QSharedPointer<NetworkGeometry> NetworkGeometry::getLODOrFallback(float distance, float& hysteresis, bool delayLoad) const {
|
QSharedPointer<NetworkGeometry> NetworkGeometry::getLODOrFallback(float distance, float& hysteresis, bool delayLoad) const {
|
||||||
|
@ -406,24 +377,9 @@ glm::vec4 NetworkGeometry::computeAverageColor() const {
|
||||||
return (totalTriangles == 0) ? glm::vec4(1.0f, 1.0f, 1.0f, 1.0f) : totalColor / totalTriangles;
|
return (totalTriangles == 0) ? glm::vec4(1.0f, 1.0f, 1.0f, 1.0f) : totalColor / totalTriangles;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkGeometry::makeRequest() {
|
void NetworkGeometry::downloadFinished(QNetworkReply* reply) {
|
||||||
_startedLoading = true;
|
QUrl url = reply->url();
|
||||||
_reply = Application::getInstance()->getNetworkAccessManager()->get(_request);
|
QByteArray data = reply->readAll();
|
||||||
|
|
||||||
connect(_reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(handleDownloadProgress(qint64,qint64)));
|
|
||||||
connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(handleReplyError()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkGeometry::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
|
||||||
if (!_reply->isFinished()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QUrl url = _reply->url();
|
|
||||||
QByteArray data = _reply->readAll();
|
|
||||||
_reply->disconnect(this);
|
|
||||||
_reply->deleteLater();
|
|
||||||
_reply = NULL;
|
|
||||||
|
|
||||||
if (url.path().toLower().endsWith(".fst")) {
|
if (url.path().toLower().endsWith(".fst")) {
|
||||||
// it's a mapping file; parse it and get the mesh filename
|
// it's a mapping file; parse it and get the mesh filename
|
||||||
|
@ -453,7 +409,7 @@ void NetworkGeometry::handleDownloadProgress(qint64 bytesReceived, qint64 bytesT
|
||||||
// make the request immediately only if we have no LODs to switch between
|
// make the request immediately only if we have no LODs to switch between
|
||||||
_startedLoading = false;
|
_startedLoading = false;
|
||||||
if (_lods.isEmpty()) {
|
if (_lods.isEmpty()) {
|
||||||
makeRequest();
|
attemptRequest();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -560,42 +516,6 @@ void NetworkGeometry::handleDownloadProgress(qint64 bytesReceived, qint64 bytesT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkGeometry::handleReplyError() {
|
|
||||||
QDebug debug = qDebug() << _reply->errorString();
|
|
||||||
|
|
||||||
QNetworkReply::NetworkError error = _reply->error();
|
|
||||||
_reply->disconnect(this);
|
|
||||||
_reply->deleteLater();
|
|
||||||
_reply = NULL;
|
|
||||||
|
|
||||||
// retry for certain types of failures
|
|
||||||
switch (error) {
|
|
||||||
case QNetworkReply::RemoteHostClosedError:
|
|
||||||
case QNetworkReply::TimeoutError:
|
|
||||||
case QNetworkReply::TemporaryNetworkFailureError:
|
|
||||||
case QNetworkReply::ProxyConnectionClosedError:
|
|
||||||
case QNetworkReply::ProxyTimeoutError:
|
|
||||||
case QNetworkReply::UnknownNetworkError:
|
|
||||||
case QNetworkReply::UnknownProxyError:
|
|
||||||
case QNetworkReply::UnknownContentError:
|
|
||||||
case QNetworkReply::ProtocolFailure: {
|
|
||||||
// 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(makeRequest()));
|
|
||||||
debug << " -- retrying...";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// fall through to final failure
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
_failedToLoad = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
bool NetworkMeshPart::isTranslucent() const {
|
bool NetworkMeshPart::isTranslucent() const {
|
||||||
return diffuseTexture && diffuseTexture->isTranslucent();
|
return diffuseTexture && diffuseTexture->isTranslucent();
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,24 +12,19 @@
|
||||||
// include this before QOpenGLBuffer, which includes an earlier version of OpenGL
|
// include this before QOpenGLBuffer, which includes an earlier version of OpenGL
|
||||||
#include "InterfaceConfig.h"
|
#include "InterfaceConfig.h"
|
||||||
|
|
||||||
#include <QHash>
|
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QNetworkRequest>
|
|
||||||
#include <QObject>
|
|
||||||
#include <QOpenGLBuffer>
|
#include <QOpenGLBuffer>
|
||||||
#include <QSharedPointer>
|
|
||||||
#include <QWeakPointer>
|
#include <ResourceCache.h>
|
||||||
|
|
||||||
#include "FBXReader.h"
|
#include "FBXReader.h"
|
||||||
|
|
||||||
class QNetworkReply;
|
|
||||||
|
|
||||||
class NetworkGeometry;
|
class NetworkGeometry;
|
||||||
class NetworkMesh;
|
class NetworkMesh;
|
||||||
class NetworkTexture;
|
class NetworkTexture;
|
||||||
|
|
||||||
/// Stores cached geometry.
|
/// Stores cached geometry.
|
||||||
class GeometryCache {
|
class GeometryCache : public ResourceCache {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
~GeometryCache();
|
~GeometryCache();
|
||||||
|
@ -44,6 +39,11 @@ public:
|
||||||
/// \param delayLoad if true, don't load the geometry immediately; wait until load is first requested
|
/// \param delayLoad if true, don't load the geometry immediately; wait until load is first requested
|
||||||
QSharedPointer<NetworkGeometry> getGeometry(const QUrl& url, const QUrl& fallback = QUrl(), bool delayLoad = false);
|
QSharedPointer<NetworkGeometry> getGeometry(const QUrl& url, const QUrl& fallback = QUrl(), bool delayLoad = false);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual QSharedPointer<Resource> createResource(const QUrl& url,
|
||||||
|
const QSharedPointer<Resource>& fallback, bool delayLoad, void* extra);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
typedef QPair<int, int> IntPair;
|
typedef QPair<int, int> IntPair;
|
||||||
|
@ -58,7 +58,7 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Geometry loaded from the network.
|
/// Geometry loaded from the network.
|
||||||
class NetworkGeometry : public QObject {
|
class NetworkGeometry : public Resource {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -68,14 +68,10 @@ public:
|
||||||
|
|
||||||
NetworkGeometry(const QUrl& url, const QSharedPointer<NetworkGeometry>& fallback, bool delayLoad,
|
NetworkGeometry(const QUrl& url, const QSharedPointer<NetworkGeometry>& fallback, bool delayLoad,
|
||||||
const QVariantHash& mapping = QVariantHash(), const QUrl& textureBase = QUrl());
|
const QVariantHash& mapping = QVariantHash(), const QUrl& textureBase = QUrl());
|
||||||
~NetworkGeometry();
|
|
||||||
|
|
||||||
/// Checks whether the geometry is fulled loaded.
|
/// Checks whether the geometry is fulled loaded.
|
||||||
bool isLoaded() const { return !_geometry.joints.isEmpty(); }
|
bool isLoaded() const { return !_geometry.joints.isEmpty(); }
|
||||||
|
|
||||||
/// Makes sure that the geometry has started loading.
|
|
||||||
void ensureLoading();
|
|
||||||
|
|
||||||
/// Returns a pointer to the geometry appropriate for the specified distance.
|
/// Returns a pointer to the geometry appropriate for the specified distance.
|
||||||
/// \param hysteresis a hysteresis parameter that prevents rapid model switching
|
/// \param hysteresis a hysteresis parameter that prevents rapid model switching
|
||||||
QSharedPointer<NetworkGeometry> getLODOrFallback(float distance, float& hysteresis, bool delayLoad = false) const;
|
QSharedPointer<NetworkGeometry> getLODOrFallback(float distance, float& hysteresis, bool delayLoad = false) const;
|
||||||
|
@ -86,11 +82,9 @@ public:
|
||||||
/// Returns the average color of all meshes in the geometry.
|
/// Returns the average color of all meshes in the geometry.
|
||||||
glm::vec4 computeAverageColor() const;
|
glm::vec4 computeAverageColor() const;
|
||||||
|
|
||||||
private slots:
|
protected:
|
||||||
|
|
||||||
void makeRequest();
|
virtual void downloadFinished(QNetworkReply* reply);
|
||||||
void handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
|
||||||
void handleReplyError();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
@ -98,15 +92,10 @@ private:
|
||||||
|
|
||||||
void setLODParent(const QWeakPointer<NetworkGeometry>& lodParent) { _lodParent = lodParent; }
|
void setLODParent(const QWeakPointer<NetworkGeometry>& lodParent) { _lodParent = lodParent; }
|
||||||
|
|
||||||
QNetworkRequest _request;
|
|
||||||
QNetworkReply* _reply;
|
|
||||||
QVariantHash _mapping;
|
QVariantHash _mapping;
|
||||||
QUrl _textureBase;
|
QUrl _textureBase;
|
||||||
QSharedPointer<NetworkGeometry> _fallback;
|
QSharedPointer<NetworkGeometry> _fallback;
|
||||||
bool _startedLoading;
|
|
||||||
bool _failedToLoad;
|
|
||||||
|
|
||||||
int _attempts;
|
|
||||||
QMap<float, QSharedPointer<NetworkGeometry> > _lods;
|
QMap<float, QSharedPointer<NetworkGeometry> > _lods;
|
||||||
FBXGeometry _geometry;
|
FBXGeometry _geometry;
|
||||||
QVector<NetworkMesh> _meshes;
|
QVector<NetworkMesh> _meshes;
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
#include <QGLWidget>
|
#include <QGLWidget>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QOpenGLFramebufferObject>
|
#include <QOpenGLFramebufferObject>
|
||||||
#include <QTimer>
|
|
||||||
|
|
||||||
#include <glm/gtc/random.hpp>
|
#include <glm/gtc/random.hpp>
|
||||||
|
|
||||||
|
@ -123,22 +122,15 @@ GLuint TextureCache::getFileTextureID(const QString& filename) {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TextureExtra {
|
||||||
|
public:
|
||||||
|
bool normalMap;
|
||||||
|
bool dilatable;
|
||||||
|
};
|
||||||
|
|
||||||
QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool normalMap, bool dilatable) {
|
QSharedPointer<NetworkTexture> TextureCache::getTexture(const QUrl& url, bool normalMap, bool dilatable) {
|
||||||
QSharedPointer<NetworkTexture> texture;
|
TextureExtra extra = { normalMap, dilatable };
|
||||||
if (dilatable) {
|
return getResource(url, QUrl(), false, &extra).staticCast<NetworkTexture>();
|
||||||
texture = _dilatableNetworkTextures.value(url);
|
|
||||||
if (texture.isNull()) {
|
|
||||||
texture = QSharedPointer<NetworkTexture>(new DilatableNetworkTexture(url, normalMap));
|
|
||||||
_dilatableNetworkTextures.insert(url, texture);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
texture = _networkTextures.value(url);
|
|
||||||
if (texture.isNull()) {
|
|
||||||
texture = QSharedPointer<NetworkTexture>(new NetworkTexture(url, normalMap));
|
|
||||||
_networkTextures.insert(url, texture);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return texture;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() {
|
QOpenGLFramebufferObject* TextureCache::getPrimaryFramebufferObject() {
|
||||||
|
@ -233,6 +225,13 @@ bool TextureCache::eventFilter(QObject* watched, QEvent* event) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QSharedPointer<Resource> TextureCache::createResource(const QUrl& url,
|
||||||
|
const QSharedPointer<Resource>& fallback, bool delayLoad, void* extra) {
|
||||||
|
TextureExtra* textureExtra = static_cast<TextureExtra*>(extra);
|
||||||
|
return QSharedPointer<Resource>(textureExtra->dilatable ? new DilatableNetworkTexture(url, textureExtra->normalMap) :
|
||||||
|
new NetworkTexture(url, textureExtra->normalMap));
|
||||||
|
}
|
||||||
|
|
||||||
QOpenGLFramebufferObject* TextureCache::createFramebufferObject() {
|
QOpenGLFramebufferObject* TextureCache::createFramebufferObject() {
|
||||||
QOpenGLFramebufferObject* fbo = new QOpenGLFramebufferObject(Application::getInstance()->getGLWidget()->size());
|
QOpenGLFramebufferObject* fbo = new QOpenGLFramebufferObject(Application::getInstance()->getGLWidget()->size());
|
||||||
Application::getInstance()->getGLWidget()->installEventFilter(this);
|
Application::getInstance()->getGLWidget()->installEventFilter(this);
|
||||||
|
@ -254,9 +253,7 @@ Texture::~Texture() {
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkTexture::NetworkTexture(const QUrl& url, bool normalMap) :
|
NetworkTexture::NetworkTexture(const QUrl& url, bool normalMap) :
|
||||||
_request(url),
|
Resource(url),
|
||||||
_reply(NULL),
|
|
||||||
_attempts(0),
|
|
||||||
_averageColor(1.0f, 1.0f, 1.0f, 1.0f),
|
_averageColor(1.0f, 1.0f, 1.0f, 1.0f),
|
||||||
_translucent(false),
|
_translucent(false),
|
||||||
_loaded(false) {
|
_loaded(false) {
|
||||||
|
@ -265,8 +262,6 @@ NetworkTexture::NetworkTexture(const QUrl& url, bool normalMap) :
|
||||||
_loaded = true;
|
_loaded = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
|
|
||||||
makeRequest();
|
|
||||||
|
|
||||||
// default to white/blue
|
// default to white/blue
|
||||||
glBindTexture(GL_TEXTURE_2D, getID());
|
glBindTexture(GL_TEXTURE_2D, getID());
|
||||||
|
@ -274,35 +269,13 @@ NetworkTexture::NetworkTexture(const QUrl& url, bool normalMap) :
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkTexture::~NetworkTexture() {
|
void NetworkTexture::downloadFinished(QNetworkReply* reply) {
|
||||||
if (_reply != NULL) {
|
|
||||||
delete _reply;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkTexture::imageLoaded(const QImage& image) {
|
|
||||||
// nothing by default
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkTexture::makeRequest() {
|
|
||||||
_reply = Application::getInstance()->getNetworkAccessManager()->get(_request);
|
|
||||||
|
|
||||||
connect(_reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(handleDownloadProgress(qint64,qint64)));
|
|
||||||
connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(handleReplyError()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
|
||||||
if (bytesReceived < bytesTotal && !_reply->isFinished()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QByteArray entirety = _reply->readAll();
|
|
||||||
_reply->disconnect(this);
|
|
||||||
_reply->deleteLater();
|
|
||||||
_reply = NULL;
|
|
||||||
_loaded = true;
|
_loaded = true;
|
||||||
|
|
||||||
QImage image = QImage::fromData(entirety).convertToFormat(QImage::Format_ARGB32);
|
QImage image = QImage::fromData(reply->readAll());
|
||||||
|
if (image.format() != QImage::Format_ARGB32) {
|
||||||
|
image = image.convertToFormat(QImage::Format_ARGB32);
|
||||||
|
}
|
||||||
|
|
||||||
// sum up the colors for the average and check for translucency
|
// sum up the colors for the average and check for translucency
|
||||||
glm::vec4 accumulated;
|
glm::vec4 accumulated;
|
||||||
|
@ -334,23 +307,8 @@ void NetworkTexture::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTo
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
glBindTexture(GL_TEXTURE_2D, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkTexture::handleReplyError() {
|
void NetworkTexture::imageLoaded(const QImage& image) {
|
||||||
QDebug debug = qDebug() << _reply->errorString();
|
// nothing by default
|
||||||
|
|
||||||
_reply->disconnect(this);
|
|
||||||
_reply->deleteLater();
|
|
||||||
_reply = NULL;
|
|
||||||
|
|
||||||
// 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(makeRequest()));
|
|
||||||
debug << " -- retrying...";
|
|
||||||
|
|
||||||
} else {
|
|
||||||
_loaded = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DilatableNetworkTexture::DilatableNetworkTexture(const QUrl& url, bool normalMap) :
|
DilatableNetworkTexture::DilatableNetworkTexture(const QUrl& url, bool normalMap) :
|
||||||
|
|
|
@ -9,23 +9,19 @@
|
||||||
#ifndef __interface__TextureCache__
|
#ifndef __interface__TextureCache__
|
||||||
#define __interface__TextureCache__
|
#define __interface__TextureCache__
|
||||||
|
|
||||||
#include <QHash>
|
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QNetworkRequest>
|
|
||||||
#include <QObject>
|
#include <ResourceCache.h>
|
||||||
#include <QSharedPointer>
|
|
||||||
#include <QWeakPointer>
|
|
||||||
|
|
||||||
#include "InterfaceConfig.h"
|
#include "InterfaceConfig.h"
|
||||||
|
|
||||||
class QNetworkReply;
|
|
||||||
class QOpenGLFramebufferObject;
|
class QOpenGLFramebufferObject;
|
||||||
|
|
||||||
class NetworkTexture;
|
class NetworkTexture;
|
||||||
|
|
||||||
/// Stores cached textures, including render-to-texture targets.
|
/// Stores cached textures, including render-to-texture targets.
|
||||||
class TextureCache : public QObject {
|
class TextureCache : public ResourceCache {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -73,6 +69,11 @@ public:
|
||||||
|
|
||||||
virtual bool eventFilter(QObject* watched, QEvent* event);
|
virtual bool eventFilter(QObject* watched, QEvent* event);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual QSharedPointer<Resource> createResource(const QUrl& url,
|
||||||
|
const QSharedPointer<Resource>& fallback, bool delayLoad, void* extra);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
QOpenGLFramebufferObject* createFramebufferObject();
|
QOpenGLFramebufferObject* createFramebufferObject();
|
||||||
|
@ -83,7 +84,6 @@ private:
|
||||||
|
|
||||||
QHash<QString, GLuint> _fileTextureIDs;
|
QHash<QString, GLuint> _fileTextureIDs;
|
||||||
|
|
||||||
QHash<QUrl, QWeakPointer<NetworkTexture> > _networkTextures;
|
|
||||||
QHash<QUrl, QWeakPointer<NetworkTexture> > _dilatableNetworkTextures;
|
QHash<QUrl, QWeakPointer<NetworkTexture> > _dilatableNetworkTextures;
|
||||||
|
|
||||||
GLuint _primaryDepthTextureID;
|
GLuint _primaryDepthTextureID;
|
||||||
|
@ -110,13 +110,12 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A texture loaded from the network.
|
/// A texture loaded from the network.
|
||||||
class NetworkTexture : public QObject, public Texture {
|
class NetworkTexture : public Resource, public Texture {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
NetworkTexture(const QUrl& url, bool normalMap);
|
NetworkTexture(const QUrl& url, bool normalMap);
|
||||||
~NetworkTexture();
|
|
||||||
|
|
||||||
bool isLoaded() const { return _loaded; }
|
bool isLoaded() const { return _loaded; }
|
||||||
|
|
||||||
|
@ -129,19 +128,11 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
|
virtual void downloadFinished(QNetworkReply* reply);
|
||||||
virtual void imageLoaded(const QImage& image);
|
virtual void imageLoaded(const QImage& image);
|
||||||
|
|
||||||
private slots:
|
|
||||||
|
|
||||||
void makeRequest();
|
|
||||||
void handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
|
||||||
void handleReplyError();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
QNetworkRequest _request;
|
|
||||||
QNetworkReply* _reply;
|
|
||||||
int _attempts;
|
|
||||||
glm::vec4 _averageColor;
|
glm::vec4 _averageColor;
|
||||||
bool _translucent;
|
bool _translucent;
|
||||||
bool _loaded;
|
bool _loaded;
|
||||||
|
|
|
@ -11,8 +11,6 @@
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
#include <QScriptEngine>
|
#include <QScriptEngine>
|
||||||
#include <QTextStream>
|
#include <QTextStream>
|
||||||
#include <QTimer>
|
|
||||||
#include <QtDebug>
|
|
||||||
|
|
||||||
#include "AttributeRegistry.h"
|
#include "AttributeRegistry.h"
|
||||||
#include "ScriptCache.h"
|
#include "ScriptCache.h"
|
||||||
|
@ -23,7 +21,6 @@ ScriptCache* ScriptCache::getInstance() {
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptCache::ScriptCache() :
|
ScriptCache::ScriptCache() :
|
||||||
_networkAccessManager(NULL),
|
|
||||||
_engine(NULL) {
|
_engine(NULL) {
|
||||||
|
|
||||||
setEngine(new QScriptEngine(this));
|
setEngine(new QScriptEngine(this));
|
||||||
|
@ -41,15 +38,6 @@ void ScriptCache::setEngine(QScriptEngine* engine) {
|
||||||
_generatorString = engine->toStringHandle("generator");
|
_generatorString = engine->toStringHandle("generator");
|
||||||
}
|
}
|
||||||
|
|
||||||
QSharedPointer<NetworkProgram> ScriptCache::getProgram(const QUrl& url) {
|
|
||||||
QSharedPointer<NetworkProgram> program = _networkPrograms.value(url);
|
|
||||||
if (program.isNull()) {
|
|
||||||
program = QSharedPointer<NetworkProgram>(new NetworkProgram(this, url));
|
|
||||||
_networkPrograms.insert(url, program);
|
|
||||||
}
|
|
||||||
return program;
|
|
||||||
}
|
|
||||||
|
|
||||||
QSharedPointer<NetworkValue> ScriptCache::getValue(const ParameterizedURL& url) {
|
QSharedPointer<NetworkValue> ScriptCache::getValue(const ParameterizedURL& url) {
|
||||||
QSharedPointer<NetworkValue> value = _networkValues.value(url);
|
QSharedPointer<NetworkValue> value = _networkValues.value(url);
|
||||||
if (value.isNull()) {
|
if (value.isNull()) {
|
||||||
|
@ -61,65 +49,21 @@ QSharedPointer<NetworkValue> ScriptCache::getValue(const ParameterizedURL& url)
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QSharedPointer<Resource> ScriptCache::createResource(const QUrl& url,
|
||||||
|
const QSharedPointer<Resource>& fallback, bool delayLoad, void* extra) {
|
||||||
|
return QSharedPointer<Resource>(new NetworkProgram(this, url));
|
||||||
|
}
|
||||||
|
|
||||||
NetworkProgram::NetworkProgram(ScriptCache* cache, const QUrl& url) :
|
NetworkProgram::NetworkProgram(ScriptCache* cache, const QUrl& url) :
|
||||||
_cache(cache),
|
Resource(url),
|
||||||
_request(url),
|
_cache(cache) {
|
||||||
_reply(NULL),
|
|
||||||
_attempts(0) {
|
|
||||||
|
|
||||||
if (!url.isValid()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
|
|
||||||
makeRequest();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkProgram::~NetworkProgram() {
|
void NetworkProgram::downloadFinished(QNetworkReply* reply) {
|
||||||
if (_reply != NULL) {
|
_program = QScriptProgram(QTextStream(reply).readAll(), reply->url().toString());
|
||||||
delete _reply;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkProgram::makeRequest() {
|
|
||||||
QNetworkAccessManager* manager = _cache->getNetworkAccessManager();
|
|
||||||
if (manager == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_reply = manager->get(_request);
|
|
||||||
|
|
||||||
connect(_reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(handleDownloadProgress(qint64,qint64)));
|
|
||||||
connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(handleReplyError()));
|
|
||||||
}
|
|
||||||
|
|
||||||
void NetworkProgram::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
|
||||||
if (bytesReceived < bytesTotal && !_reply->isFinished()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
_program = QScriptProgram(QTextStream(_reply).readAll(), _reply->url().toString());
|
|
||||||
|
|
||||||
_reply->disconnect(this);
|
|
||||||
_reply->deleteLater();
|
|
||||||
_reply = NULL;
|
|
||||||
|
|
||||||
emit loaded();
|
emit loaded();
|
||||||
}
|
}
|
||||||
|
|
||||||
void NetworkProgram::handleReplyError() {
|
|
||||||
QDebug debug = qDebug() << _reply->errorString();
|
|
||||||
|
|
||||||
_reply->disconnect(this);
|
|
||||||
_reply->deleteLater();
|
|
||||||
_reply = NULL;
|
|
||||||
|
|
||||||
// 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(makeRequest()));
|
|
||||||
debug << " -- retrying...";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
NetworkValue::~NetworkValue() {
|
NetworkValue::~NetworkValue() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,26 +9,20 @@
|
||||||
#ifndef __interface__ScriptCache__
|
#ifndef __interface__ScriptCache__
|
||||||
#define __interface__ScriptCache__
|
#define __interface__ScriptCache__
|
||||||
|
|
||||||
#include <QHash>
|
|
||||||
#include <QList>
|
|
||||||
#include <QNetworkRequest>
|
|
||||||
#include <QObject>
|
|
||||||
#include <QScriptProgram>
|
#include <QScriptProgram>
|
||||||
#include <QScriptValue>
|
#include <QScriptValue>
|
||||||
#include <QSharedPointer>
|
|
||||||
#include <QWeakPointer>
|
#include <ResourceCache.h>
|
||||||
|
|
||||||
#include "MetavoxelUtil.h"
|
#include "MetavoxelUtil.h"
|
||||||
|
|
||||||
class QNetworkAccessManager;
|
|
||||||
class QNetworkReply;
|
|
||||||
class QScriptEngine;
|
class QScriptEngine;
|
||||||
|
|
||||||
class NetworkProgram;
|
class NetworkProgram;
|
||||||
class NetworkValue;
|
class NetworkValue;
|
||||||
|
|
||||||
/// Maintains a cache of loaded scripts.
|
/// Maintains a cache of loaded scripts.
|
||||||
class ScriptCache : public QObject {
|
class ScriptCache : public ResourceCache {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -37,14 +31,11 @@ public:
|
||||||
|
|
||||||
ScriptCache();
|
ScriptCache();
|
||||||
|
|
||||||
void setNetworkAccessManager(QNetworkAccessManager* manager) { _networkAccessManager = manager; }
|
|
||||||
QNetworkAccessManager* getNetworkAccessManager() const { return _networkAccessManager; }
|
|
||||||
|
|
||||||
void setEngine(QScriptEngine* engine);
|
void setEngine(QScriptEngine* engine);
|
||||||
QScriptEngine* getEngine() const { return _engine; }
|
QScriptEngine* getEngine() const { return _engine; }
|
||||||
|
|
||||||
/// Loads a script program from the specified URL.
|
/// Loads a script program from the specified URL.
|
||||||
QSharedPointer<NetworkProgram> getProgram(const QUrl& url);
|
QSharedPointer<NetworkProgram> getProgram(const QUrl& url) { return getResource(url).staticCast<NetworkProgram>(); }
|
||||||
|
|
||||||
/// Loads a script value from the specified URL.
|
/// Loads a script value from the specified URL.
|
||||||
QSharedPointer<NetworkValue> getValue(const ParameterizedURL& url);
|
QSharedPointer<NetworkValue> getValue(const ParameterizedURL& url);
|
||||||
|
@ -55,11 +46,14 @@ public:
|
||||||
const QScriptString& getTypeString() const { return _typeString; }
|
const QScriptString& getTypeString() const { return _typeString; }
|
||||||
const QScriptString& getGeneratorString() const { return _generatorString; }
|
const QScriptString& getGeneratorString() const { return _generatorString; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual QSharedPointer<Resource> createResource(const QUrl& url,
|
||||||
|
const QSharedPointer<Resource>& fallback, bool delayLoad, void* extra);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
QNetworkAccessManager* _networkAccessManager;
|
|
||||||
QScriptEngine* _engine;
|
QScriptEngine* _engine;
|
||||||
QHash<QUrl, QWeakPointer<NetworkProgram> > _networkPrograms;
|
|
||||||
QHash<ParameterizedURL, QWeakPointer<NetworkValue> > _networkValues;
|
QHash<ParameterizedURL, QWeakPointer<NetworkValue> > _networkValues;
|
||||||
QScriptString _parametersString;
|
QScriptString _parametersString;
|
||||||
QScriptString _lengthString;
|
QScriptString _lengthString;
|
||||||
|
@ -69,13 +63,12 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
/// A program loaded from the network.
|
/// A program loaded from the network.
|
||||||
class NetworkProgram : public QObject {
|
class NetworkProgram : public Resource {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
NetworkProgram(ScriptCache* cache, const QUrl& url);
|
NetworkProgram(ScriptCache* cache, const QUrl& url);
|
||||||
~NetworkProgram();
|
|
||||||
|
|
||||||
ScriptCache* getCache() const { return _cache; }
|
ScriptCache* getCache() const { return _cache; }
|
||||||
|
|
||||||
|
@ -87,18 +80,13 @@ signals:
|
||||||
|
|
||||||
void loaded();
|
void loaded();
|
||||||
|
|
||||||
private slots:
|
protected:
|
||||||
|
|
||||||
void makeRequest();
|
virtual void downloadFinished(QNetworkReply* reply);
|
||||||
void handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
|
||||||
void handleReplyError();
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
ScriptCache* _cache;
|
ScriptCache* _cache;
|
||||||
QNetworkRequest _request;
|
|
||||||
QNetworkReply* _reply;
|
|
||||||
int _attempts;
|
|
||||||
QScriptProgram _program;
|
QScriptProgram _program;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
184
libraries/shared/src/ResourceCache.cpp
Normal file
184
libraries/shared/src/ResourceCache.cpp
Normal file
|
@ -0,0 +1,184 @@
|
||||||
|
//
|
||||||
|
// ResourceCache.cpp
|
||||||
|
// shared
|
||||||
|
//
|
||||||
|
// Created by Andrzej Kapolka on 2/27/14.
|
||||||
|
// Copyright (c) 2014 High Fidelity, Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <cfloat>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
#include <QNetworkReply>
|
||||||
|
#include <QTimer>
|
||||||
|
#include <QtDebug>
|
||||||
|
|
||||||
|
#include "ResourceCache.h"
|
||||||
|
|
||||||
|
ResourceCache::ResourceCache(QObject* parent) :
|
||||||
|
QObject(parent) {
|
||||||
|
}
|
||||||
|
|
||||||
|
QSharedPointer<Resource> ResourceCache::getResource(const QUrl& url, const QUrl& fallback, bool delayLoad, void* extra) {
|
||||||
|
if (!url.isValid() && fallback.isValid()) {
|
||||||
|
return getResource(fallback, QUrl(), delayLoad);
|
||||||
|
}
|
||||||
|
QSharedPointer<Resource> resource = _resources.value(url);
|
||||||
|
if (resource.isNull()) {
|
||||||
|
resource = createResource(url, fallback.isValid() ?
|
||||||
|
getResource(fallback, QUrl(), true) : QSharedPointer<Resource>(), delayLoad, extra);
|
||||||
|
_resources.insert(url, resource);
|
||||||
|
}
|
||||||
|
return resource;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ResourceCache::attemptRequest(Resource* resource) {
|
||||||
|
if (_requestLimit <= 0) {
|
||||||
|
// wait until a slot becomes available
|
||||||
|
_pendingRequests.append(resource);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_requestLimit--;
|
||||||
|
resource->makeRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ResourceCache::requestCompleted() {
|
||||||
|
_requestLimit++;
|
||||||
|
|
||||||
|
// look for the highest priority pending request
|
||||||
|
int highestIndex = -1;
|
||||||
|
float highestPriority = -FLT_MAX;
|
||||||
|
for (int i = 0; i < _pendingRequests.size(); ) {
|
||||||
|
Resource* resource = _pendingRequests.at(i).data();
|
||||||
|
if (!resource) {
|
||||||
|
_pendingRequests.removeAt(i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
float priority = resource->getLoadPriority();
|
||||||
|
if (priority >= highestPriority) {
|
||||||
|
highestPriority = priority;
|
||||||
|
highestIndex = i;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (highestIndex >= 0) {
|
||||||
|
attemptRequest(_pendingRequests.takeAt(highestIndex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QNetworkAccessManager* ResourceCache::_networkAccessManager = NULL;
|
||||||
|
|
||||||
|
const int DEFAULT_REQUEST_LIMIT = 10;
|
||||||
|
int ResourceCache::_requestLimit = DEFAULT_REQUEST_LIMIT;
|
||||||
|
|
||||||
|
QList<QPointer<Resource> > ResourceCache::_pendingRequests;
|
||||||
|
|
||||||
|
Resource::Resource(const QUrl& url, bool delayLoad) :
|
||||||
|
_request(url),
|
||||||
|
_startedLoading(false),
|
||||||
|
_failedToLoad(false),
|
||||||
|
_attempts(0),
|
||||||
|
_reply(NULL) {
|
||||||
|
|
||||||
|
if (!url.isValid()) {
|
||||||
|
_startedLoading = _failedToLoad = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
|
||||||
|
|
||||||
|
// start loading immediately unless instructed otherwise
|
||||||
|
if (!delayLoad) {
|
||||||
|
attemptRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Resource::~Resource() {
|
||||||
|
if (_reply) {
|
||||||
|
ResourceCache::requestCompleted();
|
||||||
|
delete _reply;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Resource::ensureLoading() {
|
||||||
|
if (!_startedLoading) {
|
||||||
|
attemptRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
float Resource::getLoadPriority() {
|
||||||
|
float highestPriority = -FLT_MAX;
|
||||||
|
for (QHash<QPointer<QObject>, float>::iterator it = _loadPriorities.begin(); it != _loadPriorities.end(); ) {
|
||||||
|
if (it.key().isNull()) {
|
||||||
|
it = _loadPriorities.erase(it);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
highestPriority = qMax(highestPriority, it.value());
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
return highestPriority;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Resource::attemptRequest() {
|
||||||
|
_startedLoading = true;
|
||||||
|
ResourceCache::attemptRequest(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Resource::handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) {
|
||||||
|
if (!_reply->isFinished()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
_reply->disconnect(this);
|
||||||
|
_reply->deleteLater();
|
||||||
|
QNetworkReply* reply = _reply;
|
||||||
|
_reply = NULL;
|
||||||
|
ResourceCache::requestCompleted();
|
||||||
|
|
||||||
|
downloadFinished(reply);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Resource::handleReplyError() {
|
||||||
|
QDebug debug = qDebug() << _reply->errorString();
|
||||||
|
|
||||||
|
QNetworkReply::NetworkError error = _reply->error();
|
||||||
|
_reply->disconnect(this);
|
||||||
|
_reply->deleteLater();
|
||||||
|
_reply = NULL;
|
||||||
|
ResourceCache::requestCompleted();
|
||||||
|
|
||||||
|
// retry for certain types of failures
|
||||||
|
switch (error) {
|
||||||
|
case QNetworkReply::RemoteHostClosedError:
|
||||||
|
case QNetworkReply::TimeoutError:
|
||||||
|
case QNetworkReply::TemporaryNetworkFailureError:
|
||||||
|
case QNetworkReply::ProxyConnectionClosedError:
|
||||||
|
case QNetworkReply::ProxyTimeoutError:
|
||||||
|
case QNetworkReply::UnknownNetworkError:
|
||||||
|
case QNetworkReply::UnknownProxyError:
|
||||||
|
case QNetworkReply::UnknownContentError:
|
||||||
|
case QNetworkReply::ProtocolFailure: {
|
||||||
|
// 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()));
|
||||||
|
debug << " -- retrying...";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// fall through to final failure
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
_failedToLoad = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Resource::makeRequest() {
|
||||||
|
_reply = ResourceCache::getNetworkAccessManager()->get(_request);
|
||||||
|
|
||||||
|
connect(_reply, SIGNAL(downloadProgress(qint64,qint64)), SLOT(handleDownloadProgress(qint64,qint64)));
|
||||||
|
connect(_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(handleReplyError()));
|
||||||
|
}
|
||||||
|
|
||||||
|
uint qHash(const QPointer<QObject>& value, uint seed) {
|
||||||
|
return qHash(value.data(), seed);
|
||||||
|
}
|
119
libraries/shared/src/ResourceCache.h
Normal file
119
libraries/shared/src/ResourceCache.h
Normal file
|
@ -0,0 +1,119 @@
|
||||||
|
//
|
||||||
|
// ResourceCache.h
|
||||||
|
// shared
|
||||||
|
//
|
||||||
|
// Created by Andrzej Kapolka on 2/27/14.
|
||||||
|
// Copyright (c) 2014 High Fidelity, Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef __shared__ResourceCache__
|
||||||
|
#define __shared__ResourceCache__
|
||||||
|
|
||||||
|
#include <QHash>
|
||||||
|
#include <QList>
|
||||||
|
#include <QNetworkRequest>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QPointer>
|
||||||
|
#include <QSharedPointer>
|
||||||
|
#include <QUrl>
|
||||||
|
#include <QWeakPointer>
|
||||||
|
|
||||||
|
class QNetworkAccessManager;
|
||||||
|
class QNetworkReply;
|
||||||
|
|
||||||
|
class Resource;
|
||||||
|
|
||||||
|
/// Base class for resource caches.
|
||||||
|
class ResourceCache : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
static void setNetworkAccessManager(QNetworkAccessManager* manager) { _networkAccessManager = manager; }
|
||||||
|
static QNetworkAccessManager* getNetworkAccessManager() { return _networkAccessManager; }
|
||||||
|
|
||||||
|
static void setRequestLimit(int limit) { _requestLimit = limit; }
|
||||||
|
static int getRequestLimit() { return _requestLimit; }
|
||||||
|
|
||||||
|
ResourceCache(QObject* parent = NULL);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
/// Loads a resource from the specified URL.
|
||||||
|
/// \param fallback a fallback URL to load if the desired one is unavailable
|
||||||
|
/// \param delayLoad if true, don't load the resource immediately; wait until load is first requested
|
||||||
|
/// \param extra extra data to pass to the creator, if appropriate
|
||||||
|
QSharedPointer<Resource> getResource(const QUrl& url, const QUrl& fallback = QUrl(),
|
||||||
|
bool delayLoad = false, void* extra = NULL);
|
||||||
|
|
||||||
|
/// Creates a new resource.
|
||||||
|
virtual QSharedPointer<Resource> createResource(const QUrl& url,
|
||||||
|
const QSharedPointer<Resource>& fallback, bool delayLoad, void* extra) = 0;
|
||||||
|
|
||||||
|
static void attemptRequest(Resource* resource);
|
||||||
|
static void requestCompleted();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
friend class Resource;
|
||||||
|
|
||||||
|
QHash<QUrl, QWeakPointer<Resource> > _resources;
|
||||||
|
|
||||||
|
static QNetworkAccessManager* _networkAccessManager;
|
||||||
|
static int _requestLimit;
|
||||||
|
static QList<QPointer<Resource> > _pendingRequests;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Base class for resources.
|
||||||
|
class Resource : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
Resource(const QUrl& url, bool delayLoad = false);
|
||||||
|
~Resource();
|
||||||
|
|
||||||
|
/// Makes sure that the resource has started loading.
|
||||||
|
void ensureLoading();
|
||||||
|
|
||||||
|
/// Sets the load priority for one owner.
|
||||||
|
void setLoadPriority(const QPointer<QObject>& owner, float priority) { _loadPriorities.insert(owner, priority); }
|
||||||
|
|
||||||
|
/// Clears the load priority for one owner.
|
||||||
|
void clearLoadPriority(const QPointer<QObject>& owner) { _loadPriorities.remove(owner); }
|
||||||
|
|
||||||
|
/// Returns the highest load priority across all owners.
|
||||||
|
float getLoadPriority();
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
|
||||||
|
void attemptRequest();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
virtual void downloadFinished(QNetworkReply* reply) = 0;
|
||||||
|
|
||||||
|
QNetworkRequest _request;
|
||||||
|
bool _startedLoading;
|
||||||
|
bool _failedToLoad;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
|
||||||
|
void handleDownloadProgress(qint64 bytesReceived, qint64 bytesTotal);
|
||||||
|
void handleReplyError();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
void makeRequest();
|
||||||
|
|
||||||
|
friend class ResourceCache;
|
||||||
|
|
||||||
|
QNetworkReply* _reply;
|
||||||
|
int _attempts;
|
||||||
|
|
||||||
|
QHash<QPointer<QObject>, float> _loadPriorities;
|
||||||
|
};
|
||||||
|
|
||||||
|
uint qHash(const QPointer<QObject>& value, uint seed = 0);
|
||||||
|
|
||||||
|
#endif /* defined(__shared__ResourceCache__) */
|
Loading…
Reference in a new issue