Move socket code to net_socket.c

This commit is contained in:
twinaphex 2016-05-01 21:36:28 +02:00
parent a7b856b65f
commit 0268212d40
4 changed files with 96 additions and 96 deletions

View File

@ -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:
*

View File

@ -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

View File

@ -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:
*

View File

@ -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;
}