diff --git a/libretro-common/include/net/net_compat.h b/libretro-common/include/net/net_compat.h index 89cf4142c6..e8dd2ef372 100644 --- a/libretro-common/include/net/net_compat.h +++ b/libretro-common/include/net/net_compat.h @@ -217,17 +217,6 @@ int getaddrinfo_retro(const char *node, const char *service, void freeaddrinfo_retro(struct addrinfo *res); -bool socket_nonblock(int fd); - -int socket_close(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_receive_all_blocking(int fd, void *data_, size_t size); - /** * network_init: * diff --git a/libretro-common/include/net/net_socket.h b/libretro-common/include/net/net_socket.h index 871625d633..d0dfac95e4 100644 --- a/libretro-common/include/net/net_socket.h +++ b/libretro-common/include/net/net_socket.h @@ -33,6 +33,17 @@ RETRO_BEGIN_DECLS bool socket_init(void *address, int *fd, uint16_t port, const char *server); +int socket_close(int fd); + +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_receive_all_blocking(int fd, void *data_, size_t size); + RETRO_END_DECLS #endif diff --git a/libretro-common/net/net_compat.c b/libretro-common/net/net_compat.c index db6c644881..20106999b7 100644 --- a/libretro-common/net/net_compat.c +++ b/libretro-common/net/net_compat.c @@ -160,91 +160,6 @@ void freeaddrinfo_retro(struct addrinfo *res) #endif } -bool socket_nonblock(int fd) -{ -#if defined(__CELLOS_LV2__) || defined(VITA) - int i = 1; - setsockopt(fd, SOL_SOCKET, SO_NBIO, &i, sizeof(int)); - return true; -#elif defined(_WIN32) - u_long mode = 1; - return ioctlsocket(fd, FIONBIO, &mode) == 0; -#else - 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); -#elif defined(VITA) - return sceNetSocketClose(fd); -#else - return close(fd); -#endif -} - -int socket_select(int nfds, fd_set *readfs, fd_set *writefds, - fd_set *errorfds, struct timeval *timeout) -{ -#if defined(__CELLOS_LV2__) - return socketselect(nfds, readfs, writefds, errorfds, timeout); -#elif defined(VITA) - SceNetEpollEvent ev = {0}; - - ev.events = PSP2_NET_EPOLLIN | PSP2_NET_EPOLLHUP; - ev.data.fd = nfds; - - if((sceNetEpollControl(retro_epoll_fd, PSP2_NET_EPOLL_CTL_ADD, nfds, &ev))) - { - int ret = sceNetEpollWait(retro_epoll_fd, &ev, 1, 0); - sceNetEpollControl(retro_epoll_fd, PSP2_NET_EPOLL_CTL_DEL, nfds, NULL); - return ret; - } - return 0; -#else - return select(nfds, readfs, writefds, errorfds, timeout); -#endif -} - -int socket_send_all_blocking(int fd, const void *data_, size_t size) -{ - const uint8_t *data = (const uint8_t*)data_; - - while (size) - { - ssize_t ret = send(fd, (const char*)data, size, 0); - if (ret <= 0) - return false; - - data += ret; - size -= ret; - } - - return true; -} - -int socket_receive_all_blocking(int fd, void *data_, size_t size) -{ - const uint8_t *data = (const uint8_t*)data_; - - while (size) - { - ssize_t ret = recv(fd, (char*)data, size, 0); - if (ret <= 0) - return false; - - data += ret; - size -= ret; - } - - return true; -} - /** * network_init: * diff --git a/libretro-common/net/net_socket.c b/libretro-common/net/net_socket.c index 760aff4b99..ce02827a46 100644 --- a/libretro-common/net/net_socket.c +++ b/libretro-common/net/net_socket.c @@ -60,3 +60,88 @@ bool socket_init(void *address, int *fd, uint16_t port, const char *server) error: return false; } + +int socket_receive_all_blocking(int fd, void *data_, size_t size) +{ + const uint8_t *data = (const uint8_t*)data_; + + while (size) + { + ssize_t ret = recv(fd, (char*)data, size, 0); + if (ret <= 0) + return false; + + data += ret; + size -= ret; + } + + return true; +} + +bool socket_nonblock(int fd) +{ +#if defined(__CELLOS_LV2__) || defined(VITA) + int i = 1; + setsockopt(fd, SOL_SOCKET, SO_NBIO, &i, sizeof(int)); + return true; +#elif defined(_WIN32) + u_long mode = 1; + return ioctlsocket(fd, FIONBIO, &mode) == 0; +#else + 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); +#elif defined(VITA) + return sceNetSocketClose(fd); +#else + return close(fd); +#endif +} + +int socket_select(int nfds, fd_set *readfs, fd_set *writefds, + fd_set *errorfds, struct timeval *timeout) +{ +#if defined(__CELLOS_LV2__) + return socketselect(nfds, readfs, writefds, errorfds, timeout); +#elif defined(VITA) + SceNetEpollEvent ev = {0}; + + ev.events = PSP2_NET_EPOLLIN | PSP2_NET_EPOLLHUP; + ev.data.fd = nfds; + + if((sceNetEpollControl(retro_epoll_fd, PSP2_NET_EPOLL_CTL_ADD, nfds, &ev))) + { + int ret = sceNetEpollWait(retro_epoll_fd, &ev, 1, 0); + sceNetEpollControl(retro_epoll_fd, PSP2_NET_EPOLL_CTL_DEL, nfds, NULL); + return ret; + } + return 0; +#else + return select(nfds, readfs, writefds, errorfds, timeout); +#endif +} + +int socket_send_all_blocking(int fd, const void *data_, size_t size) +{ + const uint8_t *data = (const uint8_t*)data_; + + while (size) + { + ssize_t ret = send(fd, (const char*)data, size, 0); + if (ret <= 0) + return false; + + data += ret; + size -= ret; + } + + return true; +}