From e20e877d45366ae8fd784b7bf57799e074630a3d Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Thu, 17 Mar 2011 21:16:29 -0700 Subject: [PATCH] Moved sleep and timestamp functions, fixed header ifndefs --- libguac/include/client.h | 8 ++--- libguac/include/guacio.h | 4 +-- libguac/include/log.h | 4 +-- libguac/include/protocol.h | 7 +++-- libguac/src/client.c | 61 ++++---------------------------------- libguac/src/protocol.c | 49 ++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 67 deletions(-) diff --git a/libguac/include/client.h b/libguac/include/client.h index 8eb72147..ecde725f 100644 --- a/libguac/include/client.h +++ b/libguac/include/client.h @@ -36,8 +36,8 @@ * ***** END LICENSE BLOCK ***** */ -#ifndef _CLIENT_H -#define _CLIENT_H +#ifndef _GUAC_CLIENT_H +#define _GUAC_CLIENT_H #include @@ -297,8 +297,4 @@ void guac_free_png_buffer(png_byte** png_buffer, int h); int guac_client_handle_instruction(guac_client* client, guac_instruction* instruction); void guac_client_stop(guac_client* client); -/* FIXME: MOVE THESE TO protocol.h */ -long guac_client_current_timestamp(); -void guac_client_sleep(int millis); - #endif diff --git a/libguac/include/guacio.h b/libguac/include/guacio.h index 8b57f981..e82061ed 100644 --- a/libguac/include/guacio.h +++ b/libguac/include/guacio.h @@ -35,8 +35,8 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef _GUACIO_H -#define _GUACIO_H +#ifndef _GUAC_GUACIO_H +#define _GUAC_GUACIO_H #include diff --git a/libguac/include/log.h b/libguac/include/log.h index 0bf039c1..cb0fab78 100644 --- a/libguac/include/log.h +++ b/libguac/include/log.h @@ -35,8 +35,8 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef _LOG_H -#define _LOG_H +#ifndef _GUAC_LOG_H +#define _GUAC_LOG_H #ifdef HAVE_SYSLOG_H diff --git a/libguac/include/protocol.h b/libguac/include/protocol.h index d7512d1a..ac872d38 100644 --- a/libguac/include/protocol.h +++ b/libguac/include/protocol.h @@ -35,8 +35,8 @@ * * ***** END LICENSE BLOCK ***** */ -#ifndef __PROTOCOL_H -#define __PROTOCOL_H +#ifndef _GUAC_PROTOCOL_H +#define _GUAC_PROTOCOL_H #include @@ -255,5 +255,8 @@ int guac_instructions_waiting(GUACIO* io); */ int guac_read_instruction(GUACIO* io, guac_instruction* parsed_instruction); +long guac_current_timestamp(); +void guac_sleep(int millis); + #endif diff --git a/libguac/src/client.c b/libguac/src/client.c index e2f789ed..3de42f73 100644 --- a/libguac/src/client.c +++ b/libguac/src/client.c @@ -37,11 +37,6 @@ #include #include -#ifdef HAVE_CLOCK_GETTIME -#include -#else -#include -#endif #include #include #include @@ -79,50 +74,6 @@ void guac_free_png_buffer(png_byte** png_buffer, int h) { } - -long guac_client_current_timestamp() { - -#ifdef HAVE_CLOCK_GETTIME - - struct timespec current; - - /* Get current time */ - clock_gettime(CLOCK_REALTIME, ¤t); - - /* Calculate milliseconds */ - return current.tv_sec * 1000 + current.tv_nsec / 1000000; - -#else - - struct timeval current; - - /* Get current time */ - gettimeofday(¤t, NULL); - - /* Calculate milliseconds */ - return current.tv_sec * 1000 + current.tv_usec / 1000; - -#endif - -} - -void guac_client_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 - -} - guac_client* __guac_alloc_client(GUACIO* io) { /* Allocate new client (not handoff) */ @@ -131,7 +82,7 @@ guac_client* __guac_alloc_client(GUACIO* io) { /* Init new client */ client->io = io; - client->last_received_timestamp = client->last_sent_timestamp = guac_client_current_timestamp(); + client->last_received_timestamp = client->last_sent_timestamp = guac_current_timestamp(); client->state = RUNNING; return client; @@ -338,7 +289,7 @@ void* __guac_client_output_thread(void* data) { while (client->state == RUNNING) { /* Occasionally ping client with sync */ - long timestamp = guac_client_current_timestamp(); + long timestamp = guac_current_timestamp(); if (timestamp - client->last_sent_timestamp > GUAC_SYNC_FREQUENCY) { client->last_sent_timestamp = timestamp; guac_send_sync(io, timestamp); @@ -365,10 +316,10 @@ void* __guac_client_output_thread(void* data) { if (io->total_written != last_total_written) { /* Sleep as necessary */ - guac_client_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY); + guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY); /* Send sync instruction */ - client->last_sent_timestamp = guac_client_current_timestamp(); + client->last_sent_timestamp = guac_current_timestamp(); guac_send_sync(io, client->last_sent_timestamp); } @@ -378,13 +329,13 @@ void* __guac_client_output_thread(void* data) { /* If sync threshold exceeded, don't spin waiting for resync */ else - guac_client_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY); + guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY); } /* If no message handler, just sleep until next sync ping */ else - guac_client_sleep(GUAC_SYNC_FREQUENCY); + guac_sleep(GUAC_SYNC_FREQUENCY); } /* End of output loop */ diff --git a/libguac/src/protocol.c b/libguac/src/protocol.c index dec704be..e18f65d6 100644 --- a/libguac/src/protocol.c +++ b/libguac/src/protocol.c @@ -35,6 +35,12 @@ * * ***** END LICENSE BLOCK ***** */ +#ifdef HAVE_CLOCK_GETTIME +#include +#else +#include +#endif + #include #include #include @@ -531,3 +537,46 @@ int guac_instructions_waiting(GUACIO* io) { return guac_select(io, GUAC_USEC_TIMEOUT); } +long guac_current_timestamp() { + +#ifdef HAVE_CLOCK_GETTIME + + struct timespec current; + + /* Get current time */ + clock_gettime(CLOCK_REALTIME, ¤t); + + /* Calculate milliseconds */ + return current.tv_sec * 1000 + current.tv_nsec / 1000000; + +#else + + struct timeval current; + + /* Get current time */ + gettimeofday(¤t, NULL); + + /* Calculate milliseconds */ + return current.tv_sec * 1000 + current.tv_usec / 1000; + +#endif + +} + +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 + +} +