mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-06 10:27:22 +09:00
Make use of poll() and add a poll-emulation for win32.
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@772 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
@@ -36,22 +36,6 @@
|
||||
#endif
|
||||
#include "libssh/priv.h"
|
||||
|
||||
#if !defined(HAVE_SELECT) && !defined(HAVE_POLL)
|
||||
#error Your system must have either select() or poll()
|
||||
#endif
|
||||
|
||||
#if !defined(HAVE_POLL) && !defined(_WIN32)
|
||||
#warning your system does not have poll. Select has known limitations
|
||||
#define SELECT_LIMIT_CHECK
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_POLL
|
||||
#define USE_POLL
|
||||
#include <poll.h>
|
||||
#else
|
||||
#define USE_SELECT
|
||||
#endif
|
||||
|
||||
/** \defgroup ssh_socket SSH Sockets
|
||||
* \addtogroup ssh_socket
|
||||
* @{
|
||||
@@ -472,86 +456,10 @@ int ssh_socket_wait_for_data(struct socket *s, SSH_SESSION *session, u32 len) {
|
||||
return SSH_OK;
|
||||
}
|
||||
|
||||
#ifdef USE_SELECT
|
||||
/* ssh_socket_poll, select() version */
|
||||
|
||||
/* \internal
|
||||
* \brief polls the socket for data
|
||||
* \param session ssh session
|
||||
* \param writeable value pointed to set to 1 if it is possible to write
|
||||
* \param except value pointed to set to 1 if there is an exception
|
||||
* \return 1 if it is possible to read, 0 otherwise, -1 on error
|
||||
*/
|
||||
/* ssh_socket_poll */
|
||||
int ssh_socket_poll(struct socket *s, int *writeable, int *except) {
|
||||
SSH_SESSION *session = s->session;
|
||||
struct timeval sometime;
|
||||
fd_set rdes; // read set
|
||||
fd_set wdes; // writing set
|
||||
fd_set edes; // exception set
|
||||
int fdmax =- 1;
|
||||
|
||||
enter_function();
|
||||
|
||||
FD_ZERO(&rdes);
|
||||
FD_ZERO(&wdes);
|
||||
FD_ZERO(&edes);
|
||||
|
||||
if (!ssh_socket_is_open(s)) {
|
||||
*except = 1;
|
||||
*writeable = 0;
|
||||
return 0;
|
||||
}
|
||||
#ifdef SELECT_LIMIT_CHECK
|
||||
// some systems don't handle the fds > FD_SETSIZE
|
||||
if(s->fd > FD_SETSIZE){
|
||||
ssh_set_error(session, SSH_REQUEST_DENIED,
|
||||
"File descriptor out of range for select: %d", s->fd);
|
||||
|
||||
leave_function();
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
if (!s->data_to_read) {
|
||||
ssh_socket_fd_set(s, &rdes, &fdmax);
|
||||
}
|
||||
if (!s->data_to_write) {
|
||||
ssh_socket_fd_set(s, &wdes, &fdmax);
|
||||
}
|
||||
ssh_socket_fd_set(s, &edes, &fdmax);
|
||||
|
||||
/* Set to return immediately (no blocking) */
|
||||
sometime.tv_sec = 0;
|
||||
sometime.tv_usec = 0;
|
||||
|
||||
/* Make the call, and listen for errors */
|
||||
if (select(fdmax, &rdes, &wdes, &edes, &sometime) < 0) {
|
||||
ssh_set_error(session, SSH_FATAL, "select(): %s", strerror(errno));
|
||||
leave_function();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!s->data_to_read) {
|
||||
s->data_to_read = ssh_socket_fd_isset(s, &rdes);
|
||||
}
|
||||
if (!s->data_to_write) {
|
||||
s->data_to_write = ssh_socket_fd_isset(s, &wdes);
|
||||
}
|
||||
if (!s->data_except) {
|
||||
s->data_except = ssh_socket_fd_isset(s, &edes);
|
||||
}
|
||||
*except = s->data_except;
|
||||
*writeable = s->data_to_write;
|
||||
|
||||
leave_function();
|
||||
return (s->data_to_read || (buffer_get_rest_len(s->in_buffer) > 0));
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_POLL
|
||||
/* ssh_socket_poll, poll() version */
|
||||
int ssh_socket_poll(struct socket *s, int *writeable, int *except) {
|
||||
SSH_SESSION *session = s->session;
|
||||
struct pollfd fd[1];
|
||||
pollfd_t fd[1];
|
||||
int rc = -1;
|
||||
|
||||
enter_function();
|
||||
@@ -573,7 +481,7 @@ int ssh_socket_poll(struct socket *s, int *writeable, int *except) {
|
||||
}
|
||||
|
||||
/* Make the call, and listen for errors */
|
||||
rc = poll(fd, 1, 0);
|
||||
rc = ssh_poll(fd, 1, 0);
|
||||
if (rc < 0) {
|
||||
ssh_set_error(session, SSH_FATAL, "poll(): %s", strerror(errno));
|
||||
leave_function();
|
||||
@@ -596,7 +504,6 @@ int ssh_socket_poll(struct socket *s, int *writeable, int *except) {
|
||||
leave_function();
|
||||
return (s->data_to_read || (buffer_get_rest_len(s->in_buffer) > 0));
|
||||
}
|
||||
#endif
|
||||
|
||||
/** \internal
|
||||
* \brief nonblocking flush of the output buffer
|
||||
|
||||
Reference in New Issue
Block a user