1 | //clients.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
22 | * Boston, MA 02110-1301, USA. |
---|
23 | * |
---|
24 | */ |
---|
25 | |
---|
26 | /* ckport options: |
---|
27 | * ckport: ignore-symbol: roar_event_to_blob of target libroar0 |
---|
28 | */ |
---|
29 | |
---|
30 | #include "roard.h" |
---|
31 | |
---|
32 | // declared 'extern' |
---|
33 | struct roar_client_server * g_clients[ROAR_CLIENTS_MAX]; |
---|
34 | |
---|
35 | static struct roard_proto_handle __protos[MAX_PROTOS] = { |
---|
36 | {.proto = ROAR_PROTO_ROARAUDIO, .lhandle = NULL, .type = ROARD_PROTO_TYPE_BUILDIN, |
---|
37 | .impl = {.buildin = 0}}, |
---|
38 | #if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD) |
---|
39 | {.proto = ROAR_PROTO_ESOUND, .lhandle = NULL, .type = ROARD_PROTO_TYPE_ROARDPROTO, |
---|
40 | .impl = {.roardproto = {ROAR_PROTO_ESOUND, ROAR_SUBSYS_WAVEFORM, "EsounD emulation", NULL, NULL, NULL, emul_esd_check_client, NULL, NULL}}}, |
---|
41 | #endif |
---|
42 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY |
---|
43 | {.proto = ROAR_PROTO_RPLAY, .lhandle = NULL, .type = ROARD_PROTO_TYPE_ROARDPROTO, |
---|
44 | .impl = {.roardproto = {ROAR_PROTO_RPLAY, ROAR_SUBSYS_WAVEFORM, "RPlay emulation", NULL, NULL, NULL, emul_rplay_check_client, NULL, NULL}}}, |
---|
45 | #endif |
---|
46 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER |
---|
47 | {.proto = ROAR_PROTO_GOPHER, .lhandle = NULL, .type = ROARD_PROTO_TYPE_ROARDPROTO, |
---|
48 | .impl = {.roardproto = {ROAR_PROTO_GOPHER, ROAR_SUBSYS_WAVEFORM, "The Internet Gopher Protocol", NULL, NULL, NULL, emul_gopher_check_client, NULL, |
---|
49 | emul_gopher_flushed_client}}}, |
---|
50 | #endif |
---|
51 | {.proto = -1} |
---|
52 | }; |
---|
53 | |
---|
54 | #define _CHECK_CID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_CLIENTS_MAX || g_clients[(id)] == NULL ) return (ret) |
---|
55 | #define _CHECK_CID(id) _CHECK_CID_RET((id), -1) |
---|
56 | |
---|
57 | int clients_init (void) { |
---|
58 | int i; |
---|
59 | |
---|
60 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
61 | g_clients[i] = NULL; |
---|
62 | |
---|
63 | for (i = 0; __protos[i].proto != -1; i++); |
---|
64 | |
---|
65 | for (; i < MAX_PROTOS; i++) |
---|
66 | __protos[i].proto = -1; |
---|
67 | |
---|
68 | return 0; |
---|
69 | } |
---|
70 | |
---|
71 | int clients_free (void) { |
---|
72 | int i; |
---|
73 | |
---|
74 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
75 | if ( g_clients[i] != NULL ) |
---|
76 | clients_delete(i); |
---|
77 | |
---|
78 | return 0; |
---|
79 | } |
---|
80 | |
---|
81 | int clients_new (void) { |
---|
82 | int i; |
---|
83 | int s; |
---|
84 | struct roar_client_server * ns; |
---|
85 | struct roar_client * n; |
---|
86 | |
---|
87 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
88 | if ( g_clients[i] == NULL ) { |
---|
89 | ns = roar_mm_malloc(sizeof(struct roar_client_server)); |
---|
90 | n = ROAR_CLIENT(ns); |
---|
91 | |
---|
92 | memset(ns, 0, sizeof(struct roar_client_server)); |
---|
93 | |
---|
94 | if ( n != NULL ) { |
---|
95 | n->pid = -1; |
---|
96 | n->uid = -1; |
---|
97 | n->gid = -1; |
---|
98 | n->fh = -1; |
---|
99 | |
---|
100 | *n->name = 0; |
---|
101 | |
---|
102 | n->proto = ROAR_PROTO_ROARAUDIO; |
---|
103 | n->byteorder = ROAR_BYTEORDER_NETWORK; |
---|
104 | |
---|
105 | n->acl = NULL; |
---|
106 | |
---|
107 | n->execed = -1; |
---|
108 | for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++) |
---|
109 | n->streams[s] = -1; |
---|
110 | |
---|
111 | if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) { |
---|
112 | roar_mm_free(n); |
---|
113 | return -1; |
---|
114 | } |
---|
115 | |
---|
116 | ns->blockc = 0; |
---|
117 | ns->waits = NULL; |
---|
118 | ns->acclev = ACCLEV_NONE; |
---|
119 | |
---|
120 | g_clients[i] = ns; |
---|
121 | |
---|
122 | counters_inc(clients, 1); |
---|
123 | roar_notify_core_emit_snoargs(ROAR_OE_BASICS_NEW, -1, i, ROAR_OT_CLIENT); |
---|
124 | ROAR_DBG("clients_new(void) = %i", i); |
---|
125 | return i; |
---|
126 | } else { |
---|
127 | ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno)); |
---|
128 | ROAR_ERR("clients_new(void) = -1"); |
---|
129 | return -1; |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | return -1; |
---|
135 | } |
---|
136 | |
---|
137 | int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) { |
---|
138 | struct roar_client * c; |
---|
139 | int client; |
---|
140 | |
---|
141 | if ( fh == -1 ) |
---|
142 | return -1; |
---|
143 | |
---|
144 | if ( proto != ROAR_PROTO_ROARAUDIO || byteorder != ROAR_BYTEORDER_NETWORK ) |
---|
145 | return -1; |
---|
146 | |
---|
147 | if ( (client = clients_new()) == -1 ) |
---|
148 | return -1; |
---|
149 | |
---|
150 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
151 | clients_delete(client); |
---|
152 | return -1; |
---|
153 | } |
---|
154 | |
---|
155 | if ( update_nnode ) { |
---|
156 | if ( clients_get(client, &c) != -1 ) { |
---|
157 | if ( roar_nnode_free(&(c->nnode)) != -1 ) { |
---|
158 | roar_nnode_new_from_fh(&(c->nnode), fh, 1); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | return 0; |
---|
164 | } |
---|
165 | |
---|
166 | int clients_delete (int id) { |
---|
167 | struct roar_client_server * cs; |
---|
168 | const struct roard_proto_handle * proto; |
---|
169 | int i; |
---|
170 | int close_client_fh = 1; |
---|
171 | |
---|
172 | ROAR_DBG("clients_delete(id=%i) = ?", id); |
---|
173 | |
---|
174 | _CHECK_CID(id); |
---|
175 | |
---|
176 | cs = g_clients[id]; |
---|
177 | |
---|
178 | proto = clients_get_protohandle(ROAR_CLIENT(cs)->proto); |
---|
179 | if ( proto != NULL ) { |
---|
180 | switch (proto->type) { |
---|
181 | case ROARD_PROTO_TYPE_BUILDIN: |
---|
182 | /* noop */ |
---|
183 | break; |
---|
184 | case ROARD_PROTO_TYPE_ROARDPROTO: |
---|
185 | if ( proto->impl.roardproto.delete_client != NULL ) |
---|
186 | proto->impl.roardproto.delete_client(id); |
---|
187 | break; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | if ( cs->waits != NULL ) { |
---|
192 | for (i = 0; cs->waits[i] != NULL; i++) |
---|
193 | roar_notify_core_unsubscribe(NULL, cs->waits[i]); |
---|
194 | |
---|
195 | roar_mm_free(cs->waits); |
---|
196 | cs->waits = NULL; |
---|
197 | } |
---|
198 | |
---|
199 | roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_CLIENT); |
---|
200 | |
---|
201 | counters_inc(clients, -1); |
---|
202 | |
---|
203 | if (ROAR_CLIENT(cs)->execed != -1) { |
---|
204 | // return streams_delete(g_clients[id]->execed); |
---|
205 | ROAR_CLIENT(cs)->execed = -1; |
---|
206 | close_client_fh = 0; |
---|
207 | } |
---|
208 | |
---|
209 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
210 | streams_delete(ROAR_CLIENT(cs)->streams[i]); |
---|
211 | } |
---|
212 | |
---|
213 | if ( ROAR_CLIENT(cs)->fh != -1 && close_client_fh ) |
---|
214 | close(ROAR_CLIENT(cs)->fh); |
---|
215 | |
---|
216 | roar_nnode_free(&(ROAR_CLIENT(cs)->nnode)); |
---|
217 | |
---|
218 | if ( cs->inbuf != NULL ) |
---|
219 | roar_buffer_free(cs->inbuf); |
---|
220 | |
---|
221 | if ( cs->outbuf != NULL ) |
---|
222 | roar_buffer_free(cs->outbuf); |
---|
223 | |
---|
224 | if ( cs->protoinst != NULL ) |
---|
225 | roar_mm_free(cs->protoinst); |
---|
226 | |
---|
227 | roar_mm_free(cs); |
---|
228 | g_clients[id] = NULL; |
---|
229 | |
---|
230 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
231 | return 0; |
---|
232 | } |
---|
233 | |
---|
234 | int clients_close (int id, int nocheck_exec) { |
---|
235 | struct roar_client * c; |
---|
236 | |
---|
237 | ROAR_DBG("clients_close(id=%i) = ?", id); |
---|
238 | |
---|
239 | _CHECK_CID(id); |
---|
240 | |
---|
241 | c = ROAR_CLIENT(g_clients[id]); |
---|
242 | |
---|
243 | if ( c->fh == -1 ) { |
---|
244 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
245 | return 0; |
---|
246 | } |
---|
247 | |
---|
248 | if (nocheck_exec || c->execed != -1) { |
---|
249 | close(c->fh); |
---|
250 | c->fh = -1; |
---|
251 | } |
---|
252 | |
---|
253 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
254 | return 0; |
---|
255 | } |
---|
256 | |
---|
257 | int clients_get (int id, struct roar_client ** client) { |
---|
258 | _CHECK_CID(id); |
---|
259 | |
---|
260 | *client = ROAR_CLIENT(g_clients[id]); |
---|
261 | |
---|
262 | if ( *client == NULL ) |
---|
263 | return -1; |
---|
264 | |
---|
265 | return 0; |
---|
266 | } |
---|
267 | |
---|
268 | int clients_get_server (int id, struct roar_client_server ** client) { |
---|
269 | _CHECK_CID(id); |
---|
270 | |
---|
271 | *client = g_clients[id]; |
---|
272 | |
---|
273 | if ( *client == NULL ) |
---|
274 | return -1; |
---|
275 | |
---|
276 | return 0; |
---|
277 | } |
---|
278 | |
---|
279 | int clients_set_fh (int id, int fh) { |
---|
280 | struct roar_client * c; |
---|
281 | #ifdef SO_PEERCRED |
---|
282 | struct ucred cred; |
---|
283 | socklen_t cred_len = sizeof(cred); |
---|
284 | #endif |
---|
285 | |
---|
286 | _CHECK_CID(id); |
---|
287 | |
---|
288 | if ( (c = ROAR_CLIENT(g_clients[id])) == NULL ) |
---|
289 | return -1; |
---|
290 | |
---|
291 | c->fh = fh; |
---|
292 | |
---|
293 | if ( fh == -1 ) |
---|
294 | return 0; |
---|
295 | |
---|
296 | #ifdef SO_PEERCRED |
---|
297 | if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) { |
---|
298 | if ( cred.pid != 0 ) { |
---|
299 | c->pid = cred.pid; |
---|
300 | c->uid = cred.uid; |
---|
301 | c->gid = cred.gid; |
---|
302 | } |
---|
303 | } else { |
---|
304 | ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno)); |
---|
305 | } |
---|
306 | #elif defined(ROAR_HAVE_GETPEEREID) |
---|
307 | if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) { |
---|
308 | ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno)); |
---|
309 | } |
---|
310 | #endif |
---|
311 | |
---|
312 | return 0; |
---|
313 | } |
---|
314 | |
---|
315 | int clients_get_fh (int id) { |
---|
316 | _CHECK_CID(id); |
---|
317 | |
---|
318 | return ROAR_CLIENT(g_clients[id])->fh; |
---|
319 | } |
---|
320 | |
---|
321 | int clients_set_pid (int id, int pid) { |
---|
322 | _CHECK_CID(id); |
---|
323 | |
---|
324 | ROAR_CLIENT(g_clients[id])->pid = pid; |
---|
325 | |
---|
326 | return 0; |
---|
327 | } |
---|
328 | |
---|
329 | int clients_set_uid (int id, int uid) { |
---|
330 | _CHECK_CID(id); |
---|
331 | |
---|
332 | ROAR_CLIENT(g_clients[id])->uid = uid; |
---|
333 | |
---|
334 | return 0; |
---|
335 | } |
---|
336 | |
---|
337 | int clients_set_gid (int id, int gid) { |
---|
338 | _CHECK_CID(id); |
---|
339 | |
---|
340 | ROAR_CLIENT(g_clients[id])->gid = gid; |
---|
341 | |
---|
342 | return 0; |
---|
343 | } |
---|
344 | |
---|
345 | int clients_set_proto (int id, int proto) { |
---|
346 | int byteorder = ROAR_BYTEORDER_UNKNOWN; |
---|
347 | |
---|
348 | _CHECK_CID(id); |
---|
349 | |
---|
350 | switch (proto) { |
---|
351 | case ROAR_PROTO_ROARAUDIO: |
---|
352 | case ROAR_PROTO_ESOUND: |
---|
353 | case ROAR_PROTO_RPLAY: |
---|
354 | case ROAR_PROTO_SIMPLE: |
---|
355 | byteorder = ROAR_BYTEORDER_NETWORK; |
---|
356 | break; |
---|
357 | } |
---|
358 | |
---|
359 | ROAR_CLIENT(g_clients[id])->proto = proto; |
---|
360 | ROAR_CLIENT(g_clients[id])->byteorder = byteorder; |
---|
361 | |
---|
362 | return 0; |
---|
363 | } |
---|
364 | |
---|
365 | int clients_block (int id, int unblock) { |
---|
366 | _CHECK_CID(id); |
---|
367 | |
---|
368 | if ( unblock ) { |
---|
369 | g_clients[id]->blockc--; |
---|
370 | } else { |
---|
371 | g_clients[id]->blockc++; |
---|
372 | } |
---|
373 | |
---|
374 | return 0; |
---|
375 | } |
---|
376 | |
---|
377 | |
---|
378 | #define MAX_STREAMLESS 8 |
---|
379 | |
---|
380 | int clients_check_all (void) { |
---|
381 | #ifdef ROAR_HAVE_SELECT |
---|
382 | struct roar_client * c; |
---|
383 | struct timeval tv; |
---|
384 | fd_set r, w, e; |
---|
385 | int i, j; |
---|
386 | int ret; |
---|
387 | int fh; |
---|
388 | int max_fh = -1; |
---|
389 | int have = 0; |
---|
390 | struct { |
---|
391 | int id; |
---|
392 | int fh; |
---|
393 | } streamless[MAX_STREAMLESS]; |
---|
394 | int have_streamless = 0; |
---|
395 | int have_stream; |
---|
396 | |
---|
397 | ROAR_DBG("clients_check_all(void) = ?"); |
---|
398 | |
---|
399 | FD_ZERO(&r); |
---|
400 | FD_ZERO(&w); |
---|
401 | FD_ZERO(&e); |
---|
402 | |
---|
403 | tv.tv_sec = 0; |
---|
404 | tv.tv_usec = 1; |
---|
405 | |
---|
406 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
407 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
408 | continue; |
---|
409 | |
---|
410 | ROAR_DBG("clients_check_all(void): i(client)=%i", i); |
---|
411 | |
---|
412 | if ( (fh = c->fh) != -1 ) { |
---|
413 | have++; |
---|
414 | |
---|
415 | ROAR_DBG("clients_check_all(*): fh=%i", fh); |
---|
416 | |
---|
417 | FD_SET(fh, &r); |
---|
418 | FD_SET(fh, &e); |
---|
419 | |
---|
420 | if ( g_clients[i]->outbuf != NULL ) { |
---|
421 | FD_SET(fh, &w); |
---|
422 | } |
---|
423 | |
---|
424 | if ( fh > max_fh ) |
---|
425 | max_fh = fh; |
---|
426 | } |
---|
427 | |
---|
428 | have_stream = 0; |
---|
429 | |
---|
430 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
431 | ROAR_DBG("clients_check_all(void): i(client)=%i, j(stream)=%i", i, j); |
---|
432 | if ( (fh = streams_get_fh(c->streams[j])) != -1 ) { |
---|
433 | ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, c->streams[j], fh); |
---|
434 | if ( fh > -1 ) { |
---|
435 | FD_SET(fh, &r); |
---|
436 | |
---|
437 | if ( fh > max_fh ) |
---|
438 | max_fh = fh; |
---|
439 | } else if ( fh == -2 ) { |
---|
440 | streams_check(c->streams[j]); |
---|
441 | } |
---|
442 | |
---|
443 | have_stream = 1; |
---|
444 | } |
---|
445 | //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh); |
---|
446 | } |
---|
447 | |
---|
448 | if ( !have_stream && have_streamless < MAX_STREAMLESS ) { |
---|
449 | streamless[have_streamless ].id = i; |
---|
450 | if ( (streamless[have_streamless++].fh = c->fh) == -1 ) |
---|
451 | have_streamless--; |
---|
452 | } |
---|
453 | } |
---|
454 | |
---|
455 | ROAR_DBG("clients_check_all(void): max_fh=%i", max_fh); |
---|
456 | |
---|
457 | if ( max_fh == -1 ) |
---|
458 | return 0; |
---|
459 | |
---|
460 | if ( (ret = select(max_fh + 1, &r, &w, &e, &tv)) < 1 ) { |
---|
461 | return ret < 0 ? ret : have; |
---|
462 | } |
---|
463 | |
---|
464 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
465 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
466 | continue; |
---|
467 | |
---|
468 | if ( (fh = c->fh) != -1 ) { |
---|
469 | if ( FD_ISSET(fh, &r) ) { |
---|
470 | if ( c->execed == -1 ) { |
---|
471 | clients_check(i); |
---|
472 | if ( g_clients[i] != NULL && ROAR_CLIENT(g_clients[i])->execed != -1 ) { |
---|
473 | FD_CLR(fh, &r); |
---|
474 | } |
---|
475 | /* |
---|
476 | } else { |
---|
477 | streams_check(g_clients[i]->execed); |
---|
478 | */ |
---|
479 | } |
---|
480 | } |
---|
481 | if ( FD_ISSET(fh, &w) ) { |
---|
482 | clients_flush(i); |
---|
483 | } |
---|
484 | |
---|
485 | if ( FD_ISSET(fh, &e) ) { |
---|
486 | clients_delete(i); |
---|
487 | continue; |
---|
488 | } |
---|
489 | } |
---|
490 | |
---|
491 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
492 | continue; |
---|
493 | |
---|
494 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
495 | ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]); |
---|
496 | if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?) |
---|
497 | break; |
---|
498 | |
---|
499 | //ROAR_WARN("clients_check_all(*): client=%i: client exists", i); |
---|
500 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, c->streams[j]); |
---|
501 | |
---|
502 | if ( (fh = streams_get_fh(c->streams[j])) != -1 ) { |
---|
503 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh); |
---|
504 | if ( fh > -1 && FD_ISSET(fh, &r) ) { |
---|
505 | streams_check(c->streams[j]); |
---|
506 | } |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | |
---|
511 | if ( have_streamless ) { |
---|
512 | FD_ZERO(&r); |
---|
513 | FD_ZERO(&w); |
---|
514 | |
---|
515 | tv.tv_sec = 0; |
---|
516 | tv.tv_usec = 1; |
---|
517 | |
---|
518 | max_fh = -1; |
---|
519 | |
---|
520 | for (i = 0; i < have_streamless; i++) { |
---|
521 | if ( g_clients[j = streamless[i].id] == NULL ) |
---|
522 | continue; |
---|
523 | |
---|
524 | if ( ROAR_CLIENT(g_clients[j])->execed != -1 ) |
---|
525 | continue; |
---|
526 | |
---|
527 | fh = streamless[i].fh; |
---|
528 | |
---|
529 | ROAR_DBG("clients_check_all(void): fh=%i", fh); |
---|
530 | FD_SET(fh, &r); |
---|
531 | |
---|
532 | if ( g_clients[j]->outbuf != NULL ) { |
---|
533 | FD_SET(fh, &w); |
---|
534 | } |
---|
535 | |
---|
536 | if ( fh > max_fh ) |
---|
537 | max_fh = fh; |
---|
538 | } |
---|
539 | |
---|
540 | if ( (ret = select(max_fh + 1, &r, &w, NULL, &tv)) < 0 ) { |
---|
541 | return ret; |
---|
542 | } |
---|
543 | |
---|
544 | for (i = 0; i < have_streamless; i++) { |
---|
545 | if ( FD_ISSET(streamless[i].fh, &r) ) { |
---|
546 | clients_check(streamless[i].id); |
---|
547 | } |
---|
548 | if ( FD_ISSET(streamless[i].fh, &w) ) { |
---|
549 | clients_flush(streamless[i].id); |
---|
550 | } |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
555 | return have; |
---|
556 | #else |
---|
557 | ROAR_DBG("clients_check_all(void) = -1"); |
---|
558 | return -1; |
---|
559 | #endif |
---|
560 | } |
---|
561 | |
---|
562 | int clients_check (int id) { |
---|
563 | struct roar_client * c; |
---|
564 | struct roar_message m; |
---|
565 | struct roar_connection con; |
---|
566 | struct roar_vio_calls vio; |
---|
567 | struct roar_error_state errstate; |
---|
568 | const struct roard_proto_handle * proto; |
---|
569 | int command_error; |
---|
570 | char * data = NULL; |
---|
571 | int oldcmd; |
---|
572 | int r; |
---|
573 | int rv = 0; |
---|
574 | uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE}; |
---|
575 | uint32_t event; |
---|
576 | |
---|
577 | ROAR_DBG("clients_check(id=%i) = ?", id); |
---|
578 | |
---|
579 | _CHECK_CID(id); |
---|
580 | |
---|
581 | c = ROAR_CLIENT(g_clients[id]); |
---|
582 | |
---|
583 | if ( c->fh == -1 ) |
---|
584 | return -1; |
---|
585 | |
---|
586 | roar_connect_fh(&con, c->fh); |
---|
587 | |
---|
588 | ROAR_DBG("clients_check(id=%i): c->proto=%i", id, c->proto); |
---|
589 | |
---|
590 | switch (c->proto) { |
---|
591 | case ROAR_PROTO_ROARAUDIO: |
---|
592 | r = roar_recv_message(&con, &m, &data); |
---|
593 | |
---|
594 | if ( r == -1 ) { // should we drop the client? |
---|
595 | clients_delete(id); |
---|
596 | return -1; |
---|
597 | } |
---|
598 | |
---|
599 | event = ROAR_NOTIFY_CMD2EVENT(m.cmd); |
---|
600 | |
---|
601 | oldcmd = m.cmd; |
---|
602 | |
---|
603 | roar_err_store(&errstate); |
---|
604 | roar_err_clear_all(); |
---|
605 | r = command_exec(id, &m, &data, flags); |
---|
606 | roar_err_update(); |
---|
607 | command_error = roar_error; |
---|
608 | roar_err_restore(&errstate); |
---|
609 | |
---|
610 | if ( r == -1 ) { |
---|
611 | // use command_error to create an error frame here as soon as this is supported. |
---|
612 | m.cmd = ROAR_CMD_ERROR; |
---|
613 | m.datalen = 0; |
---|
614 | ROAR_DBG("clients_check(*): Exec of command faild!"); |
---|
615 | } else { |
---|
616 | if ( m.cmd == oldcmd ) { |
---|
617 | m.cmd = ROAR_CMD_OK; |
---|
618 | m.datalen = 0; |
---|
619 | } else if ( m.cmd == ROAR_CMD_OK_STOP ) { |
---|
620 | m.cmd = ROAR_CMD_OK; |
---|
621 | rv = 1; |
---|
622 | } |
---|
623 | } |
---|
624 | ROAR_DBG("clients_check(*): data=%p", data); |
---|
625 | |
---|
626 | if ( flags[1] & COMMAND_FLAG_OUT_NOSEND ) { |
---|
627 | roar_notify_core_emit_simple(event, id, -1, -1, -1, -1, NULL, 0); |
---|
628 | } else { |
---|
629 | roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0); |
---|
630 | roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL); |
---|
631 | } |
---|
632 | |
---|
633 | if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON ) |
---|
634 | clients_close(id, 1); |
---|
635 | |
---|
636 | break; |
---|
637 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
638 | case ROAR_PROTO_RSOUND: |
---|
639 | rv = emul_rsound_check_client(id, NULL); |
---|
640 | if ( rv == 0 ) { // loop as long as we don't get an error. |
---|
641 | while (rv == 0) |
---|
642 | rv = emul_rsound_check_client(id, NULL); |
---|
643 | rv = 0; // restore |
---|
644 | } else { // in case of error delete the client |
---|
645 | if ( |
---|
646 | #ifdef EAGAIN |
---|
647 | errno != EAGAIN && |
---|
648 | #endif |
---|
649 | #ifdef EWOULDBLOCK |
---|
650 | errno != EWOULDBLOCK && |
---|
651 | #endif |
---|
652 | #ifdef EINTR |
---|
653 | errno != EINTR && |
---|
654 | #endif |
---|
655 | 1 ) { |
---|
656 | rv = clients_delete(id); |
---|
657 | } else { |
---|
658 | rv = 0; |
---|
659 | } |
---|
660 | } |
---|
661 | break; |
---|
662 | #endif |
---|
663 | default: |
---|
664 | rv = -1; |
---|
665 | proto = clients_get_protohandle(c->proto); |
---|
666 | if ( proto != NULL ) { |
---|
667 | roar_vio_open_fh_socket(&vio, clients_get_fh(id)); |
---|
668 | if ( proto->lhandle != NULL ) |
---|
669 | roar_dl_context_restore(proto->lhandle); |
---|
670 | |
---|
671 | switch (proto->type) { |
---|
672 | case ROARD_PROTO_TYPE_BUILDIN: |
---|
673 | ROAR_WARN("clients_check(id=%i): proto(%i) marked as buildin but isn't. BAD.", id, proto->proto); |
---|
674 | break; |
---|
675 | case ROARD_PROTO_TYPE_ROARDPROTO: |
---|
676 | if ( proto->impl.roardproto.check_client != NULL ) |
---|
677 | rv = proto->impl.roardproto.check_client(id, &vio); |
---|
678 | break; |
---|
679 | } |
---|
680 | |
---|
681 | if ( proto->lhandle != NULL ) |
---|
682 | roar_dl_context_store(proto->lhandle); |
---|
683 | } |
---|
684 | } |
---|
685 | |
---|
686 | if ( data != NULL ) |
---|
687 | roar_mm_free(data); |
---|
688 | |
---|
689 | ROAR_DBG("clients_check(id=%i) = %i", id, rv); |
---|
690 | return rv; |
---|
691 | } |
---|
692 | |
---|
693 | int clients_flush (int id) { |
---|
694 | struct roar_vio_calls vio; |
---|
695 | struct roar_client_server * cs; |
---|
696 | struct roar_client * c; |
---|
697 | const struct roard_proto_handle * p; |
---|
698 | size_t len; |
---|
699 | ssize_t ret; |
---|
700 | void * buf; |
---|
701 | int rv; |
---|
702 | |
---|
703 | _CHECK_CID(id); |
---|
704 | |
---|
705 | c = ROAR_CLIENT(cs = g_clients[id]); |
---|
706 | |
---|
707 | p = clients_get_protohandle(c->proto); |
---|
708 | |
---|
709 | if ( p == NULL ) |
---|
710 | return -1; |
---|
711 | |
---|
712 | roar_vio_open_fh_socket(&vio, clients_get_fh(id)); |
---|
713 | |
---|
714 | switch (p->type) { |
---|
715 | case ROARD_PROTO_TYPE_BUILDIN: |
---|
716 | /* noop */ |
---|
717 | break; |
---|
718 | case ROARD_PROTO_TYPE_ROARDPROTO: |
---|
719 | if ( p->impl.roardproto.flush_client != NULL ) { |
---|
720 | if ( p->lhandle != NULL ) |
---|
721 | roar_dl_context_restore(p->lhandle); |
---|
722 | rv = p->impl.roardproto.flush_client(id, &vio); |
---|
723 | if ( p->lhandle != NULL ) |
---|
724 | roar_dl_context_store(p->lhandle); |
---|
725 | return rv; |
---|
726 | } |
---|
727 | break; |
---|
728 | } |
---|
729 | |
---|
730 | if ( roar_buffer_get_len(cs->outbuf, &len) == -1 ) |
---|
731 | return -1; |
---|
732 | |
---|
733 | if ( roar_buffer_get_data(cs->outbuf, &buf) == -1 ) |
---|
734 | return -1; |
---|
735 | |
---|
736 | ret = roar_vio_write(&vio, buf, len); |
---|
737 | |
---|
738 | if ( ret < 1 ) { |
---|
739 | clients_delete(id); |
---|
740 | return -1; |
---|
741 | } |
---|
742 | |
---|
743 | if ( ret == len ) { |
---|
744 | if ( roar_buffer_next(&(cs->outbuf)) == -1 ) { |
---|
745 | clients_delete(id); |
---|
746 | return -1; |
---|
747 | } |
---|
748 | } else { |
---|
749 | if ( roar_buffer_set_offset(cs->outbuf, ret) == -1 ) { |
---|
750 | clients_delete(id); |
---|
751 | return -1; |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | if ( cs->outbuf == NULL ) { |
---|
756 | switch (p->type) { |
---|
757 | case ROARD_PROTO_TYPE_BUILDIN: |
---|
758 | /* noop */ |
---|
759 | break; |
---|
760 | case ROARD_PROTO_TYPE_ROARDPROTO: |
---|
761 | if ( p->impl.roardproto.flushed_client != NULL ) { |
---|
762 | if ( p->lhandle != NULL ) |
---|
763 | roar_dl_context_restore(p->lhandle); |
---|
764 | rv = p->impl.roardproto.flushed_client(id, &vio); |
---|
765 | if ( p->lhandle != NULL ) |
---|
766 | roar_dl_context_store(p->lhandle); |
---|
767 | return rv; |
---|
768 | } |
---|
769 | break; |
---|
770 | } |
---|
771 | } |
---|
772 | |
---|
773 | return 0; |
---|
774 | } |
---|
775 | |
---|
776 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
777 | int i; |
---|
778 | // int fh; |
---|
779 | int j; |
---|
780 | int keep_going; |
---|
781 | |
---|
782 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
783 | if ( g_clients[i] == NULL ) |
---|
784 | continue; |
---|
785 | |
---|
786 | keep_going = 1; |
---|
787 | |
---|
788 | /* |
---|
789 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
790 | continue; |
---|
791 | */ |
---|
792 | |
---|
793 | ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed); |
---|
794 | |
---|
795 | /* |
---|
796 | if ( g_clients[i]->execed == -1 ) { |
---|
797 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
798 | */ |
---|
799 | for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
800 | //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
801 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j); |
---|
802 | if ( ROAR_CLIENT(g_clients[i])->streams[j] != -1 ) { |
---|
803 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, ROAR_CLIENT(g_clients[i])->streams[j]); |
---|
804 | streams_send_mon(ROAR_CLIENT(g_clients[i])->streams[j]); |
---|
805 | |
---|
806 | // the client may be deleted here, check if it still exists: |
---|
807 | if ( g_clients[i] == NULL ) |
---|
808 | keep_going = 0; |
---|
809 | } |
---|
810 | } |
---|
811 | /* |
---|
812 | } else { |
---|
813 | // streams_check(g_clients[i]->execed); |
---|
814 | streams_send_mon(g_clients[i]->execed); |
---|
815 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
816 | // clients_delete(i); // delete client in case we could not write |
---|
817 | } |
---|
818 | */ |
---|
819 | } |
---|
820 | |
---|
821 | // TODO: FIXME: should this really be -1? |
---|
822 | return -1; |
---|
823 | } |
---|
824 | |
---|
825 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
826 | struct roar_client * c; |
---|
827 | int i; |
---|
828 | int fh; |
---|
829 | |
---|
830 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
831 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
832 | continue; |
---|
833 | |
---|
834 | if ( (fh = c->fh) == -1 ) |
---|
835 | continue; |
---|
836 | |
---|
837 | if ( c->execed == -1 ) { |
---|
838 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
839 | } else { |
---|
840 | // streams_check(g_clients[i]->execed); |
---|
841 | streams_send_filter(c->execed); |
---|
842 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
843 | // clients_delete(i); // delete client in case we could not write |
---|
844 | } |
---|
845 | } |
---|
846 | |
---|
847 | return -1; |
---|
848 | } |
---|
849 | |
---|
850 | int clients_add_output (int id, struct roar_buffer ** buf) { |
---|
851 | struct roar_client_server * cs; |
---|
852 | |
---|
853 | _CHECK_CID(id); |
---|
854 | cs = g_clients[id]; |
---|
855 | |
---|
856 | if ( cs->outbuf == NULL ) { |
---|
857 | cs->outbuf = *buf; |
---|
858 | } else { |
---|
859 | return roar_buffer_moveinto(cs->outbuf, buf); |
---|
860 | } |
---|
861 | |
---|
862 | return 0; |
---|
863 | } |
---|
864 | |
---|
865 | // proto support |
---|
866 | const struct roard_proto_handle * clients_get_protohandle(const int proto) { |
---|
867 | size_t i; |
---|
868 | |
---|
869 | if ( proto < 0 ) { |
---|
870 | roar_err_set(ROAR_ERROR_INVAL); |
---|
871 | return NULL; |
---|
872 | } |
---|
873 | |
---|
874 | for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) |
---|
875 | if ( __protos[i].proto == proto ) |
---|
876 | return &(__protos[i]); |
---|
877 | |
---|
878 | roar_err_set(ROAR_ERROR_NOENT); |
---|
879 | return NULL; |
---|
880 | } |
---|
881 | |
---|
882 | int clients_register_proto(struct roard_proto * proto, struct roar_dl_lhandle * lhandle) { |
---|
883 | const size_t len = sizeof(__protos)/sizeof(*__protos); |
---|
884 | size_t i; |
---|
885 | |
---|
886 | if ( proto == NULL ) |
---|
887 | return -1; |
---|
888 | |
---|
889 | for (i = 0; __protos[i].proto != -1; i++); |
---|
890 | |
---|
891 | // i is now at pos of current EOS entry. |
---|
892 | |
---|
893 | // test if we have space for one more entry: |
---|
894 | if ( (i+1) >= len ) |
---|
895 | return -1; |
---|
896 | |
---|
897 | memcpy(&(__protos[i].impl.roardproto), proto, sizeof(__protos[i].impl.roardproto)); |
---|
898 | |
---|
899 | __protos[i].impl.roardproto.lhandle = lhandle; |
---|
900 | |
---|
901 | __protos[i].proto = proto->proto; |
---|
902 | __protos[i].type = ROARD_PROTO_TYPE_ROARDPROTO; |
---|
903 | __protos[i].lhandle = lhandle; |
---|
904 | |
---|
905 | return 0; |
---|
906 | } |
---|
907 | |
---|
908 | int clients_unregister_proto(int proto) { |
---|
909 | size_t i; |
---|
910 | |
---|
911 | if ( proto < 0 ) { |
---|
912 | roar_err_set(ROAR_ERROR_RANGE); |
---|
913 | return -1; |
---|
914 | } |
---|
915 | |
---|
916 | for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) { |
---|
917 | if ( __protos[i].proto == proto ) { |
---|
918 | memset(&(__protos[i]), 0, sizeof(*__protos)); |
---|
919 | __protos[i].proto = -1; |
---|
920 | return 0; |
---|
921 | } |
---|
922 | } |
---|
923 | |
---|
924 | roar_err_set(ROAR_ERROR_NOENT); |
---|
925 | return -1; |
---|
926 | } |
---|
927 | |
---|
928 | void print_protolist (enum output_format format) { |
---|
929 | struct roard_proto_handle * p; |
---|
930 | char subsys[7] = " "; |
---|
931 | const char * description; |
---|
932 | size_t i; |
---|
933 | |
---|
934 | switch (format) { |
---|
935 | case FORMAT_NATIVE: |
---|
936 | printf(" Protocol Flag Subsys - Description\n"); |
---|
937 | printf("------------------------------------------------------\n"); |
---|
938 | printf(" roar WM LRX - RoarAudio native protocol\n"); |
---|
939 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
940 | printf(" simple WM LRX - PulseAudio simple protocol\n"); |
---|
941 | #endif |
---|
942 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
943 | printf(" rsound W - RSound emulation\n"); |
---|
944 | #endif |
---|
945 | break; |
---|
946 | case FORMAT_WIKI: |
---|
947 | printf("||=Protocol =||=Flag =||=Subsys =||=Description =||\n"); |
---|
948 | printf("||roar || ||WM LRX ||RoarAudio native protocol ||\n"); |
---|
949 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
950 | printf("||simple || ||WM LRX ||PulseAudio simple protocol ||\n"); |
---|
951 | #endif |
---|
952 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
953 | printf("||rsound || ||W ||RSound emulation ||\n"); |
---|
954 | #endif |
---|
955 | break; |
---|
956 | case FORMAT_CSV: |
---|
957 | printf("Protocol,Flag,Subsys,Description\n"); |
---|
958 | printf("roar,,WM LRX,RoarAudio native protocol\n"); |
---|
959 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
960 | printf("simple,,WM LRX,PulseAudio simple protocol\n"); |
---|
961 | #endif |
---|
962 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
963 | printf("rsound,,W,RSound emulation\n"); |
---|
964 | #endif |
---|
965 | break; |
---|
966 | default: |
---|
967 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
968 | return; |
---|
969 | } |
---|
970 | |
---|
971 | for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) { |
---|
972 | p = &(__protos[i]); |
---|
973 | if ( p->proto == -1 ) |
---|
974 | continue; |
---|
975 | |
---|
976 | strncpy(subsys, " ", 6); |
---|
977 | description = "(none)"; |
---|
978 | |
---|
979 | if ( p->type == ROARD_PROTO_TYPE_ROARDPROTO ) { |
---|
980 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_WAVEFORM ) |
---|
981 | subsys[0] = 'W'; |
---|
982 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_MIDI ) |
---|
983 | subsys[1] = 'M'; |
---|
984 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_CB ) |
---|
985 | subsys[2] = 'C'; |
---|
986 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_LIGHT ) |
---|
987 | subsys[3] = 'L'; |
---|
988 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_RAW ) |
---|
989 | subsys[4] = 'R'; |
---|
990 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_COMPLEX ) |
---|
991 | subsys[5] = 'X'; |
---|
992 | |
---|
993 | description = p->impl.roardproto.description; |
---|
994 | } |
---|
995 | |
---|
996 | switch (format) { |
---|
997 | case FORMAT_NATIVE: |
---|
998 | printf(" %-13s %s - %s\n", roar_proto2str(p->proto), subsys, description); |
---|
999 | break; |
---|
1000 | case FORMAT_WIKI: |
---|
1001 | printf("||%s || ||%s ||%s ||\n", roar_proto2str(p->proto), subsys, description); |
---|
1002 | break; |
---|
1003 | case FORMAT_CSV: |
---|
1004 | printf("%s,,%s,%s\n", roar_proto2str(p->proto), subsys, description); |
---|
1005 | break; |
---|
1006 | } |
---|
1007 | } |
---|
1008 | } |
---|
1009 | |
---|
1010 | int client_stream_exec (int client, int stream) { |
---|
1011 | struct roar_client * c; |
---|
1012 | int i; |
---|
1013 | int fh; |
---|
1014 | |
---|
1015 | _CHECK_CID(client); |
---|
1016 | |
---|
1017 | #if 0 |
---|
1018 | if ( g_clients[client] == NULL ) { |
---|
1019 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream); |
---|
1020 | return -1; |
---|
1021 | } |
---|
1022 | #endif |
---|
1023 | |
---|
1024 | c = ROAR_CLIENT(g_clients[client]); |
---|
1025 | |
---|
1026 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
1027 | if ( c->streams[i] == stream ) { |
---|
1028 | c->execed = stream; |
---|
1029 | if ( streams_is_ready(stream) == 0 ) { |
---|
1030 | streams_set_fh(stream, c->fh); |
---|
1031 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR); |
---|
1032 | } else { |
---|
1033 | ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream); |
---|
1034 | if ( (fh = c->fh) != -1 ) { |
---|
1035 | close(fh); |
---|
1036 | c->fh = -1; |
---|
1037 | } |
---|
1038 | } |
---|
1039 | ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream); |
---|
1040 | return 0; |
---|
1041 | } |
---|
1042 | } |
---|
1043 | |
---|
1044 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream); |
---|
1045 | return -1; |
---|
1046 | } |
---|
1047 | |
---|
1048 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
1049 | int i; |
---|
1050 | |
---|
1051 | ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh); |
---|
1052 | |
---|
1053 | _CHECK_CID(client); |
---|
1054 | |
---|
1055 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
1056 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) { |
---|
1057 | ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i); |
---|
1058 | return streams_set_fh(stream, fh); |
---|
1059 | } |
---|
1060 | } |
---|
1061 | |
---|
1062 | ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh); |
---|
1063 | return -1; |
---|
1064 | } |
---|
1065 | |
---|
1066 | int client_stream_add (int client, int stream) { |
---|
1067 | int i; |
---|
1068 | |
---|
1069 | _CHECK_CID(client); |
---|
1070 | |
---|
1071 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
1072 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) { |
---|
1073 | ROAR_CLIENT(g_clients[client])->streams[i] = stream; |
---|
1074 | streams_set_client(stream, client); |
---|
1075 | return 0; |
---|
1076 | } |
---|
1077 | } |
---|
1078 | |
---|
1079 | return -1; |
---|
1080 | } |
---|
1081 | |
---|
1082 | int client_stream_delete (int client, int stream) { |
---|
1083 | int i; |
---|
1084 | |
---|
1085 | _CHECK_CID(client); |
---|
1086 | |
---|
1087 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
1088 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) { |
---|
1089 | ROAR_CLIENT(g_clients[client])->streams[i] = -1; |
---|
1090 | |
---|
1091 | if ( stream == ROAR_CLIENT(g_clients[client])->execed ) { |
---|
1092 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
1093 | clients_delete(client); |
---|
1094 | } |
---|
1095 | |
---|
1096 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
1097 | return 0; |
---|
1098 | } |
---|
1099 | } |
---|
1100 | |
---|
1101 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
1102 | return -1; |
---|
1103 | } |
---|
1104 | |
---|
1105 | int client_stream_move (int client, int stream) { |
---|
1106 | int old_client = streams_get_client(stream); |
---|
1107 | |
---|
1108 | ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client); |
---|
1109 | |
---|
1110 | if ( old_client != -1 ) |
---|
1111 | if ( client_stream_delete(old_client, stream) == -1 ) |
---|
1112 | return -1; |
---|
1113 | |
---|
1114 | return client_stream_add(client, stream); |
---|
1115 | } |
---|
1116 | |
---|
1117 | |
---|
1118 | // notify thingys |
---|
1119 | int clients_wait (int client, struct roar_event * events, size_t num) { |
---|
1120 | struct roar_client_server * cs; |
---|
1121 | size_t i, c; |
---|
1122 | |
---|
1123 | ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = ?", client, events, (long long unsigned int)num); |
---|
1124 | |
---|
1125 | _CHECK_CID(client); |
---|
1126 | |
---|
1127 | cs = g_clients[client]; |
---|
1128 | |
---|
1129 | if ( cs->waits != NULL ) |
---|
1130 | return -1; |
---|
1131 | |
---|
1132 | cs->waits = roar_mm_malloc((num+1) * sizeof(struct roar_subscriber *)); |
---|
1133 | |
---|
1134 | if ( cs->waits == NULL ) |
---|
1135 | return -1; |
---|
1136 | |
---|
1137 | if ( clients_block(client, 0) != 0 ) |
---|
1138 | return -1; |
---|
1139 | |
---|
1140 | for (i = 0; i < num; i++) { |
---|
1141 | #if defined(DEBUG) && 0 |
---|
1142 | dbg_notify_cb(NULL, &(events[i]), cs); |
---|
1143 | #endif |
---|
1144 | cs->waits[i] = roar_notify_core_subscribe(NULL, &(events[i]), clients_ncb_wait, cs); |
---|
1145 | if ( cs->waits[i] == NULL ) { |
---|
1146 | for (c = 0; c < i; c++) |
---|
1147 | roar_notify_core_unsubscribe(NULL, cs->waits[c]); |
---|
1148 | roar_mm_free(cs->waits); |
---|
1149 | cs->waits = NULL; |
---|
1150 | clients_block(client, 1); |
---|
1151 | return -1; |
---|
1152 | } |
---|
1153 | } |
---|
1154 | |
---|
1155 | cs->waits[num] = NULL; |
---|
1156 | |
---|
1157 | ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = 0", client, events, (long long unsigned int)num); |
---|
1158 | return 0; |
---|
1159 | } |
---|
1160 | |
---|
1161 | void clients_ncb_wait(struct roar_notify_core * core, struct roar_event * event, void * userdata) { |
---|
1162 | struct roar_client_server * cs = userdata; |
---|
1163 | struct roar_message m; |
---|
1164 | struct roar_connection con; |
---|
1165 | uint16_t * u16 = (uint16_t *) m.data; |
---|
1166 | size_t tmp; |
---|
1167 | size_t i; |
---|
1168 | |
---|
1169 | ROAR_DBG("clients_ncb_wait(core=%p, event=%p, userdata=%p) = ?", core, event, userdata); |
---|
1170 | |
---|
1171 | for (i = 0; cs->waits[i] != NULL; i++) |
---|
1172 | roar_notify_core_unsubscribe(NULL, cs->waits[i]); |
---|
1173 | |
---|
1174 | roar_mm_free(cs->waits); |
---|
1175 | cs->waits = NULL; |
---|
1176 | |
---|
1177 | // protocol depended handling... |
---|
1178 | memset(&m, 0, sizeof(m)); |
---|
1179 | m.cmd = ROAR_CMD_OK; |
---|
1180 | u16[0] = ROAR_HOST2NET16(0); // Version |
---|
1181 | u16[1] = ROAR_HOST2NET16(0); // flags |
---|
1182 | |
---|
1183 | tmp = sizeof(m.data) - 4; |
---|
1184 | |
---|
1185 | roar_event_to_blob(event, m.data + 4, &tmp); |
---|
1186 | |
---|
1187 | m.datalen = tmp + 4; |
---|
1188 | |
---|
1189 | roar_connect_fh(&con, ROAR_CLIENT(cs)->fh); |
---|
1190 | roar_send_message(&con, &m, NULL); |
---|
1191 | // ...end of protocol depended handling. |
---|
1192 | |
---|
1193 | // clients_block(, 1); |
---|
1194 | // TODO: FIXME: bad hack... |
---|
1195 | cs->blockc--; |
---|
1196 | } |
---|
1197 | |
---|
1198 | |
---|
1199 | // acclev: |
---|
1200 | static struct { |
---|
1201 | const enum roard_client_acclev acclev; |
---|
1202 | const char * name; |
---|
1203 | } _g_acclevs[] = { |
---|
1204 | {ACCLEV_NONE, "none" }, |
---|
1205 | {ACCLEV_IDENTED, "idented"}, |
---|
1206 | {ACCLEV_CONCTL, "conctl" }, |
---|
1207 | {ACCLEV_GUEST, "guest" }, |
---|
1208 | {ACCLEV_USER, "user" }, |
---|
1209 | {ACCLEV_PWRUSER, "pwruser"}, |
---|
1210 | {ACCLEV_ALL, "all" }, |
---|
1211 | {-1, NULL} |
---|
1212 | }; |
---|
1213 | |
---|
1214 | enum roard_client_acclev clients_str2acclev(const char * acclev) { |
---|
1215 | int i; |
---|
1216 | |
---|
1217 | for (i = 0; _g_acclevs[i].name != NULL; i++) |
---|
1218 | if ( !strcasecmp(_g_acclevs[i].name, acclev) ) |
---|
1219 | return _g_acclevs[i].acclev; |
---|
1220 | |
---|
1221 | return ACCLEV_ERROR; |
---|
1222 | } |
---|
1223 | |
---|
1224 | const char * clients_acclev2str(const enum roard_client_acclev acclev) { |
---|
1225 | int i; |
---|
1226 | |
---|
1227 | for (i = 0; _g_acclevs[i].name != NULL; i++) |
---|
1228 | if ( _g_acclevs[i].acclev == acclev ) |
---|
1229 | return _g_acclevs[i].name; |
---|
1230 | |
---|
1231 | return NULL; |
---|
1232 | } |
---|
1233 | |
---|
1234 | //ll |
---|