source: roaraudio/roard/clients.c @ 5607:9b10a70d82e4

Last change on this file since 5607:9b10a70d82e4 was 5607:9b10a70d82e4, checked in by phi, 12 years ago

added clients_set_name()

File size: 36.6 KB
Line 
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'
33struct roar_client_server * g_clients[ROAR_CLIENTS_MAX];
34
35static 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
57int 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
71int 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
81int 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
137int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) {
138 return clients_new_from_fh2(fh, proto, byteorder, update_nnode, NULL, NULL, 0);
139}
140
141int clients_new_from_fh2(int fh, int proto, int byteorder,
142                         int update_nnode,
143                         struct roard_listen * lsock, struct sockaddr * sockaddr, socklen_t addrlen) {
144 const struct roard_proto_handle * protohandle;
145 struct roar_dl_librarypara * pluginpara;
146 struct roar_vio_calls    vio;
147 struct roar_client_server * cs;
148 int supported = 0;
149 int client;
150
151 if ( fh == -1 ) {
152  roar_err_set(ROAR_ERROR_BADFH);
153  return -1;
154 }
155
156 if ( lsock == NULL ) {
157  switch (proto) {
158   case ROAR_PROTO_RSOUND:
159   case ROAR_PROTO_SIMPLE:
160     roar_err_set(ROAR_ERROR_FAULT);
161     return -1;
162    break;
163  }
164 }
165
166 if ( byteorder != ROAR_BYTEORDER_NETWORK )
167  return -1;
168
169
170 if ( proto == ROAR_PROTO_RSOUND ) {
171#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
172  client = emul_rsound_on_connect(fh, lsock);
173  switch (client) {
174   case -1: return -1; break;
175   case -2: return  0; break;
176   default: // TODO: write error handling
177     fh = ROAR_CLIENT(cs)->fh;
178    break;
179  }
180#else
181  roar_err_set(ROAR_ERROR_PROTONOSUP);
182  return -1;
183#endif
184 } else {
185  client = clients_new();
186
187  if ( client == -1 ) {
188   ROAR_DBG("clients_new_from_fh2(*) = -1 // can not create new client");
189   return -1;
190  }
191
192  if ( clients_set_fh(client, fh) == -1 ) {
193   ROAR_ERR("clients_new_from_fh2(*): Can not set client's fh");
194
195   clients_delete(client);
196
197   ROAR_DBG("clients_new_from_fh2(*) = -1");
198   return -1;
199  }
200 }
201
202 if ( clients_get_server(client, &cs) == -1 ) {
203  clients_delete(client);
204  return -1;
205 }
206
207 if ( update_nnode ) {
208  if ( roar_nnode_free(&(ROAR_CLIENT(cs)->nnode)) != -1 ) {
209   if ( sockaddr != NULL && addrlen != 0 ) {
210    roar_nnode_new_from_sockaddr(&(ROAR_CLIENT(cs)->nnode), sockaddr, addrlen);
211   } else {
212    roar_nnode_new_from_fh(&(ROAR_CLIENT(cs)->nnode), fh, 1);
213   }
214  }
215 }
216
217 if ( clients_set_proto(client, proto) == -1 ) {
218  ROAR_WARN("clients_new_from_fh2(*): Setting proto(0x%.4x) of client %i failed.", proto, client);
219  clients_delete(client);
220  return -1;
221 }
222
223 switch (proto) {
224  case ROAR_PROTO_ROARAUDIO:
225    // nothing needed to be done here
226   break;
227#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
228#ifdef ROAR_HAVE_H_ESD
229  case ROAR_PROTO_ESOUND:
230    ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command");
231
232    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
233     return -1;
234
235    ROAR_DBG("net_get_new_client(*): creating VIO OK");
236
237    if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 )
238     return -1;
239
240    ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully");
241   break;
242#endif
243#endif
244#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
245  case ROAR_PROTO_SIMPLE:
246    if ( emul_simple_on_connect(client, lsock) == -1 )
247     return -1;
248   break;
249#endif
250#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
251  case ROAR_PROTO_RSOUND: // nothing to do here.
252   break;
253#endif
254#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
255  case ROAR_PROTO_RPLAY:
256    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
257     return -1;
258
259    if ( emul_rplay_on_status(client, NULL, &vio, NULL, 0) == -1 )
260     return -1;
261   break;
262#endif
263  default:
264    // OS independiend code to close the socket:
265    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
266     return -1;
267
268    protohandle = clients_get_protohandle(proto);
269    supported = 0;
270    if ( protohandle != NULL ) {
271     if ( protohandle->lhandle != NULL )
272      roar_dl_context_restore(protohandle->lhandle);
273     switch (protohandle->type) {
274      case ROARD_PROTO_TYPE_BUILDIN:
275        //this should not end up here.
276        ROAR_WARN("net_get_new_client(lsock=%p): proto(%i) marked as buildin but isn't. BAD.", lsock, proto);
277        supported = 0;
278       break;
279      case ROARD_PROTO_TYPE_ROARDPROTO:
280        supported = 1;
281        if ( protohandle->impl.roardproto.new_client != NULL ) {
282         if ( protohandle->impl.roardproto.new_client(client, &vio, lsock) == -1 ) {
283          supported = 0;
284         }
285        }
286       break;
287      case ROARD_PROTO_TYPE_COMMON:
288        supported = 1;
289        if ( protohandle->impl.common->set_proto != NULL ) {
290         pluginpara = roar_dl_getpara(protohandle->lhandle); // will return NULL in case protohandle->lhandle is NULL.
291         if ( protohandle->impl.common->set_proto(client, &vio, &(cs->outbuf), &(cs->protoinst), protohandle->para, protohandle->paralen, pluginpara) == -1 ) {
292          supported = 0;
293         }
294         if ( pluginpara != NULL )
295          roar_dl_para_unref(pluginpara);
296        }
297       break;
298     }
299     if ( protohandle->lhandle != NULL )
300      roar_dl_context_store(protohandle->lhandle);
301    }
302
303    if ( !supported ) {
304     clients_delete(client);
305     //roar_vio_close(&vio);
306     return -1;
307    }
308   break;
309 }
310
311
312 return 0;
313}
314
315int clients_delete (int id) {
316 struct roar_client_server * cs;
317 const struct roard_proto_handle * proto;
318 struct roar_dl_librarypara * pluginpara;
319 struct roar_vio_calls vio;
320 int i;
321 int close_client_fh = 1;
322
323 ROAR_DBG("clients_delete(id=%i) = ?", id);
324
325 _CHECK_CID(id);
326
327 cs = g_clients[id];
328
329 proto = clients_get_protohandle(ROAR_CLIENT(cs)->proto);
330 if ( proto != NULL ) {
331  if ( proto->lhandle != NULL )
332   roar_dl_context_restore(proto->lhandle);
333  switch (proto->type) {
334   case ROARD_PROTO_TYPE_BUILDIN:
335     /* noop */
336    break;
337   case ROARD_PROTO_TYPE_ROARDPROTO:
338     if ( proto->impl.roardproto.delete_client != NULL )
339      proto->impl.roardproto.delete_client(id);
340    break;
341   case ROARD_PROTO_TYPE_COMMON:
342     if ( proto->impl.common->unset_proto != NULL ) {
343      roar_vio_open_fh_socket(&vio, clients_get_fh(id));
344      pluginpara = roar_dl_getpara(proto->lhandle); // will return NULL in case protohandle->lhandle is NULL.
345      proto->impl.common->unset_proto(id, &vio, &(cs->outbuf), &(cs->protoinst), proto->para, proto->paralen, pluginpara);
346      if ( pluginpara != NULL )
347       roar_dl_para_unref(pluginpara);
348     }
349    break;
350  }
351  if ( proto->lhandle != NULL )
352   roar_dl_context_store(proto->lhandle);
353 }
354
355 if ( cs->waits != NULL ) {
356  for (i = 0; cs->waits[i] != NULL; i++)
357   roar_notify_core_unsubscribe(NULL, cs->waits[i]);
358
359  roar_mm_free(cs->waits);
360  cs->waits = NULL;
361 }
362
363 roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_CLIENT);
364
365 counters_inc(clients, -1);
366
367 if (ROAR_CLIENT(cs)->execed != -1) {
368//  return streams_delete(g_clients[id]->execed);
369  ROAR_CLIENT(cs)->execed = -1;
370  close_client_fh = 0;
371 }
372
373 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
374  streams_delete(ROAR_CLIENT(cs)->streams[i]);
375 }
376
377 if ( ROAR_CLIENT(cs)->fh != -1 && close_client_fh )
378  close(ROAR_CLIENT(cs)->fh);
379
380 roar_nnode_free(&(ROAR_CLIENT(cs)->nnode));
381
382 if ( cs->inbuf != NULL )
383  roar_buffer_free(cs->inbuf);
384
385 if ( cs->outbuf != NULL )
386  roar_buffer_free(cs->outbuf);
387
388 if ( cs->protoinst != NULL )
389  roar_mm_free(cs->protoinst);
390
391 roar_mm_free(cs);
392 g_clients[id] = NULL;
393
394 ROAR_DBG("clients_delete(id=%i) = 0", id);
395 return 0;
396}
397
398int clients_close      (int id, int nocheck_exec) {
399 struct roar_client * c;
400
401 ROAR_DBG("clients_close(id=%i) = ?", id);
402
403 _CHECK_CID(id);
404
405 c = ROAR_CLIENT(g_clients[id]);
406
407 if ( c->fh == -1 ) {
408  ROAR_DBG("clients_delete(id=%i) = 0", id);
409  return 0;
410 }
411
412 if (nocheck_exec || c->execed != -1) {
413  close(c->fh);
414  c->fh = -1;
415 }
416
417 ROAR_DBG("clients_delete(id=%i) = 0", id);
418 return 0;
419}
420
421int clients_get       (int id, struct roar_client ** client) {
422 _CHECK_CID(id);
423
424 *client = ROAR_CLIENT(g_clients[id]);
425
426 if ( *client == NULL )
427  return -1;
428
429 return 0;
430}
431
432int clients_get_server (int id, struct roar_client_server ** client) {
433 _CHECK_CID(id);
434
435 *client = g_clients[id];
436
437 if ( *client == NULL )
438  return -1;
439
440 return 0;
441}
442
443int clients_set_fh    (int id, int    fh) {
444 struct roar_client * c;
445#ifdef SO_PEERCRED
446 struct ucred cred;
447 socklen_t cred_len = sizeof(cred);
448#endif
449
450 _CHECK_CID(id);
451
452 if ( (c = ROAR_CLIENT(g_clients[id])) == NULL )
453  return -1;
454
455 c->fh = fh;
456
457 if ( fh == -1 )
458  return 0;
459
460#ifdef SO_PEERCRED
461 if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
462  if ( cred.pid != 0 ) {
463   c->pid = cred.pid;
464   c->uid = cred.uid;
465   c->gid = cred.gid;
466  }
467 } else {
468  ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
469 }
470#elif defined(ROAR_HAVE_GETPEEREID)
471 if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
472  ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
473 }
474#endif
475
476 return 0;
477}
478
479int clients_get_fh    (int id) {
480 _CHECK_CID(id);
481
482 return ROAR_CLIENT(g_clients[id])->fh;
483}
484
485int clients_set_pid   (int id, int    pid) {
486 _CHECK_CID(id);
487
488 ROAR_CLIENT(g_clients[id])->pid = pid;
489
490 return 0;
491}
492
493int clients_set_uid   (int id, int    uid) {
494 _CHECK_CID(id);
495
496 ROAR_CLIENT(g_clients[id])->uid = uid;
497
498 return 0;
499}
500
501int clients_set_gid   (int id, int    gid) {
502 _CHECK_CID(id);
503
504 ROAR_CLIENT(g_clients[id])->gid = gid;
505
506 return 0;
507}
508
509int clients_set_name   (int id, const char * name) {
510 _CHECK_CID(id);
511
512 strncpy(ROAR_CLIENT(g_clients[id])->name, name, ROAR_BUFFER_NAME-1);
513 ROAR_CLIENT(g_clients[id])->name[ROAR_BUFFER_NAME-1] = 0;
514
515 return 0;
516}
517
518int clients_set_proto (int id, int    proto) {
519 int byteorder = ROAR_BYTEORDER_UNKNOWN;
520
521 _CHECK_CID(id);
522
523 switch (proto) {
524  case ROAR_PROTO_ROARAUDIO:
525  case ROAR_PROTO_ESOUND:
526  case ROAR_PROTO_RPLAY:
527  case ROAR_PROTO_SIMPLE:
528    byteorder = ROAR_BYTEORDER_NETWORK;
529   break;
530 }
531
532 ROAR_CLIENT(g_clients[id])->proto     = proto;
533 ROAR_CLIENT(g_clients[id])->byteorder = byteorder;
534
535 return 0;
536}
537
538int clients_block      (int id, int unblock) {
539 _CHECK_CID(id);
540
541 if ( unblock ) {
542  g_clients[id]->blockc--;
543 } else {
544  g_clients[id]->blockc++;
545 }
546
547 return 0;
548}
549
550
551#define MAX_STREAMLESS 8
552
553int clients_check_all (void) {
554#ifdef ROAR_HAVE_SELECT
555 struct roar_client * c;
556 struct timeval tv;
557 fd_set r, w, e;
558 int i, j;
559 int ret;
560 int fh;
561 int max_fh = -1;
562 int have = 0;
563 struct {
564  int id;
565  int fh;
566 } streamless[MAX_STREAMLESS];
567 int have_streamless = 0;
568 int have_stream;
569
570 ROAR_DBG("clients_check_all(void) = ?");
571
572 FD_ZERO(&r);
573 FD_ZERO(&w);
574 FD_ZERO(&e);
575
576 tv.tv_sec  = 0;
577 tv.tv_usec = 1;
578
579 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
580  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
581   continue;
582
583  ROAR_DBG("clients_check_all(void): i(client)=%i", i);
584
585  if ( (fh = c->fh) != -1 ) {
586   have++;
587
588   ROAR_DBG("clients_check_all(*): fh=%i", fh);
589
590   FD_SET(fh, &r);
591   FD_SET(fh, &e);
592
593   if ( g_clients[i]->outbuf != NULL ) {
594    FD_SET(fh, &w);
595   }
596
597   if ( fh > max_fh )
598    max_fh = fh;
599  }
600
601  have_stream = 0;
602
603  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
604   ROAR_DBG("clients_check_all(void): i(client)=%i, j(stream)=%i", i, j);
605   if ( (fh = streams_get_fh(c->streams[j])) != -1 ) {
606    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, c->streams[j], fh);
607    if ( fh > -1 ) {
608     FD_SET(fh, &r);
609
610     if ( fh > max_fh )
611      max_fh = fh;
612    } else if ( fh == -2 ) {
613     streams_check(c->streams[j]);
614    }
615
616    have_stream = 1;
617   }
618   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
619  }
620
621  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
622   streamless[have_streamless  ].id = i;
623   if ( (streamless[have_streamless++].fh = c->fh) == -1 )
624    have_streamless--;
625  }
626 }
627
628 ROAR_DBG("clients_check_all(void): max_fh=%i", max_fh);
629
630 if ( max_fh == -1 )
631  return 0;
632
633 if ( (ret = select(max_fh + 1, &r, &w, &e, &tv)) < 1 ) {
634  return ret < 0 ? ret : have;
635 }
636
637 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
638  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
639   continue;
640
641  if ( (fh = c->fh) != -1 ) {
642   if ( FD_ISSET(fh, &r) ) {
643    if ( c->execed == -1 ) {
644     clients_check(i);
645     if ( g_clients[i] != NULL && ROAR_CLIENT(g_clients[i])->execed != -1 ) {
646      FD_CLR(fh, &r);
647     }
648/*
649    } else {
650     streams_check(g_clients[i]->execed);
651*/
652    }
653   }
654   if ( FD_ISSET(fh, &w) ) {
655    clients_flush(i);
656   }
657
658   if ( FD_ISSET(fh, &e) ) {
659    clients_delete(i);
660    continue;
661   }
662  }
663
664  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
665   continue;
666
667  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
668   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
669   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
670    break;
671
672   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
673   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, c->streams[j]);
674
675   if ( (fh = streams_get_fh(c->streams[j])) != -1 ) {
676    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
677    if ( fh > -1 && FD_ISSET(fh, &r) ) {
678     streams_check(c->streams[j]);
679    }
680   }
681  }
682 }
683
684 if ( have_streamless ) {
685   FD_ZERO(&r);
686   FD_ZERO(&w);
687
688   tv.tv_sec  = 0;
689   tv.tv_usec = 1;
690
691   max_fh = -1;
692
693   for (i = 0; i < have_streamless; i++) {
694    if ( g_clients[j = streamless[i].id] == NULL )
695     continue;
696
697    if ( ROAR_CLIENT(g_clients[j])->execed != -1 )
698     continue;
699
700    fh = streamless[i].fh;
701
702    ROAR_DBG("clients_check_all(void): fh=%i", fh);
703    FD_SET(fh, &r);
704
705    if ( g_clients[j]->outbuf != NULL ) {
706     FD_SET(fh, &w);
707    }
708
709    if ( fh > max_fh )
710     max_fh = fh;
711   }
712
713   if ( (ret = select(max_fh + 1, &r, &w, NULL, &tv)) < 0 ) {
714    return ret;
715   }
716
717   for (i = 0; i < have_streamless; i++) {
718    if ( FD_ISSET(streamless[i].fh, &r) ) {
719     clients_check(streamless[i].id);
720    }
721    if ( FD_ISSET(streamless[i].fh, &w) ) {
722     clients_flush(streamless[i].id);
723    }
724   }
725 }
726
727 ROAR_DBG("clients_check_all(void) = %i // have value", have);
728 return have;
729#else
730 ROAR_DBG("clients_check_all(void) = -1");
731 return -1;
732#endif
733}
734
735int clients_check     (int id) {
736 struct roar_client_server * cs;
737 struct roar_message    m;
738 struct roar_connection con;
739 struct roar_vio_calls  vio;
740 struct roar_error_state errstate;
741 const struct roard_proto_handle * proto;
742 struct roar_dl_librarypara * pluginpara;
743 int command_error;
744 char * data = NULL;
745 int oldcmd;
746 int r;
747 int rv = 0;
748 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
749 uint32_t event;
750
751 ROAR_DBG("clients_check(id=%i) = ?", id);
752
753 _CHECK_CID(id);
754
755 cs = g_clients[id];
756
757 if ( ROAR_CLIENT(cs)->fh == -1 )
758  return -1;
759
760 roar_connect_fh(&con, ROAR_CLIENT(cs)->fh);
761
762 ROAR_DBG("clients_check(id=%i): c->proto=%i", id, ROAR_CLIENT(cs)->proto);
763
764 switch (ROAR_CLIENT(cs)->proto) {
765  case ROAR_PROTO_ROARAUDIO:
766    r = roar_recv_message(&con, &m, &data);
767
768    if ( r == -1 ) { // should we drop the client?
769     clients_delete(id);
770     return -1;
771    }
772
773    event = ROAR_NOTIFY_CMD2EVENT(m.cmd);
774
775    oldcmd = m.cmd;
776
777    roar_err_store(&errstate);
778    roar_err_clear_all();
779    r = command_exec(id, &m, &data, flags);
780    roar_err_update();
781    command_error = roar_error;
782    roar_err_restore(&errstate);
783
784    if ( r == -1 ) {
785     // use command_error to create an error frame here as soon as this is supported.
786     m.cmd     = ROAR_CMD_ERROR;
787     m.datalen = 0;
788     ROAR_DBG("clients_check(*): Exec of command faild!");
789    } else {
790     if ( m.cmd == oldcmd ) {
791      m.cmd     = ROAR_CMD_OK;
792      m.datalen = 0;
793     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
794      m.cmd     = ROAR_CMD_OK;
795      rv        = 1;
796     }
797    }
798    ROAR_DBG("clients_check(*): data=%p", data);
799
800    if ( flags[1] & COMMAND_FLAG_OUT_NOSEND ) {
801     roar_notify_core_emit_simple(event, id, -1, -1, -1, -1, NULL, 0);
802    } else {
803     roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0);
804     roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL);
805    }
806
807    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
808     clients_close(id, 1);
809
810   break;
811#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
812  case ROAR_PROTO_RSOUND:
813    rv = emul_rsound_check_client(id, NULL);
814    if ( rv == 0 ) { // loop as long as we don't get an error.
815     while (rv == 0)
816      rv = emul_rsound_check_client(id, NULL);
817     rv = 0; // restore
818    } else { // in case of error delete the client
819     if (
820#ifdef EAGAIN
821          errno != EAGAIN      &&
822#endif
823#ifdef EWOULDBLOCK
824          errno != EWOULDBLOCK &&
825#endif
826#ifdef EINTR
827          errno != EINTR       &&
828#endif
829          1 ) {
830      rv = clients_delete(id);
831     } else {
832      rv = 0;
833     }
834    }
835   break;
836#endif
837  default:
838    rv = -1;
839    proto = clients_get_protohandle(ROAR_CLIENT(cs)->proto);
840    if ( proto != NULL ) {
841     roar_vio_open_fh_socket(&vio, clients_get_fh(id));
842     if ( proto->lhandle != NULL )
843      roar_dl_context_restore(proto->lhandle);
844
845     switch (proto->type) {
846      case ROARD_PROTO_TYPE_BUILDIN:
847        ROAR_WARN("clients_check(id=%i): proto(%i) marked as buildin but isn't. BAD.", id, proto->proto);
848       break;
849      case ROARD_PROTO_TYPE_ROARDPROTO:
850        if ( proto->impl.roardproto.check_client != NULL )
851         rv = proto->impl.roardproto.check_client(id, &vio);
852       break;
853      case ROARD_PROTO_TYPE_COMMON:
854        if ( proto->impl.common->handle != NULL ) {
855         pluginpara = roar_dl_getpara(proto->lhandle); // will return NULL in case protohandle->lhandle is NULL.
856         rv = proto->impl.common->handle(id, &vio, &(cs->outbuf), &(cs->protoinst), proto->para, proto->paralen, pluginpara);
857         if ( pluginpara != NULL )
858          roar_dl_para_unref(pluginpara);
859        }
860        if ( rv == -1 )
861         rv = clients_delete(id);
862       break;
863     }
864
865     if ( proto->lhandle != NULL )
866      roar_dl_context_store(proto->lhandle);
867    }
868 }
869
870 if ( data != NULL )
871  roar_mm_free(data);
872
873 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
874 return rv;
875}
876
877int clients_flush      (int id) {
878 struct roar_vio_calls             vio;
879 struct roar_client_server       * cs;
880 struct roar_client              * c;
881 const struct roard_proto_handle * p;
882 struct roar_dl_librarypara * pluginpara;
883 size_t len;
884 ssize_t ret;
885 void * buf;
886 int rv;
887
888 _CHECK_CID(id);
889
890 c = ROAR_CLIENT(cs = g_clients[id]);
891
892 p = clients_get_protohandle(c->proto);
893
894 if ( p == NULL )
895  return -1;
896
897 roar_vio_open_fh_socket(&vio, clients_get_fh(id));
898
899 switch (p->type) {
900  case ROARD_PROTO_TYPE_BUILDIN:
901    /* noop */
902   break;
903  case ROARD_PROTO_TYPE_ROARDPROTO:
904    if ( p->impl.roardproto.flush_client != NULL ) {
905     if ( p->lhandle != NULL )
906      roar_dl_context_restore(p->lhandle);
907     rv = p->impl.roardproto.flush_client(id, &vio);
908     if ( p->lhandle != NULL )
909      roar_dl_context_store(p->lhandle);
910     return rv;
911    }
912   break;
913  case ROARD_PROTO_TYPE_COMMON:
914    if ( p->impl.common->flush != NULL ) {
915     if ( p->lhandle != NULL )
916      roar_dl_context_restore(p->lhandle);
917     pluginpara = roar_dl_getpara(p->lhandle); // will return NULL in case protohandle->lhandle is NULL.
918     rv = p->impl.common->flush(id, &vio, &(cs->outbuf), &(cs->protoinst), p->para, p->paralen, pluginpara);
919     if ( p->lhandle != NULL )
920      roar_dl_context_store(p->lhandle);
921     if ( pluginpara != NULL )
922      roar_dl_para_unref(pluginpara);
923     if ( rv == -1 )
924      rv = clients_delete(id);
925     return rv;
926    }
927   break;
928 }
929
930 if ( roar_buffer_get_len(cs->outbuf, &len) == -1 )
931  return -1;
932
933 if ( roar_buffer_get_data(cs->outbuf, &buf) == -1 )
934  return -1;
935
936 ret = roar_vio_write(&vio, buf, len);
937
938 if ( ret < 1 ) {
939  clients_delete(id);
940  return -1;
941 }
942
943 if ( ret == (ssize_t)len ) {
944  if ( roar_buffer_next(&(cs->outbuf)) == -1 ) {
945   clients_delete(id);
946   return -1;
947  }
948 } else {
949  if ( roar_buffer_set_offset(cs->outbuf, ret) == -1 ) {
950   clients_delete(id);
951   return -1;
952  }
953 }
954
955 if ( cs->outbuf == NULL ) {
956  switch (p->type) {
957   case ROARD_PROTO_TYPE_BUILDIN:
958     /* noop */
959    break;
960   case ROARD_PROTO_TYPE_ROARDPROTO:
961     if ( p->impl.roardproto.flushed_client != NULL ) {
962      if ( p->lhandle != NULL )
963       roar_dl_context_restore(p->lhandle);
964      rv = p->impl.roardproto.flushed_client(id, &vio);
965      if ( p->lhandle != NULL )
966       roar_dl_context_store(p->lhandle);
967      return rv;
968     }
969    break;
970   case ROARD_PROTO_TYPE_COMMON:
971     if ( p->impl.common->flushed != NULL ) {
972      if ( p->lhandle != NULL )
973       roar_dl_context_restore(p->lhandle);
974      pluginpara = roar_dl_getpara(p->lhandle); // will return NULL in case protohandle->lhandle is NULL.
975      rv = p->impl.common->flushed(id, &vio, &(cs->outbuf), &(cs->protoinst), p->para, p->paralen, pluginpara);
976      if ( p->lhandle != NULL )
977       roar_dl_context_store(p->lhandle);
978      if ( pluginpara != NULL )
979       roar_dl_para_unref(pluginpara);
980      if ( rv == -1 )
981       rv = clients_delete(id);
982      return rv;
983     }
984    break;
985  }
986 }
987
988 return 0;
989}
990
991int clients_send_mon  (struct roar_audio_info * sa) {
992 int i;
993// int fh;
994 int j;
995 int keep_going;
996
997 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
998  if ( g_clients[i] == NULL )
999   continue;
1000
1001  keep_going = 1;
1002
1003/*
1004  if ( (fh = g_clients[i]->fh) == -1 )
1005   continue;
1006*/
1007
1008  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed);
1009
1010/*
1011  if ( g_clients[i]->execed == -1 ) {
1012   // TODO: add some code to send a message to the client insetd of the raw data.
1013*/
1014   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
1015    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
1016    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
1017    if ( ROAR_CLIENT(g_clients[i])->streams[j] != -1 ) {
1018     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, ROAR_CLIENT(g_clients[i])->streams[j]);
1019     streams_send_mon(ROAR_CLIENT(g_clients[i])->streams[j]);
1020
1021     // the client may be deleted here, check if it still exists:
1022     if ( g_clients[i] == NULL )
1023      keep_going = 0;
1024    }
1025   }
1026/*
1027  } else {
1028//   streams_check(g_clients[i]->execed);
1029   streams_send_mon(g_clients[i]->execed);
1030//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
1031//    clients_delete(i); // delete client in case we could not write
1032  }
1033*/
1034 }
1035
1036 // TODO: FIXME: should this really be -1?
1037 return -1;
1038}
1039
1040int clients_send_filter(struct roar_audio_info * sa) {
1041 struct roar_client * c;
1042 int i;
1043 int fh;
1044
1045 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
1046  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
1047   continue;
1048
1049  if ( (fh = c->fh) == -1 )
1050   continue;
1051
1052  if ( c->execed == -1 ) {
1053   // TODO: add some code to send a message to the client insetd of the raw data.
1054  } else {
1055//   streams_check(g_clients[i]->execed);
1056   streams_send_filter(c->execed);
1057//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
1058//    clients_delete(i); // delete client in case we could not write
1059  }
1060 }
1061
1062 return -1;
1063}
1064
1065int clients_add_output (int id, struct roar_buffer ** buf) {
1066 struct roar_client_server   * cs;
1067
1068 _CHECK_CID(id);
1069 cs = g_clients[id];
1070
1071 if ( cs->outbuf == NULL ) {
1072  cs->outbuf = *buf;
1073 } else {
1074  return roar_buffer_moveinto(cs->outbuf, buf);
1075 }
1076
1077 return 0;
1078}
1079
1080// proto support
1081const struct roard_proto_handle * clients_get_protohandle(const int proto) {
1082 size_t i;
1083
1084 if ( proto < 0 ) {
1085  roar_err_set(ROAR_ERROR_INVAL);
1086  return NULL;
1087 }
1088
1089 for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++)
1090  if ( __protos[i].proto == proto )
1091   return &(__protos[i]);
1092
1093 roar_err_set(ROAR_ERROR_NOENT);
1094 return NULL;
1095}
1096
1097int clients_register_proto(struct roard_proto * proto, struct roar_dl_lhandle * lhandle) {
1098 const size_t len = sizeof(__protos)/sizeof(*__protos);
1099 size_t i;
1100
1101 if ( proto == NULL )
1102  return -1;
1103
1104 for (i = 0; __protos[i].proto != -1; i++);
1105
1106 // i is now at pos of current EOS entry.
1107
1108 // test if we have space for one more entry:
1109 if ( (i+1) >= len )
1110  return -1;
1111
1112 memcpy(&(__protos[i].impl.roardproto), proto, sizeof(__protos[i].impl.roardproto));
1113
1114 __protos[i].impl.roardproto.lhandle = lhandle;
1115
1116 __protos[i].proto   = proto->proto;
1117 __protos[i].type    = ROARD_PROTO_TYPE_ROARDPROTO;
1118 __protos[i].lhandle = lhandle;
1119
1120 return 0;
1121}
1122
1123int clients_register_proto_common(const struct roar_dl_proto * proto, struct roar_dl_lhandle * lhandle) {
1124 const size_t len = sizeof(__protos)/sizeof(*__protos);
1125 size_t i;
1126
1127 ROAR_DBG("clients_register_proto_common(proto=%p, lhandle=%p) = ?", proto, lhandle);
1128
1129 if ( proto == NULL )
1130  return -1;
1131
1132 for (i = 0; __protos[i].proto != -1; i++);
1133
1134 // i is now at pos of current EOS entry.
1135
1136 // test if we have space for one more entry:
1137 if ( (i+1) >= len )
1138  return -1;
1139
1140 __protos[i].impl.common = proto;
1141
1142 __protos[i].proto   = proto->proto;
1143 __protos[i].type    = ROARD_PROTO_TYPE_COMMON;
1144 __protos[i].lhandle = lhandle;
1145
1146 return 0;
1147}
1148
1149int clients_unregister_proto(int proto) {
1150 size_t i;
1151
1152 if ( proto < 0 ) {
1153  roar_err_set(ROAR_ERROR_RANGE);
1154  return -1;
1155 }
1156
1157 for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) {
1158  if ( __protos[i].proto == proto ) {
1159   memset(&(__protos[i]), 0, sizeof(*__protos));
1160   __protos[i].proto = -1;
1161   return 0;
1162  }
1163 }
1164
1165 roar_err_set(ROAR_ERROR_NOENT);
1166 return -1;
1167}
1168
1169void print_protolist        (enum output_format format) {
1170 struct roard_proto_handle * p;
1171 char flags[5] = "    ";
1172 char subsys[7] = "      ";
1173 const char * description;
1174 size_t i;
1175
1176 switch (format) {
1177  case FORMAT_NATIVE:
1178    printf("  Protocol Flag Subsys - Description\n");
1179    printf("------------------------------------------------------\n");
1180    printf("  roar     bb   WM LRX - RoarAudio native protocol\n");
1181#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
1182    printf("  simple   bb   WM LRX - PulseAudio simple protocol\n");
1183#endif
1184#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
1185    printf("  rsound   bb   W      - RSound emulation\n");
1186#endif
1187   break;
1188  case FORMAT_WIKI:
1189    printf("||=Protocol =||=Flag =||=Subsys =||=Description              =||\n");
1190    printf("||roar       ||bb     ||WM LRX   ||RoarAudio native protocol  ||\n");
1191#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
1192    printf("||simple     ||bb     ||WM LRX   ||PulseAudio simple protocol ||\n");
1193#endif
1194#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
1195    printf("||rsound     ||bb     ||W        ||RSound emulation           ||\n");
1196#endif
1197   break;
1198  case FORMAT_CSV:
1199    printf("Protocol,Flag,Subsys,Description\n");
1200    printf("roar,bb,WM LRX,RoarAudio native protocol\n");
1201#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
1202    printf("simple,bb,WM LRX,PulseAudio simple protocol\n");
1203#endif
1204#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
1205    printf("rsound,bb,W,RSound emulation\n");
1206#endif
1207   break;
1208  default:
1209    roar_err_set(ROAR_ERROR_NOTSUP);
1210    return;
1211 }
1212
1213 for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) {
1214  p = &(__protos[i]);
1215  if ( p->proto == -1 )
1216   continue;
1217
1218  strncpy(flags, "    ", 4);
1219  strncpy(subsys, "      ", 6);
1220  description = "(none)";
1221
1222  if ( p->lhandle != NULL ) {
1223   flags[0] = 'P';
1224  } else {
1225   flags[0] = 'b';
1226  }
1227
1228  switch (p->type) {
1229   case ROARD_PROTO_TYPE_BUILDIN:
1230     continue;
1231    break;
1232   case ROARD_PROTO_TYPE_ROARDPROTO:
1233     flags[1] = 'R';
1234
1235     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_WAVEFORM )
1236      subsys[0] = 'W';
1237     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_MIDI )
1238      subsys[1] = 'M';
1239     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_CB )
1240      subsys[2] = 'C';
1241     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_LIGHT )
1242      subsys[3] = 'L';
1243     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_RAW )
1244      subsys[4] = 'R';
1245     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_COMPLEX )
1246      subsys[5] = 'X';
1247
1248     description = p->impl.roardproto.description;
1249    break;
1250   case ROARD_PROTO_TYPE_COMMON:
1251     flags[1] = 'C';
1252     description = p->impl.common->description;
1253    break;
1254  }
1255
1256  switch (format) {
1257   case FORMAT_NATIVE:
1258     printf("  %-8s %s %s - %s\n", roar_proto2str(p->proto), flags, subsys, description);
1259    break;
1260   case FORMAT_WIKI:
1261     printf("||%s ||%s ||%s ||%s ||\n", roar_proto2str(p->proto), flags, subsys, description);
1262    break;
1263   case FORMAT_CSV:
1264     printf("%s,%s,%s,%s\n", roar_proto2str(p->proto), flags, subsys, description);
1265    break;
1266  }
1267 }
1268}
1269
1270int client_stream_exec   (int client, int stream) {
1271 struct roar_client * c;
1272 int i;
1273 int fh;
1274
1275 _CHECK_CID(client);
1276
1277#if 0
1278 if ( g_clients[client] == NULL ) {
1279  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
1280  return -1;
1281 }
1282#endif
1283
1284 c = ROAR_CLIENT(g_clients[client]);
1285
1286 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
1287  if ( c->streams[i] == stream ) {
1288   c->execed = stream;
1289   if ( streams_is_ready(stream) == 0 ) {
1290    streams_set_fh(stream, c->fh);
1291    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
1292   } else {
1293    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream);
1294    if ( (fh = c->fh) != -1 ) {
1295     close(fh);
1296     c->fh = -1;
1297    }
1298   }
1299   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
1300   return 0;
1301  }
1302 }
1303
1304 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
1305 return -1;
1306}
1307
1308int client_stream_set_fh (int client, int stream, int fh) {
1309 int i;
1310
1311 ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh);
1312
1313 _CHECK_CID(client);
1314
1315 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
1316  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
1317   ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i);
1318   return streams_set_fh(stream, fh);
1319  }
1320 }
1321
1322 ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh);
1323 return -1;
1324}
1325
1326int client_stream_add    (int client, int stream) {
1327 int i;
1328
1329 _CHECK_CID(client);
1330
1331 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
1332  if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) {
1333   ROAR_CLIENT(g_clients[client])->streams[i] = stream;
1334   streams_set_client(stream, client);
1335   return 0;
1336  }
1337 }
1338
1339 return -1;
1340}
1341
1342int client_stream_delete (int client, int stream) {
1343 int i;
1344
1345 _CHECK_CID(client);
1346
1347 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
1348  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
1349   ROAR_CLIENT(g_clients[client])->streams[i] = -1;
1350
1351   if ( stream == ROAR_CLIENT(g_clients[client])->execed ) {
1352    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
1353    clients_delete(client);
1354   }
1355
1356   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
1357   return 0;
1358  }
1359 }
1360
1361 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
1362 return -1;
1363}
1364
1365int client_stream_move   (int client, int stream) {
1366 int old_client = streams_get_client(stream);
1367
1368 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
1369
1370 if ( old_client != -1 )
1371  if ( client_stream_delete(old_client, stream) == -1 )
1372   return -1;
1373
1374 return client_stream_add(client, stream);
1375}
1376
1377
1378// notify thingys
1379int clients_wait    (int client, struct roar_event * events, size_t num) {
1380 struct roar_client_server * cs;
1381 size_t i, c;
1382
1383 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = ?", client, events, (long long unsigned int)num);
1384
1385 _CHECK_CID(client);
1386
1387 cs = g_clients[client];
1388
1389 if ( cs->waits != NULL )
1390  return -1;
1391
1392 cs->waits = roar_mm_malloc((num+1) * sizeof(struct roar_subscriber *));
1393
1394 if ( cs->waits == NULL )
1395  return -1;
1396
1397 if ( clients_block(client, 0) != 0 )
1398  return -1;
1399
1400 for (i = 0; i < num; i++) {
1401#if defined(DEBUG) && 0
1402  dbg_notify_cb(NULL, &(events[i]), cs);
1403#endif
1404  cs->waits[i] = roar_notify_core_subscribe(NULL, &(events[i]), clients_ncb_wait, cs);
1405  if ( cs->waits[i] == NULL ) {
1406   for (c = 0; c < i; c++)
1407    roar_notify_core_unsubscribe(NULL, cs->waits[c]);
1408   roar_mm_free(cs->waits);
1409   cs->waits = NULL;
1410   clients_block(client, 1);
1411   return -1;
1412  }
1413 }
1414
1415 cs->waits[num] = NULL;
1416
1417 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = 0", client, events, (long long unsigned int)num);
1418 return 0;
1419}
1420
1421void clients_ncb_wait(struct roar_notify_core * core, struct roar_event * event, void * userdata) {
1422 struct roar_client_server * cs = userdata;
1423 struct roar_message m;
1424 struct roar_connection con;
1425 uint16_t * u16 = (uint16_t *) m.data;
1426 size_t tmp;
1427 size_t i;
1428
1429 ROAR_DBG("clients_ncb_wait(core=%p, event=%p, userdata=%p) = ?", core, event, userdata);
1430
1431 for (i = 0; cs->waits[i] != NULL; i++)
1432  roar_notify_core_unsubscribe(NULL, cs->waits[i]);
1433
1434 roar_mm_free(cs->waits);
1435 cs->waits = NULL;
1436
1437 // protocol depended handling...
1438 memset(&m, 0, sizeof(m));
1439 m.cmd = ROAR_CMD_OK;
1440 u16[0] = ROAR_HOST2NET16(0); // Version
1441 u16[1] = ROAR_HOST2NET16(0); // flags
1442
1443 tmp = sizeof(m.data) - 4;
1444
1445 roar_event_to_blob(event, m.data + 4, &tmp);
1446
1447 m.datalen = tmp + 4;
1448
1449 roar_connect_fh(&con, ROAR_CLIENT(cs)->fh);
1450 roar_send_message(&con, &m, NULL);
1451 // ...end of protocol depended handling.
1452
1453// clients_block(, 1);
1454 // TODO: FIXME: bad hack...
1455 cs->blockc--;
1456}
1457
1458
1459// acclev:
1460static struct {
1461 const enum roard_client_acclev acclev;
1462 const char *                   name;
1463} _g_acclevs[] = {
1464 {ACCLEV_NONE,    "none"   },
1465 {ACCLEV_IDENTED, "idented"},
1466 {ACCLEV_CONCTL,  "conctl" },
1467 {ACCLEV_GUEST,   "guest"  },
1468 {ACCLEV_USER,    "user"   },
1469 {ACCLEV_PWRUSER, "pwruser"},
1470 {ACCLEV_ALL,     "all"    },
1471 {-1, NULL}
1472};
1473
1474enum roard_client_acclev clients_str2acclev(const char * acclev) {
1475 int i;
1476
1477 for (i = 0; _g_acclevs[i].name != NULL; i++)
1478  if ( !strcasecmp(_g_acclevs[i].name, acclev) )
1479   return _g_acclevs[i].acclev;
1480
1481 return ACCLEV_ERROR;
1482}
1483
1484const char * clients_acclev2str(const enum roard_client_acclev acclev) {
1485 int i;
1486
1487 for (i = 0; _g_acclevs[i].name != NULL; i++)
1488  if ( _g_acclevs[i].acclev == acclev )
1489   return _g_acclevs[i].name;
1490
1491 return NULL;
1492}
1493
1494//ll
Note: See TracBrowser for help on using the repository browser.