fb8e9d84fd30a8bfc73e07537dd772b27ab3e814
[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/guacio.h>
42 #include <guacamole/client.h>
43 #include <guacamole/log.h>
44 #include <guacamole/error.h>
45
46 #include "client.h"
47 #include "thread.h"
48
49 void __guacd_sleep(int millis) {
50
51     struct timespec sleep_period;
52
53     sleep_period.tv_sec =   millis / 1000;
54     sleep_period.tv_nsec = (millis % 1000) * 1000000L;
55
56     nanosleep(&sleep_period, NULL);
57
58 }
59
60 void guac_client_stop(guac_client* client) {
61     client->state = STOPPING;
62 }
63
64 void* __guac_client_output_thread(void* data) {
65
66     guac_client* client = (guac_client*) data;
67     guac_socket* socket = client->socket;
68
69     guac_timestamp last_ping_timestamp = guac_protocol_get_timestamp();
70
71     /* Guacamole client output loop */
72     while (client->state == RUNNING) {
73
74         /* Occasionally ping client with repeat of last sync */
75         guac_timestamp timestamp = guac_protocol_get_timestamp();
76         if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
77
78             /* Record time of last synnc */
79             last_ping_timestamp = timestamp;
80
81             /* Send sync */
82             if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
83                 guac_client_log_error(client, "Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
84                 guac_client_stop(client);
85                 return NULL;
86             }
87
88             /* Flush */
89             if (guac_socket_flush(socket)) {
90                 guac_client_log_error(client, "Error flushing output: %s", guac_status_string(guac_error));
91                 guac_client_stop(client);
92                 return NULL;
93             }
94
95         }
96
97         /* Handle server messages */
98         if (client->handle_messages) {
99
100             /* Only handle messages if synced within threshold */
101             if (client->last_sent_timestamp - client->last_received_timestamp
102                     < GUAC_SYNC_THRESHOLD) {
103
104                 int retval = client->handle_messages(client);
105                 if (retval) {
106                     guac_client_log_error(client, "Error handling server messages: %s", guac_status_string(guac_error));
107                     guac_client_stop(client);
108                     return NULL;
109                 }
110
111                 /* Sleep as necessary */
112                 __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
113
114                 /* Send sync instruction */
115                 client->last_sent_timestamp = guac_protocol_get_timestamp();
116                 if (guac_protocol_send_sync(socket, client->last_sent_timestamp)) {
117                     guac_client_log_error(client, "Error sending \"sync\" instruction: %s", guac_status_string(guac_error));
118                     guac_client_stop(client);
119                     return NULL;
120                 }
121
122                 if (guac_socket_flush(socket)) {
123                     guac_client_log_error(client, "Error flushing output: %s", guac_status_string(guac_error));
124                     guac_client_stop(client);
125                     return NULL;
126                 }
127
128             }
129
130             /* If sync threshold exceeded, don't spin waiting for resync */
131             else
132                 __guacd_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
133
134         }
135
136         /* If no message handler, just sleep until next sync ping */
137         else
138             __guacd_sleep(GUAC_SYNC_FREQUENCY);
139
140     } /* End of output loop */
141
142     guac_client_stop(client);
143     return NULL;
144
145 }
146
147 void* __guac_client_input_thread(void* data) {
148
149     guac_client* client = (guac_client*) data;
150     guac_socket* socket = client->socket;
151
152     /* Guacamole client input loop */
153     while (client->state == RUNNING) {
154
155         /* Read instruction */
156         guac_instruction* instruction =
157             guac_protocol_read_instruction(socket, GUAC_USEC_TIMEOUT);
158
159         /* Stop on error */
160         if (instruction == NULL) {
161             guac_client_log_error(client, "Error reading instruction: %s", guac_status_string(guac_error));
162             guac_client_stop(client);
163             return NULL;
164         }
165
166         /* Call handler, stop on error */
167         if (guac_client_handle_instruction(client, instruction) < 0) {
168             guac_client_log_error(client, "Error in client \"%s\" instruction handler: %s", instruction->opcode, guac_status_string(guac_error));
169             guac_instruction_free(instruction);
170             guac_client_stop(client);
171             return NULL;
172         }
173
174         /* Free allocated instruction */
175         guac_instruction_free(instruction);
176
177     }
178
179     return NULL;
180
181 }
182
183 int guac_start_client(guac_client* client) {
184
185     guac_thread input_thread, output_thread;
186
187     if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
188         return -1;
189     }
190
191     if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
192         guac_client_stop(client);
193         guac_thread_join(output_thread);
194         return -1;
195     }
196
197     /* Wait for I/O threads */
198     guac_thread_join(input_thread);
199     guac_thread_join(output_thread);
200
201     /* Done */
202     return 0;
203
204 }
205
206