Removing forced sleep between message handling. Can cause excess delays if handle_mes...
[guacd.git] / src / client.c
1
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
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/
9  *
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
13  * License.
14  *
15  * The Original Code is guacd.
16  *
17  * The Initial Developer of the Original Code is
18  * Michael Jumper.
19  * Portions created by the Initial Developer are Copyright (C) 2010
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
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.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #include <stdlib.h>
39 #include <time.h>
40
41 #include <guacamole/socket.h>
42 #include <guacamole/client.h>
43 #include <guacamole/error.h>
44
45 #include "client.h"
46 #include "thread.h"
47 #include "log.h"
48
49 /**
50  * Sleep for the given number of milliseconds.
51  *
52  * @param millis The number of milliseconds to sleep.
53  */
54 void __guacd_sleep(int millis) {
55
56     struct timespec sleep_period;
57
58     sleep_period.tv_sec =   millis / 1000;
59     sleep_period.tv_nsec = (millis % 1000) * 1000000L;
60
61     nanosleep(&sleep_period, NULL);
62
63 }
64
65 void guac_client_stop(guac_client* client) {
66     client->state = STOPPING;
67 }
68
69 void* __guac_client_output_thread(void* data) {
70
71     guac_client* client = (guac_client*) data;
72     guac_socket* socket = client->socket;
73
74     guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
75
76     /* Guacamole client output loop */
77     while (client->state == RUNNING) {
78
79         /* Occasionally ping client with repeat of last sync */
80         guac_timestamp timestamp = guac_protocol_get_timestamp();
81         if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
82
83             /* Record time of last synnc */
84             last_ping_timestamp = timestamp;
85
86             /* Send sync */
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);
91                 return NULL;
92             }
93
94             /* Flush */
95             if (guac_socket_flush(socket)) {
96                 guacd_client_log_guac_error(client,
97                         "Error flushing output");
98                 guac_client_stop(client);
99                 return NULL;
100             }
101
102         }
103
104         /* Handle server messages */
105         if (client->handle_messages) {
106
107             /* Only handle messages if synced within threshold */
108             if (client->last_sent_timestamp - client->last_received_timestamp
109                     < GUAC_SYNC_THRESHOLD) {
110
111                 int retval = client->handle_messages(client);
112                 if (retval) {
113                     guacd_client_log_guac_error(client,
114                             "Error handling server messages");
115                     guac_client_stop(client);
116                     return NULL;
117                 }
118
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);
125                     return NULL;
126                 }
127
128                 /* Flush */
129                 if (guac_socket_flush(socket)) {
130                     guacd_client_log_guac_error(client,
131                             "Error flushing output");
132                     guac_client_stop(client);
133                     return NULL;
134                 }
135
136             }
137
138             /* Do not spin while waiting for old sync */
139             else
140                 __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
141
142         }
143
144         /* If no message handler, just sleep until next sync ping */
145         else
146             __guacd_sleep(GUAC_SYNC_FREQUENCY);
147
148     } /* End of output loop */
149
150     guac_client_stop(client);
151     return NULL;
152
153 }
154
155 void* __guac_client_input_thread(void* data) {
156
157     guac_client* client = (guac_client*) data;
158     guac_socket* socket = client->socket;
159
160     /* Guacamole client input loop */
161     while (client->state == RUNNING) {
162
163         /* Read instruction */
164         guac_instruction* instruction =
165             guac_protocol_read_instruction(socket, GUAC_USEC_TIMEOUT);
166
167         /* Stop on error */
168         if (instruction == NULL) {
169             guacd_client_log_guac_error(client,
170                     "Error reading instruction");
171             guac_client_stop(client);
172             return NULL;
173         }
174
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;
179
180         /* Call handler, stop on error */
181         if (guac_client_handle_instruction(client, instruction) < 0) {
182
183             /* Log error */
184             guacd_client_log_guac_error(client,
185                     "Client instruction handler error");
186
187             /* Log handler details */
188             guac_client_log_info(client,
189                     "Failing instruction handler in client was \"%s\"",
190                     instruction->opcode);
191
192             guac_instruction_free(instruction);
193             guac_client_stop(client);
194             return NULL;
195         }
196
197         /* Free allocated instruction */
198         guac_instruction_free(instruction);
199
200     }
201
202     return NULL;
203
204 }
205
206 int guac_start_client(guac_client* client) {
207
208     guac_thread input_thread, output_thread;
209
210     if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
211         guac_client_log_error(client, "Unable to start output thread");
212         return -1;
213     }
214
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);
219         return -1;
220     }
221
222     /* Wait for I/O threads */
223     guac_thread_join(input_thread);
224     guac_thread_join(output_thread);
225
226     /* Done */
227     return 0;
228
229 }
230