From 31f3fc0abb9df4939b9d8e5a76cfc776d136e7d3 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Mon, 2 May 2016 02:09:22 +0200 Subject: [PATCH] Create socket_receive_all_nonblocking --- libretro-common/include/net/net_socket.h | 3 ++ libretro-common/net/net_http.c | 41 ++++++++---------------- libretro-common/net/net_socket.c | 19 +++++++++++ 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/libretro-common/include/net/net_socket.h b/libretro-common/include/net/net_socket.h index 31960cd96e..a1ab722fa3 100644 --- a/libretro-common/include/net/net_socket.h +++ b/libretro-common/include/net/net_socket.h @@ -50,6 +50,9 @@ int socket_send_all_blocking(int fd, const void *data_, size_t size, bool no_sig int socket_receive_all_blocking(int fd, void *data_, size_t size); +ssize_t socket_receive_all_nonblocking(int fd, bool *error, + void *data_, size_t size); + bool socket_bind(int fd, void *data); int socket_connect(int fd, void *data, bool timeout_enable); diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index 350fc30015..e1bca08d30 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -105,33 +105,12 @@ static void net_http_send_str(int fd, bool *error, const char *text) } } -static ssize_t net_http_recv(int fd, bool *error, - uint8_t *data, size_t maxlen) -{ - ssize_t bytes; - - if (*error) - return -1; - - bytes = recv(fd, (char*)data, maxlen, 0); - - if (bytes > 0) - return bytes; - else if (bytes == 0) - return -1; - else if (isagain(bytes)) - return 0; - - *error=true; - return -1; -} - static char* urlencode(const char* url) { unsigned i; unsigned outpos = 0; unsigned outlen = 0; - char *ret = NULL; + char *ret = NULL; for (i = 0; url[i] != '\0'; i++) { @@ -326,8 +305,11 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total) if (state->part < P_BODY) { - newlen = net_http_recv(state->fd, &state->error, - (uint8_t*)state->data + state->pos, state->buflen - state->pos); + if (state->error) + newlen = -1; + else + newlen = socket_receive_all_nonblocking(state->fd, &state->error, + (uint8_t*)state->data + state->pos, state->buflen - state->pos); if (newlen < 0) goto fail; @@ -392,9 +374,14 @@ bool net_http_update(struct http_t *state, size_t* progress, size_t* total) { if (!newlen) { - newlen = net_http_recv(state->fd, &state->error, - (uint8_t*)state->data + state->pos, - state->buflen - state->pos); + if (state->error) + newlen = -1; + else + newlen = socket_receive_all_nonblocking( + state->fd, + &state->error, + (uint8_t*)state->data + state->pos, + state->buflen - state->pos); if (newlen < 0) { diff --git a/libretro-common/net/net_socket.c b/libretro-common/net/net_socket.c index b1e827775d..22e07fb2f9 100644 --- a/libretro-common/net/net_socket.c +++ b/libretro-common/net/net_socket.c @@ -70,6 +70,25 @@ error: return -1; } +ssize_t socket_receive_all_nonblocking(int fd, bool *error, + void *data_, size_t size) +{ + const uint8_t *data = (const uint8_t*)data_; + ssize_t ret = recv(fd, (char*)data, size, 0); + + if (ret > 0) + return ret; + + if (ret == 0) + return -1; + + if (isagain(ret)) + return 0; + + *error = true; + return -1; +} + int socket_receive_all_blocking(int fd, void *data_, size_t size) { const uint8_t *data = (const uint8_t*)data_;