source: roaraudio/roard/clients.c @ 3928:6e8e191af6a7

Last change on this file since 3928:6e8e191af6a7 was 3928:6e8e191af6a7, checked in by phi, 14 years ago

fixed exec of ready stream bug :)

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