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 ***** */
41 #include <guacamole/socket.h>
42 #include <guacamole/client.h>
43 #include <guacamole/error.h>
48 void __guacd_sleep(int millis) {
50 struct timespec sleep_period;
52 sleep_period.tv_sec = millis / 1000;
53 sleep_period.tv_nsec = (millis % 1000) * 1000000L;
55 nanosleep(&sleep_period, NULL);
59 void guac_client_stop(guac_client* client) {
60 client->state = STOPPING;
63 void* __guac_client_output_thread(void* data) {
65 guac_client* client = (guac_client*) data;
66 guac_socket* socket = client->socket;
68 guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
70 /* Guacamole client output loop */
71 while (client->state == RUNNING) {
73 /* Occasionally ping client with repeat of last sync */
74 guac_timestamp timestamp = guac_protocol_get_timestamp();
75 if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
77 /* Record time of last synnc */
78 last_ping_timestamp = timestamp;
81 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
82 guac_client_log_error(client, "Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
83 guac_client_stop(client);
88 if (guac_socket_flush(socket)) {
89 guac_client_log_error(client, "Error flushing output: %s", guac_status_string(guac_error));
90 guac_client_stop(client);
96 /* Handle server messages */
97 if (client->handle_messages) {
99 /* Only handle messages if synced within threshold */
100 if (client->last_sent_timestamp - client->last_received_timestamp
101 < GUAC_SYNC_THRESHOLD) {
103 int retval = client->handle_messages(client);
105 guac_client_log_error(client, "Error handling server messages: %s", guac_status_string(guac_error));
106 guac_client_stop(client);
110 /* Sleep as necessary */
111 __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
113 /* Send sync instruction */
114 client->last_sent_timestamp = guac_protocol_get_timestamp();
115 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
116 guac_client_log_error(client, "Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
117 guac_client_stop(client);
121 if (guac_socket_flush(socket)) {
122 guac_client_log_error(client, "Error flushing output: %s", guac_status_string(guac_error));
123 guac_client_stop(client);
129 /* If sync threshold exceeded, don't spin waiting for resync */
131 __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
135 /* If no message handler, just sleep until next sync ping */
137 __guacd_sleep(GUAC_SYNC_FREQUENCY);
139 } /* End of output loop */
141 guac_client_stop(client);
146 void* __guac_client_input_thread(void* data) {
148 guac_client* client = (guac_client*) data;
149 guac_socket* socket = client->socket;
151 /* Guacamole client input loop */
152 while (client->state == RUNNING) {
154 /* Read instruction */
155 guac_instruction* instruction =
156 guac_protocol_read_instruction(socket, GUAC_USEC_TIMEOUT);
159 if (instruction == NULL) {
160 guac_client_log_error(client, "Error reading instruction: %s", guac_status_string(guac_error));
161 guac_client_stop(client);
165 /* Call handler, stop on error */
166 if (guac_client_handle_instruction(client, instruction) < 0) {
167 guac_client_log_error(client, "Error in client \"%s\" instruction handler: %s", instruction->opcode, guac_status_string(guac_error));
168 guac_instruction_free(instruction);
169 guac_client_stop(client);
173 /* Free allocated instruction */
174 guac_instruction_free(instruction);
182 int guac_start_client(guac_client* client) {
184 guac_thread input_thread, output_thread;
186 if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
190 if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
191 guac_client_stop(client);
192 guac_thread_join(output_thread);
196 /* Wait for I/O threads */
197 guac_thread_join(input_thread);
198 guac_thread_join(output_thread);