a918f68e3153eca63a32379e801dc365dfd4703b
[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             /* Get previous GUACIO state */
78             int last_total_written = io->total_written;
79
80             /* Only handle messages if synced within threshold */
81             if (client->last_sent_timestamp - client->last_received_timestamp
82                     < GUAC_SYNC_THRESHOLD) {
83
84                 int retval = client->handle_messages(client);
85                 if (retval) {
86                     guac_log_error("Error handling server messages");
87                     guac_client_stop(client);
88                     return NULL;
89                 }
90
91                 /* If data was written during message handling */
92                 if (io->total_written != last_total_written) {
93
94                     /* Sleep as necessary */
95                     guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
96
97                     /* Send sync instruction */
98                     client->last_sent_timestamp = guac_current_timestamp();
99                     if (guac_send_sync(io, client->last_sent_timestamp)) {
100                         guac_client_stop(client);
101                         return NULL;
102                     }
103
104                 }
105
106                 if (guac_flush(io)) {
107                     guac_client_stop(client);
108                     return NULL;
109                 }
110
111             }
112
113             /* If sync threshold exceeded, don't spin waiting for resync */
114             else
115                 guac_sleep(GUAC_SERVER_MESSAGE_HANDLE_FREQUENCY);
116
117         }
118
119         /* If no message handler, just sleep until next sync ping */
120         else
121             guac_sleep(GUAC_SYNC_FREQUENCY);
122
123     } /* End of output loop */
124
125     guac_client_stop(client);
126     return NULL;
127
128 }
129
130 void* __guac_client_input_thread(void* data) {
131
132     guac_client* client = (guac_client*) data;
133     GUACIO* io = client->io;
134
135     guac_instruction instruction;
136
137     /* Guacamole client input loop */
138     while (client->state == RUNNING && guac_read_instruction(io, GUAC_USEC_TIMEOUT, &instruction) > 0) {
139
140         /* Call handler, stop on error */
141         if (guac_client_handle_instruction(client, &instruction) < 0) {
142             guac_free_instruction_data(&instruction);
143             break;
144         }
145
146         /* Free allocate instruction data */
147         guac_free_instruction_data(&instruction);
148
149     }
150
151     guac_client_stop(client);
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