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>
47 void guac_client_stop(guac_client* client) {
48 client->state = STOPPING;
51 void* __guac_client_output_thread(void* data) {
53 guac_client* client = (guac_client*) data;
54 GUACIO* io = client->io;
56 guac_timestamp last_ping_timestamp = guac_current_timestamp();
58 /* Guacamole client output loop */
59 while (client->state == RUNNING) {
61 /* Occasionally ping client with repeat of last sync */
62 guac_timestamp timestamp = guac_current_timestamp();
63 if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
64 last_ping_timestamp = timestamp;
66 guac_send_sync(io, client->last_sent_timestamp)
69 guac_client_stop(client);
74 /* Handle server messages */
75 if (client->handle_messages) {
77 /* Only handle messages if synced within threshold */
78 if (client->last_sent_timestamp - client->last_received_timestamp
79 < GUAC_SYNC_THRESHOLD) {
81 int retval = client->handle_messages(client);
83 guac_log_error("Error handling server messages");
84 guac_client_stop(client);
88 /* Sleep as necessary */
89 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
91 /* Send sync instruction */
92 client->last_sent_timestamp = guac_current_timestamp();
93 if (guac_send_sync(io, client->last_sent_timestamp)) {
94 guac_client_stop(client);
99 guac_client_stop(client);
105 /* If sync threshold exceeded, don't spin waiting for resync */
107 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
111 /* If no message handler, just sleep until next sync ping */
113 guac_sleep(GUAC_SYNC_FREQUENCY);
115 } /* End of output loop */
117 guac_client_stop(client);
122 void* __guac_client_input_thread(void* data) {
124 guac_client* client = (guac_client*) data;
125 GUACIO* io = client->io;
127 /* Guacamole client input loop */
128 while (client->state == RUNNING) {
130 /* Read instruction */
131 guac_instruction* instruction =
132 guac_read_instruction(io, GUAC_USEC_TIMEOUT);
135 if (instruction == NULL) {
136 guac_client_stop(client);
140 /* Call handler, stop on error */
141 if (guac_client_handle_instruction(client, instruction) < 0) {
142 guac_free_instruction(instruction);
143 guac_client_stop(client);
147 /* Free allocated instruction */
148 guac_free_instruction(instruction);
156 int guac_start_client(guac_client* client) {
158 guac_thread input_thread, output_thread;
160 if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
164 if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
165 guac_client_stop(client);
166 guac_thread_join(output_thread);
170 /* Wait for I/O threads */
171 guac_thread_join(input_thread);
172 guac_thread_join(output_thread);