source: roaraudio/roard/clients.c @ 4101:42b7ee5d2f76

Last change on this file since 4101:42b7ee5d2f76 was 4101:42b7ee5d2f76, checked in by phi, 14 years ago

some work to support counters

File size: 14.9 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
443 _CHECK_CID(id);
444
445 if ( g_clients[id]->fh == -1 )
446  return -1;
447
448 roar_connect_fh(&con, g_clients[id]->fh);
449
450 switch (g_clients[id]->proto) {
451  case ROAR_PROTO_ROARAUDIO:
452    r = roar_recv_message(&con, &m, &data);
453
454    if ( r == -1 ) { // should we drop the client?
455     clients_delete(id);
456     return -1;
457    }
458
459    roar_debug_message_print(&m);
460
461    oldcmd = m.cmd;
462
463    if ( (r = command_exec(id, &m, &data, flags)) == -1 ) {
464     m.cmd     = ROAR_CMD_ERROR;
465     m.datalen = 0;
466     ROAR_DBG("clients_check(*): Exec of command faild!");
467    } else {
468     if ( m.cmd == oldcmd ) {
469      m.cmd     = ROAR_CMD_OK;
470      m.datalen = 0;
471     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
472      m.cmd     = ROAR_CMD_OK;
473      rv        = 1;
474     }
475    }
476
477    roar_send_message(&con, &m, NULL);
478
479    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
480     clients_close(id, 1);
481
482   break;
483#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
484#ifdef ROAR_HAVE_H_ESD
485  case ROAR_PROTO_ESOUND:
486    rv = emul_esd_check_client(id, NULL);
487   break;
488#endif
489#endif
490#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
491  case ROAR_PROTO_RPLAY:
492    rv = emul_rplay_check_client(id, NULL);
493   break;
494#endif
495#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
496  case ROAR_PROTO_RSOUND:
497    rv = emul_rsound_check_client(id, NULL);
498    if ( rv == 0 ) { // loop as long as we don't get an error.
499     while (rv == 0)
500      rv = emul_rsound_check_client(id, NULL);
501     rv = 0; // restore
502    } else { // in case of error delete the client
503     rv = clients_delete(id);
504    }
505   break;
506#endif
507  default:
508    rv = -1;
509 }
510
511 if ( data )
512  free(data);
513
514 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
515 return rv;
516}
517
518int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
519 int i;
520// int fh;
521 int j;
522 int keep_going;
523
524 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
525  if ( g_clients[i] == NULL )
526   continue;
527
528  keep_going = 1;
529
530/*
531  if ( (fh = g_clients[i]->fh) == -1 )
532   continue;
533*/
534
535  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
536
537/*
538  if ( g_clients[i]->execed == -1 ) {
539   // TODO: add some code to send a message to the client insetd of the raw data.
540*/
541   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
542    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
543    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
544    if ( g_clients[i]->streams[j] != -1 ) {
545     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
546     streams_send_mon(g_clients[i]->streams[j]);
547
548     // the client may be deleted here, check if it still exists:
549     if ( g_clients[i] == NULL )
550      keep_going = 0;
551    }
552   }
553/*
554  } else {
555//   streams_check(g_clients[i]->execed);
556   streams_send_mon(g_clients[i]->execed);
557//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
558//    clients_delete(i); // delete client in case we could not write
559  }
560*/
561 }
562
563 // TODO: FIXME: should this really be -1?
564 return -1;
565}
566
567int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
568 int i;
569 int fh;
570
571 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
572  if ( g_clients[i] == NULL )
573   continue;
574
575  if ( (fh = g_clients[i]->fh) == -1 )
576   continue;
577
578  if ( g_clients[i]->execed == -1 ) {
579   // TODO: add some code to send a message to the client insetd of the raw data.
580  } else {
581//   streams_check(g_clients[i]->execed);
582   streams_send_filter(g_clients[i]->execed);
583//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
584//    clients_delete(i); // delete client in case we could not write
585  }
586 }
587
588 return -1;
589}
590
591int client_stream_exec   (int client, int stream) {
592 int i;
593 int fh;
594
595 _CHECK_CID(client);
596
597#if 0
598 if ( g_clients[client] == NULL ) {
599  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
600  return -1;
601 }
602#endif
603
604 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
605  if ( g_clients[client]->streams[i] == stream ) {
606   g_clients[client]->execed = stream;
607   if ( streams_is_ready(stream) == 0 ) {
608    streams_set_fh(stream, g_clients[client]->fh);
609    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
610   } else {
611    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=%i", client, stream, fh);
612    if ( (fh = g_clients[client]->fh) != -1 ) {
613     close(fh);
614     g_clients[client]->fh = -1;
615    }
616   }
617   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
618   return 0;
619  }
620 }
621
622 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
623 return -1;
624}
625
626int client_stream_set_fh (int client, int stream, int fh) {
627 int i;
628
629 _CHECK_CID(client);
630
631 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
632  if ( g_clients[client]->streams[i] == stream ) {
633   streams_set_fh(stream, fh);
634   return 0;
635  }
636 }
637
638 return -1;
639}
640
641int client_stream_add    (int client, int stream) {
642 int i;
643
644 _CHECK_CID(client);
645
646 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
647  if ( g_clients[client]->streams[i] == -1 ) {
648   g_clients[client]->streams[i] = stream;
649   streams_set_client(stream, client);
650   return 0;
651  }
652 }
653
654 return -1;
655}
656
657int client_stream_delete (int client, int stream) {
658 int i;
659
660 _CHECK_CID(client);
661
662 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
663  if ( g_clients[client]->streams[i] == stream ) {
664   g_clients[client]->streams[i] = -1;
665
666   if ( stream == g_clients[client]->execed ) {
667    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
668    clients_delete(client);
669   }
670
671   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
672   return 0;
673  }
674 }
675
676 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
677 return -1;
678}
679
680int client_stream_move   (int client, int stream) {
681 int old_client = streams_get_client(stream);
682
683 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
684
685 if ( old_client != -1 )
686  if ( client_stream_delete(old_client, stream) == -1 )
687   return -1;
688
689 return client_stream_add(client, stream);
690}
691
692//ll
Note: See TracBrowser for help on using the repository browser.