diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-09-29 12:27:34 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-09-29 12:27:34 +0200 |
commit | 46e06bf378e5edc38b5ce1c3ab1e4567eb756d24 (patch) | |
tree | 452414d3ed8fdf82a12af25f3a1bfb116e073330 | |
parent | b9628c86543ffe819b030c3d84f65fa09e084850 (diff) | |
download | qtlocation-mapboxgl-46e06bf378e5edc38b5ce1c3ab1e4567eb756d24.tar.gz |
don't pass the loop through all function calls
-rw-r--r-- | include/mbgl/storage/http_request.hpp | 8 | ||||
-rw-r--r-- | src/storage/http_request.cpp | 25 |
2 files changed, 20 insertions, 13 deletions
diff --git a/include/mbgl/storage/http_request.hpp b/include/mbgl/storage/http_request.hpp index c81e23ef8d..4c8d064d65 100644 --- a/include/mbgl/storage/http_request.hpp +++ b/include/mbgl/storage/http_request.hpp @@ -27,16 +27,18 @@ public: void cancel(); private: - void handleCacheResponse(std::unique_ptr<Response> &&response, uv_loop_t *loop); - void handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&response, uv_loop_t *loop); + void startCacheRequest(); + void handleCacheResponse(std::unique_ptr<Response> &&response); + void startHTTPRequest(std::unique_ptr<Response> &&res); + void handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&response); - void startRequest(std::unique_ptr<Response> &&res, uv_loop_t *loop); void removeCacheBaton(); void removeHTTPBaton(); private: const unsigned long thread_id; + uv_loop_t *const loop; CacheRequestBaton *cache_baton = nullptr; util::ptr<HTTPRequestBaton> http_baton; uv_timer_t *backoff_timer = nullptr; diff --git a/src/storage/http_request.cpp b/src/storage/http_request.cpp index 9dc289e141..fb079d2588 100644 --- a/src/storage/http_request.cpp +++ b/src/storage/http_request.cpp @@ -18,27 +18,31 @@ struct CacheRequestBaton { HTTPRequest *request = nullptr; std::string path; util::ptr<SQLiteStore> store; - uv_loop_t *loop; }; -HTTPRequest::HTTPRequest(ResourceType type_, const std::string &path, uv_loop_t *loop, util::ptr<SQLiteStore> store_) - : BaseRequest(path), thread_id(uv_thread_self()), store(store_), type(type_) { +HTTPRequest::HTTPRequest(ResourceType type_, const std::string &path, uv_loop_t *loop_, util::ptr<SQLiteStore> store_) + : BaseRequest(path), thread_id(uv_thread_self()), loop(loop_), store(store_), type(type_) { + startCacheRequest(); +} + +void HTTPRequest::startCacheRequest() { + assert(uv_thread_self() == thread_id); + cache_baton = new CacheRequestBaton; cache_baton->request = this; cache_baton->path = path; cache_baton->store = store; - cache_baton->loop = loop; store->get(path, [](std::unique_ptr<Response> &&response, void *ptr) { // Wrap in a unique_ptr, so it'll always get auto-destructed. std::unique_ptr<CacheRequestBaton> baton((CacheRequestBaton *)ptr); if (baton->request) { baton->request->cache_baton = nullptr; - baton->request->handleCacheResponse(std::move(response), baton->loop); + baton->request->handleCacheResponse(std::move(response)); } }, cache_baton); } -void HTTPRequest::handleCacheResponse(std::unique_ptr<Response> &&res, uv_loop_t *loop) { +void HTTPRequest::handleCacheResponse(std::unique_ptr<Response> &&res) { assert(uv_thread_self() == thread_id); if (res) { @@ -56,10 +60,10 @@ void HTTPRequest::handleCacheResponse(std::unique_ptr<Response> &&res, uv_loop_t } } - startRequest(std::move(res), loop); + startHTTPRequest(std::move(res)); } -void HTTPRequest::startRequest(std::unique_ptr<Response> &&res, uv_loop_t *loop) { +void HTTPRequest::startHTTPRequest(std::unique_ptr<Response> &&res) { assert(uv_thread_self() == thread_id); assert(!http_baton); @@ -76,7 +80,7 @@ void HTTPRequest::startRequest(std::unique_ptr<Response> &&res, uv_loop_t *loop) HTTPRequest *request = http_baton->request; request->http_baton.reset(); http_baton->request = nullptr; - request->handleHTTPResponse(http_baton->type, std::move(http_baton->response), async->loop); + request->handleHTTPResponse(http_baton->type, std::move(http_baton->response)); } delete (util::ptr<HTTPRequestBaton> *)async->data; @@ -85,10 +89,11 @@ void HTTPRequest::startRequest(std::unique_ptr<Response> &&res, uv_loop_t *loop) delete async; }); }); + attempts++; HTTPRequestBaton::start(http_baton); } -void HTTPRequest::handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&res, uv_loop_t *loop) { +void HTTPRequest::handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&res) { assert(uv_thread_self() == thread_id); assert(!http_baton); assert(!response); |