1 /*
2  *  Copyright (C) 2014 Steve Harris et al. (see AUTHORS)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU Lesser General Public License as
6  *  published by the Free Software Foundation; either version 2.1 of the
7  *  License, or (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Lesser General Public License for more details.
13  *
14  *  $Id$
15  */
16 
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #ifdef HAVE_LIBPTHREAD
25 #include <pthread.h>
26 #endif
27 #include <sys/types.h>
28 
29 #if defined(WIN32) || defined(_MSC_VER)
30 #include <winsock2.h>
31 #include <ws2tcpip.h>
32 #include <process.h>
33 #else
34 #include <unistd.h>
35 #include <netdb.h>
36 #include <sys/socket.h>
37 #endif
38 
39 #include "lo_types_internal.h"
40 #include "lo/lo.h"
41 #include "lo/lo_throw.h"
42 
43 #ifdef HAVE_WIN32_THREADS
44 static unsigned __stdcall thread_func(void *data);
45 #else
46 static void* thread_func(void *data);
47 #endif
48 
lo_server_thread_new(const char * port,lo_err_handler err_h)49 lo_server_thread lo_server_thread_new(const char *port,
50                                       lo_err_handler err_h)
51 {
52     return lo_server_thread_new_with_proto(port, LO_DEFAULT, err_h);
53 }
54 
alloc_server_thread(lo_server s)55 static lo_server_thread alloc_server_thread(lo_server s)
56 {
57     lo_server_thread st;
58 
59     if (!s)
60         return NULL;
61 
62     st = (lo_server_thread) malloc(sizeof(struct _lo_server_thread));
63 
64     st->s = s;
65     st->active = 0;
66     st->done = 0;
67     st->cb_init = NULL;
68     st->cb_cleanup = NULL;
69     st->user_data = NULL;
70     return st;
71 }
72 
lo_server_thread_new_multicast(const char * group,const char * port,lo_err_handler err_h)73 lo_server_thread lo_server_thread_new_multicast(const char *group,
74                                                 const char *port,
75                                                 lo_err_handler err_h)
76 {
77     lo_server_thread st = alloc_server_thread(
78         lo_server_new_multicast(group, port, err_h));
79     return st;
80 }
81 
lo_server_thread_new_multicast_iface(const char * group,const char * port,const char * iface,const char * ip,lo_err_handler err_h)82 lo_server_thread lo_server_thread_new_multicast_iface(const char *group, const char *port,
83 						      const char *iface, const char *ip,
84 						      lo_err_handler err_h)
85 {
86     lo_server_thread st = alloc_server_thread(
87         lo_server_new_multicast_iface(group, port, iface, ip, err_h));
88     return st;
89 }
90 
lo_server_thread_new_with_proto(const char * port,int proto,lo_err_handler err_h)91 lo_server_thread lo_server_thread_new_with_proto(const char *port,
92                                                  int proto,
93                                                  lo_err_handler err_h)
94 {
95     lo_server_thread st = alloc_server_thread(
96         lo_server_new_with_proto(port, proto, err_h));
97     return st;
98 }
99 
lo_server_thread_new_from_url(const char * url,lo_err_handler err_h)100 lo_server_thread lo_server_thread_new_from_url(const char *url,
101                                                lo_err_handler err_h)
102 {
103     lo_server_thread st = alloc_server_thread(
104         lo_server_new_from_url(url, err_h));
105     return st;
106 }
107 
lo_server_thread_set_error_context(lo_server_thread st,void * user_data)108 void lo_server_thread_set_error_context(lo_server_thread st,
109                                         void *user_data)
110 {
111     lo_server_set_error_context(st->s, user_data);
112 }
113 
lo_server_thread_free(lo_server_thread st)114 void lo_server_thread_free(lo_server_thread st)
115 {
116     if (st) {
117         if (st->active) {
118             lo_server_thread_stop(st);
119         }
120         lo_server_free(st->s);
121     }
122     free(st);
123 }
124 
lo_server_thread_add_method(lo_server_thread st,const char * path,const char * typespec,lo_method_handler h,const void * user_data)125 lo_method lo_server_thread_add_method(lo_server_thread st,
126                                       const char *path,
127                                       const char *typespec,
128                                       lo_method_handler h,
129                                       const void *user_data)
130 {
131     return lo_server_add_method(st->s, path, typespec, h, user_data);
132 }
133 
lo_server_thread_del_method(lo_server_thread st,const char * path,const char * typespec)134 void lo_server_thread_del_method(lo_server_thread st, const char *path,
135                                  const char *typespec)
136 {
137     lo_server_del_method(st->s, path, typespec);
138 }
139 
lo_server_thread_del_lo_method(lo_server_thread st,lo_method m)140 int lo_server_thread_del_lo_method(lo_server_thread st, lo_method m)
141 {
142     return lo_server_del_lo_method(st->s, m);
143 }
144 
lo_server_thread_set_callbacks(lo_server_thread st,lo_server_thread_init_callback init,lo_server_thread_cleanup_callback cleanup,void * user_data)145 void lo_server_thread_set_callbacks(lo_server_thread st,
146                                     lo_server_thread_init_callback init,
147                                     lo_server_thread_cleanup_callback cleanup,
148                                     void *user_data)
149 {
150     st->cb_init = init;
151     st->cb_cleanup = cleanup;
152     st->user_data = user_data;
153 }
154 
lo_server_thread_start(lo_server_thread st)155 int lo_server_thread_start(lo_server_thread st)
156 {
157     if (!st->active) {
158         st->active = 1;
159         st->done = 0;
160 
161         // Create the server thread
162 #ifdef HAVE_LIBPTHREAD
163         int result;
164         result=pthread_create(&(st->thread), NULL, &thread_func, st);
165         if (result)
166         {
167             fprintf (stderr,
168                 "Failed to create thread: pthread_create(), %s",
169                 strerror (result));
170             return -result;
171         }
172 #else
173 #ifdef HAVE_WIN32_THREADS
174         st->thread = (HANDLE)_beginthreadex (NULL, 0, &thread_func, st, 0, NULL);
175 
176         if (st->thread == NULL)
177         {
178             fprintf (stderr,
179                 "Failed to create thread: Win _beginthreadex(), %s",
180                 strerror (errno));
181             return -1;
182         }
183 #else
184 #error "No threading implementation available."
185 #endif
186 #endif
187     }
188     return 0;
189 }
190 
lo_server_thread_stop(lo_server_thread st)191 int lo_server_thread_stop(lo_server_thread st)
192 {
193     int result;
194 
195     if (st->active) {
196         // Signal thread to stop
197         st->active = 0;
198 
199 #ifdef HAVE_LIBPTHREAD
200         // pthread_join waits for thread to terminate
201         // and then releases the thread's resources
202         result = pthread_join(st->thread, NULL);
203 
204         if (result) {
205             fprintf(stderr, "Failed to stop thread: pthread_join(), %s",
206                     strerror(result));
207             return -result;
208         }
209 #else
210 #ifdef HAVE_WIN32_THREADS
211         result = WaitForSingleObject (st->thread, INFINITE);
212         CloseHandle (st->thread);
213         st->thread = NULL;
214 
215         if (result != 0)
216         {
217             fprintf (stderr, "Failed to join thread: waitForSingleObject(), %d",
218                 result);
219             return -1;
220         }
221 #else
222 #error "No threading implementation available."
223 #endif
224 #endif
225     }
226 
227     return 0;
228 }
229 
lo_server_thread_get_port(lo_server_thread st)230 int lo_server_thread_get_port(lo_server_thread st)
231 {
232     return lo_server_get_port(st->s);
233 }
234 
lo_server_thread_get_url(lo_server_thread st)235 char *lo_server_thread_get_url(lo_server_thread st)
236 {
237     return lo_server_get_url(st->s);
238 }
239 
lo_server_thread_get_server(lo_server_thread st)240 lo_server lo_server_thread_get_server(lo_server_thread st)
241 {
242     return st->s;
243 }
244 
lo_server_thread_events_pending(lo_server_thread st)245 int lo_server_thread_events_pending(lo_server_thread st)
246 {
247     return lo_server_events_pending(st->s);
248 }
249 
250 #ifdef HAVE_WIN32_THREADS
thread_func(void * data)251 static unsigned __stdcall thread_func(void *data)
252 #else
253 static void* thread_func(void *data)
254 #endif
255 {
256     lo_server_thread st = (lo_server_thread) data;
257 
258     if (st->cb_init) {
259         if ( (st->cb_init)(st, st->user_data) )
260         {
261             st->done = 1;
262 #ifdef HAVE_LIBPTHREAD
263             pthread_exit (NULL);
264 #else
265 #ifdef HAVE_WIN32_THREADS
266             _endthread ();
267 #else
268 #error "No threading implementation selected."
269 #endif
270 #endif
271             return 0;
272         }
273     }
274 
275     while (st->active) {
276         lo_server_recv_noblock(st->s, 10);
277     }
278     st->done = 1;
279 
280     if (st->cb_cleanup) {
281         (st->cb_cleanup)(st, st->user_data);
282     }
283 
284 #ifdef HAVE_LIBPTHREAD
285     pthread_exit(NULL);
286 #else
287 #ifdef HAVE_WIN32_THREADS
288     _endthread ();
289 #else
290 #error "No threading implementation selected."
291 #endif
292 #endif
293     return 0;
294 }
295 
lo_server_thread_pp(lo_server_thread st)296 void lo_server_thread_pp(lo_server_thread st)
297 {
298     lo_server_pp(st->s);
299 }
300 
301 /* vi:set ts=8 sts=4 sw=4: */
302