From bd9dd06dddb19a83eb446284fc321945b1ce89be Mon Sep 17 00:00:00 2001 From: twinaphex Date: Sun, 1 May 2016 23:17:17 +0200 Subject: [PATCH] Get rid of duplicate function net_http_send --- libretro-common/include/net/net_socket.h | 2 +- libretro-common/net/net_http.c | 32 +++++------------------- libretro-common/net/net_socket.c | 11 ++++++-- netplay/netplay.c | 8 +++--- netplay/netplay_spectate.c | 5 ++-- 5 files changed, 23 insertions(+), 35 deletions(-) diff --git a/libretro-common/include/net/net_socket.h b/libretro-common/include/net/net_socket.h index d5929ff0ea..15b0785a14 100644 --- a/libretro-common/include/net/net_socket.h +++ b/libretro-common/include/net/net_socket.h @@ -46,7 +46,7 @@ bool socket_nonblock(int fd); int socket_select(int nfds, fd_set *readfs, fd_set *writefds, fd_set *errorfds, struct timeval *timeout); -int socket_send_all_blocking(int fd, const void *data_, size_t size); +int socket_send_all_blocking(int fd, const void *data_, size_t size, bool no_signal); int socket_receive_all_blocking(int fd, void *data_, size_t size); diff --git a/libretro-common/net/net_http.c b/libretro-common/net/net_http.c index 7052520b17..df0eb39d26 100644 --- a/libretro-common/net/net_http.c +++ b/libretro-common/net/net_http.c @@ -109,36 +109,16 @@ error: return -1; } -static void net_http_send(int fd, bool * error, - const char * data, size_t len) -{ - if (*error) - return; - - while (len) - { - ssize_t thislen = send(fd, data, len, MSG_NOSIGNAL); - - if (thislen <= 0) - { - if (!isagain(thislen)) - continue; - - *error=true; - return; - } - - data += thislen; - len -= thislen; - } -} - static void net_http_send_str(int fd, bool *error, const char *text) { #if 0 printf("%s",text); #endif - net_http_send(fd, error, text, strlen(text)); + if (!*error) + { + if (!socket_send_all_blocking(fd, text, strlen(text), true)) + *error = true; + } } static ssize_t net_http_recv(int fd, bool *error, @@ -298,7 +278,7 @@ struct http_t *net_http_new(struct http_connection_t *conn) if (fd == -1) goto error; - error=false; + error = false; /* This is a bit lazy, but it works. */ net_http_send_str(fd, &error, "GET /"); diff --git a/libretro-common/net/net_socket.c b/libretro-common/net/net_socket.c index e236d915c6..45f7724276 100644 --- a/libretro-common/net/net_socket.c +++ b/libretro-common/net/net_socket.c @@ -138,15 +138,22 @@ int socket_select(int nfds, fd_set *readfs, fd_set *writefds, #endif } -int socket_send_all_blocking(int fd, const void *data_, size_t size) +int socket_send_all_blocking(int fd, const void *data_, size_t size, + bool no_signal) { const uint8_t *data = (const uint8_t*)data_; while (size) { - ssize_t ret = send(fd, (const char*)data, size, 0); + ssize_t ret = send(fd, (const char*)data, size, + no_signal ? MSG_NOSIGNAL : 0); if (ret <= 0) + { + if (!isagain(ret)) + continue; + return false; + } data += ret; size -= ret; diff --git a/netplay/netplay.c b/netplay/netplay.c index bed296d9f5..b6fc9feb96 100644 --- a/netplay/netplay.c +++ b/netplay/netplay.c @@ -200,13 +200,13 @@ static bool get_self_input_state(netplay_t *netplay) static bool netplay_cmd_ack(netplay_t *netplay) { uint32_t cmd = htonl(NETPLAY_CMD_ACK); - return socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd)); + return socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd), false); } static bool netplay_cmd_nak(netplay_t *netplay) { uint32_t cmd = htonl(NETPLAY_CMD_NAK); - return socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd)); + return socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd), false); } static bool netplay_get_response(netplay_t *netplay) @@ -866,10 +866,10 @@ static bool netplay_send_raw_cmd(netplay_t *netplay, uint32_t cmd, cmd = (cmd << 16) | (size & 0xffff); cmd = htonl(cmd); - if (!socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd))) + if (!socket_send_all_blocking(netplay->fd, &cmd, sizeof(cmd), false)) return false; - if (!socket_send_all_blocking(netplay->fd, data, size)) + if (!socket_send_all_blocking(netplay->fd, data, size, false)) return false; return true; diff --git a/netplay/netplay_spectate.c b/netplay/netplay_spectate.c index b6d4411259..d257c61212 100644 --- a/netplay/netplay_spectate.c +++ b/netplay/netplay_spectate.c @@ -106,7 +106,7 @@ static void netplay_spectate_pre_frame(netplay_t *netplay) setsockopt(new_fd, SOL_SOCKET, SO_SNDBUF, (const char*)&bufsize, sizeof(int)); - if (!socket_send_all_blocking(new_fd, header, header_size)) + if (!socket_send_all_blocking(new_fd, header, header_size, false)) { RARCH_ERR("Failed to send header to client.\n"); socket_close(new_fd); @@ -145,7 +145,8 @@ static void netplay_spectate_post_frame(netplay_t *netplay) if (socket_send_all_blocking(netplay->spectate.fds[i], netplay->spectate.input, - netplay->spectate.input_ptr * sizeof(int16_t))) + netplay->spectate.input_ptr * sizeof(int16_t), + false)) continue; RARCH_LOG("Client (#%u) disconnected ...\n", i);