source: roaraudio/roard/clients.c @ 3920:8135cd9db0e5

Last change on this file since 3920:8135cd9db0e5 was 3920:8135cd9db0e5, checked in by phi, 14 years ago

support exec on (ready) streams with state NEW, OLD, ...

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