source: roaraudio/roard/clients.c @ 3234:33163b08ad79

Last change on this file since 3234:33163b08ad79 was 3063:955233719a84, checked in by phi, 14 years ago

use memory functions from libroar, not libc, fixed a small memory leak

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