Migrated to namespaced RUNNING state.
[guacd.git] / src / client.c
index ce84eb5..2645980 100644 (file)
  * ***** END LICENSE BLOCK ***** */
 
 #include <stdlib.h>
+#include <time.h>
+#include <pthread.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 __guacdd_sleep(int millis) {
+
+    struct timespec sleep_period;
+
+    sleep_period.tv_sec =   millis / 1000;
+    sleep_period.tv_nsec = (millis % 1000) * 1000000L;
+
+    nanosleep(&sleep_period, NULL);
 
-void guac_client_stop(guac_client* client) {
-    client->state = STOPPING;
 }
 
-void* __guac_client_output_thread(void* data) {
+void* __guacd_client_output_thread(void* data) {
 
     guac_client* client = (guac_client*) data;
-    GUACIO* io = client->io;
+    guac_socket* socket = client->socket;
 
-    guac_timestamp last_ping_timestamp = guac_current_timestamp();
+    guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
 
     /* Guacamole client output loop */
-    while (client->state == RUNNING) {
+    while (client->state == GUAC_CLIENT_RUNNING) {
 
         /* Occasionally ping client with repeat of last sync */
-        guac_timestamp timestamp = guac_current_timestamp();
-        if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
+        guac_timestamp timestamp = guac_protocol_get_timestamp();
+        if (timestamp - last_ping_timestamp > GUACD_SYNC_FREQUENCY) {
+
+            /* Record time of last synnc */
             last_ping_timestamp = timestamp;
-            if (
-                   guac_send_sync(io, client->last_sent_timestamp)
-                || guac_flush(io)
-               ) {
+
+            /* Send sync */
+            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(socket)) {
+                guacd_client_log_guac_error(client,
+                        "Error flushing output");
+                guac_client_stop(client);
+                return NULL;
+            }
+
         }
 
         /* 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 (client->last_sent_timestamp - client->last_received_timestamp
-                    < GUAC_SYNC_THRESHOLD) {
+                    < GUACD_SYNC_THRESHOLD) {
 
                 int retval = client->handle_messages(client);
                 if (retval) {
-                    guac_log_error("Error handling server messages");
+                    guacd_client_log_guac_error(client,
+                            "Error handling server messages");
                     guac_client_stop(client);
                     return NULL;
                 }
 
-                /* If data was written during message handling */
-                if (io->total_written != last_total_written) {
-
-                    /* Sleep as necessary */
-                    guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
-
-                    /* Send sync instruction */
-                    client->last_sent_timestamp = guac_current_timestamp();
-                    if (guac_send_sync(io, client->last_sent_timestamp)) {
-                        guac_client_stop(client);
-                        return NULL;
-                    }
-
+                /* Send sync instruction */
+                client->last_sent_timestamp = guac_protocol_get_timestamp();
+                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_flush(io)) {
+                /* 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
-                guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
+                __guacdd_sleep(GUACD_MESSAGE_HANDLE_FREQUENCY);
 
         }
 
         /* If no message handler, just sleep until next sync ping */
         else
-            guac_sleep(GUAC_SYNC_FREQUENCY);
+            __guacdd_sleep(GUACD_SYNC_FREQUENCY);
 
     } /* End of output loop */
 
@@ -127,53 +148,79 @@ void* __guac_client_output_thread(void* data) {
 
 }
 
-void* __guac_client_input_thread(void* data) {
+void* __guacd_client_input_thread(void* data) {
 
     guac_client* client = (guac_client*) data;
-    GUACIO* io = client->io;
-
-    guac_instruction instruction;
+    guac_socket* socket = client->socket;
 
     /* Guacamole client input loop */
-    while (client->state == RUNNING && guac_read_instruction(io, &instruction) > 0) {
+    while (client->state == GUAC_CLIENT_RUNNING) {
+
+        /* Read instruction */
+        guac_instruction* instruction =
+            guac_protocol_read_instruction(socket, GUACD_USEC_TIMEOUT);
+
+        /* Stop on error */
+        if (instruction == NULL) {
+            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_free_instruction_data(&instruction);
-            break;
+        if (guac_client_handle_instruction(client, instruction) < 0) {
+
+            /* 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;
         }
 
-        /* Free allocate instruction data */
-        guac_free_instruction_data(&instruction);
+        /* Free allocated instruction */
+        guac_instruction_free(instruction);
 
     }
 
-    guac_client_stop(client);
     return NULL;
 
 }
 
-int guac_start_client(guac_client* client) {
+int guacd_client_start(guac_client* client) {
 
-    guac_thread input_thread, output_thread;
+    pthread_t input_thread, output_thread;
 
-    if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
+    if (pthread_create(&output_thread, NULL, __guacd_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)) {
+    if (pthread_create(&input_thread, NULL, __guacd_client_input_thread, (void*) client)) {
+        guac_client_log_error(client, "Unable to start input thread");
         guac_client_stop(client);
-        guac_thread_join(output_thread);
+        pthread_join(output_thread, NULL);
         return -1;
     }
 
     /* Wait for I/O threads */
-    guac_thread_join(input_thread);
-    guac_thread_join(output_thread);
+    pthread_join(input_thread, NULL);
+    pthread_join(output_thread, NULL);
 
     /* Done */
     return 0;
 
 }
 
-