2013-12-29 04:53:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Glyptodon LLC
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-11-26 00:30:17 +00:00
|
|
|
#include <time.h>
|
2011-12-06 08:41:08 +00:00
|
|
|
#include <pthread.h>
|
2011-11-23 08:01:27 +00:00
|
|
|
|
2011-11-27 06:08:25 +00:00
|
|
|
#include <guacamole/socket.h>
|
2011-11-23 08:01:27 +00:00
|
|
|
#include <guacamole/client.h>
|
2011-11-25 02:51:05 +00:00
|
|
|
#include <guacamole/error.h>
|
2012-09-07 03:45:21 +00:00
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/timestamp.h>
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
#include "client.h"
|
2011-11-28 00:29:42 +00:00
|
|
|
#include "log.h"
|
2011-11-23 08:01:27 +00:00
|
|
|
|
2011-11-27 06:26:39 +00:00
|
|
|
/**
|
|
|
|
* Sleep for the given number of milliseconds.
|
|
|
|
*
|
|
|
|
* @param millis The number of milliseconds to sleep.
|
|
|
|
*/
|
2011-12-06 08:41:08 +00:00
|
|
|
void __guacdd_sleep(int millis) {
|
2011-11-26 00:30:17 +00:00
|
|
|
|
|
|
|
struct timespec sleep_period;
|
|
|
|
|
|
|
|
sleep_period.tv_sec = millis / 1000;
|
|
|
|
sleep_period.tv_nsec = (millis % 1000) * 1000000L;
|
|
|
|
|
|
|
|
nanosleep(&sleep_period, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-06 08:41:08 +00:00
|
|
|
void* __guacd_client_output_thread(void* data) {
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
2011-11-26 00:35:50 +00:00
|
|
|
guac_socket* socket = client->socket;
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
/* Guacamole client output loop */
|
2011-12-25 06:51:31 +00:00
|
|
|
while (client->state == GUAC_CLIENT_RUNNING) {
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
/* Handle server messages */
|
|
|
|
if (client->handle_messages) {
|
|
|
|
|
|
|
|
/* Only handle messages if synced within threshold */
|
|
|
|
if (client->last_sent_timestamp - client->last_received_timestamp
|
2011-11-30 20:03:27 +00:00
|
|
|
< GUACD_SYNC_THRESHOLD) {
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
int retval = client->handle_messages(client);
|
|
|
|
if (retval) {
|
2011-11-28 00:29:42 +00:00
|
|
|
guacd_client_log_guac_error(client,
|
|
|
|
"Error handling server messages");
|
2011-11-23 08:01:27 +00:00
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-25 02:25:33 +00:00
|
|
|
/* Send sync instruction */
|
2012-09-07 03:45:21 +00:00
|
|
|
client->last_sent_timestamp = guac_timestamp_current();
|
2011-11-26 00:35:50 +00:00
|
|
|
if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
|
2011-11-28 00:29:42 +00:00
|
|
|
guacd_client_log_guac_error(client,
|
|
|
|
"Error sending \"sync\" instruction");
|
2011-11-25 02:25:33 +00:00
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
2011-11-23 08:01:27 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 06:26:39 +00:00
|
|
|
/* Flush */
|
2011-11-26 00:35:50 +00:00
|
|
|
if (guac_socket_flush(socket)) {
|
2011-11-28 00:29:42 +00:00
|
|
|
guacd_client_log_guac_error(client,
|
|
|
|
"Error flushing output");
|
2011-11-23 08:01:27 +00:00
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-28 10:29:05 +00:00
|
|
|
/* Do not spin while waiting for old sync */
|
|
|
|
else
|
2011-12-06 08:41:08 +00:00
|
|
|
__guacdd_sleep(GUACD_MESSAGE_HANDLE_FREQUENCY);
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no message handler, just sleep until next sync ping */
|
|
|
|
else
|
2011-12-06 08:41:08 +00:00
|
|
|
__guacdd_sleep(GUACD_SYNC_FREQUENCY);
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
} /* End of output loop */
|
|
|
|
|
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-06 08:41:08 +00:00
|
|
|
void* __guacd_client_input_thread(void* data) {
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
guac_client* client = (guac_client*) data;
|
2011-11-26 00:35:50 +00:00
|
|
|
guac_socket* socket = client->socket;
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
/* Guacamole client input loop */
|
2011-12-25 06:51:31 +00:00
|
|
|
while (client->state == GUAC_CLIENT_RUNNING) {
|
2011-11-25 02:25:33 +00:00
|
|
|
|
|
|
|
/* Read instruction */
|
|
|
|
guac_instruction* instruction =
|
2012-09-07 03:45:21 +00:00
|
|
|
guac_instruction_read(socket, GUACD_USEC_TIMEOUT);
|
2011-11-25 02:25:33 +00:00
|
|
|
|
|
|
|
/* Stop on error */
|
|
|
|
if (instruction == NULL) {
|
2011-11-28 00:29:42 +00:00
|
|
|
guacd_client_log_guac_error(client,
|
|
|
|
"Error reading instruction");
|
2011-11-25 02:25:33 +00:00
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-23 08:01:27 +00:00
|
|
|
|
2011-11-28 00:39:46 +00:00
|
|
|
/* Reset guac_error and guac_error_message (client handlers are not
|
|
|
|
* guaranteed to set these) */
|
|
|
|
guac_error = GUAC_STATUS_SUCCESS;
|
|
|
|
guac_error_message = NULL;
|
|
|
|
|
2011-11-23 08:01:27 +00:00
|
|
|
/* Call handler, stop on error */
|
2011-11-25 02:25:33 +00:00
|
|
|
if (guac_client_handle_instruction(client, instruction) < 0) {
|
2011-11-28 00:29:42 +00:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2011-11-26 00:30:17 +00:00
|
|
|
guac_instruction_free(instruction);
|
2011-11-25 02:25:33 +00:00
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
2011-11-23 08:01:27 +00:00
|
|
|
}
|
|
|
|
|
2011-11-25 02:25:33 +00:00
|
|
|
/* Free allocated instruction */
|
2011-11-26 00:30:17 +00:00
|
|
|
guac_instruction_free(instruction);
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-06 08:41:08 +00:00
|
|
|
int guacd_client_start(guac_client* client) {
|
2011-11-23 08:01:27 +00:00
|
|
|
|
2011-12-06 08:41:08 +00:00
|
|
|
pthread_t input_thread, output_thread;
|
2011-11-23 08:01:27 +00:00
|
|
|
|
2011-12-06 08:41:08 +00:00
|
|
|
if (pthread_create(&output_thread, NULL, __guacd_client_output_thread, (void*) client)) {
|
2011-11-27 06:26:39 +00:00
|
|
|
guac_client_log_error(client, "Unable to start output thread");
|
2011-11-23 08:01:27 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-12-06 08:41:08 +00:00
|
|
|
if (pthread_create(&input_thread, NULL, __guacd_client_input_thread, (void*) client)) {
|
2011-11-27 06:26:39 +00:00
|
|
|
guac_client_log_error(client, "Unable to start input thread");
|
2011-11-23 08:01:27 +00:00
|
|
|
guac_client_stop(client);
|
2011-12-06 08:41:08 +00:00
|
|
|
pthread_join(output_thread, NULL);
|
2011-11-23 08:01:27 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for I/O threads */
|
2011-12-06 08:41:08 +00:00
|
|
|
pthread_join(input_thread, NULL);
|
|
|
|
pthread_join(output_thread, NULL);
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|