source: roaraudio/roard/clients.c @ 5608:584b36c566e2

Last change on this file since 5608:584b36c566e2 was 5608:584b36c566e2, checked in by phi, 12 years ago

Handle super long client names well (Closes: #286)

File size: 36.9 KB
RevLine 
[0]1//clients.c:
2
[668]3/*
[5381]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
[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];
34
[5567]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}}},
[4678]41#endif
42#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
[5567]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}}},
[4678]45#endif
[4703]46#ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER
[5567]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}}},
[4703]50#endif
[5567]51 {.proto = -1}
[4678]52};
53
[3910]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
[0]57int clients_init (void) {
58 int i;
59
60 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
61  g_clients[i] = NULL;
62
[5567]63 for (i = 0; __protos[i].proto != -1; i++);
[5275]64
65 for (; i < MAX_PROTOS; i++)
[5567]66  __protos[i].proto = -1;
[5275]67
[0]68 return 0;
69}
70
71int clients_free (void) {
72 int i;
73
74 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
[3910]75  if ( g_clients[i] != NULL )
[0]76   clients_delete(i);
77
78 return 0;
79}
80
81int clients_new (void) {
82 int i;
83 int s;
[4326]84 struct roar_client_server * ns;
[0]85 struct roar_client * n;
86
87 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
88  if ( g_clients[i] == NULL ) {
[4326]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
[0]94   if ( n != NULL ) {
95    n->pid    = -1;
[437]96    n->uid    = -1;
97    n->gid    = -1;
[0]98    n->fh     = -1;
99
100    *n->name = 0;
101
[2614]102    n->proto     = ROAR_PROTO_ROARAUDIO;
103    n->byteorder = ROAR_BYTEORDER_NETWORK;
[2517]104
[346]105    n->acl   = NULL;
106
[0]107    n->execed = -1;
108    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
109     n->streams[s] = -1;
110
[2815]111    if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) {
[3063]112     roar_mm_free(n);
[2815]113     return -1;
114    }
115
[4343]116    ns->blockc   = 0;
117    ns->waits    = NULL;
[4468]118    ns->acclev   = ACCLEV_NONE;
[4343]119
[4326]120    g_clients[i] = ns;
[0]121
[4101]122    counters_inc(clients, 1);
[4346]123    roar_notify_core_emit_snoargs(ROAR_OE_BASICS_NEW, -1, i, ROAR_OT_CLIENT);
[0]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
[3737]137int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) {
[5571]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;
[5606]145 struct roar_dl_librarypara * pluginpara;
[5571]146 struct roar_vio_calls    vio;
[5576]147 struct roar_client_server * cs;
[5571]148 int supported = 0;
[3737]149 int client;
150
[5571]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 )
[3737]167  return -1;
168
169
[5571]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
[5576]177     fh = ROAR_CLIENT(cs)->fh;
[5571]178    break;
179  }
180#else
181  roar_err_set(ROAR_ERROR_PROTONOSUP);
[3737]182  return -1;
[5571]183#endif
184 } else {
185  client = clients_new();
[3737]186
[5571]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  }
[3737]200 }
201
[5576]202 if ( clients_get_server(client, &cs) == -1 ) {
203  clients_delete(client);
204  return -1;
205 }
206
[3737]207 if ( update_nnode ) {
[5576]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);
[3737]213   }
214  }
215 }
216
[5571]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 ) {
[5576]271     if ( protohandle->lhandle != NULL )
272      roar_dl_context_restore(protohandle->lhandle);
[5571]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;
[5576]287      case ROARD_PROTO_TYPE_COMMON:
288        supported = 1;
289        if ( protohandle->impl.common->set_proto != NULL ) {
[5606]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 ) {
[5576]292          supported = 0;
293         }
[5606]294         if ( pluginpara != NULL )
295          roar_dl_para_unref(pluginpara);
[5576]296        }
297       break;
[5571]298     }
[5576]299     if ( protohandle->lhandle != NULL )
300      roar_dl_context_store(protohandle->lhandle);
[5571]301    }
302
303    if ( !supported ) {
304     clients_delete(client);
305     //roar_vio_close(&vio);
306     return -1;
307    }
308   break;
309 }
310
311
[3737]312 return 0;
313}
314
[0]315int clients_delete (int id) {
[4394]316 struct roar_client_server * cs;
[5567]317 const struct roard_proto_handle * proto;
[5606]318 struct roar_dl_librarypara * pluginpara;
[5576]319 struct roar_vio_calls vio;
[0]320 int i;
[1164]321 int close_client_fh = 1;
[0]322
[2608]323 ROAR_DBG("clients_delete(id=%i) = ?", id);
324
[4394]325 _CHECK_CID(id);
326
327 cs = g_clients[id];
328
[5567]329 proto = clients_get_protohandle(ROAR_CLIENT(cs)->proto);
330 if ( proto != NULL ) {
[5576]331  if ( proto->lhandle != NULL )
332   roar_dl_context_restore(proto->lhandle);
[5567]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;
[5576]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));
[5606]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);
[5576]348     }
349    break;
[5452]350  }
[5576]351  if ( proto->lhandle != NULL )
352   roar_dl_context_store(proto->lhandle);
[5452]353 }
354
[4394]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
[4346]363 roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_CLIENT);
364
[4101]365 counters_inc(clients, -1);
366
[4395]367 if (ROAR_CLIENT(cs)->execed != -1) {
[0]368//  return streams_delete(g_clients[id]->execed);
[4395]369  ROAR_CLIENT(cs)->execed = -1;
[1164]370  close_client_fh = 0;
[0]371 }
372
373 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4395]374  streams_delete(ROAR_CLIENT(cs)->streams[i]);
[0]375 }
376
[4395]377 if ( ROAR_CLIENT(cs)->fh != -1 && close_client_fh )
378  close(ROAR_CLIENT(cs)->fh);
[0]379
[4395]380 roar_nnode_free(&(ROAR_CLIENT(cs)->nnode));
[2815]381
[4684]382 if ( cs->inbuf != NULL )
383  roar_buffer_free(cs->inbuf);
384
385 if ( cs->outbuf != NULL )
386  roar_buffer_free(cs->outbuf);
387
[5452]388 if ( cs->protoinst != NULL )
389  roar_mm_free(cs->protoinst);
390
[4395]391 roar_mm_free(cs);
[0]392 g_clients[id] = NULL;
393
394 ROAR_DBG("clients_delete(id=%i) = 0", id);
395 return 0;
396}
397
[3927]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
[4326]405 c = ROAR_CLIENT(g_clients[id]);
[3927]406
407 if ( c->fh == -1 ) {
408  ROAR_DBG("clients_delete(id=%i) = 0", id);
409  return 0;
410 }
411
[4326]412 if (nocheck_exec || c->execed != -1) {
[3927]413  close(c->fh);
414  c->fh = -1;
415 }
416
417 ROAR_DBG("clients_delete(id=%i) = 0", id);
418 return 0;
419}
420
[0]421int clients_get       (int id, struct roar_client ** client) {
[3910]422 _CHECK_CID(id);
423
[4326]424 *client = ROAR_CLIENT(g_clients[id]);
[0]425
426 if ( *client == NULL )
427  return -1;
428
429 return 0;
430}
431
[4468]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
[0]443int clients_set_fh    (int id, int    fh) {
[3713]444 struct roar_client * c;
445#ifdef SO_PEERCRED
446 struct ucred cred;
447 socklen_t cred_len = sizeof(cred);
448#endif
[501]449
[3910]450 _CHECK_CID(id);
451
[4326]452 if ( (c = ROAR_CLIENT(g_clients[id])) == NULL )
[0]453  return -1;
454
[3713]455 c->fh = fh;
456
[4947]457 if ( fh == -1 )
458  return 0;
459
[3713]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
[0]475
476 return 0;
477}
478
[755]479int clients_get_fh    (int id) {
[3910]480 _CHECK_CID(id);
[755]481
[4326]482 return ROAR_CLIENT(g_clients[id])->fh;
[755]483}
484
[0]485int clients_set_pid   (int id, int    pid) {
[3910]486 _CHECK_CID(id);
[0]487
[4326]488 ROAR_CLIENT(g_clients[id])->pid = pid;
[0]489
490 return 0;
491}
492
[439]493int clients_set_uid   (int id, int    uid) {
[3910]494 _CHECK_CID(id);
[439]495
[4326]496 ROAR_CLIENT(g_clients[id])->uid = uid;
[439]497
498 return 0;
499}
500
501int clients_set_gid   (int id, int    gid) {
[3910]502 _CHECK_CID(id);
[439]503
[4326]504 ROAR_CLIENT(g_clients[id])->gid = gid;
[439]505
506 return 0;
507}
508
[5607]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
[2517]518int clients_set_proto (int id, int    proto) {
[2614]519 int byteorder = ROAR_BYTEORDER_UNKNOWN;
520
[3910]521 _CHECK_CID(id);
[2517]522
[2614]523 switch (proto) {
524  case ROAR_PROTO_ROARAUDIO:
[2828]525  case ROAR_PROTO_ESOUND:
[3981]526  case ROAR_PROTO_RPLAY:
[3255]527  case ROAR_PROTO_SIMPLE:
[2614]528    byteorder = ROAR_BYTEORDER_NETWORK;
529   break;
530 }
531
[4326]532 ROAR_CLIENT(g_clients[id])->proto     = proto;
533 ROAR_CLIENT(g_clients[id])->byteorder = byteorder;
[2517]534
535 return 0;
536}
537
[4343]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
[498]551#define MAX_STREAMLESS 8
552
[0]553int clients_check_all (void) {
[1480]554#ifdef ROAR_HAVE_SELECT
[4326]555 struct roar_client * c;
[0]556 struct timeval tv;
[4686]557 fd_set r, w, e;
[66]558 int i, j;
[0]559 int ret;
560 int fh;
561 int max_fh = -1;
[71]562 int have = 0;
[498]563 struct {
564  int id;
565  int fh;
566 } streamless[MAX_STREAMLESS];
567 int have_streamless = 0;
568 int have_stream;
[0]569
[4815]570 ROAR_DBG("clients_check_all(void) = ?");
571
[0]572 FD_ZERO(&r);
[4686]573 FD_ZERO(&w);
[0]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++) {
[4326]580  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
[0]581   continue;
582
[4815]583  ROAR_DBG("clients_check_all(void): i(client)=%i", i);
584
[4326]585  if ( (fh = c->fh) != -1 ) {
[71]586   have++;
587
[610]588   ROAR_DBG("clients_check_all(*): fh=%i", fh);
589
[66]590   FD_SET(fh, &r);
591   FD_SET(fh, &e);
592
[4686]593   if ( g_clients[i]->outbuf != NULL ) {
594    FD_SET(fh, &w);
595   }
596
[66]597   if ( fh > max_fh )
598    max_fh = fh;
[84]599  }
[0]600
[498]601  have_stream = 0;
602
[84]603  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[4815]604   ROAR_DBG("clients_check_all(void): i(client)=%i, j(stream)=%i", i, j);
[4326]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);
[1504]607    if ( fh > -1 ) {
608     FD_SET(fh, &r);
[0]609
[1504]610     if ( fh > max_fh )
611      max_fh = fh;
[3579]612    } else if ( fh == -2 ) {
[4326]613     streams_check(c->streams[j]);
[1504]614    }
[498]615
616    have_stream = 1;
[66]617   }
[84]618   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
[66]619  }
620
[498]621  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
622   streamless[have_streamless  ].id = i;
[4326]623   if ( (streamless[have_streamless++].fh = c->fh) == -1 )
[498]624    have_streamless--;
625  }
[0]626 }
627
[4815]628 ROAR_DBG("clients_check_all(void): max_fh=%i", max_fh);
629
[256]630 if ( max_fh == -1 )
631  return 0;
632
[4686]633 if ( (ret = select(max_fh + 1, &r, &w, &e, &tv)) < 1 ) {
[71]634  return ret < 0 ? ret : have;
[0]635 }
636
637 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
[4326]638  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
[0]639   continue;
640
[4326]641  if ( (fh = c->fh) != -1 ) {
[66]642   if ( FD_ISSET(fh, &r) ) {
[4326]643    if ( c->execed == -1 ) {
[66]644     clients_check(i);
[4326]645     if ( g_clients[i] != NULL && ROAR_CLIENT(g_clients[i])->execed != -1 ) {
[1834]646      FD_CLR(fh, &r);
647     }
[255]648/*
[66]649    } else {
650     streams_check(g_clients[i]->execed);
[255]651*/
[66]652    }
653   }
[4686]654   if ( FD_ISSET(fh, &w) ) {
655    clients_flush(i);
656   }
[0]657
[84]658   if ( FD_ISSET(fh, &e) ) {
[66]659    clients_delete(i);
[84]660    continue;
661   }
662  }
663
[4326]664  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
[84]665   continue;
666
667  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[1611]668   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
[136]669   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
670    break;
[1611]671
672   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
[4326]673   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, c->streams[j]);
[1611]674
[4326]675   if ( (fh = streams_get_fh(c->streams[j])) != -1 ) {
[1611]676    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
[3579]677    if ( fh > -1 && FD_ISSET(fh, &r) ) {
[4326]678     streams_check(c->streams[j]);
[66]679    }
[0]680   }
681  }
682 }
683
[498]684 if ( have_streamless ) {
685   FD_ZERO(&r);
[4686]686   FD_ZERO(&w);
[498]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++) {
[4326]694    if ( g_clients[j = streamless[i].id] == NULL )
[607]695     continue;
696
[4326]697    if ( ROAR_CLIENT(g_clients[j])->execed != -1 )
[601]698     continue;
699
[498]700    fh = streamless[i].fh;
701
702    ROAR_DBG("clients_check_all(void): fh=%i", fh);
703    FD_SET(fh, &r);
704
[4688]705    if ( g_clients[j]->outbuf != NULL ) {
[4686]706     FD_SET(fh, &w);
707    }
708
[498]709    if ( fh > max_fh )
710     max_fh = fh;
711   }
712
[4686]713   if ( (ret = select(max_fh + 1, &r, &w, NULL, &tv)) < 0 ) {
[498]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    }
[4686]721    if ( FD_ISSET(streamless[i].fh, &w) ) {
722     clients_flush(streamless[i].id);
723    }
[498]724   }
725 }
726
[71]727 ROAR_DBG("clients_check_all(void) = %i // have value", have);
728 return have;
[1480]729#else
[4815]730 ROAR_DBG("clients_check_all(void) = -1");
[1480]731 return -1;
732#endif
[0]733}
734
735int clients_check     (int id) {
[5576]736 struct roar_client_server * cs;
[0]737 struct roar_message    m;
738 struct roar_connection con;
[5190]739 struct roar_vio_calls  vio;
[5155]740 struct roar_error_state errstate;
[5567]741 const struct roard_proto_handle * proto;
[5606]742 struct roar_dl_librarypara * pluginpara;
[5155]743 int command_error;
[0]744 char * data = NULL;
745 int oldcmd;
746 int r;
[498]747 int rv = 0;
[3926]748 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
[4325]749 uint32_t event;
[0]750
[4713]751 ROAR_DBG("clients_check(id=%i) = ?", id);
752
[3910]753 _CHECK_CID(id);
754
[5576]755 cs = g_clients[id];
[4326]756
[5576]757 if ( ROAR_CLIENT(cs)->fh == -1 )
[0]758  return -1;
759
[5608]760 if ( roar_connect_fh(&con, ROAR_CLIENT(cs)->fh) == -1 ) {
761  ROAR_WARN("clients_check(id=%i): Can not create con object for client: %s", id, roar_errorstring);
762 }
[0]763
[5576]764 ROAR_DBG("clients_check(id=%i): c->proto=%i", id, ROAR_CLIENT(cs)->proto);
[4713]765
[5576]766 switch (ROAR_CLIENT(cs)->proto) {
[2517]767  case ROAR_PROTO_ROARAUDIO:
768    r = roar_recv_message(&con, &m, &data);
[0]769
[2517]770    if ( r == -1 ) { // should we drop the client?
771     clients_delete(id);
772     return -1;
773    }
[0]774
[4325]775    event = ROAR_NOTIFY_CMD2EVENT(m.cmd);
776
[2517]777    oldcmd = m.cmd;
[0]778
[5155]779    roar_err_store(&errstate);
780    roar_err_clear_all();
781    r = command_exec(id, &m, &data, flags);
782    roar_err_update();
783    command_error = roar_error;
784    roar_err_restore(&errstate);
785
786    if ( r == -1 ) {
787     // use command_error to create an error frame here as soon as this is supported.
[2517]788     m.cmd     = ROAR_CMD_ERROR;
789     m.datalen = 0;
790     ROAR_DBG("clients_check(*): Exec of command faild!");
791    } else {
792     if ( m.cmd == oldcmd ) {
793      m.cmd     = ROAR_CMD_OK;
794      m.datalen = 0;
795     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
796      m.cmd     = ROAR_CMD_OK;
797      rv        = 1;
798     }
799    }
[4298]800    ROAR_DBG("clients_check(*): data=%p", data);
801
[4347]802    if ( flags[1] & COMMAND_FLAG_OUT_NOSEND ) {
803     roar_notify_core_emit_simple(event, id, -1, -1, -1, -1, NULL, 0);
804    } else {
805     roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0);
[5608]806     if ( roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL) == -1 ) {
807      ROAR_WARN("clients_check(id=%i): Can not send answer to client: %s", id, roar_errorstring);
808     }
[4347]809    }
[3928]810
811    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
812     clients_close(id, 1);
813
[5608]814    if ( flags[1] & COMMAND_FLAG_OUT_DELETE )
815     clients_delete(id);
816
[2517]817   break;
[4020]818#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
[3684]819  case ROAR_PROTO_RSOUND:
[3820]820    rv = emul_rsound_check_client(id, NULL);
[3825]821    if ( rv == 0 ) { // loop as long as we don't get an error.
822     while (rv == 0)
823      rv = emul_rsound_check_client(id, NULL);
824     rv = 0; // restore
825    } else { // in case of error delete the client
[4131]826     if (
827#ifdef EAGAIN
[4153]828          errno != EAGAIN      &&
[4131]829#endif
830#ifdef EWOULDBLOCK
[4153]831          errno != EWOULDBLOCK &&
[4131]832#endif
833#ifdef EINTR
[4153]834          errno != EINTR       &&
[4131]835#endif
[4153]836          1 ) {
[4131]837      rv = clients_delete(id);
838     } else {
839      rv = 0;
840     }
[3825]841    }
[3684]842   break;
843#endif
[2517]844  default:
845    rv = -1;
[5576]846    proto = clients_get_protohandle(ROAR_CLIENT(cs)->proto);
[5567]847    if ( proto != NULL ) {
848     roar_vio_open_fh_socket(&vio, clients_get_fh(id));
849     if ( proto->lhandle != NULL )
850      roar_dl_context_restore(proto->lhandle);
851
852     switch (proto->type) {
853      case ROARD_PROTO_TYPE_BUILDIN:
854        ROAR_WARN("clients_check(id=%i): proto(%i) marked as buildin but isn't. BAD.", id, proto->proto);
855       break;
856      case ROARD_PROTO_TYPE_ROARDPROTO:
857        if ( proto->impl.roardproto.check_client != NULL )
858         rv = proto->impl.roardproto.check_client(id, &vio);
859       break;
[5576]860      case ROARD_PROTO_TYPE_COMMON:
[5606]861        if ( proto->impl.common->handle != NULL ) {
862         pluginpara = roar_dl_getpara(proto->lhandle); // will return NULL in case protohandle->lhandle is NULL.
863         rv = proto->impl.common->handle(id, &vio, &(cs->outbuf), &(cs->protoinst), proto->para, proto->paralen, pluginpara);
864         if ( pluginpara != NULL )
865          roar_dl_para_unref(pluginpara);
866        }
[5576]867        if ( rv == -1 )
868         rv = clients_delete(id);
869       break;
[4678]870     }
[5567]871
872     if ( proto->lhandle != NULL )
873      roar_dl_context_store(proto->lhandle);
[4678]874    }
[0]875 }
876
[4297]877 if ( data != NULL )
[5295]878  roar_mm_free(data);
[0]879
[2517]880 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
[498]881 return rv;
[0]882}
883
[4686]884int clients_flush      (int id) {
[5567]885 struct roar_vio_calls             vio;
886 struct roar_client_server       * cs;
887 struct roar_client              * c;
888 const struct roard_proto_handle * p;
[5606]889 struct roar_dl_librarypara * pluginpara;
[4686]890 size_t len;
891 ssize_t ret;
892 void * buf;
[5567]893 int rv;
[4686]894
895 _CHECK_CID(id);
896
897 c = ROAR_CLIENT(cs = g_clients[id]);
898
[5567]899 p = clients_get_protohandle(c->proto);
[4686]900
901 if ( p == NULL )
902  return -1;
903
904 roar_vio_open_fh_socket(&vio, clients_get_fh(id));
905
[5567]906 switch (p->type) {
907  case ROARD_PROTO_TYPE_BUILDIN:
908    /* noop */
909   break;
910  case ROARD_PROTO_TYPE_ROARDPROTO:
911    if ( p->impl.roardproto.flush_client != NULL ) {
912     if ( p->lhandle != NULL )
913      roar_dl_context_restore(p->lhandle);
914     rv = p->impl.roardproto.flush_client(id, &vio);
915     if ( p->lhandle != NULL )
916      roar_dl_context_store(p->lhandle);
917     return rv;
918    }
919   break;
[5576]920  case ROARD_PROTO_TYPE_COMMON:
921    if ( p->impl.common->flush != NULL ) {
922     if ( p->lhandle != NULL )
923      roar_dl_context_restore(p->lhandle);
[5606]924     pluginpara = roar_dl_getpara(p->lhandle); // will return NULL in case protohandle->lhandle is NULL.
925     rv = p->impl.common->flush(id, &vio, &(cs->outbuf), &(cs->protoinst), p->para, p->paralen, pluginpara);
[5576]926     if ( p->lhandle != NULL )
927      roar_dl_context_store(p->lhandle);
[5606]928     if ( pluginpara != NULL )
929      roar_dl_para_unref(pluginpara);
[5580]930     if ( rv == -1 )
931      rv = clients_delete(id);
[5576]932     return rv;
933    }
934   break;
[4686]935 }
936
937 if ( roar_buffer_get_len(cs->outbuf, &len) == -1 )
938  return -1;
939
940 if ( roar_buffer_get_data(cs->outbuf, &buf) == -1 )
941  return -1;
942
943 ret = roar_vio_write(&vio, buf, len);
944
945 if ( ret < 1 ) {
946  clients_delete(id);
947  return -1;
948 }
949
[5586]950 if ( ret == (ssize_t)len ) {
[5242]951  if ( roar_buffer_next(&(cs->outbuf)) == -1 ) {
952   clients_delete(id);
953   return -1;
954  }
[4686]955 } else {
956  if ( roar_buffer_set_offset(cs->outbuf, ret) == -1 ) {
957   clients_delete(id);
958   return -1;
959  }
960 }
961
962 if ( cs->outbuf == NULL ) {
[5567]963  switch (p->type) {
964   case ROARD_PROTO_TYPE_BUILDIN:
965     /* noop */
966    break;
967   case ROARD_PROTO_TYPE_ROARDPROTO:
968     if ( p->impl.roardproto.flushed_client != NULL ) {
969      if ( p->lhandle != NULL )
970       roar_dl_context_restore(p->lhandle);
971      rv = p->impl.roardproto.flushed_client(id, &vio);
972      if ( p->lhandle != NULL )
973       roar_dl_context_store(p->lhandle);
974      return rv;
975     }
976    break;
[5576]977   case ROARD_PROTO_TYPE_COMMON:
978     if ( p->impl.common->flushed != NULL ) {
979      if ( p->lhandle != NULL )
980       roar_dl_context_restore(p->lhandle);
[5606]981      pluginpara = roar_dl_getpara(p->lhandle); // will return NULL in case protohandle->lhandle is NULL.
982      rv = p->impl.common->flushed(id, &vio, &(cs->outbuf), &(cs->protoinst), p->para, p->paralen, pluginpara);
[5576]983      if ( p->lhandle != NULL )
984       roar_dl_context_store(p->lhandle);
[5606]985      if ( pluginpara != NULL )
986       roar_dl_para_unref(pluginpara);
[5580]987      if ( rv == -1 )
988       rv = clients_delete(id);
[5576]989      return rv;
990     }
991    break;
[4686]992  }
993 }
994
995 return 0;
996}
997
[5586]998int clients_send_mon  (struct roar_audio_info * sa) {
[0]999 int i;
[934]1000// int fh;
1001 int j;
[2715]1002 int keep_going;
[0]1003
1004 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
1005  if ( g_clients[i] == NULL )
1006   continue;
1007
[2715]1008  keep_going = 1;
1009
[934]1010/*
[0]1011  if ( (fh = g_clients[i]->fh) == -1 )
1012   continue;
[934]1013*/
[0]1014
[4326]1015  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed);
[1905]1016
[2706]1017/*
[0]1018  if ( g_clients[i]->execed == -1 ) {
1019   // TODO: add some code to send a message to the client insetd of the raw data.
[2706]1020*/
[2715]1021   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[934]1022    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2715]1023    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
[4326]1024    if ( ROAR_CLIENT(g_clients[i])->streams[j] != -1 ) {
1025     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, ROAR_CLIENT(g_clients[i])->streams[j]);
1026     streams_send_mon(ROAR_CLIENT(g_clients[i])->streams[j]);
[2715]1027
1028     // the client may be deleted here, check if it still exists:
1029     if ( g_clients[i] == NULL )
1030      keep_going = 0;
[1905]1031    }
[934]1032   }
[2706]1033/*
[0]1034  } else {
1035//   streams_check(g_clients[i]->execed);
1036   streams_send_mon(g_clients[i]->execed);
1037//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
1038//    clients_delete(i); // delete client in case we could not write
1039  }
[2706]1040*/
[0]1041 }
1042
[1902]1043 // TODO: FIXME: should this really be -1?
[0]1044 return -1;
1045}
1046
[5586]1047int clients_send_filter(struct roar_audio_info * sa) {
[4326]1048 struct roar_client * c;
[0]1049 int i;
1050 int fh;
1051
1052 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
[4326]1053  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
[0]1054   continue;
1055
[4326]1056  if ( (fh = c->fh) == -1 )
[0]1057   continue;
1058
[4326]1059  if ( c->execed == -1 ) {
[0]1060   // TODO: add some code to send a message to the client insetd of the raw data.
1061  } else {
1062//   streams_check(g_clients[i]->execed);
[4326]1063   streams_send_filter(c->execed);
[0]1064//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
1065//    clients_delete(i); // delete client in case we could not write
1066  }
1067 }
1068
1069 return -1;
1070}
1071
[5301]1072int clients_add_output (int id, struct roar_buffer ** buf) {
[4686]1073 struct roar_client_server   * cs;
1074
1075 _CHECK_CID(id);
1076 cs = g_clients[id];
1077
1078 if ( cs->outbuf == NULL ) {
[5301]1079  cs->outbuf = *buf;
[4686]1080 } else {
[5301]1081  return roar_buffer_moveinto(cs->outbuf, buf);
[4686]1082 }
1083
1084 return 0;
1085}
1086
[4679]1087// proto support
[5567]1088const struct roard_proto_handle * clients_get_protohandle(const int proto) {
1089 size_t i;
1090
1091 if ( proto < 0 ) {
1092  roar_err_set(ROAR_ERROR_INVAL);
1093  return NULL;
1094 }
1095
1096 for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++)
1097  if ( __protos[i].proto == proto )
1098   return &(__protos[i]);
1099
1100 roar_err_set(ROAR_ERROR_NOENT);
1101 return NULL;
1102}
1103
[5312]1104int clients_register_proto(struct roard_proto * proto, struct roar_dl_lhandle * lhandle) {
[5567]1105 const size_t len = sizeof(__protos)/sizeof(*__protos);
[4679]1106 size_t i;
1107
1108 if ( proto == NULL )
1109  return -1;
1110
[5567]1111 for (i = 0; __protos[i].proto != -1; i++);
[4679]1112
1113 // i is now at pos of current EOS entry.
1114
1115 // test if we have space for one more entry:
1116 if ( (i+1) >= len )
1117  return -1;
1118
[5567]1119 memcpy(&(__protos[i].impl.roardproto), proto, sizeof(__protos[i].impl.roardproto));
1120
1121 __protos[i].impl.roardproto.lhandle = lhandle;
[4679]1122
[5567]1123 __protos[i].proto   = proto->proto;
1124 __protos[i].type    = ROARD_PROTO_TYPE_ROARDPROTO;
1125 __protos[i].lhandle = lhandle;
[5312]1126
[4679]1127 return 0;
1128}
1129
[5576]1130int clients_register_proto_common(const struct roar_dl_proto * proto, struct roar_dl_lhandle * lhandle) {
1131 const size_t len = sizeof(__protos)/sizeof(*__protos);
1132 size_t i;
1133
1134 ROAR_DBG("clients_register_proto_common(proto=%p, lhandle=%p) = ?", proto, lhandle);
1135
1136 if ( proto == NULL )
1137  return -1;
1138
1139 for (i = 0; __protos[i].proto != -1; i++);
1140
1141 // i is now at pos of current EOS entry.
1142
1143 // test if we have space for one more entry:
1144 if ( (i+1) >= len )
1145  return -1;
1146
1147 __protos[i].impl.common = proto;
1148
1149 __protos[i].proto   = proto->proto;
1150 __protos[i].type    = ROARD_PROTO_TYPE_COMMON;
1151 __protos[i].lhandle = lhandle;
1152
1153 return 0;
1154}
1155
[5275]1156int clients_unregister_proto(int proto) {
1157 size_t i;
1158
1159 if ( proto < 0 ) {
1160  roar_err_set(ROAR_ERROR_RANGE);
1161  return -1;
1162 }
1163
[5567]1164 for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) {
1165  if ( __protos[i].proto == proto ) {
1166   memset(&(__protos[i]), 0, sizeof(*__protos));
1167   __protos[i].proto = -1;
[5275]1168   return 0;
1169  }
1170 }
1171
1172 roar_err_set(ROAR_ERROR_NOENT);
1173 return -1;
1174}
1175
1176void print_protolist        (enum output_format format) {
[5567]1177 struct roard_proto_handle * p;
[5577]1178 char flags[5] = "    ";
[5275]1179 char subsys[7] = "      ";
[5567]1180 const char * description;
[5275]1181 size_t i;
1182
1183 switch (format) {
1184  case FORMAT_NATIVE:
1185    printf("  Protocol Flag Subsys - Description\n");
1186    printf("------------------------------------------------------\n");
[5590]1187    printf("  roar     bb   WM LRX - RoarAudio native protocol\n");
[5275]1188#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
[5590]1189    printf("  simple   bb   WM LRX - PulseAudio simple protocol\n");
[5275]1190#endif
1191#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
[5590]1192    printf("  rsound   bb   W      - RSound emulation\n");
[5275]1193#endif
1194   break;
1195  case FORMAT_WIKI:
1196    printf("||=Protocol =||=Flag =||=Subsys =||=Description              =||\n");
[5590]1197    printf("||roar       ||bb     ||WM LRX   ||RoarAudio native protocol  ||\n");
[5275]1198#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
[5590]1199    printf("||simple     ||bb     ||WM LRX   ||PulseAudio simple protocol ||\n");
[5275]1200#endif
1201#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
[5590]1202    printf("||rsound     ||bb     ||W        ||RSound emulation           ||\n");
[5275]1203#endif
1204   break;
1205  case FORMAT_CSV:
1206    printf("Protocol,Flag,Subsys,Description\n");
[5590]1207    printf("roar,bb,WM LRX,RoarAudio native protocol\n");
[5275]1208#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
[5590]1209    printf("simple,bb,WM LRX,PulseAudio simple protocol\n");
[5275]1210#endif
1211#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
[5590]1212    printf("rsound,bb,W,RSound emulation\n");
[5275]1213#endif
1214   break;
1215  default:
1216    roar_err_set(ROAR_ERROR_NOTSUP);
1217    return;
1218 }
1219
[5567]1220 for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) {
1221  p = &(__protos[i]);
[5275]1222  if ( p->proto == -1 )
1223   continue;
1224
[5577]1225  strncpy(flags, "    ", 4);
[5275]1226  strncpy(subsys, "      ", 6);
[5567]1227  description = "(none)";
[5275]1228
[5590]1229  if ( p->lhandle != NULL ) {
[5577]1230   flags[0] = 'P';
[5590]1231  } else {
1232   flags[0] = 'b';
1233  }
[5577]1234
[5576]1235  switch (p->type) {
1236   case ROARD_PROTO_TYPE_BUILDIN:
1237     continue;
1238    break;
1239   case ROARD_PROTO_TYPE_ROARDPROTO:
[5590]1240     flags[1] = 'R';
1241
[5576]1242     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_WAVEFORM )
1243      subsys[0] = 'W';
1244     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_MIDI )
1245      subsys[1] = 'M';
1246     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_CB )
1247      subsys[2] = 'C';
1248     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_LIGHT )
1249      subsys[3] = 'L';
1250     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_RAW )
1251      subsys[4] = 'R';
1252     if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_COMPLEX )
1253      subsys[5] = 'X';
[5567]1254
[5576]1255     description = p->impl.roardproto.description;
1256    break;
1257   case ROARD_PROTO_TYPE_COMMON:
[5590]1258     flags[1] = 'C';
[5576]1259     description = p->impl.common->description;
1260    break;
[5567]1261  }
[5275]1262
1263  switch (format) {
1264   case FORMAT_NATIVE:
[5577]1265     printf("  %-8s %s %s - %s\n", roar_proto2str(p->proto), flags, subsys, description);
[5275]1266    break;
1267   case FORMAT_WIKI:
[5577]1268     printf("||%s ||%s ||%s ||%s ||\n", roar_proto2str(p->proto), flags, subsys, description);
[5275]1269    break;
1270   case FORMAT_CSV:
[5577]1271     printf("%s,%s,%s,%s\n", roar_proto2str(p->proto), flags, subsys, description);
[5275]1272    break;
1273  }
1274 }
1275}
[4679]1276
[0]1277int client_stream_exec   (int client, int stream) {
[4326]1278 struct roar_client * c;
[0]1279 int i;
[3920]1280 int fh;
[0]1281
[3910]1282 _CHECK_CID(client);
1283
1284#if 0
[1901]1285 if ( g_clients[client] == NULL ) {
1286  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
[0]1287  return -1;
[1901]1288 }
[3910]1289#endif
[0]1290
[4326]1291 c = ROAR_CLIENT(g_clients[client]);
1292
[0]1293 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4326]1294  if ( c->streams[i] == stream ) {
1295   c->execed = stream;
[3921]1296   if ( streams_is_ready(stream) == 0 ) {
[4326]1297    streams_set_fh(stream, c->fh);
[3920]1298    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
1299   } else {
[4254]1300    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream);
[4326]1301    if ( (fh = c->fh) != -1 ) {
[3925]1302     close(fh);
[4326]1303     c->fh = -1;
[3925]1304    }
[3920]1305   }
[2815]1306   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
[0]1307   return 0;
1308  }
1309 }
1310
[1901]1311 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
[0]1312 return -1;
1313}
1314
[78]1315int client_stream_set_fh (int client, int stream, int fh) {
1316 int i;
1317
[4240]1318 ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh);
1319
[3910]1320 _CHECK_CID(client);
[78]1321
1322 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4326]1323  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
[4240]1324   ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i);
[4229]1325   return streams_set_fh(stream, fh);
[78]1326  }
1327 }
1328
[4240]1329 ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh);
[78]1330 return -1;
1331}
1332
[0]1333int client_stream_add    (int client, int stream) {
1334 int i;
1335
[3910]1336 _CHECK_CID(client);
[0]1337
1338 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4326]1339  if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) {
1340   ROAR_CLIENT(g_clients[client])->streams[i] = stream;
[0]1341   streams_set_client(stream, client);
1342   return 0;
1343  }
1344 }
1345
1346 return -1;
1347}
1348
1349int client_stream_delete (int client, int stream) {
1350 int i;
1351
[3910]1352 _CHECK_CID(client);
[0]1353
1354 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
[4326]1355  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
1356   ROAR_CLIENT(g_clients[client])->streams[i] = -1;
[0]1357
[4326]1358   if ( stream == ROAR_CLIENT(g_clients[client])->execed ) {
[0]1359    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
1360    clients_delete(client);
1361   }
1362
1363   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
1364   return 0;
1365  }
1366 }
1367
1368 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
1369 return -1;
1370}
1371
[767]1372int client_stream_move   (int client, int stream) {
1373 int old_client = streams_get_client(stream);
1374
[771]1375 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
1376
[767]1377 if ( old_client != -1 )
1378  if ( client_stream_delete(old_client, stream) == -1 )
1379   return -1;
1380
1381 return client_stream_add(client, stream);
1382}
1383
[4343]1384
1385// notify thingys
1386int clients_wait    (int client, struct roar_event * events, size_t num) {
1387 struct roar_client_server * cs;
1388 size_t i, c;
1389
1390 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = ?", client, events, (long long unsigned int)num);
1391
1392 _CHECK_CID(client);
1393
1394 cs = g_clients[client];
1395
1396 if ( cs->waits != NULL )
1397  return -1;
1398
1399 cs->waits = roar_mm_malloc((num+1) * sizeof(struct roar_subscriber *));
1400
1401 if ( cs->waits == NULL )
1402  return -1;
1403
1404 if ( clients_block(client, 0) != 0 )
1405  return -1;
1406
1407 for (i = 0; i < num; i++) {
1408#if defined(DEBUG) && 0
1409  dbg_notify_cb(NULL, &(events[i]), cs);
1410#endif
1411  cs->waits[i] = roar_notify_core_subscribe(NULL, &(events[i]), clients_ncb_wait, cs);
1412  if ( cs->waits[i] == NULL ) {
1413   for (c = 0; c < i; c++)
1414    roar_notify_core_unsubscribe(NULL, cs->waits[c]);
1415   roar_mm_free(cs->waits);
1416   cs->waits = NULL;
1417   clients_block(client, 1);
1418   return -1;
1419  }
1420 }
1421
1422 cs->waits[num] = NULL;
1423
1424 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = 0", client, events, (long long unsigned int)num);
1425 return 0;
1426}
1427
1428void clients_ncb_wait(struct roar_notify_core * core, struct roar_event * event, void * userdata) {
1429 struct roar_client_server * cs = userdata;
1430 struct roar_message m;
1431 struct roar_connection con;
1432 uint16_t * u16 = (uint16_t *) m.data;
1433 size_t tmp;
1434 size_t i;
1435
1436 ROAR_DBG("clients_ncb_wait(core=%p, event=%p, userdata=%p) = ?", core, event, userdata);
1437
1438 for (i = 0; cs->waits[i] != NULL; i++)
1439  roar_notify_core_unsubscribe(NULL, cs->waits[i]);
1440
1441 roar_mm_free(cs->waits);
1442 cs->waits = NULL;
1443
1444 // protocol depended handling...
1445 memset(&m, 0, sizeof(m));
1446 m.cmd = ROAR_CMD_OK;
1447 u16[0] = ROAR_HOST2NET16(0); // Version
1448 u16[1] = ROAR_HOST2NET16(0); // flags
1449
1450 tmp = sizeof(m.data) - 4;
1451
1452 roar_event_to_blob(event, m.data + 4, &tmp);
1453
1454 m.datalen = tmp + 4;
1455
1456 roar_connect_fh(&con, ROAR_CLIENT(cs)->fh);
1457 roar_send_message(&con, &m, NULL);
1458 // ...end of protocol depended handling.
1459
1460// clients_block(, 1);
1461 // TODO: FIXME: bad hack...
1462 cs->blockc--;
1463}
1464
[4480]1465
1466// acclev:
1467static struct {
1468 const enum roard_client_acclev acclev;
1469 const char *                   name;
1470} _g_acclevs[] = {
1471 {ACCLEV_NONE,    "none"   },
1472 {ACCLEV_IDENTED, "idented"},
1473 {ACCLEV_CONCTL,  "conctl" },
1474 {ACCLEV_GUEST,   "guest"  },
1475 {ACCLEV_USER,    "user"   },
1476 {ACCLEV_PWRUSER, "pwruser"},
1477 {ACCLEV_ALL,     "all"    },
1478 {-1, NULL}
1479};
1480
1481enum roard_client_acclev clients_str2acclev(const char * acclev) {
1482 int i;
1483
1484 for (i = 0; _g_acclevs[i].name != NULL; i++)
1485  if ( !strcasecmp(_g_acclevs[i].name, acclev) )
1486   return _g_acclevs[i].acclev;
1487
[5146]1488 return ACCLEV_ERROR;
[4480]1489}
1490
1491const char * clients_acclev2str(const enum roard_client_acclev acclev) {
1492 int i;
1493
1494 for (i = 0; _g_acclevs[i].name != NULL; i++)
1495  if ( _g_acclevs[i].acclev == acclev )
1496   return _g_acclevs[i].name;
1497
1498 return NULL;
1499}
1500
[0]1501//ll
Note: See TracBrowser for help on using the repository browser.