2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is guacd.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Michael Jumper.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2011-11-26 00:30:17 +00:00
|
|
|
#include <time.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>
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
#include "client.h"
|
2011-11-23 08:23:53 +00:00
|
|
|
#include "thread.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-11-26 00:30:17 +00:00
|
|
|
void __guacd_sleep(int millis) {
|
|
|
|
|
|
|
|
struct timespec sleep_period;
|
|
|
|
|
|
|
|
sleep_period.tv_sec = millis / 1000;
|
|
|
|
sleep_period.tv_nsec = (millis % 1000) * 1000000L;
|
|
|
|
|
|
|
|
nanosleep(&sleep_period, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-23 08:01:27 +00:00
|
|
|
void guac_client_stop(guac_client* client) {
|
|
|
|
client->state = STOPPING;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* __guac_client_output_thread(void* data) {
|
|
|
|
|
|
|
|
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
|
|
|
|
2011-11-26 00:30:17 +00:00
|
|
|
guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
/* Guacamole client output loop */
|
|
|
|
while (client->state == RUNNING) {
|
|
|
|
|
|
|
|
/* Occasionally ping client with repeat of last sync */
|
2011-11-26 00:30:17 +00:00
|
|
|
guac_timestamp timestamp = guac_protocol_get_timestamp();
|
2011-11-23 08:01:27 +00:00
|
|
|
if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
|
2011-11-25 02:51:05 +00:00
|
|
|
|
|
|
|
/* Record time of last synnc */
|
2011-11-23 08:01:27 +00:00
|
|
|
last_ping_timestamp = timestamp;
|
2011-11-25 02:51:05 +00:00
|
|
|
|
|
|
|
/* Send sync */
|
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-23 08:01:27 +00:00
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-11-25 02:51:05 +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-25 02:51:05 +00:00
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
< GUAC_SYNC_THRESHOLD) {
|
|
|
|
|
|
|
|
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 */
|
2011-11-26 00:30:17 +00:00
|
|
|
client->last_sent_timestamp = guac_protocol_get_timestamp();
|
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-27 06:26:39 +00:00
|
|
|
/* Sleep before handling more messages */
|
|
|
|
__guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no message handler, just sleep until next sync ping */
|
|
|
|
else
|
2011-11-26 00:30:17 +00:00
|
|
|
__guacd_sleep(GUAC_SYNC_FREQUENCY);
|
2011-11-23 08:01:27 +00:00
|
|
|
|
|
|
|
} /* End of output loop */
|
|
|
|
|
|
|
|
guac_client_stop(client);
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void* __guac_client_input_thread(void* data) {
|
|
|
|
|
|
|
|
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-11-25 02:25:33 +00:00
|
|
|
while (client->state == RUNNING) {
|
|
|
|
|
|
|
|
/* Read instruction */
|
|
|
|
guac_instruction* instruction =
|
2011-11-26 00:35:50 +00:00
|
|
|
guac_protocol_read_instruction(socket, GUAC_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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (guac_thread_create(&input_thread, __guac_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);
|
|
|
|
guac_thread_join(output_thread);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for I/O threads */
|
|
|
|
guac_thread_join(input_thread);
|
|
|
|
guac_thread_join(output_thread);
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|