Migration to new guac_read_instruction(), remove use of guac_free_instruction_data().
[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
40 #include <guacamole/guacio.h>
41 #include <guacamole/client.h>
42 #include <guacamole/log.h>
43
44 #include "client.h"
45 #include "thread.h"
46
47 void guac_client_stop(guac_client* client) {
48     client->state = STOPPING;
49 }
50
51 void* __guac_client_output_thread(void* data) {
52
53     guac_client* client = (guac_client*) data;
54     GUACIO* io = client->io;
55
56     guac_timestamp last_ping_timestamp = guac_current_timestamp();
57
58     /* Guacamole client output loop */
59     while (client->state == RUNNING) {
60
61         /* Occasionally ping client with repeat of last sync */
62         guac_timestamp timestamp = guac_current_timestamp();
63         if (timestamp - last_ping_timestamp > GUAC_SYNC_FREQUENCY) {
64             last_ping_timestamp = timestamp;
65             if (
66                    guac_send_sync(io, client->last_sent_timestamp)
67                 || guac_flush(io)
68                ) {
69                 guac_client_stop(client);
70                 return NULL;
71             }
72         }
73
74         /* Handle server messages */
75         if (client->handle_messages) {
76
77             /* Only handle messages if synced within threshold */
78             if (client->last_sent_timestamp - client->last_received_timestamp
79                     < GUAC_SYNC_THRESHOLD) {
80
81                 int retval = client->handle_messages(client);
82                 if (retval) {
83                     guac_log_error("Error handling server messages");
84                     guac_client_stop(client);
85                     return NULL;
86                 }
87
88                 /* Sleep as necessary */
89                 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
90
91                 /* Send sync instruction */
92                 client->last_sent_timestamp = guac_current_timestamp();
93                 if (guac_send_sync(io, client->last_sent_timestamp)) {
94                     guac_client_stop(client);
95                     return NULL;
96                 }
97
98                 if (guac_flush(io)) {
99                     guac_client_stop(client);
100                     return NULL;
101                 }
102
103             }
104
105             /* If sync threshold exceeded, don't spin waiting for resync */
106             else
107                 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
108
109         }
110
111         /* If no message handler, just sleep until next sync ping */
112         else
113             guac_sleep(GUAC_SYNC_FREQUENCY);
114
115     } /* End of output loop */
116
117     guac_client_stop(client);
118     return NULL;
119
120 }
121
122 void* __guac_client_input_thread(void* data) {
123
124     guac_client* client = (guac_client*) data;
125     GUACIO* io = client->io;
126
127     /* Guacamole client input loop */
128     while (client->state == RUNNING) {
129
130         /* Read instruction */
131         guac_instruction* instruction =
132             guac_read_instruction(io, GUAC_USEC_TIMEOUT);
133
134         /* Stop on error */
135         if (instruction == NULL) {
136             guac_client_stop(client);
137             return NULL;
138         }
139
140         /* Call handler, stop on error */
141         if (guac_client_handle_instruction(client, instruction) < 0) {
142             guac_free_instruction(instruction);
143             guac_client_stop(client);
144             return NULL;
145         }
146
147         /* Free allocated instruction */
148         guac_free_instruction(instruction);
149
150     }
151
152     return NULL;
153
154 }
155
156 int guac_start_client(guac_client* client) {
157
158     guac_thread input_thread, output_thread;
159
160     if (guac_thread_create(&output_thread, __guac_client_output_thread, (void*) client)) {
161         return -1;
162     }
163
164     if (guac_thread_create(&input_thread, __guac_client_input_thread, (void*) client)) {
165         guac_client_stop(client);
166         guac_thread_join(output_thread);
167         return -1;
168     }
169
170     /* Wait for I/O threads */
171     guac_thread_join(input_thread);
172     guac_thread_join(output_thread);
173
174     /* Done */
175     return 0;
176
177 }
178
179