2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is guacd.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2010
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
40 #include <guacamole/guacio.h>
41 #include <guacamole/client.h>
42 #include <guacamole/log.h>
43 #include <guacamole/error.h>
48 void guac_client_stop(guac_client* client) {
49 client->state = STOPPING;
52 void* __guac_client_output_thread(void* data) {
54 guac_client* client = (guac_client*) data;
55 GUACIO* io = client->io;
57 guac_timestamp last_ping_timestamp = guac_current_timestamp();
59 /* Guacamole client output loop */
60 while (client->state == RUNNING) {
62 /* Occasionally ping client with repeat of last sync */
63 guac_timestamp timestamp = guac_current_timestamp();
64 if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
66 /* Record time of last synnc */
67 last_ping_timestamp = timestamp;
70 if (guac_send_sync(io, client->last_sent_timestamp)) {
71 guac_log_error("Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
72 guac_client_stop(client);
78 guac_log_error("Error flushing output: %s", guac_status_string(guac_error));
79 guac_client_stop(client);
85 /* Handle server messages */
86 if (client->handle_messages) {
88 /* Only handle messages if synced within threshold */
89 if (client->last_sent_timestamp - client->last_received_timestamp
90 < GUAC_SYNC_THRESHOLD) {
92 int retval = client->handle_messages(client);
94 guac_log_error("Error handling server messages: %s", guac_status_string(guac_error));
95 guac_client_stop(client);
99 /* Sleep as necessary */
100 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
102 /* Send sync instruction */
103 client->last_sent_timestamp = guac_current_timestamp();
104 if (guac_send_sync(io, client->last_sent_timestamp)) {
105 guac_log_error("Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
106 guac_client_stop(client);
110 if (guac_flush(io)) {
111 guac_log_error("Error flushing output: %s", guac_status_string(guac_error));
112 guac_client_stop(client);
118 /* If sync threshold exceeded, don't spin waiting for resync */
120 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
124 /* If no message handler, just sleep until next sync ping */
126 guac_sleep(GUAC_SYNC_FREQUENCY);
128 } /* End of output loop */
130 guac_client_stop(client);
135 void* __guac_client_input_thread(void* data) {
137 guac_client* client = (guac_client*) data;
138 GUACIO* io = client->io;
140 /* Guacamole client input loop */
141 while (client->state == RUNNING) {
143 /* Read instruction */
144 guac_instruction* instruction =
145 guac_read_instruction(io, GUAC_USEC_TIMEOUT);
148 if (instruction == NULL) {
149 guac_log_error("Error reading instruction: %s", guac_status_string(guac_error));
150 guac_client_stop(client);
154 /* Call handler, stop on error */
155 if (guac_client_handle_instruction(client, instruction) < 0) {
156 guac_log_error("Error in client \"%s\" instruction handler: %s", instruction->opcode, guac_status_string(guac_error));
157 guac_free_instruction(instruction);
158 guac_client_stop(client);
162 /* Free allocated instruction */
163 guac_free_instruction(instruction);
171 int guac_start_client(guac_client* client) {
173 guac_thread input_thread, output_thread;
175 if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
179 if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
180 guac_client_stop(client);
181 guac_thread_join(output_thread);
185 /* Wait for I/O threads */
186 guac_thread_join(input_thread);
187 guac_thread_join(output_thread);