GUACAMOLE-118: Merge switchover from select to poll.

This commit is contained in:
James Muehlner 2016-11-11 16:19:54 -08:00
commit 7c3430ba0f
7 changed files with 104 additions and 115 deletions

View File

@ -21,8 +21,8 @@
#include "socket-ssl.h" #include "socket-ssl.h"
#include <poll.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/select.h>
#include <guacamole/error.h> #include <guacamole/error.h>
#include <guacamole/socket.h> #include <guacamole/socket.h>
@ -70,24 +70,22 @@ static int __guac_socket_ssl_select_handler(guac_socket* socket, int usec_timeou
guac_socket_ssl_data* data = (guac_socket_ssl_data*) socket->data; guac_socket_ssl_data* data = (guac_socket_ssl_data*) socket->data;
fd_set fds;
struct timeval timeout;
int retval; int retval;
/* Initialize fd_set with single underlying file descriptor */ /* Initialize with single underlying file descriptor */
FD_ZERO(&fds); struct pollfd fds[1] = {{
FD_SET(data->fd, &fds); .fd = data->fd,
.events = POLLIN,
.revents = 0,
}};
/* No timeout if usec_timeout is negative */ /* No timeout if usec_timeout is negative */
if (usec_timeout < 0) if (usec_timeout < 0)
retval = select(data->fd + 1, &fds, NULL, NULL, NULL); retval = poll(fds, 1, -1);
/* Handle timeout if specified */ /* Handle timeout if specified, rounding up to poll()'s granularity */
else { else
timeout.tv_sec = usec_timeout/1000000; retval = poll(fds, 1, (usec_timeout + 999) / 1000);
timeout.tv_usec = usec_timeout%1000000;
retval = select(data->fd + 1, &fds, NULL, NULL, &timeout);
}
/* Properly set guac_error */ /* Properly set guac_error */
if (retval < 0) { if (retval < 0) {

View File

@ -22,6 +22,7 @@
#include "error.h" #include "error.h"
#include "socket.h" #include "socket.h"
#include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
@ -30,12 +31,6 @@
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#ifdef __MINGW32__
#include <winsock2.h>
#else
#include <sys/select.h>
#endif
/** /**
* Data associated with an open socket which writes to a file descriptor. * Data associated with an open socket which writes to a file descriptor.
*/ */
@ -337,24 +332,22 @@ static int guac_socket_fd_select_handler(guac_socket* socket,
guac_socket_fd_data* data = (guac_socket_fd_data*) socket->data; guac_socket_fd_data* data = (guac_socket_fd_data*) socket->data;
fd_set fds;
struct timeval timeout;
int retval; int retval;
/* Initialize fd_set with single underlying file descriptor */ /* Initialize with single underlying file descriptor */
FD_ZERO(&fds); struct pollfd fds[1] = {{
FD_SET(data->fd, &fds); .fd = data->fd,
.events = POLLIN,
.revents = 0,
}};
/* No timeout if usec_timeout is negative */ /* No timeout if usec_timeout is negative */
if (usec_timeout < 0) if (usec_timeout < 0)
retval = select(data->fd + 1, &fds, NULL, NULL, NULL); retval = poll(fds, 1, -1);
/* Handle timeout if specified */ /* Handle timeout if specified, rounding up to poll()'s granularity */
else { else
timeout.tv_sec = usec_timeout/1000000; retval = poll(fds, 1, (usec_timeout + 999) / 1000);
timeout.tv_usec = usec_timeout%1000000;
retval = select(data->fd + 1, &fds, NULL, NULL, &timeout);
}
/* Properly set guac_error */ /* Properly set guac_error */
if (retval < 0) { if (retval < 0) {

View File

@ -38,12 +38,12 @@
/** /**
* The amount of time to wait for a new message from the RDP server when * The amount of time to wait for a new message from the RDP server when
* beginning a new frame. This value must be kept reasonably small such that * beginning a new frame, in milliseconds. This value must be kept reasonably
* a slow RDP server will not prevent external events from being handled (such * small such that a slow RDP server will not prevent external events from
* as the stop signal from guac_client_stop()), but large enough that the * being handled (such as the stop signal from guac_client_stop()), but large
* message handling loop does not eat up CPU spinning. * enough that the message handling loop does not eat up CPU spinning.
*/ */
#define GUAC_RDP_FRAME_START_TIMEOUT 250000 #define GUAC_RDP_FRAME_START_TIMEOUT 250
/** /**
* The native resolution of most RDP connections. As Windows and other systems * The native resolution of most RDP connections. As Windows and other systems
@ -89,6 +89,12 @@
*/ */
#define GUAC_RDP_AUDIO_BPS 16 #define GUAC_RDP_AUDIO_BPS 16
/**
* The maximum number of file descriptors which can be associated with an RDP
* connection.
*/
#define GUAC_RDP_MAX_FILE_DESCRIPTORS 32
/** /**
* Handler which frees all data associated with the guac_client. * Handler which frees all data associated with the guac_client.
*/ */

View File

@ -97,10 +97,10 @@
#endif #endif
#include <errno.h> #include <errno.h>
#include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/select.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
@ -545,20 +545,20 @@ static void rdp_freerdp_context_free(freerdp* instance, rdpContext* context) {
} }
/** /**
* Waits for messages from the RDP server for the given number of microseconds. * Waits for messages from the RDP server for the given number of milliseconds.
* *
* @param client * @param client
* The client associated with the current RDP session. * The client associated with the current RDP session.
* *
* @param timeout_usecs * @param timeout_msecs
* The maximum amount of time to wait, in microseconds. * The maximum amount of time to wait, in milliseconds.
* *
* @return * @return
* A positive value if messages are ready, zero if the specified timeout * A positive value if messages are ready, zero if the specified timeout
* period elapsed, or a negative value if an error occurs. * period elapsed, or a negative value if an error occurs.
*/ */
static int rdp_guac_client_wait_for_messages(guac_client* client, static int rdp_guac_client_wait_for_messages(guac_client* client,
int timeout_usecs) { int timeout_msecs) {
guac_rdp_client* rdp_client = (guac_rdp_client*) client->data; guac_rdp_client* rdp_client = (guac_rdp_client*) client->data;
freerdp* rdp_inst = rdp_client->rdp_inst; freerdp* rdp_inst = rdp_client->rdp_inst;
@ -566,58 +566,56 @@ static int rdp_guac_client_wait_for_messages(guac_client* client,
int result; int result;
int index; int index;
int max_fd, fd;
void* read_fds[32]; /* List of all file descriptors which we may read data from */
void* write_fds[32]; void* read_fds[GUAC_RDP_MAX_FILE_DESCRIPTORS];
int read_count = 0; int read_count = 0;
/* List of all file descriptors which data may be written to. These will
* ultimately be ignored, but FreeRDP requires that both read and write
* file descriptors be retrieved simultaneously. */
void* write_fds[GUAC_RDP_MAX_FILE_DESCRIPTORS];
int write_count = 0; int write_count = 0;
fd_set rfds, wfds;
struct timeval timeout = { struct pollfd fds[GUAC_RDP_MAX_FILE_DESCRIPTORS];
.tv_sec = 0,
.tv_usec = timeout_usecs
};
/* Get RDP fds */ /* Get RDP file descriptors */
if (!freerdp_get_fds(rdp_inst, read_fds, &read_count, write_fds, &write_count)) { if (!freerdp_get_fds(rdp_inst, read_fds, &read_count,
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Unable to read RDP file descriptors."); write_fds, &write_count)) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
"Unable to read RDP file descriptors.");
return -1; return -1;
} }
/* Get channel fds */ /* Get RDP channel file descriptors */
if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count, write_fds, if (!freerdp_channels_get_fds(channels, rdp_inst, read_fds, &read_count,
&write_count)) { write_fds, &write_count)) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Unable to read RDP channel file descriptors."); guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
"Unable to read RDP channel file descriptors.");
return -1; return -1;
} }
/* Construct read fd_set */
max_fd = 0;
FD_ZERO(&rfds);
for (index = 0; index < read_count; index++) {
fd = (int)(long) (read_fds[index]);
if (fd > max_fd)
max_fd = fd;
FD_SET(fd, &rfds);
}
/* Construct write fd_set */
FD_ZERO(&wfds);
for (index = 0; index < write_count; index++) {
fd = (int)(long) (write_fds[index]);
if (fd > max_fd)
max_fd = fd;
FD_SET(fd, &wfds);
}
/* If no file descriptors, error */ /* If no file descriptors, error */
if (max_fd == 0) { if (read_count == 0) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "No file descriptors associated with RDP connection."); guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
"No file descriptors associated with RDP connection.");
return -1; return -1;
} }
/* Wait for all RDP file descriptors */ /* Populate poll() array of read file descriptors */
result = select(max_fd + 1, &rfds, &wfds, NULL, &timeout); for (index = 0; index < read_count; index++) {
struct pollfd* current = &fds[index];
/* Init poll() array element with RDP file descriptor */
current->fd = (int)(long) (read_fds[index]);
current->events = POLLIN;
current->revents = 0;
}
/* Wait until data can be read from RDP file descriptors */
result = poll(fds, read_count, timeout_msecs);
if (result < 0) { if (result < 0) {
/* If error ignorable, pretend timout occurred */ /* If error ignorable, pretend timout occurred */
@ -628,7 +626,8 @@ static int rdp_guac_client_wait_for_messages(guac_client* client,
return 0; return 0;
/* Otherwise, return as error */ /* Otherwise, return as error */
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR, "Error waiting for file descriptor."); guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
"Error waiting for file descriptor.");
return -1; return -1;
} }
@ -823,12 +822,12 @@ static int guac_rdp_handle_connection(guac_client* client) {
/* Increase the duration of this frame if client is lagging */ /* Increase the duration of this frame if client is lagging */
if (required_wait > GUAC_RDP_FRAME_TIMEOUT) if (required_wait > GUAC_RDP_FRAME_TIMEOUT)
wait_result = rdp_guac_client_wait_for_messages(client, wait_result = rdp_guac_client_wait_for_messages(client,
required_wait*1000); required_wait);
/* Wait again if frame remaining */ /* Wait again if frame remaining */
else if (frame_remaining > 0) else if (frame_remaining > 0)
wait_result = rdp_guac_client_wait_for_messages(client, wait_result = rdp_guac_client_wait_for_messages(client,
GUAC_RDP_FRAME_TIMEOUT*1000); GUAC_RDP_FRAME_TIMEOUT);
else else
break; break;

View File

@ -44,13 +44,13 @@
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/select.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/time.h> #include <sys/time.h>
@ -362,19 +362,18 @@ void* ssh_client_thread(void* data) {
/* Wait for more data if reads turn up empty */ /* Wait for more data if reads turn up empty */
if (total_read == 0) { if (total_read == 0) {
fd_set fds;
struct timeval timeout;
FD_ZERO(&fds); /* Wait on the SSH session file descriptor only */
FD_SET(ssh_client->session->fd, &fds); struct pollfd fds[] = {{
.fd = ssh_client->session->fd,
.events = POLLIN,
.revents = 0,
}};
/* Wait for one second */ /* Wait up to one second */
timeout.tv_sec = 1; if (poll(fds, 1, 1000) < 0)
timeout.tv_usec = 0;
if (select(ssh_client->session->fd + 1, &fds,
NULL, NULL, &timeout) < 0)
break; break;
} }
} }

View File

@ -29,10 +29,10 @@
#include <errno.h> #include <errno.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/select.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
@ -442,17 +442,15 @@ void guac_telnet_send_user(telnet_t* telnet, const char* username) {
*/ */
static int __guac_telnet_wait(int socket_fd) { static int __guac_telnet_wait(int socket_fd) {
fd_set fds; /* Build array of file descriptors */
struct timeval timeout; struct pollfd fds[] = {{
.fd = socket_fd,
FD_ZERO(&fds); .events = POLLIN,
FD_SET(socket_fd, &fds); .revents = 0,
}};
/* Wait for one second */ /* Wait for one second */
timeout.tv_sec = 1; return poll(fds, 1, 1000);
timeout.tv_usec = 0;
return select(socket_fd+1, &fds, NULL, NULL, &timeout);
} }

View File

@ -32,12 +32,12 @@
#include "typescript.h" #include "typescript.h"
#include <errno.h> #include <errno.h>
#include <poll.h>
#include <pthread.h> #include <pthread.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/select.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <wchar.h> #include <wchar.h>
@ -463,19 +463,15 @@ void guac_terminal_free(guac_terminal* term) {
*/ */
static int guac_terminal_wait_for_data(int fd, int msec_timeout) { static int guac_terminal_wait_for_data(int fd, int msec_timeout) {
/* Build fd_set */ /* Build array of file descriptors */
fd_set fds; struct pollfd fds[] = {{
FD_ZERO(&fds); .fd = fd,
FD_SET(fd, &fds); .events = POLLIN,
.revents = 0,
/* Split millisecond timeout into seconds and microseconds */ }};
struct timeval timeout = {
.tv_sec = msec_timeout / 1000,
.tv_usec = (msec_timeout % 1000) * 1000
};
/* Wait for data */ /* Wait for data */
return select(fd+1, &fds, NULL, NULL, &timeout); return poll(fds, 1, msec_timeout);
} }