From 12d497ca3f3e1c21367dd081cbfa0b084cde89af Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 11 Mar 2011 19:35:44 -0800 Subject: [PATCH] Working sync control. --- libguac/include/guacio.h | 5 +++++ libguac/src/client.c | 11 ++++++++--- libguac/src/guacio.c | 6 ++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/libguac/include/guacio.h b/libguac/include/guacio.h index 86dcd22a..8b57f981 100644 --- a/libguac/include/guacio.h +++ b/libguac/include/guacio.h @@ -74,6 +74,11 @@ typedef struct GUACIO { */ int written; + /** + * The number of bytes written total, since this GUACIO was opened. + */ + int total_written; + /** * The main write buffer. Bytes written go here before being flushed * to the open file descriptor. diff --git a/libguac/src/client.c b/libguac/src/client.c index 89485565..1b132af6 100644 --- a/libguac/src/client.c +++ b/libguac/src/client.c @@ -291,6 +291,9 @@ void guac_start_client(guac_client* client) { /* Handle server messages */ if (client->handle_messages) { + /* Get previous GUACIO state */ + int last_total_written = io->total_written; + /* Only handle messages if synced within threshold */ if (last_sent_timestamp - last_received_timestamp < 200) { @@ -300,9 +303,11 @@ void guac_start_client(guac_client* client) { return; } - /* Send sync after updates */ - last_sent_timestamp = __guac_current_timestamp(); - guac_send_sync(io, last_sent_timestamp); + /* If data was written during message handling */ + if (io->total_written != last_total_written) { + last_sent_timestamp = __guac_current_timestamp(); + guac_send_sync(io, last_sent_timestamp); + } guac_flush(io); } diff --git a/libguac/src/guacio.c b/libguac/src/guacio.c index c7135cd3..902c84fb 100644 --- a/libguac/src/guacio.c +++ b/libguac/src/guacio.c @@ -126,6 +126,7 @@ ssize_t guac_write_string(GUACIO* io, const char* str) { for (; *str != '\0'; str++) { out_buf[io->written++] = *str; + io->total_written++; /* Flush when necessary, return on error */ if (io->written > 8188 /* sizeof(out_buf) - 4 */) { @@ -152,23 +153,28 @@ ssize_t __guac_write_base64_triplet(GUACIO* io, int a, int b, int c) { /* Byte 1 */ out_buf[io->written++] = __GUACIO_BASE64_CHARACTERS[(a & 0xFC) >> 2]; /* [AAAAAA]AABBBB BBBBCC CCCCCC */ + io->total_written++; if (b >= 0) { out_buf[io->written++] = __GUACIO_BASE64_CHARACTERS[((a & 0x03) << 4) | ((b & 0xF0) >> 4)]; /* AAAAAA[AABBBB]BBBBCC CCCCCC */ + io->total_written++; if (c >= 0) { out_buf[io->written++] = __GUACIO_BASE64_CHARACTERS[((b & 0x0F) << 2) | ((c & 0xC0) >> 6)]; /* AAAAAA AABBBB[BBBBCC]CCCCCC */ out_buf[io->written++] = __GUACIO_BASE64_CHARACTERS[c & 0x3F]; /* AAAAAA AABBBB BBBBCC[CCCCCC] */ + io->total_written += 2; } else { out_buf[io->written++] = __GUACIO_BASE64_CHARACTERS[((b & 0x0F) << 2)]; /* AAAAAA AABBBB[BBBB--]------ */ out_buf[io->written++] = '='; /* AAAAAA AABBBB BBBB--[------] */ + io->total_written += 2; } } else { out_buf[io->written++] = __GUACIO_BASE64_CHARACTERS[((a & 0x03) << 4)]; /* AAAAAA[AA----]------ ------ */ out_buf[io->written++] = '='; /* AAAAAA AA----[------]------ */ out_buf[io->written++] = '='; /* AAAAAA AA---- ------[------] */ + io->total_written += 3; } /* At this point, 4 bytes have been io->written */