18#ifndef QGSTILEDOWNLOADMANAGER_H
19#define QGSTILEDOWNLOADMANAGER_H
25#include <QNetworkAccessManager>
26#include <QNetworkReply>
47class CORE_EXPORT QgsTileDownloadManagerReply :
public QObject
51 ~QgsTileDownloadManagerReply()
override;
56 QByteArray
data()
const {
return mData; }
58 QUrl
url()
const {
return mUrl; }
60 QVariant attribute( QNetworkRequest::Attribute code );
62 QVariant header( QNetworkRequest::KnownHeaders header );
64 const QList<QNetworkReply::RawHeaderPair>
rawHeaderPairs()
const {
return mRawHeaderPairs; }
66 QNetworkReply::NetworkError
error()
const {
return mError; }
70 QNetworkRequest
request()
const {
return mRequest; }
80 const QMap<QNetworkRequest::Attribute, QVariant> &attributes,
81 const QMap<QNetworkRequest::KnownHeaders, QVariant> &headers,
83 QNetworkReply::NetworkError
error,
86 void cachedRangeRequestFinished();
96 QNetworkRequest mRequest;
97 bool mHasFinished =
false;
99 QNetworkReply::NetworkError mError = QNetworkReply::NoError;
100 QString mErrorString;
102 QMap<QNetworkRequest::Attribute, QVariant> mAttributes;
103 QMap<QNetworkRequest::KnownHeaders, QVariant> mHeaders;
104 QList<QNetworkReply::RawHeaderPair> mRawHeaderPairs;
115class QgsTileDownloadManagerReplyWorkerObject :
public QObject
119 QgsTileDownloadManagerReplyWorkerObject(
QgsTileDownloadManager *manager,
const QNetworkRequest &request )
120 : mManager( manager )
121 , mRequest( request )
125 void replyFinished();
131 const QMap<QNetworkRequest::Attribute, QVariant> &attributes,
132 const QMap<QNetworkRequest::KnownHeaders, QVariant> &headers,
133 const QList<QNetworkReply::RawHeaderPair> rawHeaderPairs,
134 QNetworkReply::NetworkError error,
135 const QString &errorString
140 QgsTileDownloadManager *mManager =
nullptr;
141 QNetworkRequest mRequest;
150class QgsTileDownloadManagerWorker :
public QObject
156 QgsTileDownloadManagerWorker( QgsTileDownloadManager *manager, QObject *parent =
nullptr );
158 void startIdleTimer();
162 void idleTimerTimeout();
165 void requestFinished( QString url, QByteArray data );
172 QgsTileDownloadManager *mManager =
nullptr;
225 bool isValid()
const {
return !request.url().isEmpty(); }
228 QNetworkRequest request;
232 QNetworkReply *networkReply =
nullptr;
296 void resetStatistics();
304 QueueEntry findEntryForRequest(
const QNetworkRequest &request );
305 void addEntry(
const QueueEntry &entry );
306 void updateEntry(
const QueueEntry &entry );
307 void removeEntry(
const QNetworkRequest &request );
308 void processStagedEntryRemovals();
310 void signalQueueModified();
312 bool isRangeRequest(
const QNetworkRequest &request );
313 bool isCachedRangeRequest(
const QNetworkRequest &request );
316 std::vector<QueueEntry> mQueue;
318 bool mStageQueueRemovals =
false;
319 std::vector< QNetworkRequest > mStagedQueueRemovals;
321 bool mShuttingDown =
false;
322 mutable QRecursiveMutex mMutex;
324 QThread *mWorkerThread =
nullptr;
328 int mIdleThreadTimeoutMs = 10000;
330 std::unique_ptr<QgsRangeRequestCache> mRangesCache;
A custom cache for handling the storage and retrieval of HTTP range requests on disk.
QString errorString() const
Returns error string (only valid when already finished).
bool hasFinished() const
Returns whether the reply has already finished (with success/failure).
QNetworkRequest request() const
Returns the original request for this reply object.
friend class QgsTileDownloadManager
const QList< QNetworkReply::RawHeaderPair > rawHeaderPairs() const
Returns a list of raw header pairs.
QByteArray data() const
Returns binary data returned in the reply (only valid when already finished).
QNetworkReply::NetworkError error() const
Returns error code (only valid when already finished).
QUrl url() const
Returns the reply URL.
void finished()
Emitted when the reply has finished (either with a success or with a failure).
Encapsulates any statistics we would like to keep about requests.
int networkRequestsStarted
How many actual network requests were started.
int requestsMerged
How many requests were same as some other pending request and got "merged".
int networkRequestsOk
How many network requests have been successful.
int networkRequestsFailed
How many network requests have failed.
int requestsEarlyDeleted
How many requests were deleted early by the client (i.e. lost interest).
int requestsTotal
How many requests were done through the download manager.
Tile download manager handles downloads of map tiles for the purpose of map rendering.
Stats statistics() const
Returns basic statistics of the queries handled by this class.
bool hasWorkerThreadRunning() const
Returns whether the worker thread is running currently (it may be stopped if there were no requests r...
friend class QgsTileDownloadManagerReplyWorkerObject
bool waitForPendingRequests(int msec=-1) const
Blocks the current thread until the queue is empty.
QgsTileDownloadManagerReply * get(const QNetworkRequest &request)
Starts a request.
void setIdleThreadTimeout(int timeoutMs)
Sets after how many milliseconds the idle worker therad should terminate.
friend class QgsTileDownloadManagerReply
bool hasPendingRequests() const
Returns whether there are any pending requests in the queue.
friend class QgsTileDownloadManagerWorker
void shutdown()
Asks the worker thread to stop and blocks until it is not stopped.