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
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:
[3255]260  case ROAR_PROTO_SIMPLE:
[2614]261    byteorder = ROAR_BYTEORDER_NETWORK;
262   break;
263 }
264
265 g_clients[id]->proto     = proto;
266 g_clients[id]->byteorder = byteorder;
[2517]267
268 return 0;
269}
270
[498]271#define MAX_STREAMLESS 8
272
[0]273int clients_check_all (void) {
[1480]274#ifdef ROAR_HAVE_SELECT
[0]275 struct timeval tv;
276 fd_set r, e;
[66]277 int i, j;
[0]278 int ret;
279 int fh;
280 int max_fh = -1;
[71]281 int have = 0;
[498]282 struct {
283  int id;
284  int fh;
285 } streamless[MAX_STREAMLESS];
286 int have_streamless = 0;
287 int have_stream;
[0]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
[66]299  if ( (fh = g_clients[i]->fh) != -1 ) {
[71]300   have++;
301
[610]302   ROAR_DBG("clients_check_all(*): fh=%i", fh);
303
[66]304   FD_SET(fh, &r);
305   FD_SET(fh, &e);
306
307   if ( fh > max_fh )
308    max_fh = fh;
[84]309  }
[0]310
[498]311  have_stream = 0;
312
[84]313  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
314   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2598]315    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
[1504]316    if ( fh > -1 ) {
317     FD_SET(fh, &r);
[0]318
[1504]319     if ( fh > max_fh )
320      max_fh = fh;
[3579]321    } else if ( fh == -2 ) {
322     streams_check(g_clients[i]->streams[j]);
[1504]323    }
[498]324
325    have_stream = 1;
[66]326   }
[84]327   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
[66]328  }
329
[498]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  }
[0]335 }
336
[256]337 if ( max_fh == -1 )
338  return 0;
339
[0]340 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
[71]341  return ret < 0 ? ret : have;
[0]342 }
343
344 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
345  if ( g_clients[i] == NULL )
346   continue;
347
[66]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);
[1835]352     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
[1834]353      FD_CLR(fh, &r);
354     }
[255]355/*
[66]356    } else {
357     streams_check(g_clients[i]->execed);
[255]358*/
[66]359    }
360   }
[0]361
[84]362   if ( FD_ISSET(fh, &e) ) {
[66]363    clients_delete(i);
[84]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++) {
[1611]372   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
[136]373   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
374    break;
[1611]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
[84]379   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[1611]380    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
[3579]381    if ( fh > -1 && FD_ISSET(fh, &r) ) {
[84]382     streams_check(g_clients[i]->streams[j]);
[66]383    }
[0]384   }
385  }
386 }
387
[498]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++) {
[607]397    if ( ! g_clients[j = streamless[i].id] )
398     continue;
399
400    if ( g_clients[j]->execed != -1 )
[601]401     continue;
402
[498]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
[71]423 ROAR_DBG("clients_check_all(void) = %i // have value", have);
424 return have;
[1480]425#else
426 return -1;
427#endif
[0]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;
[498]436 int rv = 0;
[3926]437 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
[0]438
[3910]439 _CHECK_CID(id);
440
[0]441 if ( g_clients[id]->fh == -1 )
442  return -1;
443
[1662]444 roar_connect_fh(&con, g_clients[id]->fh);
[0]445
[2517]446 switch (g_clients[id]->proto) {
447  case ROAR_PROTO_ROARAUDIO:
448    r = roar_recv_message(&con, &m, &data);
[0]449
[2517]450    if ( r == -1 ) { // should we drop the client?
451     clients_delete(id);
452     return -1;
453    }
[0]454
[2517]455    roar_debug_message_print(&m);
[0]456
[2517]457    oldcmd = m.cmd;
[0]458
[3926]459    if ( (r = command_exec(id, &m, &data, flags)) == -1 ) {
[2517]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);
[3928]474
475    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
476     clients_close(id, 1);
477
[2517]478   break;
[2546]479#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
[2791]480#ifdef ROAR_HAVE_H_ESD
[2523]481  case ROAR_PROTO_ESOUND:
482    rv = emul_esd_check_client(id, NULL);
483   break;
[2546]484#endif
485#endif
[3684]486#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
487  case ROAR_PROTO_RSOUND:
[3820]488    rv = emul_rsound_check_client(id, NULL);
[3825]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    }
[3684]496   break;
497#endif
[2517]498  default:
499    rv = -1;
[0]500 }
501
502 if ( data )
503  free(data);
504
[2517]505 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
[498]506 return rv;
[0]507}
508
509int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
510 int i;
[934]511// int fh;
512 int j;
[2715]513 int keep_going;
[0]514
515 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
516  if ( g_clients[i] == NULL )
517   continue;
518
[2715]519  keep_going = 1;
520
[934]521/*
[0]522  if ( (fh = g_clients[i]->fh) == -1 )
523   continue;
[934]524*/
[0]525
[1905]526  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
527
[2706]528/*
[0]529  if ( g_clients[i]->execed == -1 ) {
530   // TODO: add some code to send a message to the client insetd of the raw data.
[2706]531*/
[2715]532   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[934]533    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2715]534    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
[1905]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]);
[934]537     streams_send_mon(g_clients[i]->streams[j]);
[2715]538
539     // the client may be deleted here, check if it still exists:
540     if ( g_clients[i] == NULL )
541      keep_going = 0;
[1905]542    }
[934]543   }
[2706]544/*
[0]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  }
[2706]551*/
[0]552 }
553
[1902]554 // TODO: FIXME: should this really be -1?
[0]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;
[3920]584 int fh;
[0]585
[3910]586 _CHECK_CID(client);
587
588#if 0
[1901]589 if ( g_clients[client] == NULL ) {
590  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
[0]591  return -1;
[1901]592 }
[3910]593#endif
[0]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;
[3921]598   if ( streams_is_ready(stream) == 0 ) {
[3920]599    streams_set_fh(stream, g_clients[client]->fh);
600    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
601   } else {
[3921]602    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=%i", client, stream, fh);
[3925]603    if ( (fh = g_clients[client]->fh) != -1 ) {
604     close(fh);
605     g_clients[client]->fh = -1;
606    }
[3920]607   }
[2815]608   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
[0]609   return 0;
610  }
611 }
612
[1901]613 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
[0]614 return -1;
615}
616
[78]617int client_stream_set_fh (int client, int stream, int fh) {
618 int i;
619
[3910]620 _CHECK_CID(client);
[78]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
[0]632int client_stream_add    (int client, int stream) {
633 int i;
634
[3910]635 _CHECK_CID(client);
[0]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
[3910]651 _CHECK_CID(client);
[0]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
[767]671int client_stream_move   (int client, int stream) {
672 int old_client = streams_get_client(stream);
673
[771]674 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
675
[767]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
[0]683//ll
Note: See TracBrowser for help on using the repository browser.