source: roaraudio/roard/clients.c @ 3684:69134bcda4c2

Last change on this file since 3684:69134bcda4c2 was 3684:69134bcda4c2, checked in by phi, 14 years ago

added most of the rsound interface

File size: 12.6 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#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
408  case ROAR_PROTO_RSOUND:
409    rv = clients_delete(id);
410   break;
411#endif
412  default:
413    rv = -1;
414 }
415
416 if ( data )
417  free(data);
418
419 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
420 return rv;
421}
422
423int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
424 int i;
425// int fh;
426 int j;
427 int keep_going;
428
429 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
430  if ( g_clients[i] == NULL )
431   continue;
432
433  keep_going = 1;
434
435/*
436  if ( (fh = g_clients[i]->fh) == -1 )
437   continue;
438*/
439
440  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
441
442/*
443  if ( g_clients[i]->execed == -1 ) {
444   // TODO: add some code to send a message to the client insetd of the raw data.
445*/
446   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
447    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
448    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
449    if ( g_clients[i]->streams[j] != -1 ) {
450     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
451     streams_send_mon(g_clients[i]->streams[j]);
452
453     // the client may be deleted here, check if it still exists:
454     if ( g_clients[i] == NULL )
455      keep_going = 0;
456    }
457   }
458/*
459  } else {
460//   streams_check(g_clients[i]->execed);
461   streams_send_mon(g_clients[i]->execed);
462//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
463//    clients_delete(i); // delete client in case we could not write
464  }
465*/
466 }
467
468 // TODO: FIXME: should this really be -1?
469 return -1;
470}
471
472int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
473 int i;
474 int fh;
475
476 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
477  if ( g_clients[i] == NULL )
478   continue;
479
480  if ( (fh = g_clients[i]->fh) == -1 )
481   continue;
482
483  if ( g_clients[i]->execed == -1 ) {
484   // TODO: add some code to send a message to the client insetd of the raw data.
485  } else {
486//   streams_check(g_clients[i]->execed);
487   streams_send_filter(g_clients[i]->execed);
488//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
489//    clients_delete(i); // delete client in case we could not write
490  }
491 }
492
493 return -1;
494}
495
496int client_stream_exec   (int client, int stream) {
497 int i;
498
499 if ( g_clients[client] == NULL ) {
500  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
501  return -1;
502 }
503
504 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
505  if ( g_clients[client]->streams[i] == stream ) {
506   g_clients[client]->execed = stream;
507   streams_set_fh(stream, g_clients[client]->fh);
508   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
509   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
510   return 0;
511  }
512 }
513
514 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
515 return -1;
516}
517
518int client_stream_set_fh (int client, int stream, int fh) {
519 int i;
520
521 if ( g_clients[client] == NULL )
522  return -1;
523
524 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
525  if ( g_clients[client]->streams[i] == stream ) {
526   streams_set_fh(stream, fh);
527   return 0;
528  }
529 }
530
531 return -1;
532}
533
534int client_stream_add    (int client, int stream) {
535 int i;
536
537 if ( g_clients[client] == NULL )
538  return -1;
539
540 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
541  if ( g_clients[client]->streams[i] == -1 ) {
542   g_clients[client]->streams[i] = stream;
543   streams_set_client(stream, client);
544   return 0;
545  }
546 }
547
548 return -1;
549}
550
551int client_stream_delete (int client, int stream) {
552 int i;
553
554 if ( g_clients[client] == NULL )
555  return -1;
556
557 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
558  if ( g_clients[client]->streams[i] == stream ) {
559   g_clients[client]->streams[i] = -1;
560
561   if ( stream == g_clients[client]->execed ) {
562    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
563    clients_delete(client);
564   }
565
566   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
567   return 0;
568  }
569 }
570
571 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
572 return -1;
573}
574
575int client_stream_move   (int client, int stream) {
576 int old_client = streams_get_client(stream);
577
578 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
579
580 if ( old_client != -1 )
581  if ( client_stream_delete(old_client, stream) == -1 )
582   return -1;
583
584 return client_stream_add(client, stream);
585}
586
587//ll
Note: See TracBrowser for help on using the repository browser.