Removing forced sleep between message handling. Can cause excess delays if handle_mes...
[guacd.git] / src / client.c
index a5daea8..abaf442 100644 (file)
 #include <stdlib.h>
 #include <time.h>
 
-#include <guacamole/guacio.h>
+#include <guacamole/socket.h>
 #include <guacamole/client.h>
-#include <guacamole/log.h>
 #include <guacamole/error.h>
 
 #include "client.h"
 #include "thread.h"
+#include "log.h"
 
+/**
+ * Sleep for the given number of milliseconds.
+ *
+ * @param millis The number of milliseconds to sleep.
+ */
 void __guacd_sleep(int millis) {
 
     struct timespec sleep_period;
@@ -64,7 +69,7 @@ void guac_client_stop(guac_client* client) {
 void* __guac_client_output_thread(void* data) {
 
     guac_client* client = (guac_client*) data;
-    guac_socket* io = client->io;
+    guac_socket* socket = client->socket;
 
     guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
 
@@ -79,15 +84,17 @@ void* __guac_client_output_thread(void* data) {
             last_ping_timestamp = timestamp;
 
             /* Send sync */
-            if (guac_protocol_send_sync(io, client->last_sent_timestamp)) {
-                guac_client_log_error(client, "Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
+            if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
+                guacd_client_log_guac_error(client,
+                        "Error sending \"sync\" instruction");
                 guac_client_stop(client);
                 return NULL;
             }
 
             /* Flush */
-            if (guac_socket_flush(io)) {
-                guac_client_log_error(client, "Error flushing output: %s", guac_status_string(guac_error));
+            if (guac_socket_flush(socket)) {
+                guacd_client_log_guac_error(client,
+                        "Error flushing output");
                 guac_client_stop(client);
                 return NULL;
             }
@@ -103,31 +110,32 @@ void* __guac_client_output_thread(void* data) {
 
                 int retval = client->handle_messages(client);
                 if (retval) {
-                    guac_client_log_error(client, "Error handling server messages: %s", guac_status_string(guac_error));
+                    guacd_client_log_guac_error(client,
+                            "Error handling server messages");
                     guac_client_stop(client);
                     return NULL;
                 }
 
-                /* Sleep as necessary */
-                __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
-
                 /* Send sync instruction */
                 client->last_sent_timestamp = guac_protocol_get_timestamp();
-                if (guac_protocol_send_sync(io, client->last_sent_timestamp)) {
-                    guac_client_log_error(client, "Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
+                if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
+                    guacd_client_log_guac_error(client, 
+                            "Error sending \"sync\" instruction");
                     guac_client_stop(client);
                     return NULL;
                 }
 
-                if (guac_socket_flush(io)) {
-                    guac_client_log_error(client, "Error flushing output: %s", guac_status_string(guac_error));
+                /* Flush */
+                if (guac_socket_flush(socket)) {
+                    guacd_client_log_guac_error(client,
+                            "Error flushing output");
                     guac_client_stop(client);
                     return NULL;
                 }
 
             }
 
-            /* If sync threshold exceeded, don't spin waiting for resync */
+            /* Do not spin while waiting for old sync */
             else
                 __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
 
@@ -147,25 +155,40 @@ void* __guac_client_output_thread(void* data) {
 void* __guac_client_input_thread(void* data) {
 
     guac_client* client = (guac_client*) data;
-    guac_socket* io = client->io;
+    guac_socket* socket = client->socket;
 
     /* Guacamole client input loop */
     while (client->state == RUNNING) {
 
         /* Read instruction */
         guac_instruction* instruction =
-            guac_protocol_read_instruction(io, GUAC_USEC_TIMEOUT);
+            guac_protocol_read_instruction(socket, GUAC_USEC_TIMEOUT);
 
         /* Stop on error */
         if (instruction == NULL) {
-            guac_client_log_error(client, "Error reading instruction: %s", guac_status_string(guac_error));
+            guacd_client_log_guac_error(client,
+                    "Error reading instruction");
             guac_client_stop(client);
             return NULL;
         }
 
+        /* Reset guac_error and guac_error_message (client handlers are not
+         * guaranteed to set these) */
+        guac_error = GUAC_STATUS_SUCCESS;
+        guac_error_message = NULL;
+
         /* Call handler, stop on error */
         if (guac_client_handle_instruction(client, instruction) < 0) {
-            guac_client_log_error(client, "Error in client \"%s\" instruction handler: %s", instruction->opcode, guac_status_string(guac_error));
+
+            /* Log error */
+            guacd_client_log_guac_error(client,
+                    "Client instruction handler error");
+
+            /* Log handler details */
+            guac_client_log_info(client,
+                    "Failing instruction handler in client was \"%s\"",
+                    instruction->opcode);
+
             guac_instruction_free(instruction);
             guac_client_stop(client);
             return NULL;
@@ -185,10 +208,12 @@ int guac_start_client(guac_client* client) {
     guac_thread input_thread, output_thread;
 
     if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
+        guac_client_log_error(client, "Unable to start output thread");
         return -1;
     }
 
     if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
+        guac_client_log_error(client, "Unable to start input thread");
         guac_client_stop(client);
         guac_thread_join(output_thread);
         return -1;
@@ -203,4 +228,3 @@ int guac_start_client(guac_client* client) {
 
 }
 
-