From cec500ece58a497fa1a0c26649ba7320ad9ae22e Mon Sep 17 00:00:00 2001 From: twinaphex Date: Fri, 23 Jan 2015 11:36:45 +0100 Subject: [PATCH] Create socket_close in netplay_compat.c --- command.c | 6 +++--- net_http.c | 8 ++++---- netplay.c | 28 ++++++++++++++-------------- netplay_compat.c | 12 ++++++++++++ netplay_compat.h | 11 +++-------- 5 files changed, 36 insertions(+), 29 deletions(-) diff --git a/command.c b/command.c index 4b79a44144..799b6b3553 100644 --- a/command.c +++ b/command.c @@ -153,7 +153,7 @@ void rarch_cmd_free(rarch_cmd_t *handle) { #if defined(HAVE_NETWORK_CMD) && defined(HAVE_NETPLAY) if (handle && handle->net_fd >= 0) - close(handle->net_fd); + socket_close(handle->net_fd); #endif free(handle); @@ -564,7 +564,7 @@ static bool send_udp_packet(const char *host, goto end; } - close(fd); + socket_close(fd); fd = -1; tmp = tmp->ai_next; } @@ -572,7 +572,7 @@ static bool send_udp_packet(const char *host, end: freeaddrinfo_rarch(res); if (fd >= 0) - close(fd); + socket_close(fd); return ret; } diff --git a/net_http.c b/net_http.c index 554d5a0810..add65c5c54 100644 --- a/net_http.c +++ b/net_http.c @@ -110,7 +110,7 @@ static int net_http_new_socket(const char * domain, int port) if (connect(fd, addr->ai_addr, addr->ai_addrlen) != 0) { freeaddrinfo_rarch(addr); - close(fd); + socket_close(fd); return -1; } @@ -118,7 +118,7 @@ static int net_http_new_socket(const char * domain, int port) if (!socket_nonblock(fd)) { - close(fd); + socket_close(fd); return -1; } @@ -243,7 +243,7 @@ http_t *net_http_new(const char * url) fail: if (fd != -1) - close(fd); + socket_close(fd); free(urlcopy); return NULL; } @@ -467,7 +467,7 @@ uint8_t* net_http_data(http_t *state, size_t* len, bool accept_error) void net_http_delete(http_t *state) { if (state->fd != -1) - close(state->fd); + socket_close(state->fd); if (state->data) free(state->data); free(state); diff --git a/netplay.c b/netplay.c index 831d403895..5e041608bb 100644 --- a/netplay.c +++ b/netplay.c @@ -686,7 +686,7 @@ static int init_tcp_connection(const struct addrinfo *res, goto end; } - close(fd); + socket_close(fd); fd = new_fd; } } @@ -694,7 +694,7 @@ static int init_tcp_connection(const struct addrinfo *res, end: if (!ret && fd >= 0) { - close(fd); + socket_close(fd); fd = -1; } @@ -801,7 +801,7 @@ static bool init_udp_socket(netplay_t *netplay, const char *server, netplay->addr->ai_addrlen) < 0) { RARCH_ERR("Failed to bind socket.\n"); - close(netplay->udp_fd); + socket_close(netplay->udp_fd); netplay->udp_fd = -1; } @@ -1286,9 +1286,9 @@ netplay_t *netplay_new(const char *server, uint16_t port, error: if (netplay->fd >= 0) - close(netplay->fd); + socket_close(netplay->fd); if (netplay->udp_fd >= 0) - close(netplay->udp_fd); + socket_close(netplay->udp_fd); free(netplay); return NULL; @@ -1374,19 +1374,19 @@ void netplay_free(netplay_t *netplay) { unsigned i; - close(netplay->fd); + socket_close(netplay->fd); if (netplay->spectate) { for (i = 0; i < MAX_SPECTATORS; i++) if (netplay->spectate_fds[i] >= 0) - close(netplay->spectate_fds[i]); + socket_close(netplay->spectate_fds[i]); free(netplay->spectate_input); } else { - close(netplay->udp_fd); + socket_close(netplay->udp_fd); for (i = 0; i < netplay->buffer_size; i++) free(netplay->buffer[i].state); @@ -1512,21 +1512,21 @@ static void netplay_pre_frame_spectate(netplay_t *netplay) /* No vacant client streams :( */ if (idx == -1) { - close(new_fd); + socket_close(new_fd); return; } if (!get_nickname(netplay, new_fd)) { RARCH_ERR("Failed to get nickname from client.\n"); - close(new_fd); + socket_close(new_fd); return; } if (!send_nickname(netplay, new_fd)) { RARCH_ERR("Failed to send nickname to client.\n"); - close(new_fd); + socket_close(new_fd); return; } @@ -1536,7 +1536,7 @@ static void netplay_pre_frame_spectate(netplay_t *netplay) if (!header) { RARCH_ERR("Failed to generate BSV header.\n"); - close(new_fd); + socket_close(new_fd); return; } @@ -1547,7 +1547,7 @@ static void netplay_pre_frame_spectate(netplay_t *netplay) if (!send_all(new_fd, header, header_size)) { RARCH_ERR("Failed to send header to client.\n"); - close(new_fd); + socket_close(new_fd); free(header); return; } @@ -1668,7 +1668,7 @@ static void netplay_post_frame_spectate(netplay_t *netplay) snprintf(msg, sizeof(msg), "Client (#%u) disconnected.", i); msg_queue_push(g_extern.msg_queue, msg, 1, 180); - close(netplay->spectate_fds[i]); + socket_close(netplay->spectate_fds[i]); netplay->spectate_fds[i] = -1; break; } diff --git a/netplay_compat.c b/netplay_compat.c index 19f6e5867b..fe322e305a 100644 --- a/netplay_compat.c +++ b/netplay_compat.c @@ -98,3 +98,15 @@ bool socket_nonblock(int fd) return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == 0; #endif } + +int socket_close(int fd) +{ +#if defined(_WIN32) && !defined(_XBOX360) + /* WinSock has headers from the stone age. */ + return closesocket(fd); +#elif defined(__CELLOS_LV2__) + return socketclose(fd); +#else + return close(fd); +#endif +} diff --git a/netplay_compat.h b/netplay_compat.h index 0f318184d4..6814311f7b 100644 --- a/netplay_compat.h +++ b/netplay_compat.h @@ -77,18 +77,11 @@ #define MSG_NOSIGNAL 0 #endif -#if defined(_WIN32) -/* Woohoo, Winsock has headers - * from the STONE AGE. :D */ -#ifndef _XBOX360 -#define close(x) closesocket(x) -#endif -#else +#ifndef _WIN32 #include #include #if defined(__CELLOS_LV2__) -#define close(x) socketclose(x) #define select(nfds, readfds, writefds, errorfds, timeout) socketselect(nfds, readfds, writefds, errorfds, timeout) #endif #endif @@ -130,5 +123,7 @@ void freeaddrinfo_rarch(struct addrinfo *res); bool socket_nonblock(int fd); +int socket_close(int fd); + #endif