mirror of
https://github.com/libretro/RetroArch
synced 2025-02-01 20:54:10 +00:00
Move code to tasks/task_http.c
This commit is contained in:
parent
54550912f5
commit
684a0555ed
@ -737,7 +737,8 @@ endif
|
||||
ifeq ($(HAVE_NETWORKING), 1)
|
||||
DEFINES += -DHAVE_NETWORKING
|
||||
OBJ += libretro-common/net/net_compat.o \
|
||||
libretro-common/net/net_http.o
|
||||
libretro-common/net/net_http.o \
|
||||
tasks/task_http.o
|
||||
|
||||
ifneq ($(findstring Win32,$(OS)),)
|
||||
LIBS += -lws2_32
|
||||
|
@ -684,6 +684,7 @@ NETPLAY
|
||||
#include "../netplay.c"
|
||||
#include "../libretro-common/net/net_compat.c"
|
||||
#include "../libretro-common/net/net_http.c"
|
||||
#include "../tasks/task_http.c"
|
||||
#endif
|
||||
|
||||
/*============================================================
|
||||
|
193
runloop_data.c
193
runloop_data.c
@ -39,159 +39,6 @@ static void *rarch_main_data_get_ptr(void)
|
||||
return g_data_runloop;
|
||||
}
|
||||
|
||||
#ifdef HAVE_NETWORKING
|
||||
int cb_core_updater_download(void *data_, size_t len);
|
||||
int cb_core_updater_list(void *data_, size_t len);
|
||||
|
||||
/**
|
||||
* rarch_main_data_http_iterate_transfer:
|
||||
*
|
||||
* Resumes HTTP transfer update.
|
||||
*
|
||||
* Returns: 0 when finished, -1 when we should continue
|
||||
* with the transfer on the next frame.
|
||||
**/
|
||||
static int rarch_main_data_http_iterate_transfer(http_handle_t *http)
|
||||
{
|
||||
size_t pos = 0, tot = 0;
|
||||
int percent = 0;
|
||||
if (!net_http_update(http->handle, &pos, &tot))
|
||||
{
|
||||
if(tot != 0)
|
||||
percent=(unsigned long long)pos*100/(unsigned long long)tot;
|
||||
else
|
||||
percent=0;
|
||||
|
||||
if (percent > 0)
|
||||
{
|
||||
snprintf(data_runloop_msg,
|
||||
sizeof(data_runloop_msg), "Download progress: %d%%", percent);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rarch_main_data_http_con_iterate_transfer(http_handle_t *http)
|
||||
{
|
||||
if (!net_http_connection_iterate(http->connection.handle))
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rarch_main_data_http_conn_iterate_transfer_parse(http_handle_t *http)
|
||||
{
|
||||
if (net_http_connection_done(http->connection.handle))
|
||||
{
|
||||
if (http->connection.handle && http->connection.cb)
|
||||
http->connection.cb(http, 0);
|
||||
}
|
||||
|
||||
net_http_connection_free(http->connection.handle);
|
||||
|
||||
http->connection.handle = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rarch_main_data_http_iterate_transfer_parse(http_handle_t *http)
|
||||
{
|
||||
size_t len = 0;
|
||||
char *data = (char*)net_http_data(http->handle, &len, false);
|
||||
|
||||
if (data && http->cb)
|
||||
http->cb(data, len);
|
||||
|
||||
net_http_delete(http->handle);
|
||||
|
||||
http->handle = NULL;
|
||||
msg_queue_clear(http->msg_queue);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cb_http_conn_default(void *data_, size_t len)
|
||||
{
|
||||
http_handle_t *http = (http_handle_t*)data_;
|
||||
|
||||
if (!http)
|
||||
return -1;
|
||||
|
||||
http->handle = net_http_new(http->connection.handle);
|
||||
|
||||
if (!http->handle)
|
||||
{
|
||||
RARCH_ERR("Could not create new HTTP session handle.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
http->cb = NULL;
|
||||
|
||||
if (http->connection.elem1[0] != '\0')
|
||||
{
|
||||
if (!strcmp(http->connection.elem1, "cb_core_updater_download"))
|
||||
http->cb = &cb_core_updater_download;
|
||||
if (!strcmp(http->connection.elem1, "cb_core_updater_list"))
|
||||
http->cb = &cb_core_updater_list;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* rarch_main_data_http_iterate_poll:
|
||||
*
|
||||
* Polls HTTP message queue to see if any new URLs
|
||||
* are pending.
|
||||
*
|
||||
* If handle is freed, will set up a new http handle.
|
||||
* The transfer will be started on the next frame.
|
||||
*
|
||||
* Returns: 0 when an URL has been pulled and we will
|
||||
* begin transferring on the next frame. Returns -1 if
|
||||
* no HTTP URL has been pulled. Do nothing in that case.
|
||||
**/
|
||||
static int rarch_main_data_http_iterate_poll(http_handle_t *http)
|
||||
{
|
||||
char elem0[PATH_MAX_LENGTH];
|
||||
struct string_list *str_list = NULL;
|
||||
const char *url = msg_queue_pull(http->msg_queue);
|
||||
|
||||
if (!url)
|
||||
return -1;
|
||||
|
||||
/* Can only deal with one HTTP transfer at a time for now */
|
||||
if (http->handle)
|
||||
return -1;
|
||||
|
||||
str_list = string_split(url, "|");
|
||||
|
||||
if (!str_list)
|
||||
return -1;
|
||||
|
||||
if (str_list->size > 0)
|
||||
strlcpy(elem0, str_list->elems[0].data, sizeof(elem0));
|
||||
|
||||
http->connection.handle = net_http_connection_new(elem0);
|
||||
|
||||
if (!http->connection.handle)
|
||||
return -1;
|
||||
|
||||
http->connection.cb = &cb_http_conn_default;
|
||||
|
||||
if (str_list->size > 1)
|
||||
strlcpy(http->connection.elem1,
|
||||
str_list->elems[1].data,
|
||||
sizeof(http->connection.elem1));
|
||||
|
||||
string_list_free(str_list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_MENU
|
||||
|
||||
#ifdef HAVE_RPNG
|
||||
@ -644,39 +491,6 @@ static void rarch_main_data_nbio_iterate(bool is_thread, data_runloop_t *runloop
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef HAVE_NETWORKING
|
||||
static void rarch_main_data_http_iterate(bool is_thread, data_runloop_t *runloop)
|
||||
{
|
||||
http_handle_t *http = runloop ? &runloop->http : NULL;
|
||||
if (!http)
|
||||
return;
|
||||
|
||||
switch (http->status)
|
||||
{
|
||||
case HTTP_STATUS_CONNECTION_TRANSFER_PARSE:
|
||||
rarch_main_data_http_conn_iterate_transfer_parse(http);
|
||||
http->status = HTTP_STATUS_TRANSFER;
|
||||
break;
|
||||
case HTTP_STATUS_CONNECTION_TRANSFER:
|
||||
if (!rarch_main_data_http_con_iterate_transfer(http))
|
||||
http->status = HTTP_STATUS_CONNECTION_TRANSFER_PARSE;
|
||||
break;
|
||||
case HTTP_STATUS_TRANSFER_PARSE:
|
||||
rarch_main_data_http_iterate_transfer_parse(http);
|
||||
http->status = HTTP_STATUS_POLL;
|
||||
break;
|
||||
case HTTP_STATUS_TRANSFER:
|
||||
if (!rarch_main_data_http_iterate_transfer(http))
|
||||
http->status = HTTP_STATUS_TRANSFER_PARSE;
|
||||
break;
|
||||
case HTTP_STATUS_POLL:
|
||||
default:
|
||||
if (rarch_main_data_http_iterate_poll(http) == 0)
|
||||
http->status = HTTP_STATUS_CONNECTION_TRANSFER;
|
||||
break;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_OVERLAY
|
||||
static void rarch_main_data_overlay_image_upload_iterate(bool is_thread, data_runloop_t *runloop)
|
||||
@ -813,7 +627,6 @@ static void data_runloop_iterate(bool is_thread, data_runloop_t *runloop)
|
||||
#ifdef HAVE_NETWORKING
|
||||
rarch_main_data_http_iterate (is_thread, runloop);
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_MENU
|
||||
#ifdef HAVE_LIBRETRODB
|
||||
rarch_main_data_db_iterate (is_thread, runloop);
|
||||
@ -1041,7 +854,6 @@ void rarch_main_data_clear_state(void)
|
||||
g_data_runloop = rarch_main_data_new();
|
||||
}
|
||||
|
||||
|
||||
void rarch_main_data_init_queues(void)
|
||||
{
|
||||
data_runloop_t *runloop = (data_runloop_t*)rarch_main_data_get_ptr();
|
||||
@ -1099,3 +911,8 @@ void rarch_main_data_msg_queue_push(unsigned type,
|
||||
msg_queue_clear(queue);
|
||||
msg_queue_push(queue, new_msg, prio, duration);
|
||||
}
|
||||
|
||||
void data_runloop_osd_msg(const char *msg, size_t sizeof_msg)
|
||||
{
|
||||
strlcpy(data_runloop_msg, msg, sizeof_msg);
|
||||
}
|
||||
|
@ -20,9 +20,10 @@
|
||||
#include <formats/image.h>
|
||||
#include <formats/rpng.h>
|
||||
#include <queues/message_queue.h>
|
||||
#ifdef HAVE_NETWORKING
|
||||
#include <net/net_http.h>
|
||||
#ifdef HAVE_THREADS
|
||||
#include <rthreads/rthreads.h>
|
||||
#endif
|
||||
#include "tasks/tasks.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
|
@ -0,0 +1,209 @@
|
||||
/* RetroArch - A frontend for libretro.
|
||||
* Copyright (C) 2011-2015 - Daniel De Matteis
|
||||
*
|
||||
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with RetroArch.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <retro_miscellaneous.h>
|
||||
#include <net/net_http.h>
|
||||
#include <queues/message_queue.h>
|
||||
#include <string/string_list.h>
|
||||
|
||||
#include "../runloop_data.h"
|
||||
#include "tasks.h"
|
||||
|
||||
int cb_core_updater_download(void *data_, size_t len);
|
||||
int cb_core_updater_list(void *data_, size_t len);
|
||||
|
||||
|
||||
static int rarch_main_data_http_con_iterate_transfer(http_handle_t *http)
|
||||
{
|
||||
if (!net_http_connection_iterate(http->connection.handle))
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rarch_main_data_http_conn_iterate_transfer_parse(http_handle_t *http)
|
||||
{
|
||||
if (net_http_connection_done(http->connection.handle))
|
||||
{
|
||||
if (http->connection.handle && http->connection.cb)
|
||||
http->connection.cb(http, 0);
|
||||
}
|
||||
|
||||
net_http_connection_free(http->connection.handle);
|
||||
|
||||
http->connection.handle = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int rarch_main_data_http_iterate_transfer_parse(http_handle_t *http)
|
||||
{
|
||||
size_t len = 0;
|
||||
char *data = (char*)net_http_data(http->handle, &len, false);
|
||||
|
||||
if (data && http->cb)
|
||||
http->cb(data, len);
|
||||
|
||||
net_http_delete(http->handle);
|
||||
|
||||
http->handle = NULL;
|
||||
msg_queue_clear(http->msg_queue);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cb_http_conn_default(void *data_, size_t len)
|
||||
{
|
||||
http_handle_t *http = (http_handle_t*)data_;
|
||||
|
||||
if (!http)
|
||||
return -1;
|
||||
|
||||
http->handle = net_http_new(http->connection.handle);
|
||||
|
||||
if (!http->handle)
|
||||
{
|
||||
RARCH_ERR("Could not create new HTTP session handle.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
http->cb = NULL;
|
||||
|
||||
if (http->connection.elem1[0] != '\0')
|
||||
{
|
||||
if (!strcmp(http->connection.elem1, "cb_core_updater_download"))
|
||||
http->cb = &cb_core_updater_download;
|
||||
if (!strcmp(http->connection.elem1, "cb_core_updater_list"))
|
||||
http->cb = &cb_core_updater_list;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* rarch_main_data_http_iterate_poll:
|
||||
*
|
||||
* Polls HTTP message queue to see if any new URLs
|
||||
* are pending.
|
||||
*
|
||||
* If handle is freed, will set up a new http handle.
|
||||
* The transfer will be started on the next frame.
|
||||
*
|
||||
* Returns: 0 when an URL has been pulled and we will
|
||||
* begin transferring on the next frame. Returns -1 if
|
||||
* no HTTP URL has been pulled. Do nothing in that case.
|
||||
**/
|
||||
static int rarch_main_data_http_iterate_poll(http_handle_t *http)
|
||||
{
|
||||
char elem0[PATH_MAX_LENGTH];
|
||||
struct string_list *str_list = NULL;
|
||||
const char *url = msg_queue_pull(http->msg_queue);
|
||||
|
||||
if (!url)
|
||||
return -1;
|
||||
|
||||
/* Can only deal with one HTTP transfer at a time for now */
|
||||
if (http->handle)
|
||||
return -1;
|
||||
|
||||
str_list = string_split(url, "|");
|
||||
|
||||
if (!str_list)
|
||||
return -1;
|
||||
|
||||
if (str_list->size > 0)
|
||||
strlcpy(elem0, str_list->elems[0].data, sizeof(elem0));
|
||||
|
||||
http->connection.handle = net_http_connection_new(elem0);
|
||||
|
||||
if (!http->connection.handle)
|
||||
return -1;
|
||||
|
||||
http->connection.cb = &cb_http_conn_default;
|
||||
|
||||
if (str_list->size > 1)
|
||||
strlcpy(http->connection.elem1,
|
||||
str_list->elems[1].data,
|
||||
sizeof(http->connection.elem1));
|
||||
|
||||
string_list_free(str_list);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* rarch_main_data_http_iterate_transfer:
|
||||
*
|
||||
* Resumes HTTP transfer update.
|
||||
*
|
||||
* Returns: 0 when finished, -1 when we should continue
|
||||
* with the transfer on the next frame.
|
||||
**/
|
||||
static int rarch_main_data_http_iterate_transfer(void *data)
|
||||
{
|
||||
http_handle_t *http = (http_handle_t*)data;
|
||||
size_t pos = 0, tot = 0;
|
||||
int percent = 0;
|
||||
if (!net_http_update(http->handle, &pos, &tot))
|
||||
{
|
||||
if(tot != 0)
|
||||
percent=(unsigned long long)pos*100/(unsigned long long)tot;
|
||||
else
|
||||
percent=0;
|
||||
|
||||
if (percent > 0)
|
||||
{
|
||||
char tmp[PATH_MAX_LENGTH];
|
||||
snprintf(tmp, sizeof(tmp), "Download progress: %d%%", percent);
|
||||
data_runloop_osd_msg(tmp, sizeof(tmp));
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void rarch_main_data_http_iterate(bool is_thread, void *data)
|
||||
{
|
||||
data_runloop_t *runloop = (data_runloop_t*)data;
|
||||
http_handle_t *http = runloop ? &runloop->http : NULL;
|
||||
if (!http)
|
||||
return;
|
||||
|
||||
switch (http->status)
|
||||
{
|
||||
case HTTP_STATUS_CONNECTION_TRANSFER_PARSE:
|
||||
rarch_main_data_http_conn_iterate_transfer_parse(http);
|
||||
http->status = HTTP_STATUS_TRANSFER;
|
||||
break;
|
||||
case HTTP_STATUS_CONNECTION_TRANSFER:
|
||||
if (!rarch_main_data_http_con_iterate_transfer(http))
|
||||
http->status = HTTP_STATUS_CONNECTION_TRANSFER_PARSE;
|
||||
break;
|
||||
case HTTP_STATUS_TRANSFER_PARSE:
|
||||
rarch_main_data_http_iterate_transfer_parse(http);
|
||||
http->status = HTTP_STATUS_POLL;
|
||||
break;
|
||||
case HTTP_STATUS_TRANSFER:
|
||||
if (!rarch_main_data_http_iterate_transfer(http))
|
||||
http->status = HTTP_STATUS_TRANSFER_PARSE;
|
||||
break;
|
||||
case HTTP_STATUS_POLL:
|
||||
default:
|
||||
if (rarch_main_data_http_iterate_poll(http) == 0)
|
||||
http->status = HTTP_STATUS_CONNECTION_TRANSFER;
|
||||
break;
|
||||
}
|
||||
}
|
46
tasks/tasks.h
Normal file
46
tasks/tasks.h
Normal file
@ -0,0 +1,46 @@
|
||||
/* RetroArch - A frontend for libretro.
|
||||
* Copyright (C) 2011-2015 - Daniel De Matteis
|
||||
*
|
||||
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
||||
* of the GNU General Public License as published by the Free Software Found-
|
||||
* ation, either version 3 of the License, or (at your option) any later version.
|
||||
*
|
||||
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
||||
* PURPOSE. See the GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with RetroArch.
|
||||
* If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef COMMON_TASKS_H
|
||||
#define COMMON_TASKS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <boolean.h>
|
||||
#include "../runloop_data.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_NETWORKING
|
||||
/**
|
||||
* rarch_main_data_http_iterate_transfer:
|
||||
*
|
||||
* Resumes HTTP transfer update.
|
||||
*
|
||||
* Returns: 0 when finished, -1 when we should continue
|
||||
* with the transfer on the next frame.
|
||||
**/
|
||||
void rarch_main_data_http_iterate(bool is_thread,
|
||||
void *data);
|
||||
#endif
|
||||
|
||||
void data_runloop_osd_msg(const char *msg, size_t sizeof_msg);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user