Remove RequestBase::start

Requests are always started immediately upon construction.

While here, use typed pointers, not void*.
This commit is contained in:
John Firebaugh
2015-04-17 12:37:43 -07:00
parent cdd8f3ac32
commit 8e5fa341eb
8 changed files with 70 additions and 104 deletions

View File

@@ -26,7 +26,7 @@ class AssetRequestImpl {
MBGL_STORE_THREAD(tid)
public:
AssetRequestImpl(AssetRequest *request, uv_loop_t *loop);
AssetRequestImpl(AssetRequest*, uv_loop_t*, const std::string& assetRoot);
~AssetRequestImpl();
static void fileOpened(uv_fs_t *req);
@@ -36,8 +36,8 @@ public:
static void notifyError(uv_fs_t *req);
static void cleanup(uv_fs_t *req);
AssetRequest *request = nullptr;
std::string assetRoot;
bool canceled = false;
uv_fs_t req;
uv_file fd = -1;
@@ -49,11 +49,13 @@ AssetRequestImpl::~AssetRequestImpl() {
MBGL_VERIFY_THREAD(tid);
if (request) {
request->ptr = nullptr;
request->impl = nullptr;
}
}
AssetRequestImpl::AssetRequestImpl(AssetRequest *request_, uv_loop_t *loop) : request(request_) {
AssetRequestImpl::AssetRequestImpl(AssetRequest* request_, uv_loop_t* loop, const std::string& assetRoot_)
: request(request_)
, assetRoot(assetRoot_) {
req.data = this;
const auto &url = request->resource.url;
@@ -63,7 +65,7 @@ AssetRequestImpl::AssetRequestImpl(AssetRequest *request_, uv_loop_t *loop) : re
path = url.substr(8);
} else {
// This is a relative path. Prefix with the application root.
path = request->assetRoot + "/" + url.substr(8);
path = assetRoot + "/" + url.substr(8);
}
uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, fileOpened);
@@ -208,40 +210,26 @@ void AssetRequestImpl::cleanup(uv_fs_t *req) {
// -------------------------------------------------------------------------------------------------
AssetRequest::AssetRequest(const Resource& resource_, Callback callback_, const std::string& assetRoot_)
AssetRequest::AssetRequest(const Resource& resource_, Callback callback_,
uv_loop_t* loop, const std::string& assetRoot)
: RequestBase(resource_, callback_)
, assetRoot(assetRoot_) {
, impl(new AssetRequestImpl(this, loop, assetRoot)) {
assert(algo::starts_with(resource.url, "asset://"));
}
AssetRequest::~AssetRequest() {
if (ptr) {
reinterpret_cast<AssetRequestImpl *>(ptr)->request = nullptr;
}
}
void AssetRequest::start(uv_loop_t *loop, std::shared_ptr<const Response> response) {
// We're ignoring the existing response if any.
(void(response));
assert(!ptr);
ptr = new AssetRequestImpl(this, loop);
// Note: the AssetRequestImpl deletes itself.
}
void AssetRequest::cancel() {
if (ptr) {
reinterpret_cast<AssetRequestImpl *>(ptr)->canceled = true;
// uv_cancel fails frequently when the request has already been started.
// In that case, we have to let it complete and check the canceled bool
// instead. The cancelation callback will delete the AssetRequest object.
uv_cancel((uv_req_t *)&reinterpret_cast<AssetRequestImpl *>(ptr)->req);
} else {
// This request is canceled before we called start. We're safe to delete
// ourselves now.
delete this;
AssetRequest::~AssetRequest() {
if (impl) {
impl->request = nullptr;
}
}
void AssetRequest::cancel() {
impl->canceled = true;
// uv_cancel fails frequently when the request has already been started.
// In that case, we have to let it complete and check the canceled bool
// instead. The cancelation callback will delete the AssetRequest object.
uv_cancel((uv_req_t *)&impl->req);
}
}