From c6e5f421e2738b6005f6c3da6700b1343214f7e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Wed, 9 Jun 2021 19:51:18 +0800 Subject: Add support for Emscripten --- request.h | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) (limited to 'request.h') diff --git a/request.h b/request.h index a943687..73b26d8 100644 --- a/request.h +++ b/request.h @@ -7,18 +7,34 @@ #include #include #include +#ifndef __EMSCRIPTEN__ #include +#endif +#include "icclient/typedefs.h" +#ifdef __EMSCRIPTEN__ +extern emscripten_fetch_attr_t attr; +#else extern CURL *curl; extern char *server_url; +#endif -inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), - void *writedata, struct curl_httppost *post, char *fmt, ...) +inline void request(icclient_handler writefunction, void *writedata, +#ifdef __EMSCRIPTEN__ + int +#else + struct curl_httppost * +#endif + post, char *fmt, ...) { va_list ap; char *p, *sval; unsigned int ival; - size_t length = strlen(server_url) + strlen(fmt); + size_t length = +#ifndef __EMSCRIPTEN__ + strlen(server_url) + +#endif + strlen(fmt); va_start(ap, fmt); for (p = fmt; *p; p++) { @@ -43,7 +59,11 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), va_end(ap); char url[length + 1]; +#ifdef __EMSCRIPTEN__ + memset(url, 0, length + 1); +#else strcpy(url, server_url); +#endif va_start(ap, fmt); for (p = fmt; *p; p++) { @@ -64,17 +84,36 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), } va_end(ap); +#ifdef __EMSCRIPTEN__ + attr.onsuccess = writefunction; +#else curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunction); +#endif if (writedata) +#ifdef __EMSCRIPTEN__ + attr.userData = writedata; +#else curl_easy_setopt(curl, CURLOPT_WRITEDATA, writedata); else curl_easy_setopt(curl, CURLOPT_WRITEDATA, stdout); +#endif if (post) +#ifdef __EMSCRIPTEN__ + strcpy(attr.requestMethod, "POST"); +#else curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); +#endif else +#ifdef __EMSCRIPTEN__ + strcpy(attr.requestMethod, "GET"); +#else curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); +#endif +#ifdef __EMSCRIPTEN__ + emscripten_fetch(&attr, url); +#else #ifdef DEBUG CURLcode res = #endif @@ -86,6 +125,7 @@ inline void request(size_t (*writefunction)(void *, size_t, size_t, void *), __android_log_print(ANDROID_LOG_ERROR, "libicclient.so", "%s: %s", __func__, error); #else fprintf(stderr, "%s: %s\n", __func__, error); +#endif #endif } #endif -- cgit v1.2.3