diff --git a/guacamole/proxy/guacio.c b/guacamole/proxy/guacio.c index 234092607..df0531765 100644 --- a/guacamole/proxy/guacio.c +++ b/guacamole/proxy/guacio.c @@ -1,7 +1,12 @@ #include +#include #include +#include #include +#include + +#include #include "guacio.h" @@ -14,11 +19,22 @@ char __GUACIO_BAS64_CHARACTERS[64] = { GUACIO* guac_open(int fd) { + int flags; + GUACIO* io = malloc(sizeof(GUACIO)); io->ready = 0; io->written = 0; io->fd = fd; + /* Set O_NONBLOCK */ + flags = fcntl(io->fd, F_GETFL, 0); + fcntl(io->fd, F_SETFL, flags | O_NONBLOCK); + + /* Allocate message buffer */ + io->messagebuf_size = 1024; + io->messagebuf = malloc(io->messagebuf_size); + io->messagebuf_used_length = 0; + return io; } @@ -194,3 +210,91 @@ ssize_t guac_flush_base64(GUACIO* io) { } + +int guac_select(GUACIO* io, int usec_timeout) { + + fd_set fds; + struct timeval timeout; + + timeout.tv_sec = usec_timeout/1000000; + timeout.tv_usec = usec_timeout%1000000; + + FD_ZERO(&fds); + FD_SET(io->fd, &fds); + + return select(io->fd + 1, &fds, NULL, NULL, &timeout); + +} + +int __guac_fill_messagebuf(GUACIO* io) { + + int retval; + + /* Attempt to fill buffer */ + retval = read( + io->fd, + io->messagebuf + io->messagebuf_used_length, + io->messagebuf_size - io->messagebuf_used_length + ); + + if (retval < 0) + return retval; + + io->messagebuf_used_length += retval; + + /* Expand buffer if necessary */ + if (io->messagebuf_used_length > io->messagebuf_size / 2) { + io->messagebuf_size *= 2; + io->messagebuf = realloc(io->messagebuf, io->messagebuf_size); + } + + return retval; + +} + +int guac_read_message(GUACIO* io) { + + int retval; + int i = 0; + + /* Loop until a message is read */ + for (;;) { + + /* Search for end of message */ + for (; i < io->messagebuf_used_length; i++) { + + if (io->messagebuf[i] == ';') { + + char* message = malloc(i+1); + memcpy(message, io->messagebuf, i+1); + message[i] = '\0'; /* Replace semicolon with null terminator. */ + + fprintf(stderr, "RECEIVED MESSAGE: %s\n", message); + + /* Found. Reset buffer */ + memmove(io->messagebuf, io->messagebuf + i + 1, io->messagebuf_used_length - i - 1); + io->messagebuf_used_length -= i + 1; + + /* Done */ + return 0; + } + + } + + /* No message yet? Get more data ... */ + retval = guac_select(io, 1000); + if (retval < 0) + return retval; + + /* Break if descriptor doesn't have enough data */ + if (retval == 0) + return 0; /* SOFT FAIL: No message ... yet, but is still in buffer */ + + retval = __guac_fill_messagebuf(io); + if (retval < 0 && errno != EAGAIN && errno != EWOULDBLOCK) + return retval; + + } + +} + diff --git a/guacamole/proxy/guacio.h b/guacamole/proxy/guacio.h index 680492849..7a03cdc86 100644 --- a/guacamole/proxy/guacio.h +++ b/guacamole/proxy/guacio.h @@ -14,6 +14,10 @@ typedef struct GUACIO { int written; char out_buf[8192]; + int messagebuf_size; + int messagebuf_used_length; + char* messagebuf; + } GUACIO; GUACIO* guac_open(int fd); @@ -22,6 +26,8 @@ ssize_t guac_write_string(GUACIO* io, const char* str); ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count); ssize_t guac_flush_base64(GUACIO* io); ssize_t guac_flush(GUACIO* io); +int guac_select(GUACIO* io, int usec_timeout); +int guac_read_message(GUACIO* io); void guac_close(GUACIO* io); #endif diff --git a/guacamole/proxy/proxy.c b/guacamole/proxy/proxy.c index 3e19d669f..6285a68cf 100644 --- a/guacamole/proxy/proxy.c +++ b/guacamole/proxy/proxy.c @@ -162,6 +162,8 @@ void proxy(int client_fd) { /* VNC Client Loop */ for (;;) { + /* TODO: Separate into THREADS ... no need to do this weird polling thing */ + wait_result = WaitForMessage(rfb_client, 2000); if (wait_result < 0) { fprintf(stderr, "WAIT FAIL\n"); @@ -176,6 +178,12 @@ void proxy(int client_fd) { } } + + wait_result = guac_select(io, 2000); + if (wait_result > 0) { + guac_read_message(io); + } + } /* Free PNG data */