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
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
155int clients_get       (int id, struct roar_client ** client) {
[3910]156 _CHECK_CID(id);
157
[0]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) {
[3713]167 struct roar_client * c;
168#ifdef SO_PEERCRED
169 struct ucred cred;
170 socklen_t cred_len = sizeof(cred);
171#endif
[501]172
[3910]173 _CHECK_CID(id);
174
[3713]175 if ( (c = g_clients[id]) == NULL )
[0]176  return -1;
177
[3713]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
[0]195
196 return 0;
197}
198
[755]199int clients_get_fh    (int id) {
[3910]200 _CHECK_CID(id);
[755]201
202 return g_clients[id]->fh;
203}
204
[0]205int clients_set_pid   (int id, int    pid) {
[3910]206 _CHECK_CID(id);
[0]207
208 g_clients[id]->pid = pid;
209
210 return 0;
211}
212
[439]213int clients_set_uid   (int id, int    uid) {
[3910]214 _CHECK_CID(id);
[439]215
216 g_clients[id]->uid = uid;
217
218 return 0;
219}
220
221int clients_set_gid   (int id, int    gid) {
[3910]222 _CHECK_CID(id);
[439]223
224 g_clients[id]->gid = gid;
225
226 return 0;
227}
228
[2517]229int clients_set_proto (int id, int    proto) {
[2614]230 int byteorder = ROAR_BYTEORDER_UNKNOWN;
231
[3910]232 _CHECK_CID(id);
[2517]233
[2614]234 switch (proto) {
235  case ROAR_PROTO_ROARAUDIO:
[2828]236  case ROAR_PROTO_ESOUND:
[3255]237  case ROAR_PROTO_SIMPLE:
[2614]238    byteorder = ROAR_BYTEORDER_NETWORK;
239   break;
240 }
241
242 g_clients[id]->proto     = proto;
243 g_clients[id]->byteorder = byteorder;
[2517]244
245 return 0;
246}
247
[498]248#define MAX_STREAMLESS 8
249
[0]250int clients_check_all (void) {
[1480]251#ifdef ROAR_HAVE_SELECT
[0]252 struct timeval tv;
253 fd_set r, e;
[66]254 int i, j;
[0]255 int ret;
256 int fh;
257 int max_fh = -1;
[71]258 int have = 0;
[498]259 struct {
260  int id;
261  int fh;
262 } streamless[MAX_STREAMLESS];
263 int have_streamless = 0;
264 int have_stream;
[0]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
[66]276  if ( (fh = g_clients[i]->fh) != -1 ) {
[71]277   have++;
278
[610]279   ROAR_DBG("clients_check_all(*): fh=%i", fh);
280
[66]281   FD_SET(fh, &r);
282   FD_SET(fh, &e);
283
284   if ( fh > max_fh )
285    max_fh = fh;
[84]286  }
[0]287
[498]288  have_stream = 0;
289
[84]290  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
291   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2598]292    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
[1504]293    if ( fh > -1 ) {
294     FD_SET(fh, &r);
[0]295
[1504]296     if ( fh > max_fh )
297      max_fh = fh;
[3579]298    } else if ( fh == -2 ) {
299     streams_check(g_clients[i]->streams[j]);
[1504]300    }
[498]301
302    have_stream = 1;
[66]303   }
[84]304   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
[66]305  }
306
[498]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  }
[0]312 }
313
[256]314 if ( max_fh == -1 )
315  return 0;
316
[0]317 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
[71]318  return ret < 0 ? ret : have;
[0]319 }
320
321 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
322  if ( g_clients[i] == NULL )
323   continue;
324
[66]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);
[1835]329     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
[1834]330      FD_CLR(fh, &r);
331     }
[255]332/*
[66]333    } else {
334     streams_check(g_clients[i]->execed);
[255]335*/
[66]336    }
337   }
[0]338
[84]339   if ( FD_ISSET(fh, &e) ) {
[66]340    clients_delete(i);
[84]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++) {
[1611]349   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
[136]350   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
351    break;
[1611]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
[84]356   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[1611]357    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
[3579]358    if ( fh > -1 && FD_ISSET(fh, &r) ) {
[84]359     streams_check(g_clients[i]->streams[j]);
[66]360    }
[0]361   }
362  }
363 }
364
[498]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++) {
[607]374    if ( ! g_clients[j = streamless[i].id] )
375     continue;
376
377    if ( g_clients[j]->execed != -1 )
[601]378     continue;
379
[498]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
[71]400 ROAR_DBG("clients_check_all(void) = %i // have value", have);
401 return have;
[1480]402#else
403 return -1;
404#endif
[0]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;
[498]413 int rv = 0;
[0]414
[3910]415 _CHECK_CID(id);
416
[0]417 if ( g_clients[id]->fh == -1 )
418  return -1;
419
[1662]420 roar_connect_fh(&con, g_clients[id]->fh);
[0]421
[2517]422 switch (g_clients[id]->proto) {
423  case ROAR_PROTO_ROARAUDIO:
424    r = roar_recv_message(&con, &m, &data);
[0]425
[2517]426    if ( r == -1 ) { // should we drop the client?
427     clients_delete(id);
428     return -1;
429    }
[0]430
[2517]431    roar_debug_message_print(&m);
[0]432
[2517]433    oldcmd = m.cmd;
[0]434
[2517]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;
[2546]451#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
[2791]452#ifdef ROAR_HAVE_H_ESD
[2523]453  case ROAR_PROTO_ESOUND:
454    rv = emul_esd_check_client(id, NULL);
455   break;
[2546]456#endif
457#endif
[3684]458#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
459  case ROAR_PROTO_RSOUND:
[3820]460    rv = emul_rsound_check_client(id, NULL);
[3825]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    }
[3684]468   break;
469#endif
[2517]470  default:
471    rv = -1;
[0]472 }
473
474 if ( data )
475  free(data);
476
[2517]477 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
[498]478 return rv;
[0]479}
480
481int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
482 int i;
[934]483// int fh;
484 int j;
[2715]485 int keep_going;
[0]486
487 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
488  if ( g_clients[i] == NULL )
489   continue;
490
[2715]491  keep_going = 1;
492
[934]493/*
[0]494  if ( (fh = g_clients[i]->fh) == -1 )
495   continue;
[934]496*/
[0]497
[1905]498  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
499
[2706]500/*
[0]501  if ( g_clients[i]->execed == -1 ) {
502   // TODO: add some code to send a message to the client insetd of the raw data.
[2706]503*/
[2715]504   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[934]505    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2715]506    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
[1905]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]);
[934]509     streams_send_mon(g_clients[i]->streams[j]);
[2715]510
511     // the client may be deleted here, check if it still exists:
512     if ( g_clients[i] == NULL )
513      keep_going = 0;
[1905]514    }
[934]515   }
[2706]516/*
[0]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  }
[2706]523*/
[0]524 }
525
[1902]526 // TODO: FIXME: should this really be -1?
[0]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;
[3920]556 int fh;
[0]557
[3910]558 _CHECK_CID(client);
559
560#if 0
[1901]561 if ( g_clients[client] == NULL ) {
562  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
[0]563  return -1;
[1901]564 }
[3910]565#endif
[0]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;
[3920]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   }
[2815]577   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
[0]578   return 0;
579  }
580 }
581
[1901]582 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
[0]583 return -1;
584}
585
[78]586int client_stream_set_fh (int client, int stream, int fh) {
587 int i;
588
[3910]589 _CHECK_CID(client);
[78]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
[0]601int client_stream_add    (int client, int stream) {
602 int i;
603
[3910]604 _CHECK_CID(client);
[0]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
[3910]620 _CHECK_CID(client);
[0]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
[767]640int client_stream_move   (int client, int stream) {
641 int old_client = streams_get_client(stream);
642
[771]643 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
644
[767]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
[0]652//ll
Note: See TracBrowser for help on using the repository browser.