source: roaraudio/roard/clients.c @ 1164:34acf1586649

Last change on this file since 1164:34acf1586649 was 1164:34acf1586649, checked in by phi, 15 years ago

solved double close bug

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