diff --git a/http_intf.c b/http_intf.c index bce107bae8..14ff6db0de 100644 --- a/http_intf.c +++ b/http_intf.c @@ -42,49 +42,3 @@ http_retcode http_get_file(char *url, char **buf, int *len) return http_get(urlfilename, buf, len, NULL); } - -/** - * http_download_file: - * @url : URL to file. - * @output_dir : Output directory for new file. - * @output_basename : Output basename for new file. - * - * Downloads a file at specified URL. - * - * Returns: bool (1) if successful, otherwise false (0). - **/ -bool http_download_file(char *url, const char *output_dir, - const char *output_basename) -{ - http_retcode status; - int len; - FILE *f; - char output_path[PATH_MAX_LENGTH]; - char *buf; - - status = http_get_file(url, &buf, &len); - - if (status < 0) - { - RARCH_ERR("%i - Failure.\n", status); - return false; - } - - fill_pathname_join(output_path, output_dir, output_basename, - sizeof(output_path)); - - f = fopen(output_path, "wb"); - - if (!f) - goto cleanup; - - fwrite(buf, 1, len, f); - - fclose(f); - -cleanup: - if (buf) - free(buf); - - return true; -} diff --git a/http_intf.h b/http_intf.h index 510203573b..945bce6b05 100644 --- a/http_intf.h +++ b/http_intf.h @@ -39,19 +39,6 @@ extern "C" { **/ http_retcode http_get_file(char *url, char **buf, int *len); -/** - * http_download_file: - * @url : URL to file. - * @output_dir : Output directory for new file. - * @output_basename : Output basename for new file. - * - * Downloads a file at specified URL. - * - * Returns: bool (1) if successful, otherwise false (0). - **/ -bool http_download_file(char *url, const char *output_dir, - const char *output_basename); - #ifdef __cplusplus } #endif diff --git a/menu/menu_entries_cbs.c b/menu/menu_entries_cbs.c index fcd06fcb1b..7acefea9bc 100644 --- a/menu/menu_entries_cbs.c +++ b/menu/menu_entries_cbs.c @@ -30,6 +30,7 @@ #ifdef HAVE_NETPLAY #include "../http_intf.h" +#include "../net_http.h" #endif #ifdef HAVE_LIBRETRODB @@ -810,6 +811,34 @@ static int action_ok_save_state(const char *path, } +/* HACK - we have to find some way to pass state inbetween + * function pointer callback functions that don't necessarily + * call each other. */ +static char core_manager_path[PATH_MAX_LENGTH]; + +static int cb_core_manager_download(void *data_, size_t len) +{ + FILE *f; + char output_path[PATH_MAX_LENGTH]; + char *data = (char*)data_; + + if (!data) + return -1; + + fill_pathname_join(output_path, g_settings.libretro_directory, + core_manager_path, sizeof(output_path)); + + f = fopen(output_path, "wb"); + + if (!f) + return -1; + + fwrite(data, 1, len, f); + fclose(f); + + return 0; +} + static int action_ok_core_manager_list(const char *path, const char *label, unsigned type, size_t idx) { @@ -818,8 +847,12 @@ static int action_ok_core_manager_list(const char *path, fill_pathname_join(core_path, g_settings.network.buildbot_url, path, sizeof(core_path)); - if (!http_download_file(core_path, g_settings.libretro_directory, path)) - return -1; + strlcpy(core_manager_path, path, sizeof(core_manager_path)); + + msg_queue_clear(g_extern.http_msg_queue); + msg_queue_push(g_extern.http_msg_queue, core_path, 0, 180); + + net_http_set_pending_cb(cb_core_manager_download); #endif return 0; } diff --git a/net_http.c b/net_http.c index add65c5c54..6e453300af 100644 --- a/net_http.c +++ b/net_http.c @@ -183,6 +183,23 @@ static ssize_t net_http_recv(int fd, bool *error, return -1; } +static http_cb_t pending_cb; + +void net_http_set_pending_cb(http_cb_t cb) +{ + pending_cb = cb; +} + +static http_cb_t net_http_get_pending_cb(void) +{ + return pending_cb; +} + +static void net_http_clear_pending_cb(void) +{ + pending_cb = NULL; +} + http_t *net_http_new(const char * url) { bool error; @@ -238,6 +255,9 @@ http_t *net_http_new(const char * url) state->len = 0; state->buflen = 512; state->data = (char*)malloc(state->buflen); + state->cb = net_http_get_pending_cb(); + + net_http_clear_pending_cb(); return state; diff --git a/net_http.h b/net_http.h index 94cebeff01..861305eb22 100644 --- a/net_http.h +++ b/net_http.h @@ -25,6 +25,8 @@ extern "C" { #endif +typedef int (*http_cb_t )(void *data, size_t len); + typedef struct { int fd; @@ -41,10 +43,13 @@ typedef struct size_t len; size_t buflen; char * data; + http_cb_t cb; } http_t; http_t *net_http_new(const char * url); +void net_http_set_pending_cb(http_cb_t cb); + /* You can use this to call net_http_update * only when something will happen; select() it for reading. */ int net_http_fd(http_t *state); diff --git a/net_http_test.c b/net_http_test.c index d48c020de9..3fa34593e5 100644 --- a/net_http_test.c +++ b/net_http_test.c @@ -5,7 +5,7 @@ int main(void) { char *w; http_t *http1, *http2, *http3; - size_t q, pos = 0; size_t tot = 0; + size_t len, pos = 0; size_t tot = 0; http1 = net_http_new("http://buildbot.libretro.com/nightly/win-x86/latest/mednafen_psx_libretro.dll.zip"); @@ -15,7 +15,7 @@ int main(void) http3 = net_http_new("http://www.wikipedia.org/"); while (!net_http_update(http3, NULL, NULL)) {} - w = (char*)net_http_data(http3, &q, false); + w = (char*)net_http_data(http3, &len, false); printf("%.*s\n", (int)256, w); diff --git a/runloop.c b/runloop.c index ffe379f95a..f568eeea16 100644 --- a/runloop.c +++ b/runloop.c @@ -888,6 +888,19 @@ static int rarch_main_iterate_http_transfer(void) return 0; } +static int rarch_main_iterate_http_parse(void) +{ + size_t len; + char *w = (char*)net_http_data(g_extern.http_handle, &len, false); + + if (w && g_extern.http_handle->cb) + g_extern.http_handle->cb(w, len); + + net_http_delete(g_extern.http_handle); + + return 0; +} + /** * rarch_main_iterate_http_transfer: * @@ -952,10 +965,7 @@ int rarch_main_iterate(void) if (g_extern.http_handle) { if (!rarch_main_iterate_http_transfer()) - { - /* TODO - we should have some function pointer - * we can call here. */ - } + rarch_main_iterate_http_parse(); } else rarch_main_iterate_http_poll();