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
RevLine 
[0]1//clients.c:
2
[668]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[668]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[668]23 *
24 */
25
[4921]26/* ckport options:
[4923]27 * ckport: ignore-symbol: roar_event_to_blob of target libroar0
[4921]28 */
29
[0]30#include "roard.h"
31
[5192]32// declared 'extern'
33struct roar_client_server * g_clients[ROAR_CLIENTS_MAX];
34struct roard_proto g_proto[MAX_PROTOS];
35
36
[4683]37struct roard_proto g_proto[MAX_PROTOS] = {
[4678]38#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
39#ifdef ROAR_HAVE_H_ESD
[5275]40 {ROAR_PROTO_ESOUND, ROAR_SUBSYS_WAVEFORM, "EsounD emulation", NULL, emul_esd_check_client, NULL, NULL},
[4678]41#endif
42#endif
43#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
[5275]44 {ROAR_PROTO_RPLAY, ROAR_SUBSYS_WAVEFORM, "RPlay emulation", NULL, emul_rplay_check_client, NULL, NULL},
[4678]45#endif
[4703]46#ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER
[5275]47 {ROAR_PROTO_GOPHER, ROAR_SUBSYS_WAVEFORM, "The Internet Gopher Protocol", NULL, emul_gopher_check_client, NULL, emul_gopher_flushed_client},
[4703]48#endif
[5275]49 {-1, 0, NULL, NULL, NULL, NULL, NULL}
[4678]50};
51
[3910]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
[0]55int clients_init (void) {
56 int i;
57
58 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
59  g_clients[i] = NULL;
60
[5275]61 for (i = 0; g_proto[i].proto != -1; i++);
62
63 for (; i < MAX_PROTOS; i++)
64  g_proto[i].proto = -1;
65
[0]66 return 0;
67}
68
69int clients_free (void) {
70 int i;
71
72 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
[3910]73  if ( g_clients[i] != NULL )
[0]74   clients_delete(i);
75
76 return 0;
77}
78
79int clients_new (void) {
80 int i;
81 int s;
[4326]82 struct roar_client_server * ns;
[0]83 struct roar_client * n;
84
85 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
86  if ( g_clients[i] == NULL ) {
[4326]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
[0]92   if ( n != NULL ) {
93    n->pid    = -1;
[437]94    n->uid    = -1;
95    n->gid    = -1;
[0]96    n->fh     = -1;
97
98    *n->name = 0;
99
[2614]100    n->proto     = ROAR_PROTO_ROARAUDIO;
101    n->byteorder = ROAR_BYTEORDER_NETWORK;
[2517]102
[346]103    n->acl   = NULL;
104
[0]105    n->execed = -1;
106    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
107     n->streams[s] = -1;
108
[2815]109    if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) {
[3063]110     roar_mm_free(n);
[2815]111     return -1;
112    }
113
[4343]114    ns->blockc   = 0;
115    ns->waits    = NULL;
[4468]116    ns->acclev   = ACCLEV_NONE;
[4343]117
[4326]118    g_clients[i] = ns;
[0]119
[4101]120    counters_inc(clients, 1);
[4346]121    roar_notify_core_emit_snoargs(ROAR_OE_BASICS_NEW, -1, i, ROAR_OT_CLIENT);
[0]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
[3737]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
[0]164int clients_delete (int id) {
[4394]165 struct roar_client_server * cs;
[0]166 int i;
[1164]167 int close_client_fh = 1;
[0]168
[2608]169 ROAR_DBG("clients_delete(id=%i) = ?", id);
170
[4394]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
[4346]183 roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_CLIENT);
184
[4101]185 counters_inc(clients, -1);
186
[4395]187 if (ROAR_CLIENT(cs)->execed != -1) {
[0]188//  return streams_delete(g_clients[id]->execed);
[4395]189  ROAR_CLIENT(cs)->execed = -1;
[1164]190  close_client_fh = 0;
[0]191 }
192
193 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4395]194  streams_delete(ROAR_CLIENT(cs)->streams[i]);
[0]195 }
196
[4395]197 if ( ROAR_CLIENT(cs)->fh != -1 && close_client_fh )
198  close(ROAR_CLIENT(cs)->fh);
[0]199
[4395]200 roar_nnode_free(&(ROAR_CLIENT(cs)->nnode));
[2815]201
[4684]202 if ( cs->inbuf != NULL )
203  roar_buffer_free(cs->inbuf);
204
205 if ( cs->outbuf != NULL )
206  roar_buffer_free(cs->outbuf);
207
[4395]208 roar_mm_free(cs);
[0]209 g_clients[id] = NULL;
210
211 ROAR_DBG("clients_delete(id=%i) = 0", id);
212 return 0;
213}
214
[3927]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
[4326]222 c = ROAR_CLIENT(g_clients[id]);
[3927]223
224 if ( c->fh == -1 ) {
225  ROAR_DBG("clients_delete(id=%i) = 0", id);
226  return 0;
227 }
228
[4326]229 if (nocheck_exec || c->execed != -1) {
[3927]230  close(c->fh);
231  c->fh = -1;
232 }
233
234 ROAR_DBG("clients_delete(id=%i) = 0", id);
235 return 0;
236}
237
[0]238int clients_get       (int id, struct roar_client ** client) {
[3910]239 _CHECK_CID(id);
240
[4326]241 *client = ROAR_CLIENT(g_clients[id]);
[0]242
243 if ( *client == NULL )
244  return -1;
245
246 return 0;
247}
248
[4468]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
[0]260int clients_set_fh    (int id, int    fh) {
[3713]261 struct roar_client * c;
262#ifdef SO_PEERCRED
263 struct ucred cred;
264 socklen_t cred_len = sizeof(cred);
265#endif
[501]266
[3910]267 _CHECK_CID(id);
268
[4326]269 if ( (c = ROAR_CLIENT(g_clients[id])) == NULL )
[0]270  return -1;
271
[3713]272 c->fh = fh;
273
[4947]274 if ( fh == -1 )
275  return 0;
276
[3713]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
[0]292
293 return 0;
294}
295
[755]296int clients_get_fh    (int id) {
[3910]297 _CHECK_CID(id);
[755]298
[4326]299 return ROAR_CLIENT(g_clients[id])->fh;
[755]300}
301
[0]302int clients_set_pid   (int id, int    pid) {
[3910]303 _CHECK_CID(id);
[0]304
[4326]305 ROAR_CLIENT(g_clients[id])->pid = pid;
[0]306
307 return 0;
308}
309
[439]310int clients_set_uid   (int id, int    uid) {
[3910]311 _CHECK_CID(id);
[439]312
[4326]313 ROAR_CLIENT(g_clients[id])->uid = uid;
[439]314
315 return 0;
316}
317
318int clients_set_gid   (int id, int    gid) {
[3910]319 _CHECK_CID(id);
[439]320
[4326]321 ROAR_CLIENT(g_clients[id])->gid = gid;
[439]322
323 return 0;
324}
325
[2517]326int clients_set_proto (int id, int    proto) {
[2614]327 int byteorder = ROAR_BYTEORDER_UNKNOWN;
328
[3910]329 _CHECK_CID(id);
[2517]330
[2614]331 switch (proto) {
332  case ROAR_PROTO_ROARAUDIO:
[2828]333  case ROAR_PROTO_ESOUND:
[3981]334  case ROAR_PROTO_RPLAY:
[3255]335  case ROAR_PROTO_SIMPLE:
[2614]336    byteorder = ROAR_BYTEORDER_NETWORK;
337   break;
338 }
339
[4326]340 ROAR_CLIENT(g_clients[id])->proto     = proto;
341 ROAR_CLIENT(g_clients[id])->byteorder = byteorder;
[2517]342
343 return 0;
344}
345
[4343]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
[498]359#define MAX_STREAMLESS 8
360
[0]361int clients_check_all (void) {
[1480]362#ifdef ROAR_HAVE_SELECT
[4326]363 struct roar_client * c;
[0]364 struct timeval tv;
[4686]365 fd_set r, w, e;
[66]366 int i, j;
[0]367 int ret;
368 int fh;
369 int max_fh = -1;
[71]370 int have = 0;
[498]371 struct {
372  int id;
373  int fh;
374 } streamless[MAX_STREAMLESS];
375 int have_streamless = 0;
376 int have_stream;
[0]377
[4815]378 ROAR_DBG("clients_check_all(void) = ?");
379
[0]380 FD_ZERO(&r);
[4686]381 FD_ZERO(&w);
[0]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++) {
[4326]388  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
[0]389   continue;
390
[4815]391  ROAR_DBG("clients_check_all(void): i(client)=%i", i);
392
[4326]393  if ( (fh = c->fh) != -1 ) {
[71]394   have++;
395
[610]396   ROAR_DBG("clients_check_all(*): fh=%i", fh);
397
[66]398   FD_SET(fh, &r);
399   FD_SET(fh, &e);
400
[4686]401   if ( g_clients[i]->outbuf != NULL ) {
402    FD_SET(fh, &w);
403   }
404
[66]405   if ( fh > max_fh )
406    max_fh = fh;
[84]407  }
[0]408
[498]409  have_stream = 0;
410
[84]411  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[4815]412   ROAR_DBG("clients_check_all(void): i(client)=%i, j(stream)=%i", i, j);
[4326]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);
[1504]415    if ( fh > -1 ) {
416     FD_SET(fh, &r);
[0]417
[1504]418     if ( fh > max_fh )
419      max_fh = fh;
[3579]420    } else if ( fh == -2 ) {
[4326]421     streams_check(c->streams[j]);
[1504]422    }
[498]423
424    have_stream = 1;
[66]425   }
[84]426   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
[66]427  }
428
[498]429  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
430   streamless[have_streamless  ].id = i;
[4326]431   if ( (streamless[have_streamless++].fh = c->fh) == -1 )
[498]432    have_streamless--;
433  }
[0]434 }
435
[4815]436 ROAR_DBG("clients_check_all(void): max_fh=%i", max_fh);
437
[256]438 if ( max_fh == -1 )
439  return 0;
440
[4686]441 if ( (ret = select(max_fh + 1, &r, &w, &e, &tv)) < 1 ) {
[71]442  return ret < 0 ? ret : have;
[0]443 }
444
445 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
[4326]446  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
[0]447   continue;
448
[4326]449  if ( (fh = c->fh) != -1 ) {
[66]450   if ( FD_ISSET(fh, &r) ) {
[4326]451    if ( c->execed == -1 ) {
[66]452     clients_check(i);
[4326]453     if ( g_clients[i] != NULL && ROAR_CLIENT(g_clients[i])->execed != -1 ) {
[1834]454      FD_CLR(fh, &r);
455     }
[255]456/*
[66]457    } else {
458     streams_check(g_clients[i]->execed);
[255]459*/
[66]460    }
461   }
[4686]462   if ( FD_ISSET(fh, &w) ) {
463    clients_flush(i);
464   }
[0]465
[84]466   if ( FD_ISSET(fh, &e) ) {
[66]467    clients_delete(i);
[84]468    continue;
469   }
470  }
471
[4326]472  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
[84]473   continue;
474
475  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[1611]476   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
[136]477   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
478    break;
[1611]479
480   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
[4326]481   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, c->streams[j]);
[1611]482
[4326]483   if ( (fh = streams_get_fh(c->streams[j])) != -1 ) {
[1611]484    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
[3579]485    if ( fh > -1 && FD_ISSET(fh, &r) ) {
[4326]486     streams_check(c->streams[j]);
[66]487    }
[0]488   }
489  }
490 }
491
[498]492 if ( have_streamless ) {
493   FD_ZERO(&r);
[4686]494   FD_ZERO(&w);
[498]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++) {
[4326]502    if ( g_clients[j = streamless[i].id] == NULL )
[607]503     continue;
504
[4326]505    if ( ROAR_CLIENT(g_clients[j])->execed != -1 )
[601]506     continue;
507
[498]508    fh = streamless[i].fh;
509
510    ROAR_DBG("clients_check_all(void): fh=%i", fh);
511    FD_SET(fh, &r);
512
[4688]513    if ( g_clients[j]->outbuf != NULL ) {
[4686]514     FD_SET(fh, &w);
515    }
516
[498]517    if ( fh > max_fh )
518     max_fh = fh;
519   }
520
[4686]521   if ( (ret = select(max_fh + 1, &r, &w, NULL, &tv)) < 0 ) {
[498]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    }
[4686]529    if ( FD_ISSET(streamless[i].fh, &w) ) {
530     clients_flush(streamless[i].id);
531    }
[498]532   }
533 }
534
[71]535 ROAR_DBG("clients_check_all(void) = %i // have value", have);
536 return have;
[1480]537#else
[4815]538 ROAR_DBG("clients_check_all(void) = -1");
[1480]539 return -1;
540#endif
[0]541}
542
543int clients_check     (int id) {
[4326]544 struct roar_client   * c;
[0]545 struct roar_message    m;
546 struct roar_connection con;
[5190]547 struct roar_vio_calls  vio;
[5155]548 struct roar_error_state errstate;
549 int command_error;
[0]550 char * data = NULL;
551 int oldcmd;
552 int r;
[498]553 int rv = 0;
[3926]554 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
[4325]555 uint32_t event;
[4678]556 size_t i;
[0]557
[4713]558 ROAR_DBG("clients_check(id=%i) = ?", id);
559
[3910]560 _CHECK_CID(id);
561
[4326]562 c = ROAR_CLIENT(g_clients[id]);
563
564 if ( c->fh == -1 )
[0]565  return -1;
566
[4326]567 roar_connect_fh(&con, c->fh);
[0]568
[4713]569 ROAR_DBG("clients_check(id=%i): c->proto=%i", id, c->proto);
570
[4326]571 switch (c->proto) {
[2517]572  case ROAR_PROTO_ROARAUDIO:
573    r = roar_recv_message(&con, &m, &data);
[0]574
[2517]575    if ( r == -1 ) { // should we drop the client?
576     clients_delete(id);
577     return -1;
578    }
[0]579
[4325]580    event = ROAR_NOTIFY_CMD2EVENT(m.cmd);
581
[2517]582    oldcmd = m.cmd;
[0]583
[5155]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.
[2517]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    }
[4298]605    ROAR_DBG("clients_check(*): data=%p", data);
606
[4347]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);
[4343]611     roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL);
[4347]612    }
[3928]613
614    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
615     clients_close(id, 1);
616
[2517]617   break;
[4020]618#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
[3684]619  case ROAR_PROTO_RSOUND:
[3820]620    rv = emul_rsound_check_client(id, NULL);
[3825]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
[4131]626     if (
627#ifdef EAGAIN
[4153]628          errno != EAGAIN      &&
[4131]629#endif
630#ifdef EWOULDBLOCK
[4153]631          errno != EWOULDBLOCK &&
[4131]632#endif
633#ifdef EINTR
[4153]634          errno != EINTR       &&
[4131]635#endif
[4153]636          1 ) {
[4131]637      rv = clients_delete(id);
638     } else {
639      rv = 0;
640     }
[3825]641    }
[3684]642   break;
643#endif
[2517]644  default:
645    rv = -1;
[4679]646    for (i = 0; g_proto[i].proto != -1; i++) {
[4678]647     if ( g_proto[i].proto == c->proto ) {
[5190]648      roar_vio_open_fh_socket(&vio, clients_get_fh(id));
649      rv = g_proto[i].check_client(id, &vio);
[4678]650     }
651    }
[0]652 }
653
[4297]654 if ( data != NULL )
[5295]655  roar_mm_free(data);
[0]656
[2517]657 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
[498]658 return rv;
[0]659}
660
[4686]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 ) {
[5242]705  if ( roar_buffer_next(&(cs->outbuf)) == -1 ) {
706   clients_delete(id);
707   return -1;
708  }
[4686]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
[0]725int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
726 int i;
[934]727// int fh;
728 int j;
[2715]729 int keep_going;
[0]730
731 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
732  if ( g_clients[i] == NULL )
733   continue;
734
[2715]735  keep_going = 1;
736
[934]737/*
[0]738  if ( (fh = g_clients[i]->fh) == -1 )
739   continue;
[934]740*/
[0]741
[4326]742  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed);
[1905]743
[2706]744/*
[0]745  if ( g_clients[i]->execed == -1 ) {
746   // TODO: add some code to send a message to the client insetd of the raw data.
[2706]747*/
[2715]748   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[934]749    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2715]750    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
[4326]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]);
[2715]754
755     // the client may be deleted here, check if it still exists:
756     if ( g_clients[i] == NULL )
757      keep_going = 0;
[1905]758    }
[934]759   }
[2706]760/*
[0]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  }
[2706]767*/
[0]768 }
769
[1902]770 // TODO: FIXME: should this really be -1?
[0]771 return -1;
772}
773
774int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
[4326]775 struct roar_client * c;
[0]776 int i;
777 int fh;
778
779 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
[4326]780  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
[0]781   continue;
782
[4326]783  if ( (fh = c->fh) == -1 )
[0]784   continue;
785
[4326]786  if ( c->execed == -1 ) {
[0]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);
[4326]790   streams_send_filter(c->execed);
[0]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
[5301]799int clients_add_output (int id, struct roar_buffer ** buf) {
[4686]800 struct roar_client_server   * cs;
801
802 _CHECK_CID(id);
803 cs = g_clients[id];
804
805 if ( cs->outbuf == NULL ) {
[5301]806  cs->outbuf = *buf;
[4686]807 } else {
[5301]808  return roar_buffer_moveinto(cs->outbuf, buf);
[4686]809 }
810
811 return 0;
812}
813
[4679]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
[5275]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}
[4679]932
[0]933int client_stream_exec   (int client, int stream) {
[4326]934 struct roar_client * c;
[0]935 int i;
[3920]936 int fh;
[0]937
[3910]938 _CHECK_CID(client);
939
940#if 0
[1901]941 if ( g_clients[client] == NULL ) {
942  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
[0]943  return -1;
[1901]944 }
[3910]945#endif
[0]946
[4326]947 c = ROAR_CLIENT(g_clients[client]);
948
[0]949 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4326]950  if ( c->streams[i] == stream ) {
951   c->execed = stream;
[3921]952   if ( streams_is_ready(stream) == 0 ) {
[4326]953    streams_set_fh(stream, c->fh);
[3920]954    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
955   } else {
[4254]956    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream);
[4326]957    if ( (fh = c->fh) != -1 ) {
[3925]958     close(fh);
[4326]959     c->fh = -1;
[3925]960    }
[3920]961   }
[2815]962   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
[0]963   return 0;
964  }
965 }
966
[1901]967 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
[0]968 return -1;
969}
970
[78]971int client_stream_set_fh (int client, int stream, int fh) {
972 int i;
973
[4240]974 ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh);
975
[3910]976 _CHECK_CID(client);
[78]977
978 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4326]979  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
[4240]980   ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i);
[4229]981   return streams_set_fh(stream, fh);
[78]982  }
983 }
984
[4240]985 ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh);
[78]986 return -1;
987}
988
[0]989int client_stream_add    (int client, int stream) {
990 int i;
991
[3910]992 _CHECK_CID(client);
[0]993
994 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4326]995  if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) {
996   ROAR_CLIENT(g_clients[client])->streams[i] = stream;
[0]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
[3910]1008 _CHECK_CID(client);
[0]1009
1010 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4326]1011  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
1012   ROAR_CLIENT(g_clients[client])->streams[i] = -1;
[0]1013
[4326]1014   if ( stream == ROAR_CLIENT(g_clients[client])->execed ) {
[0]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
[767]1028int client_stream_move   (int client, int stream) {
1029 int old_client = streams_get_client(stream);
1030
[771]1031 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
1032
[767]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
[4343]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
[4480]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
[5146]1144 return ACCLEV_ERROR;
[4480]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
[0]1157//ll
Note: See TracBrowser for help on using the repository browser.