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 = SCE_NET_EPOLLIN | SCE_NET_EPOLLHUP; ev.data.fd = nfds; if((sceNetEpollControl(retro_epoll_fd, SCE_NET_EPOLL_CTL_ADD, nfds, &ev))) { int ret = sceNetEpollWait(retro_epoll_fd, &ev, 1, 0); sceNetEpollControl(retro_epoll_fd, SCE_NET_EPOLL_CTL_DEL, nfds, NULL); return ret; } return 0; #else return select(nfds, readfs, writefds, errorfds, timeout); #endif }
int socket_waitfd(p_socket ps, int sw, p_timeout tm) { /*fd_set rfds, wfds, *rp, *wp;*/ /*struct timeval tv, *tp;*/ double t; SceNetId eid = -1; SceNetEpollEvent ev; int nevents; SceNetEpollEvent events[MAX_EVENTS]; int ret; if (timeout_iszero(tm)) return IO_TIMEOUT; /* optimize timeout == 0 case */ do { eid = sceNetEpollCreate("waitfd", 0); if (eid < 0) { printf("sceNetEpollCreate() failed (errno=%d)\n", peusock_errno); return sce_net_errno; } /* must set bits within loop, because select may have modifed them */ memset(&ev, 0, sizeof(ev)); if (sw & WAITFD_R) ev.events = SCE_NET_EPOLLIN; if (sw & WAITFD_W) ev.events = ev.events | SCE_NET_EPOLLOUT; ev.data.ext.id = *ps; ret = sceNetEpollControl(eid, SCE_NET_EPOLL_CTL_ADD, *ps, &ev); if (ret < 0) { printf("sceNetEpollControl(ADD) failed (errno=%d)\n", sce_net_errno); return sce_net_errno; } /*do stuff*/ t = timeout_getretry(tm); /* time left for the call*/ /* tp = NULL; if (t >= 0.0) { tv.tv_sec = (int)t; tv.tv_usec = (int)((t-tv.tv_sec)*1.0e6); tp = &tv; } */ /*ret = socketselect(*ps+1, rp, wp, NULL, tp);*/ nevents = sceNetEpollWait(eid, events, MAX_EVENTS, t >= 0? t: -1); /* reference code for deleting events from epoll */ #if 0 /* delete SCE_NET_EPOLLOUT */ memset(&ev, 0, sizeof(ev)); ev.events = SCE_NET_EPOLLIN; ev.data.ext.id = s; ret = sceNetEpollControl(eid, SCE_NET_EPOLL_CTL_MOD, *ps, &ev); if (ret < 0) { printf("sceNetEpollControl(MOD) failed (errno=%d)\n", sce_net_errno); return sce_net_errno; } #endif /* destroy epoll object*/ sceNetEpollDestroy(eid); } while (nevents == -1 && peusock_errno == peusock_EINTR); if (nevents == -1) return peusock_errno; if (nevents == 0) return IO_TIMEOUT; /*if (sw == WAITFD_C && FD_ISSET(*ps, &rfds)) return IO_CLOSED;*/ return IO_DONE; }
/* * Read at most 'len' characters, blocking for at most 'timeout' ms */ int mbedtls_net_recv_timeout( void *ctx, unsigned char *buf, size_t len, uint32_t timeout ) { int ret; struct timeval tv; fd_set read_fds; int fd = ((mbedtls_net_context *) ctx)->fd; if( fd < 0 ) return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); FD_ZERO( &read_fds ); FD_SET( fd, &read_fds ); tv.tv_sec = timeout / 1000; tv.tv_usec = ( timeout % 1000 ) * 1000; #if defined(__CELLOS_LV2__) ret = socketselect(fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv); #elif defined(VITA) extern int retro_epoll_fd; SceNetEpollEvent ev = {0}; ev.events = SCE_NET_EPOLLIN | SCE_NET_EPOLLHUP; ev.data.fd = fd + 1; if((sceNetEpollControl(retro_epoll_fd, SCE_NET_EPOLL_CTL_ADD, fd + 1, &ev))) { int ret = sceNetEpollWait(retro_epoll_fd, &ev, 1, 0); sceNetEpollControl(retro_epoll_fd, SCE_NET_EPOLL_CTL_DEL, fd + 1, NULL); #else ret = select( fd + 1, &read_fds, NULL, NULL, timeout == 0 ? NULL : &tv ); #endif /* Zero fds ready means we timed out */ if( ret == 0 ) return( MBEDTLS_ERR_SSL_TIMEOUT ); if( ret < 0 ) { #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ !defined(EFI32) if( WSAGetLastError() == WSAEINTR ) return( MBEDTLS_ERR_SSL_WANT_READ ); #else if( errno == EINTR ) return( MBEDTLS_ERR_SSL_WANT_READ ); #endif return( MBEDTLS_ERR_NET_RECV_FAILED ); } /* This call will not block */ return( mbedtls_net_recv( ctx, buf, len ) ); } /* * Write at most 'len' characters */ int mbedtls_net_send( void *ctx, const unsigned char *buf, size_t len ) { int ret; int fd = ((mbedtls_net_context *) ctx)->fd; if( fd < 0 ) return( MBEDTLS_ERR_NET_INVALID_CONTEXT ); ret = (int) write( fd, buf, len ); if( ret < 0 ) { if( net_would_block( ctx ) != 0 ) return( MBEDTLS_ERR_SSL_WANT_WRITE ); #if ( defined(_WIN32) || defined(_WIN32_WCE) ) && !defined(EFIX64) && \ !defined(EFI32) if( WSAGetLastError() == WSAECONNRESET ) return( MBEDTLS_ERR_NET_CONN_RESET ); #else if( errno == EPIPE || errno == ECONNRESET ) return( MBEDTLS_ERR_NET_CONN_RESET ); if( errno == EINTR ) return( MBEDTLS_ERR_SSL_WANT_WRITE ); #endif return( MBEDTLS_ERR_NET_SEND_FAILED ); } return( ret ); }