source: roaraudio/roard/clients.c @ 3737:e79803f4911d

Last change on this file since 3737:e79803f4911d was 3737:e79803f4911d, checked in by phi, 14 years ago

added clients_new_from_fh(), converted some code to use the function

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