source: roaraudio/roard/clients.c @ 3579:2b241a01ce6b

Last change on this file since 3579:2b241a01ce6b was 3579:2b241a01ce6b, checked in by phi, 14 years ago

check full-vio streams as soon as we can

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