source: roaraudio/roard/clients.c @ 3981:0dda28251f72

Last change on this file since 3981:0dda28251f72 was 3981:0dda28251f72, checked in by phi, 14 years ago

activate rplay

File size: 14.9 KB
RevLine 
[0]1//clients.c:
2
[668]3/*
[3358]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
[668]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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[668]23 *
24 */
25
[0]26#include "roard.h"
27
[3910]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
[0]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++)
[3910]44  if ( g_clients[i] != NULL )
[0]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 ) {
[3063]57   n = roar_mm_malloc(sizeof(struct roar_client));
[0]58   if ( n != NULL ) {
59    n->pid    = -1;
[437]60    n->uid    = -1;
61    n->gid    = -1;
[0]62    n->fh     = -1;
63
64    *n->name = 0;
65    *n->host = 0;
66
[2614]67    n->proto     = ROAR_PROTO_ROARAUDIO;
68    n->byteorder = ROAR_BYTEORDER_NETWORK;
[2517]69
[346]70    n->acl   = NULL;
71
[0]72    n->execed = -1;
73    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
74     n->streams[s] = -1;
75
[2815]76    if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) {
[3063]77     roar_mm_free(n);
[2815]78     return -1;
79    }
80
[0]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
[3737]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
[0]125int clients_delete (int id) {
126 int i;
[1164]127 int close_client_fh = 1;
[0]128
[2608]129 ROAR_DBG("clients_delete(id=%i) = ?", id);
130
[3910]131 _CHECK_CID(id);
[0]132
133 if (g_clients[id]->execed != -1) {
134//  return streams_delete(g_clients[id]->execed);
135  g_clients[id]->execed = -1;
[1164]136  close_client_fh = 0;
[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
[1164]143 if ( g_clients[id]->fh != -1 && close_client_fh )
[0]144  close(g_clients[id]->fh);
145
[2815]146 roar_nnode_free(&(g_clients[id]->nnode));
147
[3063]148 roar_mm_free(g_clients[id]);
[0]149 g_clients[id] = NULL;
150
151 ROAR_DBG("clients_delete(id=%i) = 0", id);
152 return 0;
153}
154
[3927]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
[0]178int clients_get       (int id, struct roar_client ** client) {
[3910]179 _CHECK_CID(id);
180
[0]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) {
[3713]190 struct roar_client * c;
191#ifdef SO_PEERCRED
192 struct ucred cred;
193 socklen_t cred_len = sizeof(cred);
194#endif
[501]195
[3910]196 _CHECK_CID(id);
197
[3713]198 if ( (c = g_clients[id]) == NULL )
[0]199  return -1;
200
[3713]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
[0]218
219 return 0;
220}
221
[755]222int clients_get_fh    (int id) {
[3910]223 _CHECK_CID(id);
[755]224
225 return g_clients[id]->fh;
226}
227
[0]228int clients_set_pid   (int id, int    pid) {
[3910]229 _CHECK_CID(id);
[0]230
231 g_clients[id]->pid = pid;
232
233 return 0;
234}
235
[439]236int clients_set_uid   (int id, int    uid) {
[3910]237 _CHECK_CID(id);
[439]238
239 g_clients[id]->uid = uid;
240
241 return 0;
242}
243
244int clients_set_gid   (int id, int    gid) {
[3910]245 _CHECK_CID(id);
[439]246
247 g_clients[id]->gid = gid;
248
249 return 0;
250}
251
[2517]252int clients_set_proto (int id, int    proto) {
[2614]253 int byteorder = ROAR_BYTEORDER_UNKNOWN;
254
[3910]255 _CHECK_CID(id);
[2517]256
[2614]257 switch (proto) {
258  case ROAR_PROTO_ROARAUDIO:
[2828]259  case ROAR_PROTO_ESOUND:
[3981]260  case ROAR_PROTO_RPLAY:
[3255]261  case ROAR_PROTO_SIMPLE:
[2614]262    byteorder = ROAR_BYTEORDER_NETWORK;
263   break;
264 }
265
266 g_clients[id]->proto     = proto;
267 g_clients[id]->byteorder = byteorder;
[2517]268
269 return 0;
270}
271
[498]272#define MAX_STREAMLESS 8
273
[0]274int clients_check_all (void) {
[1480]275#ifdef ROAR_HAVE_SELECT
[0]276 struct timeval tv;
277 fd_set r, e;
[66]278 int i, j;
[0]279 int ret;
280 int fh;
281 int max_fh = -1;
[71]282 int have = 0;
[498]283 struct {
284  int id;
285  int fh;
286 } streamless[MAX_STREAMLESS];
287 int have_streamless = 0;
288 int have_stream;
[0]289
290 FD_ZERO(&r);
291 FD_ZERO(&e);
292
293 tv.tv_sec  = 0;
294 tv.tv_usec = 1;
295
296 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
297  if ( g_clients[i] == NULL )
298   continue;
299
[66]300  if ( (fh = g_clients[i]->fh) != -1 ) {
[71]301   have++;
302
[610]303   ROAR_DBG("clients_check_all(*): fh=%i", fh);
304
[66]305   FD_SET(fh, &r);
306   FD_SET(fh, &e);
307
308   if ( fh > max_fh )
309    max_fh = fh;
[84]310  }
[0]311
[498]312  have_stream = 0;
313
[84]314  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
315   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2598]316    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
[1504]317    if ( fh > -1 ) {
318     FD_SET(fh, &r);
[0]319
[1504]320     if ( fh > max_fh )
321      max_fh = fh;
[3579]322    } else if ( fh == -2 ) {
323     streams_check(g_clients[i]->streams[j]);
[1504]324    }
[498]325
326    have_stream = 1;
[66]327   }
[84]328   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
[66]329  }
330
[498]331  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
332   streamless[have_streamless  ].id = i;
333   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
334    have_streamless--;
335  }
[0]336 }
337
[256]338 if ( max_fh == -1 )
339  return 0;
340
[0]341 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
[71]342  return ret < 0 ? ret : have;
[0]343 }
344
345 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
346  if ( g_clients[i] == NULL )
347   continue;
348
[66]349  if ( (fh = g_clients[i]->fh) != -1 ) {
350   if ( FD_ISSET(fh, &r) ) {
351    if ( g_clients[i]->execed == -1 ) {
352     clients_check(i);
[1835]353     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
[1834]354      FD_CLR(fh, &r);
355     }
[255]356/*
[66]357    } else {
358     streams_check(g_clients[i]->execed);
[255]359*/
[66]360    }
361   }
[0]362
[84]363   if ( FD_ISSET(fh, &e) ) {
[66]364    clients_delete(i);
[84]365    continue;
366   }
367  }
368
369  if ( g_clients[i] == NULL )
370   continue;
371
372  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[1611]373   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
[136]374   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
375    break;
[1611]376
377   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
378   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, g_clients[i]->streams[j]);
379
[84]380   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[1611]381    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
[3579]382    if ( fh > -1 && FD_ISSET(fh, &r) ) {
[84]383     streams_check(g_clients[i]->streams[j]);
[66]384    }
[0]385   }
386  }
387 }
388
[498]389 if ( have_streamless ) {
390   FD_ZERO(&r);
391
392   tv.tv_sec  = 0;
393   tv.tv_usec = 1;
394
395   max_fh = -1;
396
397   for (i = 0; i < have_streamless; i++) {
[607]398    if ( ! g_clients[j = streamless[i].id] )
399     continue;
400
401    if ( g_clients[j]->execed != -1 )
[601]402     continue;
403
[498]404    fh = streamless[i].fh;
405
406    ROAR_DBG("clients_check_all(void): fh=%i", fh);
407    FD_SET(fh, &r);
408
409    if ( fh > max_fh )
410     max_fh = fh;
411   }
412
413   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
414    return ret;
415   }
416
417   for (i = 0; i < have_streamless; i++) {
418    if ( FD_ISSET(streamless[i].fh, &r) ) {
419     clients_check(streamless[i].id);
420    }
421   }
422 }
423
[71]424 ROAR_DBG("clients_check_all(void) = %i // have value", have);
425 return have;
[1480]426#else
427 return -1;
428#endif
[0]429}
430
431int clients_check     (int id) {
432 struct roar_message    m;
433 struct roar_connection con;
434 char * data = NULL;
435 int oldcmd;
436 int r;
[498]437 int rv = 0;
[3926]438 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
[0]439
[3910]440 _CHECK_CID(id);
441
[0]442 if ( g_clients[id]->fh == -1 )
443  return -1;
444
[1662]445 roar_connect_fh(&con, g_clients[id]->fh);
[0]446
[2517]447 switch (g_clients[id]->proto) {
448  case ROAR_PROTO_ROARAUDIO:
449    r = roar_recv_message(&con, &m, &data);
[0]450
[2517]451    if ( r == -1 ) { // should we drop the client?
452     clients_delete(id);
453     return -1;
454    }
[0]455
[2517]456    roar_debug_message_print(&m);
[0]457
[2517]458    oldcmd = m.cmd;
[0]459
[3926]460    if ( (r = command_exec(id, &m, &data, flags)) == -1 ) {
[2517]461     m.cmd     = ROAR_CMD_ERROR;
462     m.datalen = 0;
463     ROAR_DBG("clients_check(*): Exec of command faild!");
464    } else {
465     if ( m.cmd == oldcmd ) {
466      m.cmd     = ROAR_CMD_OK;
467      m.datalen = 0;
468     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
469      m.cmd     = ROAR_CMD_OK;
470      rv        = 1;
471     }
472    }
473
474    roar_send_message(&con, &m, NULL);
[3928]475
476    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
477     clients_close(id, 1);
478
[2517]479   break;
[2546]480#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
[2791]481#ifdef ROAR_HAVE_H_ESD
[2523]482  case ROAR_PROTO_ESOUND:
483    rv = emul_esd_check_client(id, NULL);
484   break;
[2546]485#endif
486#endif
[3981]487#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
488  case ROAR_PROTO_RPLAY:
489    rv = emul_esd_check_client(id, NULL);
490   break;
491#endif
[3684]492#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
493  case ROAR_PROTO_RSOUND:
[3820]494    rv = emul_rsound_check_client(id, NULL);
[3825]495    if ( rv == 0 ) { // loop as long as we don't get an error.
496     while (rv == 0)
497      rv = emul_rsound_check_client(id, NULL);
498     rv = 0; // restore
499    } else { // in case of error delete the client
500     rv = clients_delete(id);
501    }
[3684]502   break;
503#endif
[2517]504  default:
505    rv = -1;
[0]506 }
507
508 if ( data )
509  free(data);
510
[2517]511 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
[498]512 return rv;
[0]513}
514
515int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
516 int i;
[934]517// int fh;
518 int j;
[2715]519 int keep_going;
[0]520
521 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
522  if ( g_clients[i] == NULL )
523   continue;
524
[2715]525  keep_going = 1;
526
[934]527/*
[0]528  if ( (fh = g_clients[i]->fh) == -1 )
529   continue;
[934]530*/
[0]531
[1905]532  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
533
[2706]534/*
[0]535  if ( g_clients[i]->execed == -1 ) {
536   // TODO: add some code to send a message to the client insetd of the raw data.
[2706]537*/
[2715]538   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[934]539    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2715]540    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
[1905]541    if ( g_clients[i]->streams[j] != -1 ) {
542     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
[934]543     streams_send_mon(g_clients[i]->streams[j]);
[2715]544
545     // the client may be deleted here, check if it still exists:
546     if ( g_clients[i] == NULL )
547      keep_going = 0;
[1905]548    }
[934]549   }
[2706]550/*
[0]551  } else {
552//   streams_check(g_clients[i]->execed);
553   streams_send_mon(g_clients[i]->execed);
554//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
555//    clients_delete(i); // delete client in case we could not write
556  }
[2706]557*/
[0]558 }
559
[1902]560 // TODO: FIXME: should this really be -1?
[0]561 return -1;
562}
563
564int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
565 int i;
566 int fh;
567
568 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
569  if ( g_clients[i] == NULL )
570   continue;
571
572  if ( (fh = g_clients[i]->fh) == -1 )
573   continue;
574
575  if ( g_clients[i]->execed == -1 ) {
576   // TODO: add some code to send a message to the client insetd of the raw data.
577  } else {
578//   streams_check(g_clients[i]->execed);
579   streams_send_filter(g_clients[i]->execed);
580//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
581//    clients_delete(i); // delete client in case we could not write
582  }
583 }
584
585 return -1;
586}
587
588int client_stream_exec   (int client, int stream) {
589 int i;
[3920]590 int fh;
[0]591
[3910]592 _CHECK_CID(client);
593
594#if 0
[1901]595 if ( g_clients[client] == NULL ) {
596  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
[0]597  return -1;
[1901]598 }
[3910]599#endif
[0]600
601 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
602  if ( g_clients[client]->streams[i] == stream ) {
603   g_clients[client]->execed = stream;
[3921]604   if ( streams_is_ready(stream) == 0 ) {
[3920]605    streams_set_fh(stream, g_clients[client]->fh);
606    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
607   } else {
[3921]608    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=%i", client, stream, fh);
[3925]609    if ( (fh = g_clients[client]->fh) != -1 ) {
610     close(fh);
611     g_clients[client]->fh = -1;
612    }
[3920]613   }
[2815]614   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
[0]615   return 0;
616  }
617 }
618
[1901]619 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
[0]620 return -1;
621}
622
[78]623int client_stream_set_fh (int client, int stream, int fh) {
624 int i;
625
[3910]626 _CHECK_CID(client);
[78]627
628 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
629  if ( g_clients[client]->streams[i] == stream ) {
630   streams_set_fh(stream, fh);
631   return 0;
632  }
633 }
634
635 return -1;
636}
637
[0]638int client_stream_add    (int client, int stream) {
639 int i;
640
[3910]641 _CHECK_CID(client);
[0]642
643 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
644  if ( g_clients[client]->streams[i] == -1 ) {
645   g_clients[client]->streams[i] = stream;
646   streams_set_client(stream, client);
647   return 0;
648  }
649 }
650
651 return -1;
652}
653
654int client_stream_delete (int client, int stream) {
655 int i;
656
[3910]657 _CHECK_CID(client);
[0]658
659 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
660  if ( g_clients[client]->streams[i] == stream ) {
661   g_clients[client]->streams[i] = -1;
662
663   if ( stream == g_clients[client]->execed ) {
664    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
665    clients_delete(client);
666   }
667
668   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
669   return 0;
670  }
671 }
672
673 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
674 return -1;
675}
676
[767]677int client_stream_move   (int client, int stream) {
678 int old_client = streams_get_client(stream);
679
[771]680 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
681
[767]682 if ( old_client != -1 )
683  if ( client_stream_delete(old_client, stream) == -1 )
684   return -1;
685
686 return client_stream_add(client, stream);
687}
688
[0]689//ll
Note: See TracBrowser for help on using the repository browser.