[go: up one dir, main page]

Menu

[e97ff7]: / src / server_thread.c  Maximize  Restore  History

Download this file

211 lines (172 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright (C) 2014 Steve Harris et al. (see AUTHORS)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* $Id$
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_LIBPTHREAD
#include <pthread.h>
#endif
#include <sys/types.h>
#if defined(WIN32) || defined(_MSC_VER)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#endif
#include "lo_types_internal.h"
#include "lo/lo.h"
#include "lo/lo_throw.h"
static void thread_func(void *data);
lo_server_thread lo_server_thread_new(const char *port,
lo_err_handler err_h)
{
return lo_server_thread_new_with_proto(port, LO_DEFAULT, err_h);
}
lo_server_thread lo_server_thread_new_multicast(const char *group,
const char *port,
lo_err_handler err_h)
{
lo_server_thread st = malloc(sizeof(struct _lo_server_thread));
st->s = lo_server_new_multicast(group, port, err_h);
st->active = 0;
st->done = 0;
if (!st->s) {
free(st);
return NULL;
}
return st;
}
lo_server_thread lo_server_thread_new_with_proto(const char *port,
int proto,
lo_err_handler err_h)
{
lo_server_thread st = malloc(sizeof(struct _lo_server_thread));
st->s = lo_server_new_with_proto(port, proto, err_h);
st->active = 0;
st->done = 0;
if (!st->s) {
free(st);
return NULL;
}
return st;
}
lo_server_thread lo_server_thread_new_from_url(const char *url,
lo_err_handler err_h)
{
lo_server_thread st = malloc(sizeof(struct _lo_server_thread));
st->s = lo_server_new_from_url(url, err_h);
st->active = 0;
st->done = 0;
if (!st->s) {
free(st);
return NULL;
}
return st;
}
void lo_server_thread_set_error_context(lo_server_thread st,
void *user_data)
{
lo_server_set_error_context(st->s, user_data);
}
void lo_server_thread_free(lo_server_thread st)
{
if (st) {
if (st->active) {
lo_server_thread_stop(st);
}
lo_server_free(st->s);
}
free(st);
}
lo_method lo_server_thread_add_method(lo_server_thread st,
const char *path,
const char *typespec,
lo_method_handler h, void *user_data)
{
return lo_server_add_method(st->s, path, typespec, h, user_data);
}
void lo_server_thread_del_method(lo_server_thread st, const char *path,
const char *typespec)
{
lo_server_del_method(st->s, path, typespec);
}
int lo_server_thread_start(lo_server_thread st)
{
int result;
if (!st->active) {
st->active = 1;
st->done = 0;
// Create the server thread
result =
pthread_create(&(st->thread), NULL, (void *(*)(void *)) &thread_func, st);
if (result) {
fprintf(stderr,
"Failed to create thread: pthread_create(), %s",
strerror(result));
return -result;
}
}
return 0;
}
int lo_server_thread_stop(lo_server_thread st)
{
int result;
if (st->active) {
// Signal thread to stop
st->active = 0;
// pthread_join waits for thread to terminate
// and then releases the thread's resources
result = pthread_join(st->thread, NULL);
if (result) {
fprintf(stderr, "Failed to stop thread: pthread_join(), %s",
strerror(result));
return -result;
}
}
return 0;
}
int lo_server_thread_get_port(lo_server_thread st)
{
return lo_server_get_port(st->s);
}
char *lo_server_thread_get_url(lo_server_thread st)
{
return lo_server_get_url(st->s);
}
lo_server lo_server_thread_get_server(lo_server_thread st)
{
return st->s;
}
int lo_server_thread_events_pending(lo_server_thread st)
{
return lo_server_events_pending(st->s);
}
static void thread_func(void *data)
{
lo_server_thread st = (lo_server_thread) data;
while (st->active) {
lo_server_recv_noblock(st->s, 10);
}
st->done = 1;
pthread_exit(NULL);
}
void lo_server_thread_pp(lo_server_thread st)
{
lo_server_pp(st->s);
}
/* vi:set ts=8 sts=4 sw=4: */