alternative http_request function

This commit is contained in:
Konstantin Käfer
2014-02-03 14:53:59 +01:00
parent 6849c8d576
commit 44a3ffbd57
3 changed files with 28 additions and 7 deletions

View File

@@ -21,6 +21,7 @@ struct Response {
};
void request_http(std::string url, std::function<void(Response&)> func);
void request_http(std::string url, std::function<void(Response&)> func, std::function<void()> cb);
void async(std::function<void()> fn, std::function<void()> cb);

View File

@@ -248,6 +248,29 @@ void request_http(std::string url, std::function<void(Response&)> func) {
}];
}
void request_http(std::string url, std::function<void(Response&)> func, std::function<void()> cb) {
NSMutableURLRequest *urlRequest = [NSMutableURLRequest
requestWithURL:[NSURL
URLWithString:[NSString
stringWithUTF8String:url.c_str()]]];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler: ^ (NSURLResponse* response, NSData* data, NSError* error) {
Response res;
if (error == nil) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
res.code = [httpResponse statusCode];
res.body = { (const char *)[data bytes], [data length] };
}
func(res);
dispatch_async(dispatch_get_main_queue(), ^ {
cb();
});
}];
}
double time() {

View File

@@ -77,16 +77,13 @@ void Tile::request() {
platform::request_http(url, [=](platform::Response& res) {
if (res.code == 200) {
tile->data.swap(res.body);
platform::async([tile]() {
tile->parse();
}, []() {
// TODO: Make sure this gets passed the correct map ID/pointer.
platform::restart(NULL);
});
tile->parse();
} else {
fprintf(stderr, "tile loading failed\n");
}
}, []() {
// TODO: Make sure this gets passed the correct map ID/pointer.
platform::restart(NULL);
});
}