Working sync control.

This commit is contained in:
Michael Jumper 2011-03-11 19:35:44 -08:00
parent e463360aad
commit 12d497ca3f
3 changed files with 19 additions and 3 deletions

View File

@ -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.

View File

@ -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);
}

View File

@ -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 */