Automatic sleep on message handle

This commit is contained in:
Michael Jumper 2011-03-13 14:52:19 -07:00
parent 12d497ca3f
commit 3e90251cb8
3 changed files with 45 additions and 2 deletions

View File

@ -57,7 +57,7 @@ AC_TYPE_SSIZE_T
# Checks for library functions. # Checks for library functions.
AC_FUNC_MALLOC AC_FUNC_MALLOC
AC_FUNC_REALLOC 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_CONFIG_FILES([Makefile])
AC_OUTPUT AC_OUTPUT

View File

@ -49,6 +49,22 @@
* @file client.h * @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; typedef struct guac_client guac_client;
/** /**

View File

@ -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) { void guac_start_client(guac_client* client) {
GUACIO* io = client->io; GUACIO* io = client->io;
@ -295,7 +315,8 @@ void guac_start_client(guac_client* client) {
int last_total_written = io->total_written; int last_total_written = io->total_written;
/* Only handle messages if synced within threshold */ /* 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); int retval = client->handle_messages(client);
if (retval) { if (retval) {
@ -305,8 +326,14 @@ void guac_start_client(guac_client* client) {
/* If data was written during message handling */ /* If data was written during message handling */
if (io->total_written != last_total_written) { 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(); last_sent_timestamp = __guac_current_timestamp();
guac_send_sync(io, last_sent_timestamp); guac_send_sync(io, last_sent_timestamp);
} }
guac_flush(io); guac_flush(io);