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>
49 * Sleep for the given number of milliseconds.
51 * @param millis The number of milliseconds to sleep.
53 void __guacd_sleep(int millis) {
55 struct timespec sleep_period;
57 sleep_period.tv_sec = millis / 1000;
58 sleep_period.tv_nsec = (millis % 1000) * 1000000L;
60 nanosleep(&sleep_period, NULL);
64 void guac_client_stop(guac_client* client) {
65 client->state = STOPPING;
68 void* __guac_client_output_thread(void* data) {
70 guac_client* client = (guac_client*) data;
71 guac_socket* socket = client->socket;
73 guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
75 /* Guacamole client output loop */
76 while (client->state == RUNNING) {
78 /* Occasionally ping client with repeat of last sync */
79 guac_timestamp timestamp = guac_protocol_get_timestamp();
80 if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
82 /* Record time of last synnc */
83 last_ping_timestamp = timestamp;
86 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
87 guac_client_log_error(client,
88 "Error sending \"sync\" instruction: %s",
89 guac_status_string(guac_error));
90 guac_client_stop(client);
95 if (guac_socket_flush(socket)) {
96 guac_client_log_error(client,
97 "Error flushing output: %s",
98 guac_status_string(guac_error));
99 guac_client_stop(client);
105 /* Handle server messages */
106 if (client->handle_messages) {
108 /* Only handle messages if synced within threshold */
109 if (client->last_sent_timestamp - client->last_received_timestamp
110 < GUAC_SYNC_THRESHOLD) {
112 int retval = client->handle_messages(client);
114 guac_client_log_error(client,
115 "Error handling server messages: %s",
116 guac_status_string(guac_error));
117 guac_client_stop(client);
121 /* Send sync instruction */
122 client->last_sent_timestamp = guac_protocol_get_timestamp();
123 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
124 guac_client_log_error(client,
125 "Error sending \"sync\" instruction: %s",
126 guac_status_string(guac_error));
127 guac_client_stop(client);
132 if (guac_socket_flush(socket)) {
133 guac_client_log_error(client,
134 "Error flushing output: %s",
135 guac_status_string(guac_error));
136 guac_client_stop(client);
142 /* Sleep before handling more messages */
143 __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
147 /* If no message handler, just sleep until next sync ping */
149 __guacd_sleep(GUAC_SYNC_FREQUENCY);
151 } /* End of output loop */
153 guac_client_stop(client);
158 void* __guac_client_input_thread(void* data) {
160 guac_client* client = (guac_client*) data;
161 guac_socket* socket = client->socket;
163 /* Guacamole client input loop */
164 while (client->state == RUNNING) {
166 /* Read instruction */
167 guac_instruction* instruction =
168 guac_protocol_read_instruction(socket, GUAC_USEC_TIMEOUT);
171 if (instruction == NULL) {
172 guac_client_log_error(client,
173 "Error reading instruction: %s",
174 guac_status_string(guac_error));
175 guac_client_stop(client);
179 /* Call handler, stop on error */
180 if (guac_client_handle_instruction(client, instruction) < 0) {
181 guac_client_log_error(client,
182 "Error in client \"%s\" instruction handler: %s",
183 instruction->opcode, guac_status_string(guac_error));
184 guac_instruction_free(instruction);
185 guac_client_stop(client);
189 /* Free allocated instruction */
190 guac_instruction_free(instruction);
198 int guac_start_client(guac_client* client) {
200 guac_thread input_thread, output_thread;
202 if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
203 guac_client_log_error(client, "Unable to start output thread");
207 if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
208 guac_client_log_error(client, "Unable to start input thread");
209 guac_client_stop(client);
210 guac_thread_join(output_thread);
214 /* Wait for I/O threads */
215 guac_thread_join(input_thread);
216 guac_thread_join(output_thread);