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
RevLine 
[0]1//clients.c:
2
[668]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
[0]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 ) {
[3063]53   n = roar_mm_malloc(sizeof(struct roar_client));
[0]54   if ( n != NULL ) {
55    n->pid    = -1;
[437]56    n->uid    = -1;
57    n->gid    = -1;
[0]58    n->fh     = -1;
59
60    *n->name = 0;
61    *n->host = 0;
62
[2614]63    n->proto     = ROAR_PROTO_ROARAUDIO;
64    n->byteorder = ROAR_BYTEORDER_NETWORK;
[2517]65
[346]66    n->acl   = NULL;
67
[0]68    n->execed = -1;
69    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
70     n->streams[s] = -1;
71
[2815]72    if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) {
[3063]73     roar_mm_free(n);
[2815]74     return -1;
75    }
76
[0]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;
[1164]94 int close_client_fh = 1;
[0]95
[2608]96 ROAR_DBG("clients_delete(id=%i) = ?", id);
97
[0]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;
[1164]104  close_client_fh = 0;
[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
[1164]111 if ( g_clients[id]->fh != -1 && close_client_fh )
[0]112  close(g_clients[id]->fh);
113
[2815]114 roar_nnode_free(&(g_clients[id]->nnode));
115
[3063]116 roar_mm_free(g_clients[id]);
[0]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) {
[501]133
[0]134 if ( g_clients[id] == NULL )
135  return -1;
136
137 g_clients[id]->fh = fh;
138
139 return 0;
140}
141
[755]142int clients_get_fh    (int id) {
143 if ( g_clients[id] == NULL )
144  return -1;
145
146 return g_clients[id]->fh;
147}
148
[0]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
[439]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
[2517]176int clients_set_proto (int id, int    proto) {
[2614]177 int byteorder = ROAR_BYTEORDER_UNKNOWN;
178
[2517]179 if ( g_clients[id] == NULL )
180  return -1;
181
[2614]182 switch (proto) {
183  case ROAR_PROTO_ROARAUDIO:
[2828]184  case ROAR_PROTO_ESOUND:
[2614]185    byteorder = ROAR_BYTEORDER_NETWORK;
186   break;
187 }
188
189 g_clients[id]->proto     = proto;
190 g_clients[id]->byteorder = byteorder;
[2517]191
192 return 0;
193}
194
[498]195#define MAX_STREAMLESS 8
196
[0]197int clients_check_all (void) {
[1480]198#ifdef ROAR_HAVE_SELECT
[0]199 struct timeval tv;
200 fd_set r, e;
[66]201 int i, j;
[0]202 int ret;
203 int fh;
204 int max_fh = -1;
[71]205 int have = 0;
[498]206 struct {
207  int id;
208  int fh;
209 } streamless[MAX_STREAMLESS];
210 int have_streamless = 0;
211 int have_stream;
[0]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
[66]223  if ( (fh = g_clients[i]->fh) != -1 ) {
[71]224   have++;
225
[610]226   ROAR_DBG("clients_check_all(*): fh=%i", fh);
227
[66]228   FD_SET(fh, &r);
229   FD_SET(fh, &e);
230
231   if ( fh > max_fh )
232    max_fh = fh;
[84]233  }
[0]234
[498]235  have_stream = 0;
236
[84]237  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
238   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2598]239    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
[1504]240    if ( fh > -1 ) {
241     FD_SET(fh, &r);
[0]242
[1504]243     if ( fh > max_fh )
244      max_fh = fh;
245    }
[498]246
247    have_stream = 1;
[66]248   }
[84]249   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
[66]250  }
251
[498]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  }
[0]257 }
258
[256]259 if ( max_fh == -1 )
260  return 0;
261
[0]262 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
[71]263  return ret < 0 ? ret : have;
[0]264 }
265
266 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
267  if ( g_clients[i] == NULL )
268   continue;
269
[66]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);
[1835]274     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
[1834]275      FD_CLR(fh, &r);
276     }
[255]277/*
[66]278    } else {
279     streams_check(g_clients[i]->execed);
[255]280*/
[66]281    }
282   }
[0]283
[84]284   if ( FD_ISSET(fh, &e) ) {
[66]285    clients_delete(i);
[84]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++) {
[1611]294   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
[136]295   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
296    break;
[1611]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
[84]301   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[1611]302    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
[1504]303    if ( fh == -2 ) {
304     streams_check(g_clients[i]->streams[j]);
305    } else if ( FD_ISSET(fh, &r) ) {
[84]306     streams_check(g_clients[i]->streams[j]);
[66]307    }
[0]308   }
309  }
310 }
311
[498]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++) {
[607]321    if ( ! g_clients[j = streamless[i].id] )
322     continue;
323
324    if ( g_clients[j]->execed != -1 )
[601]325     continue;
326
[498]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
[71]347 ROAR_DBG("clients_check_all(void) = %i // have value", have);
348 return have;
[1480]349#else
350 return -1;
351#endif
[0]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;
[498]360 int rv = 0;
[0]361
362 if ( g_clients[id] == NULL )
363  return -1;
364 if ( g_clients[id]->fh == -1 )
365  return -1;
366
[1662]367 roar_connect_fh(&con, g_clients[id]->fh);
[0]368
[2517]369 switch (g_clients[id]->proto) {
370  case ROAR_PROTO_ROARAUDIO:
371    r = roar_recv_message(&con, &m, &data);
[0]372
[2517]373    if ( r == -1 ) { // should we drop the client?
374     clients_delete(id);
375     return -1;
376    }
[0]377
[2517]378    roar_debug_message_print(&m);
[0]379
[2517]380    oldcmd = m.cmd;
[0]381
[2517]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;
[2546]398#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
[2791]399#ifdef ROAR_HAVE_H_ESD
[2523]400  case ROAR_PROTO_ESOUND:
401    rv = emul_esd_check_client(id, NULL);
402   break;
[2546]403#endif
404#endif
[2517]405  default:
406    rv = -1;
[0]407 }
408
409 if ( data )
410  free(data);
411
[2517]412 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
[498]413 return rv;
[0]414}
415
416int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
417 int i;
[934]418// int fh;
419 int j;
[2715]420 int keep_going;
[0]421
422 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
423  if ( g_clients[i] == NULL )
424   continue;
425
[2715]426  keep_going = 1;
427
[934]428/*
[0]429  if ( (fh = g_clients[i]->fh) == -1 )
430   continue;
[934]431*/
[0]432
[1905]433  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
434
[2706]435/*
[0]436  if ( g_clients[i]->execed == -1 ) {
437   // TODO: add some code to send a message to the client insetd of the raw data.
[2706]438*/
[2715]439   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
[934]440    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
[2715]441    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
[1905]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]);
[934]444     streams_send_mon(g_clients[i]->streams[j]);
[2715]445
446     // the client may be deleted here, check if it still exists:
447     if ( g_clients[i] == NULL )
448      keep_going = 0;
[1905]449    }
[934]450   }
[2706]451/*
[0]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  }
[2706]458*/
[0]459 }
460
[1902]461 // TODO: FIXME: should this really be -1?
[0]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
[1901]492 if ( g_clients[client] == NULL ) {
493  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
[0]494  return -1;
[1901]495 }
[0]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);
[378]501   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
[2815]502   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
[0]503   return 0;
504  }
505 }
506
[1901]507 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
[0]508 return -1;
509}
510
[78]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
[0]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
[767]568int client_stream_move   (int client, int stream) {
569 int old_client = streams_get_client(stream);
570
[771]571 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
572
[767]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
[0]580//ll
Note: See TracBrowser for help on using the repository browser.