source: roaraudio/roard/clients.c @ 4325:72ed3bb7929a

Last change on this file since 4325:72ed3bb7929a was 4325:72ed3bb7929a, checked in by phi, 14 years ago

emit event on cmds

File size: 15.7 KB
Line 
1//clients.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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#include "roard.h"
27
28#define _CHECK_CID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_CLIENTS_MAX || g_clients[(id)] == NULL ) return (ret)
29#define _CHECK_CID(id)         _CHECK_CID_RET((id), -1)
30
31int clients_init (void) {
32 int i;
33
34 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
35  g_clients[i] = NULL;
36
37 return 0;
38}
39
40int clients_free (void) {
41 int i;
42
43 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
44  if ( g_clients[i] != NULL )
45   clients_delete(i);
46
47 return 0;
48}
49
50int clients_new (void) {
51 int i;
52 int s;
53 struct roar_client * n;
54
55 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
56  if ( g_clients[i] == NULL ) {
57   n = roar_mm_malloc(sizeof(struct roar_client));
58   if ( n != NULL ) {
59    n->pid    = -1;
60    n->uid    = -1;
61    n->gid    = -1;
62    n->fh     = -1;
63
64    *n->name = 0;
65    *n->host = 0;
66
67    n->proto     = ROAR_PROTO_ROARAUDIO;
68    n->byteorder = ROAR_BYTEORDER_NETWORK;
69
70    n->acl   = NULL;
71
72    n->execed = -1;
73    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
74     n->streams[s] = -1;
75
76    if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) {
77     roar_mm_free(n);
78     return -1;
79    }
80
81    g_clients[i] = n;
82
83    counters_inc(clients, 1);
84    ROAR_DBG("clients_new(void) = %i", i);
85    return i;
86   } else {
87    ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno));
88    ROAR_ERR("clients_new(void) = -1");
89    return -1;
90   }
91  }
92 }
93
94 return -1;
95}
96
97int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) {
98 struct roar_client * c;
99 int client;
100
101 if ( fh == -1 )
102  return -1;
103
104 if ( proto != ROAR_PROTO_ROARAUDIO || byteorder != ROAR_BYTEORDER_NETWORK )
105  return -1;
106
107 if ( (client = clients_new()) == -1 )
108  return -1;
109
110 if ( clients_set_fh(client, fh) == -1 ) {
111  clients_delete(client);
112  return -1;
113 }
114
115 if ( update_nnode ) {
116  if ( clients_get(client, &c) != -1 ) {
117   if ( roar_nnode_free(&(c->nnode)) != -1 ) {
118    roar_nnode_new_from_fh(&(c->nnode), fh, 1);
119   }
120  }
121 }
122
123 return 0;
124}
125
126int clients_delete (int id) {
127 int i;
128 int close_client_fh = 1;
129
130 ROAR_DBG("clients_delete(id=%i) = ?", id);
131
132 counters_inc(clients, -1);
133
134 _CHECK_CID(id);
135
136 if (g_clients[id]->execed != -1) {
137//  return streams_delete(g_clients[id]->execed);
138  g_clients[id]->execed = -1;
139  close_client_fh = 0;
140 }
141
142 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
143  streams_delete(g_clients[id]->streams[i]);
144 }
145
146 if ( g_clients[id]->fh != -1 && close_client_fh )
147  close(g_clients[id]->fh);
148
149 roar_nnode_free(&(g_clients[id]->nnode));
150
151 roar_mm_free(g_clients[id]);
152 g_clients[id] = NULL;
153
154 ROAR_DBG("clients_delete(id=%i) = 0", id);
155 return 0;
156}
157
158int clients_close      (int id, int nocheck_exec) {
159 struct roar_client * c;
160
161 ROAR_DBG("clients_close(id=%i) = ?", id);
162
163 _CHECK_CID(id);
164
165 c = g_clients[id];
166
167 if ( c->fh == -1 ) {
168  ROAR_DBG("clients_delete(id=%i) = 0", id);
169  return 0;
170 }
171
172 if (nocheck_exec || g_clients[id]->execed != -1) {
173  close(c->fh);
174  c->fh = -1;
175 }
176
177 ROAR_DBG("clients_delete(id=%i) = 0", id);
178 return 0;
179}
180
181int clients_get       (int id, struct roar_client ** client) {
182 _CHECK_CID(id);
183
184 *client = g_clients[id];
185
186 if ( *client == NULL )
187  return -1;
188
189 return 0;
190}
191
192int clients_set_fh    (int id, int    fh) {
193 struct roar_client * c;
194#ifdef SO_PEERCRED
195 struct ucred cred;
196 socklen_t cred_len = sizeof(cred);
197#endif
198
199 _CHECK_CID(id);
200
201 if ( (c = g_clients[id]) == NULL )
202  return -1;
203
204 c->fh = fh;
205
206#ifdef SO_PEERCRED
207 if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
208  if ( cred.pid != 0 ) {
209   c->pid = cred.pid;
210   c->uid = cred.uid;
211   c->gid = cred.gid;
212  }
213 } else {
214  ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
215 }
216#elif defined(ROAR_HAVE_GETPEEREID)
217 if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
218  ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
219 }
220#endif
221
222 return 0;
223}
224
225int clients_get_fh    (int id) {
226 _CHECK_CID(id);
227
228 return g_clients[id]->fh;
229}
230
231int clients_set_pid   (int id, int    pid) {
232 _CHECK_CID(id);
233
234 g_clients[id]->pid = pid;
235
236 return 0;
237}
238
239int clients_set_uid   (int id, int    uid) {
240 _CHECK_CID(id);
241
242 g_clients[id]->uid = uid;
243
244 return 0;
245}
246
247int clients_set_gid   (int id, int    gid) {
248 _CHECK_CID(id);
249
250 g_clients[id]->gid = gid;
251
252 return 0;
253}
254
255int clients_set_proto (int id, int    proto) {
256 int byteorder = ROAR_BYTEORDER_UNKNOWN;
257
258 _CHECK_CID(id);
259
260 switch (proto) {
261  case ROAR_PROTO_ROARAUDIO:
262  case ROAR_PROTO_ESOUND:
263  case ROAR_PROTO_RPLAY:
264  case ROAR_PROTO_SIMPLE:
265    byteorder = ROAR_BYTEORDER_NETWORK;
266   break;
267 }
268
269 g_clients[id]->proto     = proto;
270 g_clients[id]->byteorder = byteorder;
271
272 return 0;
273}
274
275#define MAX_STREAMLESS 8
276
277int clients_check_all (void) {
278#ifdef ROAR_HAVE_SELECT
279 struct timeval tv;
280 fd_set r, e;
281 int i, j;
282 int ret;
283 int fh;
284 int max_fh = -1;
285 int have = 0;
286 struct {
287  int id;
288  int fh;
289 } streamless[MAX_STREAMLESS];
290 int have_streamless = 0;
291 int have_stream;
292
293 FD_ZERO(&r);
294 FD_ZERO(&e);
295
296 tv.tv_sec  = 0;
297 tv.tv_usec = 1;
298
299 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
300  if ( g_clients[i] == NULL )
301   continue;
302
303  if ( (fh = g_clients[i]->fh) != -1 ) {
304   have++;
305
306   ROAR_DBG("clients_check_all(*): fh=%i", fh);
307
308   FD_SET(fh, &r);
309   FD_SET(fh, &e);
310
311   if ( fh > max_fh )
312    max_fh = fh;
313  }
314
315  have_stream = 0;
316
317  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
318   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
319    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
320    if ( fh > -1 ) {
321     FD_SET(fh, &r);
322
323     if ( fh > max_fh )
324      max_fh = fh;
325    } else if ( fh == -2 ) {
326     streams_check(g_clients[i]->streams[j]);
327    }
328
329    have_stream = 1;
330   }
331   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
332  }
333
334  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
335   streamless[have_streamless  ].id = i;
336   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
337    have_streamless--;
338  }
339 }
340
341 if ( max_fh == -1 )
342  return 0;
343
344 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
345  return ret < 0 ? ret : have;
346 }
347
348 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
349  if ( g_clients[i] == NULL )
350   continue;
351
352  if ( (fh = g_clients[i]->fh) != -1 ) {
353   if ( FD_ISSET(fh, &r) ) {
354    if ( g_clients[i]->execed == -1 ) {
355     clients_check(i);
356     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
357      FD_CLR(fh, &r);
358     }
359/*
360    } else {
361     streams_check(g_clients[i]->execed);
362*/
363    }
364   }
365
366   if ( FD_ISSET(fh, &e) ) {
367    clients_delete(i);
368    continue;
369   }
370  }
371
372  if ( g_clients[i] == NULL )
373   continue;
374
375  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
376   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
377   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
378    break;
379
380   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
381   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, g_clients[i]->streams[j]);
382
383   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
384    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
385    if ( fh > -1 && FD_ISSET(fh, &r) ) {
386     streams_check(g_clients[i]->streams[j]);
387    }
388   }
389  }
390 }
391
392 if ( have_streamless ) {
393   FD_ZERO(&r);
394
395   tv.tv_sec  = 0;
396   tv.tv_usec = 1;
397
398   max_fh = -1;
399
400   for (i = 0; i < have_streamless; i++) {
401    if ( ! g_clients[j = streamless[i].id] )
402     continue;
403
404    if ( g_clients[j]->execed != -1 )
405     continue;
406
407    fh = streamless[i].fh;
408
409    ROAR_DBG("clients_check_all(void): fh=%i", fh);
410    FD_SET(fh, &r);
411
412    if ( fh > max_fh )
413     max_fh = fh;
414   }
415
416   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
417    return ret;
418   }
419
420   for (i = 0; i < have_streamless; i++) {
421    if ( FD_ISSET(streamless[i].fh, &r) ) {
422     clients_check(streamless[i].id);
423    }
424   }
425 }
426
427 ROAR_DBG("clients_check_all(void) = %i // have value", have);
428 return have;
429#else
430 return -1;
431#endif
432}
433
434int clients_check     (int id) {
435 struct roar_message    m;
436 struct roar_connection con;
437 char * data = NULL;
438 int oldcmd;
439 int r;
440 int rv = 0;
441 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
442 uint32_t event;
443
444 _CHECK_CID(id);
445
446 if ( g_clients[id]->fh == -1 )
447  return -1;
448
449 roar_connect_fh(&con, g_clients[id]->fh);
450
451 switch (g_clients[id]->proto) {
452  case ROAR_PROTO_ROARAUDIO:
453    r = roar_recv_message(&con, &m, &data);
454
455    if ( r == -1 ) { // should we drop the client?
456     clients_delete(id);
457     return -1;
458    }
459
460    event = ROAR_NOTIFY_CMD2EVENT(m.cmd);
461
462    roar_debug_message_print(&m);
463
464    oldcmd = m.cmd;
465
466    if ( (r = command_exec(id, &m, &data, flags)) == -1 ) {
467     m.cmd     = ROAR_CMD_ERROR;
468     m.datalen = 0;
469     ROAR_DBG("clients_check(*): Exec of command faild!");
470    } else {
471     if ( m.cmd == oldcmd ) {
472      m.cmd     = ROAR_CMD_OK;
473      m.datalen = 0;
474     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
475      m.cmd     = ROAR_CMD_OK;
476      rv        = 1;
477     }
478    }
479
480    roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0);
481
482    ROAR_DBG("clients_check(*): data=%p", data);
483
484    roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL);
485
486    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
487     clients_close(id, 1);
488
489   break;
490#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
491#ifdef ROAR_HAVE_H_ESD
492  case ROAR_PROTO_ESOUND:
493    rv = emul_esd_check_client(id, NULL);
494   break;
495#endif
496#endif
497#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
498  case ROAR_PROTO_RPLAY:
499    rv = emul_rplay_check_client(id, NULL);
500   break;
501#endif
502#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
503  case ROAR_PROTO_RSOUND:
504    rv = emul_rsound_check_client(id, NULL);
505    if ( rv == 0 ) { // loop as long as we don't get an error.
506     while (rv == 0)
507      rv = emul_rsound_check_client(id, NULL);
508     rv = 0; // restore
509    } else { // in case of error delete the client
510     if (
511#ifdef EAGAIN
512          errno != EAGAIN      &&
513#endif
514#ifdef EWOULDBLOCK
515          errno != EWOULDBLOCK &&
516#endif
517#ifdef EINTR
518          errno != EINTR       &&
519#endif
520          1 ) {
521      rv = clients_delete(id);
522     } else {
523      rv = 0;
524     }
525    }
526   break;
527#endif
528  default:
529    rv = -1;
530 }
531
532 if ( data != NULL )
533  free(data);
534
535 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
536 return rv;
537}
538
539int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
540 int i;
541// int fh;
542 int j;
543 int keep_going;
544
545 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
546  if ( g_clients[i] == NULL )
547   continue;
548
549  keep_going = 1;
550
551/*
552  if ( (fh = g_clients[i]->fh) == -1 )
553   continue;
554*/
555
556  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
557
558/*
559  if ( g_clients[i]->execed == -1 ) {
560   // TODO: add some code to send a message to the client insetd of the raw data.
561*/
562   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
563    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
564    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
565    if ( g_clients[i]->streams[j] != -1 ) {
566     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
567     streams_send_mon(g_clients[i]->streams[j]);
568
569     // the client may be deleted here, check if it still exists:
570     if ( g_clients[i] == NULL )
571      keep_going = 0;
572    }
573   }
574/*
575  } else {
576//   streams_check(g_clients[i]->execed);
577   streams_send_mon(g_clients[i]->execed);
578//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
579//    clients_delete(i); // delete client in case we could not write
580  }
581*/
582 }
583
584 // TODO: FIXME: should this really be -1?
585 return -1;
586}
587
588int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
589 int i;
590 int fh;
591
592 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
593  if ( g_clients[i] == NULL )
594   continue;
595
596  if ( (fh = g_clients[i]->fh) == -1 )
597   continue;
598
599  if ( g_clients[i]->execed == -1 ) {
600   // TODO: add some code to send a message to the client insetd of the raw data.
601  } else {
602//   streams_check(g_clients[i]->execed);
603   streams_send_filter(g_clients[i]->execed);
604//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
605//    clients_delete(i); // delete client in case we could not write
606  }
607 }
608
609 return -1;
610}
611
612int client_stream_exec   (int client, int stream) {
613 int i;
614 int fh;
615
616 _CHECK_CID(client);
617
618#if 0
619 if ( g_clients[client] == NULL ) {
620  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
621  return -1;
622 }
623#endif
624
625 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
626  if ( g_clients[client]->streams[i] == stream ) {
627   g_clients[client]->execed = stream;
628   if ( streams_is_ready(stream) == 0 ) {
629    streams_set_fh(stream, g_clients[client]->fh);
630    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
631   } else {
632    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream);
633    if ( (fh = g_clients[client]->fh) != -1 ) {
634     close(fh);
635     g_clients[client]->fh = -1;
636    }
637   }
638   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
639   return 0;
640  }
641 }
642
643 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
644 return -1;
645}
646
647int client_stream_set_fh (int client, int stream, int fh) {
648 int i;
649
650 ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh);
651
652 _CHECK_CID(client);
653
654 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
655  if ( g_clients[client]->streams[i] == stream ) {
656   ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i);
657   return streams_set_fh(stream, fh);
658  }
659 }
660
661 ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh);
662 return -1;
663}
664
665int client_stream_add    (int client, int stream) {
666 int i;
667
668 _CHECK_CID(client);
669
670 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
671  if ( g_clients[client]->streams[i] == -1 ) {
672   g_clients[client]->streams[i] = stream;
673   streams_set_client(stream, client);
674   return 0;
675  }
676 }
677
678 return -1;
679}
680
681int client_stream_delete (int client, int stream) {
682 int i;
683
684 _CHECK_CID(client);
685
686 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
687  if ( g_clients[client]->streams[i] == stream ) {
688   g_clients[client]->streams[i] = -1;
689
690   if ( stream == g_clients[client]->execed ) {
691    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
692    clients_delete(client);
693   }
694
695   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
696   return 0;
697  }
698 }
699
700 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
701 return -1;
702}
703
704int client_stream_move   (int client, int stream) {
705 int old_client = streams_get_client(stream);
706
707 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
708
709 if ( old_client != -1 )
710  if ( client_stream_delete(old_client, stream) == -1 )
711   return -1;
712
713 return client_stream_add(client, stream);
714}
715
716//ll
Note: See TracBrowser for help on using the repository browser.