source: roaraudio/roard/clients.c @ 607:e49199a77993

Last change on this file since 607:e49199a77993 was 607:e49199a77993, checked in by phi, 16 years ago

fixed deleted client in streamless list bug

File size: 8.4 KB
Line 
1//clients.c:
2
3#include "roard.h"
4
5int clients_init (void) {
6 int i;
7
8 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
9  g_clients[i] = NULL;
10
11 return 0;
12}
13
14int clients_free (void) {
15 int i;
16
17 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
18  if ( g_clients[i] )
19   clients_delete(i);
20
21 return 0;
22}
23
24int clients_new (void) {
25 int i;
26 int s;
27 struct roar_client * n;
28
29 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
30  if ( g_clients[i] == NULL ) {
31   n = malloc(sizeof(struct roar_client));
32   if ( n != NULL ) {
33    n->pid    = -1;
34    n->uid    = -1;
35    n->gid    = -1;
36    n->fh     = -1;
37
38    *n->name = 0;
39    *n->host = 0;
40
41    n->acl   = NULL;
42
43    n->execed = -1;
44    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
45     n->streams[s] = -1;
46
47    g_clients[i] = n;
48
49    ROAR_DBG("clients_new(void) = %i", i);
50    return i;
51   } else {
52    ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno));
53    ROAR_ERR("clients_new(void) = -1");
54    return -1;
55   }
56  }
57 }
58
59 return -1;
60}
61
62int clients_delete (int id) {
63 int i;
64
65 if ( g_clients[id] == NULL )
66  return -1;
67
68 if (g_clients[id]->execed != -1) {
69//  return streams_delete(g_clients[id]->execed);
70  g_clients[id]->execed = -1;
71 }
72
73 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
74  streams_delete(g_clients[id]->streams[i]);
75 }
76
77 if ( g_clients[id]->fh != -1 )
78  close(g_clients[id]->fh);
79
80 free(g_clients[id]);
81 g_clients[id] = NULL;
82
83 ROAR_DBG("clients_delete(id=%i) = 0", id);
84 return 0;
85}
86
87int clients_get       (int id, struct roar_client ** client) {
88 *client = g_clients[id];
89
90 if ( *client == NULL )
91  return -1;
92
93 return 0;
94}
95
96int clients_set_fh    (int id, int    fh) {
97
98 if ( g_clients[id] == NULL )
99  return -1;
100
101 g_clients[id]->fh = fh;
102
103 return 0;
104}
105
106int clients_set_pid   (int id, int    pid) {
107 if ( g_clients[id] == NULL )
108  return -1;
109
110 g_clients[id]->pid = pid;
111
112 return 0;
113}
114
115int clients_set_uid   (int id, int    uid) {
116 if ( g_clients[id] == NULL )
117  return -1;
118
119 g_clients[id]->uid = uid;
120
121 return 0;
122}
123
124int clients_set_gid   (int id, int    gid) {
125 if ( g_clients[id] == NULL )
126  return -1;
127
128 g_clients[id]->gid = gid;
129
130 return 0;
131}
132
133#define MAX_STREAMLESS 8
134
135int clients_check_all (void) {
136 struct timeval tv;
137 fd_set r, e;
138 int i, j;
139 int ret;
140 int fh;
141 int max_fh = -1;
142 int have = 0;
143 struct {
144  int id;
145  int fh;
146 } streamless[MAX_STREAMLESS];
147 int have_streamless = 0;
148 int have_stream;
149
150 FD_ZERO(&r);
151 FD_ZERO(&e);
152
153 tv.tv_sec  = 0;
154 tv.tv_usec = 1;
155
156 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
157  if ( g_clients[i] == NULL )
158   continue;
159
160  if ( (fh = g_clients[i]->fh) != -1 ) {
161   have++;
162
163   FD_SET(fh, &r);
164   FD_SET(fh, &e);
165
166   if ( fh > max_fh )
167    max_fh = fh;
168  }
169
170  have_stream = 0;
171
172  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
173   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
174    FD_SET(fh, &r);
175
176    if ( fh > max_fh )
177     max_fh = fh;
178
179    have_stream = 1;
180   }
181   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
182  }
183
184  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
185   streamless[have_streamless  ].id = i;
186   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
187    have_streamless--;
188  }
189 }
190
191 if ( max_fh == -1 )
192  return 0;
193
194 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
195  return ret < 0 ? ret : have;
196 }
197
198 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
199  if ( g_clients[i] == NULL )
200   continue;
201
202  if ( (fh = g_clients[i]->fh) != -1 ) {
203   if ( FD_ISSET(fh, &r) ) {
204    if ( g_clients[i]->execed == -1 ) {
205     clients_check(i);
206/*
207    } else {
208     streams_check(g_clients[i]->execed);
209*/
210    }
211   }
212
213   if ( FD_ISSET(fh, &e) ) {
214    clients_delete(i);
215    continue;
216   }
217  }
218
219  if ( g_clients[i] == NULL )
220   continue;
221
222  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
223   //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]);
224   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
225    break;
226   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
227    if ( FD_ISSET(fh, &r) ) {
228     streams_check(g_clients[i]->streams[j]);
229    }
230   }
231  }
232 }
233
234 if ( have_streamless ) {
235   FD_ZERO(&r);
236
237   tv.tv_sec  = 0;
238   tv.tv_usec = 1;
239
240   max_fh = -1;
241
242   for (i = 0; i < have_streamless; i++) {
243    if ( ! g_clients[j = streamless[i].id] )
244     continue;
245
246    if ( g_clients[j]->execed != -1 )
247     continue;
248
249    fh = streamless[i].fh;
250
251    ROAR_DBG("clients_check_all(void): fh=%i", fh);
252    FD_SET(fh, &r);
253
254    if ( fh > max_fh )
255     max_fh = fh;
256   }
257
258   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
259    return ret;
260   }
261
262   for (i = 0; i < have_streamless; i++) {
263    if ( FD_ISSET(streamless[i].fh, &r) ) {
264     clients_check(streamless[i].id);
265    }
266   }
267 }
268
269 ROAR_DBG("clients_check_all(void) = %i // have value", have);
270 return have;
271}
272
273int clients_check     (int id) {
274 struct roar_message    m;
275 struct roar_connection con;
276 char * data = NULL;
277 int oldcmd;
278 int r;
279 int rv = 0;
280
281 if ( g_clients[id] == NULL )
282  return -1;
283 if ( g_clients[id]->fh == -1 )
284  return -1;
285
286 con.fh = g_clients[id]->fh;
287
288 r = roar_recv_message(&con, &m, &data);
289
290 if ( r == -1 ) { // should we drop the client?
291  clients_delete(id);
292  return -1;
293 }
294
295 roar_debug_message_print(&m);
296
297 oldcmd = m.cmd;
298
299 if ( (r = command_exec(id, &m, data)) == -1 ) {
300  m.cmd     = ROAR_CMD_ERROR;
301  m.datalen = 0;
302  ROAR_DBG("clients_check(*): Exec of command faild!");
303 } else {
304  if ( m.cmd == oldcmd ) {
305   m.cmd     = ROAR_CMD_OK;
306   m.datalen = 0;
307  } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
308   m.cmd     = ROAR_CMD_OK;
309   rv        = 1;
310  }
311 }
312
313 roar_send_message(&con, &m, NULL);
314
315 if ( data )
316  free(data);
317
318 ROAR_DBG("clients_check(id=%i) = 0", id);
319 return rv;
320}
321
322int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
323 int i;
324 int fh;
325
326 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
327  if ( g_clients[i] == NULL )
328   continue;
329
330  if ( (fh = g_clients[i]->fh) == -1 )
331   continue;
332
333  if ( g_clients[i]->execed == -1 ) {
334   // TODO: add some code to send a message to the client insetd of the raw data.
335  } else {
336//   streams_check(g_clients[i]->execed);
337   streams_send_mon(g_clients[i]->execed);
338//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
339//    clients_delete(i); // delete client in case we could not write
340  }
341 }
342
343 return -1;
344}
345
346int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
347 int i;
348 int fh;
349
350 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
351  if ( g_clients[i] == NULL )
352   continue;
353
354  if ( (fh = g_clients[i]->fh) == -1 )
355   continue;
356
357  if ( g_clients[i]->execed == -1 ) {
358   // TODO: add some code to send a message to the client insetd of the raw data.
359  } else {
360//   streams_check(g_clients[i]->execed);
361   streams_send_filter(g_clients[i]->execed);
362//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
363//    clients_delete(i); // delete client in case we could not write
364  }
365 }
366
367 return -1;
368}
369
370int client_stream_exec   (int client, int stream) {
371 int i;
372
373 if ( g_clients[client] == NULL )
374  return -1;
375
376 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
377  if ( g_clients[client]->streams[i] == stream ) {
378   g_clients[client]->execed = stream;
379   streams_set_fh(stream, g_clients[client]->fh);
380   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
381   return 0;
382  }
383 }
384
385 return -1;
386}
387
388int client_stream_set_fh (int client, int stream, int fh) {
389 int i;
390
391 if ( g_clients[client] == NULL )
392  return -1;
393
394 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
395  if ( g_clients[client]->streams[i] == stream ) {
396   streams_set_fh(stream, fh);
397   return 0;
398  }
399 }
400
401 return -1;
402}
403
404int client_stream_add    (int client, int stream) {
405 int i;
406
407 if ( g_clients[client] == NULL )
408  return -1;
409
410 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
411  if ( g_clients[client]->streams[i] == -1 ) {
412   g_clients[client]->streams[i] = stream;
413   streams_set_client(stream, client);
414   return 0;
415  }
416 }
417
418 return -1;
419}
420
421int client_stream_delete (int client, int stream) {
422 int i;
423
424 if ( g_clients[client] == NULL )
425  return -1;
426
427 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
428  if ( g_clients[client]->streams[i] == stream ) {
429   g_clients[client]->streams[i] = -1;
430
431   if ( stream == g_clients[client]->execed ) {
432    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
433    clients_delete(client);
434   }
435
436   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
437   return 0;
438  }
439 }
440
441 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
442 return -1;
443}
444
445//ll
Note: See TracBrowser for help on using the repository browser.