diff --git a/libguac/configure.in b/libguac/configure.in index 9e7381a7..00eb7162 100644 --- a/libguac/configure.in +++ b/libguac/configure.in @@ -57,7 +57,7 @@ AC_TYPE_SSIZE_T # Checks for library functions. AC_FUNC_MALLOC AC_FUNC_REALLOC -AC_CHECK_FUNCS([clock_gettime gettimeofday memmove memset select strdup png_get_io_ptr]) +AC_CHECK_FUNCS([clock_gettime gettimeofday memmove memset select strdup png_get_io_ptr nanosleep]) AC_CONFIG_FILES([Makefile]) AC_OUTPUT diff --git a/libguac/include/client.h b/libguac/include/client.h index 13938398..83c04560 100644 --- a/libguac/include/client.h +++ b/libguac/include/client.h @@ -49,6 +49,22 @@ * @file client.h */ +/** + * The time to allow between sync responses in milliseconds. If a sync + * instruction is sent to the client and no response is received within this + * timeframe, server messages will not be handled until a sync instruction is + * received from the client. + */ +#define GUAC_SYNC_THRESHOLD 500 + +/** + * The amount of time to wait after handling server messages. If a client + * plugin has a message handler, and sends instructions when server messages + * are being handled, there will be a pause of this many milliseconds before + * the next call to the message handler. + */ +#define GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY 50 + typedef struct guac_client guac_client; /** diff --git a/libguac/src/client.c b/libguac/src/client.c index 1b132af6..8b76f84f 100644 --- a/libguac/src/client.c +++ b/libguac/src/client.c @@ -274,6 +274,26 @@ long __guac_current_timestamp() { } + +void __guac_sleep(int millis) { + +#ifdef HAVE_NANOSLEEP + struct timespec sleep_period; + + sleep_period.tv_sec = 0; + sleep_period.tv_nsec = millis * 1000000L; + + nanosleep(&sleep_period, NULL); +#elif defined(__MINGW32__) + Sleep(millis) +#else +#warning No sleep/nanosleep function available. Clients may not perform as expected. Consider patching libguac to add support for your platform. +#endif + +} + + + void guac_start_client(guac_client* client) { GUACIO* io = client->io; @@ -295,7 +315,8 @@ void guac_start_client(guac_client* client) { int last_total_written = io->total_written; /* Only handle messages if synced within threshold */ - if (last_sent_timestamp - last_received_timestamp < 200) { + if (last_sent_timestamp - last_received_timestamp + < GUAC_SYNC_THRESHOLD) { int retval = client->handle_messages(client); if (retval) { @@ -305,8 +326,14 @@ void guac_start_client(guac_client* client) { /* If data was written during message handling */ if (io->total_written != last_total_written) { + + /* Sleep as necessary */ + __guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY); + + /* Update sync timestamp and send sync instruction */ last_sent_timestamp = __guac_current_timestamp(); guac_send_sync(io, last_sent_timestamp); + } guac_flush(io);