source: roaraudio/roard/clients.c @ 5155:420e4f980a8e

Last change on this file since 5155:420e4f980a8e was 5155:420e4f980a8e, checked in by phi, 13 years ago

run protocol functions in clear error context

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