source: roaraudio/roard/clients.c @ 4326:c53e2ed183a2

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

use new roar_client_server struct for clients in roard

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