source: roaraudio/roard/clients.c @ 4298:f36b7f925311

Last change on this file since 4298:f36b7f925311 was 4298:f36b7f925311, checked in by phi, 14 years ago

added support for large number of streams in LTM

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