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>
50 * Sleep for the given number of milliseconds.
52 * @param millis The number of milliseconds to sleep.
54 void __guacd_sleep(int millis) {
56 struct timespec sleep_period;
58 sleep_period.tv_sec = millis / 1000;
59 sleep_period.tv_nsec = (millis % 1000) * 1000000L;
61 nanosleep(&sleep_period, NULL);
65 void guac_client_stop(guac_client* client) {
66 client->state = STOPPING;
69 void* __guac_client_output_thread(void* data) {
71 guac_client* client = (guac_client*) data;
72 guac_socket* socket = client->socket;
74 guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
76 /* Guacamole client output loop */
77 while (client->state == RUNNING) {
79 /* Occasionally ping client with repeat of last sync */
80 guac_timestamp timestamp = guac_protocol_get_timestamp();
81 if (timestamp - last_ping_timestamp > GUACD_SYNC_FREQUENCY) {
83 /* Record time of last synnc */
84 last_ping_timestamp = timestamp;
87 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
88 guacd_client_log_guac_error(client,
89 "Error sending \"sync\" instruction");
90 guac_client_stop(client);
95 if (guac_socket_flush(socket)) {
96 guacd_client_log_guac_error(client,
97 "Error flushing output");
98 guac_client_stop(client);
104 /* Handle server messages */
105 if (client->handle_messages) {
107 /* Only handle messages if synced within threshold */
108 if (client->last_sent_timestamp - client->last_received_timestamp
109 < GUACD_SYNC_THRESHOLD) {
111 int retval = client->handle_messages(client);
113 guacd_client_log_guac_error(client,
114 "Error handling server messages");
115 guac_client_stop(client);
119 /* Send sync instruction */
120 client->last_sent_timestamp = guac_protocol_get_timestamp();
121 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
122 guacd_client_log_guac_error(client,
123 "Error sending \"sync\" instruction");
124 guac_client_stop(client);
129 if (guac_socket_flush(socket)) {
130 guacd_client_log_guac_error(client,
131 "Error flushing output");
132 guac_client_stop(client);
138 /* Do not spin while waiting for old sync */
140 __guacd_sleep(GUACD_MESSAGE_HANDLE_FREQUENCY);
144 /* If no message handler, just sleep until next sync ping */
146 __guacd_sleep(GUACD_SYNC_FREQUENCY);
148 } /* End of output loop */
150 guac_client_stop(client);
155 void* __guac_client_input_thread(void* data) {
157 guac_client* client = (guac_client*) data;
158 guac_socket* socket = client->socket;
160 /* Guacamole client input loop */
161 while (client->state == RUNNING) {
163 /* Read instruction */
164 guac_instruction* instruction =
165 guac_protocol_read_instruction(socket, GUACD_USEC_TIMEOUT);
168 if (instruction == NULL) {
169 guacd_client_log_guac_error(client,
170 "Error reading instruction");
171 guac_client_stop(client);
175 /* Reset guac_error and guac_error_message (client handlers are not
176 * guaranteed to set these) */
177 guac_error = GUAC_STATUS_SUCCESS;
178 guac_error_message = NULL;
180 /* Call handler, stop on error */
181 if (guac_client_handle_instruction(client, instruction) < 0) {
184 guacd_client_log_guac_error(client,
185 "Client instruction handler error");
187 /* Log handler details */
188 guac_client_log_info(client,
189 "Failing instruction handler in client was \"%s\"",
190 instruction->opcode);
192 guac_instruction_free(instruction);
193 guac_client_stop(client);
197 /* Free allocated instruction */
198 guac_instruction_free(instruction);
206 int guac_start_client(guac_client* client) {
208 guac_thread input_thread, output_thread;
210 if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
211 guac_client_log_error(client, "Unable to start output thread");
215 if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
216 guac_client_log_error(client, "Unable to start input thread");
217 guac_client_stop(client);
218 guac_thread_join(output_thread);
222 /* Wait for I/O threads */
223 guac_thread_join(input_thread);
224 guac_thread_join(output_thread);