Moved sleep and timestamp functions, fixed header ifndefs

This commit is contained in:
Michael Jumper 2011-03-17 21:16:29 -07:00
parent 0a3a23f26e
commit e20e877d45
6 changed files with 66 additions and 67 deletions

View File

@ -36,8 +36,8 @@
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#ifndef _CLIENT_H #ifndef _GUAC_CLIENT_H
#define _CLIENT_H #define _GUAC_CLIENT_H
#include <png.h> #include <png.h>
@ -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); int guac_client_handle_instruction(guac_client* client, guac_instruction* instruction);
void guac_client_stop(guac_client* client); 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 #endif

View File

@ -35,8 +35,8 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#ifndef _GUACIO_H #ifndef _GUAC_GUACIO_H
#define _GUACIO_H #define _GUAC_GUACIO_H
#include <unistd.h> #include <unistd.h>

View File

@ -35,8 +35,8 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#ifndef _LOG_H #ifndef _GUAC_LOG_H
#define _LOG_H #define _GUAC_LOG_H
#ifdef HAVE_SYSLOG_H #ifdef HAVE_SYSLOG_H

View File

@ -35,8 +35,8 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#ifndef __PROTOCOL_H #ifndef _GUAC_PROTOCOL_H
#define __PROTOCOL_H #define _GUAC_PROTOCOL_H
#include <png.h> #include <png.h>
@ -255,5 +255,8 @@ int guac_instructions_waiting(GUACIO* io);
*/ */
int guac_read_instruction(GUACIO* io, guac_instruction* parsed_instruction); int guac_read_instruction(GUACIO* io, guac_instruction* parsed_instruction);
long guac_current_timestamp();
void guac_sleep(int millis);
#endif #endif

View File

@ -37,11 +37,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_CLOCK_GETTIME
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <string.h> #include <string.h>
#include <dlfcn.h> #include <dlfcn.h>
#include <pthread.h> #include <pthread.h>
@ -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, &current);
/* Calculate milliseconds */
return current.tv_sec * 1000 + current.tv_nsec / 1000000;
#else
struct timeval current;
/* Get current time */
gettimeofday(&current, 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) { guac_client* __guac_alloc_client(GUACIO* io) {
/* Allocate new client (not handoff) */ /* Allocate new client (not handoff) */
@ -131,7 +82,7 @@ guac_client* __guac_alloc_client(GUACIO* io) {
/* Init new client */ /* Init new client */
client->io = io; 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; client->state = RUNNING;
return client; return client;
@ -338,7 +289,7 @@ void* __guac_client_output_thread(void* data) {
while (client->state == RUNNING) { while (client->state == RUNNING) {
/* Occasionally ping client with sync */ /* 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) { if (timestamp - client->last_sent_timestamp > GUAC_SYNC_FREQUENCY) {
client->last_sent_timestamp = timestamp; client->last_sent_timestamp = timestamp;
guac_send_sync(io, timestamp); guac_send_sync(io, timestamp);
@ -365,10 +316,10 @@ void* __guac_client_output_thread(void* data) {
if (io->total_written != last_total_written) { if (io->total_written != last_total_written) {
/* Sleep as necessary */ /* Sleep as necessary */
guac_client_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY); guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
/* Send sync instruction */ /* 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); 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 */ /* If sync threshold exceeded, don't spin waiting for resync */
else 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 */ /* If no message handler, just sleep until next sync ping */
else else
guac_client_sleep(GUAC_SYNC_FREQUENCY); guac_sleep(GUAC_SYNC_FREQUENCY);
} /* End of output loop */ } /* End of output loop */

View File

@ -35,6 +35,12 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#ifdef HAVE_CLOCK_GETTIME
#include <time.h>
#else
#include <sys/time.h>
#endif
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -531,3 +537,46 @@ int guac_instructions_waiting(GUACIO* io) {
return guac_select(io, GUAC_USEC_TIMEOUT); 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, &current);
/* Calculate milliseconds */
return current.tv_sec * 1000 + current.tv_nsec / 1000000;
#else
struct timeval current;
/* Get current time */
gettimeofday(&current, 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
}