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