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