source: roaraudio/roard/clients.c @ 5304:b4e9377c2dff

Last change on this file since 5304:b4e9377c2dff was 5304:b4e9377c2dff, checked in by phi, 12 years ago

removed old member

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