bfdd60002d2ec84ecbf2a229a4dc126ed330f2d0
[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 #include <pthread.h>
41
42 #include <guacamole/socket.h>
43 #include <guacamole/client.h>
44 #include <guacamole/error.h>
45
46 #include "client.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 __guacdd_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* __guacd_client_output_thread(void* data) {
66
67     guac_client* client = (guac_client*) data;
68     guac_socket* socket = client->socket;
69
70     guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
71
72     /* Guacamole client output loop */
73     while (client->state == RUNNING) {
74
75         /* Occasionally ping client with repeat of last sync */
76         guac_timestamp timestamp = guac_protocol_get_timestamp();
77         if (timestamp - last_ping_timestamp > GUACD_SYNC_FREQUENCY) {
78
79             /* Record time of last synnc */
80             last_ping_timestamp = timestamp;
81
82             /* Send sync */
83             if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
84                 guacd_client_log_guac_error(client,
85                         "Error sending \"sync\" instruction");
86                 guac_client_stop(client);
87                 return NULL;
88             }
89
90             /* Flush */
91             if (guac_socket_flush(socket)) {
92                 guacd_client_log_guac_error(client,
93                         "Error flushing output");
94                 guac_client_stop(client);
95                 return NULL;
96             }
97
98         }
99
100         /* Handle server messages */
101         if (client->handle_messages) {
102
103             /* Only handle messages if synced within threshold */
104             if (client->last_sent_timestamp - client->last_received_timestamp
105                     < GUACD_SYNC_THRESHOLD) {
106
107                 int retval = client->handle_messages(client);
108                 if (retval) {
109                     guacd_client_log_guac_error(client,
110                             "Error handling server messages");
111                     guac_client_stop(client);
112                     return NULL;
113                 }
114
115                 /* Send sync instruction */
116                 client->last_sent_timestamp = guac_protocol_get_timestamp();
117                 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
118                     guacd_client_log_guac_error(client, 
119                             "Error sending \"sync\" instruction");
120                     guac_client_stop(client);
121                     return NULL;
122                 }
123
124                 /* Flush */
125                 if (guac_socket_flush(socket)) {
126                     guacd_client_log_guac_error(client,
127                             "Error flushing output");
128                     guac_client_stop(client);
129                     return NULL;
130                 }
131
132             }
133
134             /* Do not spin while waiting for old sync */
135             else
136                 __guacdd_sleep(GUACD_MESSAGE_HANDLE_FREQUENCY);
137
138         }
139
140         /* If no message handler, just sleep until next sync ping */
141         else
142             __guacdd_sleep(GUACD_SYNC_FREQUENCY);
143
144     } /* End of output loop */
145
146     guac_client_stop(client);
147     return NULL;
148
149 }
150
151 void* __guacd_client_input_thread(void* data) {
152
153     guac_client* client = (guac_client*) data;
154     guac_socket* socket = client->socket;
155
156     /* Guacamole client input loop */
157     while (client->state == RUNNING) {
158
159         /* Read instruction */
160         guac_instruction* instruction =
161             guac_protocol_read_instruction(socket, GUACD_USEC_TIMEOUT);
162
163         /* Stop on error */
164         if (instruction == NULL) {
165             guacd_client_log_guac_error(client,
166                     "Error reading instruction");
167             guac_client_stop(client);
168             return NULL;
169         }
170
171         /* Reset guac_error and guac_error_message (client handlers are not
172          * guaranteed to set these) */
173         guac_error = GUAC_STATUS_SUCCESS;
174         guac_error_message = NULL;
175
176         /* Call handler, stop on error */
177         if (guac_client_handle_instruction(client, instruction) < 0) {
178
179             /* Log error */
180             guacd_client_log_guac_error(client,
181                     "Client instruction handler error");
182
183             /* Log handler details */
184             guac_client_log_info(client,
185                     "Failing instruction handler in client was \"%s\"",
186                     instruction->opcode);
187
188             guac_instruction_free(instruction);
189             guac_client_stop(client);
190             return NULL;
191         }
192
193         /* Free allocated instruction */
194         guac_instruction_free(instruction);
195
196     }
197
198     return NULL;
199
200 }
201
202 int guacd_client_start(guac_client* client) {
203
204     pthread_t input_thread, output_thread;
205
206     if (pthread_create(&output_thread, NULL, __guacd_client_output_thread, (void*) client)) {
207         guac_client_log_error(client, "Unable to start output thread");
208         return -1;
209     }
210
211     if (pthread_create(&input_thread, NULL, __guacd_client_input_thread, (void*) client)) {
212         guac_client_log_error(client, "Unable to start input thread");
213         guac_client_stop(client);
214         pthread_join(output_thread, NULL);
215         return -1;
216     }
217
218     /* Wait for I/O threads */
219     pthread_join(input_thread, NULL);
220     pthread_join(output_thread, NULL);
221
222     /* Done */
223     return 0;
224
225 }
226