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 /* Get previous GUACIO state */
78 int last_total_written = io->total_written;
80 /* Only handle messages if synced within threshold */
81 if (client->last_sent_timestamp - client->last_received_timestamp
82 < GUAC_SYNC_THRESHOLD) {
84 int retval = client->handle_messages(client);
86 guac_log_error("Error handling server messages");
87 guac_client_stop(client);
91 /* If data was written during message handling */
92 if (io->total_written != last_total_written) {
94 /* Sleep as necessary */
95 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
97 /* Send sync instruction */
98 client->last_sent_timestamp = guac_current_timestamp();
99 if (guac_send_sync(io, client->last_sent_timestamp)) {
100 guac_client_stop(client);
106 if (guac_flush(io)) {
107 guac_client_stop(client);
113 /* If sync threshold exceeded, don't spin waiting for resync */
115 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
119 /* If no message handler, just sleep until next sync ping */
121 guac_sleep(GUAC_SYNC_FREQUENCY);
123 } /* End of output loop */
125 guac_client_stop(client);
130 void* __guac_client_input_thread(void* data) {
132 guac_client* client = (guac_client*) data;
133 GUACIO* io = client->io;
135 guac_instruction instruction;
137 /* Guacamole client input loop */
138 while (client->state == RUNNING && guac_read_instruction(io, GUAC_USEC_TIMEOUT, &instruction) > 0) {
140 /* Call handler, stop on error */
141 if (guac_client_handle_instruction(client, &instruction) < 0) {
142 guac_free_instruction_data(&instruction);
146 /* Free allocate instruction data */
147 guac_free_instruction_data(&instruction);
151 guac_client_stop(client);
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);