INT32 SSL_DataAvailable( SOCK_SOCKET socket ) { SOCK_fd_set fdSock; SOCK_timeval to; INT32 avail = 0; void *sslData = g_Sockets_Driver.GetSocketSslData(socket); if(sslData == NULL || sslData == (void*)SSL_SOCKET_ATTEMPTED_CONNECT) return -1; to.tv_sec = 0; to.tv_usec = 0; fdSock.fd_count= 1; fdSock.fd_array[0] = socket; // WARNING!!!! secure_read_pending will block if there is no data in the queue if(SOCK_select(SOCK_FD_SETSIZE, &fdSock, NULL, NULL, &to) > 0) { avail = secure_read_pending( sslData ); // the socket is selected, so there must be some data (maybe a close socket) if(avail == 0) avail = 1; } return avail; }
CLR_INT32 Library_spot_net_native_Microsoft_SPOT_Net_SocketNative::Helper__SelectSocket(CLR_INT32 handle, CLR_INT32 mode ) { struct SOCK_timeval timeval; SOCK_fd_set* readfds = NULL; SOCK_fd_set* writefds = NULL; SOCK_fd_set* exceptfds = NULL; SOCK_fd_set fds; SOCK_fd_set fdsExcept; CLR_INT32 res = 0; switch(mode) { case 0: readfds = &fds; break; case 1: writefds = &fds; break; default: _ASSERTE(FALSE); // fall through case 2: exceptfds = &fds; break; } fds.fd_count = 1; fds.fd_array[ 0 ] = handle; // This Poll method is a little handicapped in the sense that it only allows the caller to wait // for a read, a write or an except. This causes a problem when there is a socket exception. The // poll will continue to block forever because the select statement wasn't looking for exceptions // Therefore, we will force the select call to look for the except case if it is not already doing it. if(exceptfds == NULL) { fdsExcept.fd_count = 1; fdsExcept.fd_array[ 0 ] = handle; exceptfds = &fdsExcept; } timeval.tv_sec = 0; timeval.tv_usec = 0; res = SOCK_select( 1, readfds, writefds, exceptfds, &timeval ); // socket is in the exception state (only return error if caller was NOT looking for the excepted state) if((mode != 2) && fdsExcept.fd_count != 0) return SOCK_SOCKET_ERROR; return res; }