diff options
-rw-r--r-- | include/llmr/platform/platform.hpp | 15 | ||||
-rw-r--r-- | macosx/main.mm | 42 |
2 files changed, 55 insertions, 2 deletions
diff --git a/include/llmr/platform/platform.hpp b/include/llmr/platform/platform.hpp index 75d491cb09..ae34dc7ba9 100644 --- a/include/llmr/platform/platform.hpp +++ b/include/llmr/platform/platform.hpp @@ -2,6 +2,8 @@ #define LLMR_PLATFORM_IOS #include <memory> +#include <functional> +#include <string> namespace llmr { @@ -18,6 +20,19 @@ void restart(void *obj); // Then, call map.tileLoaded(tile); or map.tileFailed(tile); in the main thread. void request(void *obj, std::shared_ptr<Tile> tile); + +struct Response { + Response(int16_t code, const char *body, size_t length) + : code(code), + body(body), + length(length) {} + int16_t code; + const char *body; + size_t length; +}; + +void request_http(std::string url, std::function<void(const Response&)> func); + // Returns a relative timestamp in seconds. This value must be monotonic. double time(); diff --git a/macosx/main.mm b/macosx/main.mm index cbbbd96ef0..1bcadad725 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -15,7 +15,7 @@ public: last_click(-1), settings(), map(settings) { - } + } void init() { if (!glfwInit()) { @@ -238,7 +238,7 @@ void request(void *, Tile::Ptr tile) { NSData * data, NSError * error) { if (error == nil) { - NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response; + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; int code = [httpResponse statusCode]; if (code == 200) { @@ -261,6 +261,44 @@ void request(void *, Tile::Ptr tile) { }]; } + +void request_http(std::string url, std::function<void(const Response&)> func) { + NSMutableURLRequest *urlRequest = [NSMutableURLRequest + requestWithURL:[NSURL + URLWithString:[NSString + stringWithUTF8String:url.c_str()]]]; + + if (!queue) { + queue = [[NSOperationQueue alloc] init]; + } + + [NSURLConnection + sendAsynchronousRequest:urlRequest + queue:queue + completionHandler: ^ (NSURLResponse* response, NSData* data, NSError* error) { + if (error == nil) { + NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; + dispatch_async(dispatch_get_main_queue(), ^ { + func({ + [httpResponse statusCode], + (const char *)[data bytes], + [data length] + }); + }); + } else { + dispatch_async(dispatch_get_main_queue(), ^ { + func({ + -1, + 0, + 0 + }); + }); + } + }]; +} + + + double time() { return glfwGetTime(); } |