summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2015-11-02 17:00:46 +0100
committerKonstantin Käfer <mail@kkaefer.com>2015-11-02 17:25:08 +0100
commit04273fbb1c1dc2c44b804cd209580014bc75ddd7 (patch)
tree3d562f67c0c0f2f3b28362ae25b1eede1d1739ee /src
parent4d5c6333be52aae4a9c72f4b01941e16ead503f4 (diff)
downloadqtlocation-mapboxgl-04273fbb1c1dc2c44b804cd209580014bc75ddd7.tar.gz
[core] Make DefaultFileSource react to all NetworkStatus changes
Diffstat (limited to 'src')
-rw-r--r--src/mbgl/storage/default_file_source.cpp27
-rw-r--r--src/mbgl/storage/default_file_source_impl.hpp4
2 files changed, 30 insertions, 1 deletions
diff --git a/src/mbgl/storage/default_file_source.cpp b/src/mbgl/storage/default_file_source.cpp
index 773875c390..64c802d770 100644
--- a/src/mbgl/storage/default_file_source.cpp
+++ b/src/mbgl/storage/default_file_source.cpp
@@ -2,6 +2,7 @@
#include <mbgl/storage/request.hpp>
#include <mbgl/storage/asset_context_base.hpp>
#include <mbgl/storage/http_context_base.hpp>
+#include <mbgl/storage/network_status.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/platform/platform.hpp>
@@ -87,7 +88,29 @@ DefaultFileSource::Impl::Impl(FileCache* cache_, const std::string& root)
cache(cache_),
assetRoot(root.empty() ? platform::assetRoot() : root),
assetContext(AssetContextBase::createContext(loop)),
- httpContext(HTTPContextBase::createContext(loop)) {
+ httpContext(HTTPContextBase::createContext(loop)),
+ reachability(std::make_unique<uv::async>(loop, std::bind(&Impl::networkIsReachableAgain, this))) {
+ // Subscribe to network status changes, but make sure that this async handle doesn't keep the
+ // loop alive; otherwise our app wouldn't terminate. After all, we only need status change
+ // notifications when our app is still running.
+ NetworkStatus::Subscribe(reachability->get());
+ reachability->unref();
+}
+
+DefaultFileSource::Impl::~Impl() {
+ NetworkStatus::Unsubscribe(reachability->get());
+}
+
+void DefaultFileSource::Impl::networkIsReachableAgain() {
+ for (auto& req : pending) {
+ auto& request = req.second;
+ auto& response = request.getResponse();
+ if (!request.realRequest && response && response->error && response->error->reason == Response::Error::Reason::Connection) {
+ // We need all requests to fail at least once before we are going to start retrying
+ // them, and we only immediately restart request that failed due to connection issues.
+ startRealRequest(request);
+ }
+ }
}
void DefaultFileSource::Impl::add(Request* req) {
@@ -157,6 +180,8 @@ void DefaultFileSource::Impl::startCacheRequest(DefaultFileRequest& request) {
}
void DefaultFileSource::Impl::startRealRequest(DefaultFileRequest& request) {
+ assert(!request.realRequest);
+
// Cancel the timer if we have one.
if (request.timerRequest) {
request.timerRequest->stop();
diff --git a/src/mbgl/storage/default_file_source_impl.hpp b/src/mbgl/storage/default_file_source_impl.hpp
index 2e5ca93ccc..4ca8e42721 100644
--- a/src/mbgl/storage/default_file_source_impl.hpp
+++ b/src/mbgl/storage/default_file_source_impl.hpp
@@ -69,6 +69,9 @@ private:
class DefaultFileSource::Impl {
public:
Impl(FileCache*, const std::string& = "");
+ ~Impl();
+
+ void networkIsReachableAgain();
void add(Request*);
void cancel(Request*);
@@ -85,6 +88,7 @@ private:
const std::string assetRoot;
const std::unique_ptr<AssetContextBase> assetContext;
const std::unique_ptr<HTTPContextBase> httpContext;
+ const std::unique_ptr<uv::async> reachability;
};
}