source: roaraudio/roard/clients.c @ 2715:efef3f8b01fb

Last change on this file since 2715:efef3f8b01fb was 2715:efef3f8b01fb, checked in by phi, 15 years ago

check if client got deleted

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