source: roaraudio/roard/clients.c @ 601:dfbc7c9edbd9

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

fixed(?) a bug with execed streams being re-queued as streamless

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[streamless[i].id]->execed != -1 )
244     continue;
245
246    fh = streamless[i].fh;
247
248    ROAR_DBG("clients_check_all(void): fh=%i", fh);
249    FD_SET(fh, &r);
250
251    if ( fh > max_fh )
252     max_fh = fh;
253   }
254
255   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
256    return ret;
257   }
258
259   for (i = 0; i < have_streamless; i++) {
260    if ( FD_ISSET(streamless[i].fh, &r) ) {
261     clients_check(streamless[i].id);
262    }
263   }
264 }
265
266 ROAR_DBG("clients_check_all(void) = %i // have value", have);
267 return have;
268}
269
270int clients_check     (int id) {
271 struct roar_message    m;
272 struct roar_connection con;
273 char * data = NULL;
274 int oldcmd;
275 int r;
276 int rv = 0;
277
278 if ( g_clients[id] == NULL )
279  return -1;
280 if ( g_clients[id]->fh == -1 )
281  return -1;
282
283 con.fh = g_clients[id]->fh;
284
285 r = roar_recv_message(&con, &m, &data);
286
287 if ( r == -1 ) { // should we drop the client?
288  clients_delete(id);
289  return -1;
290 }
291
292 roar_debug_message_print(&m);
293
294 oldcmd = m.cmd;
295
296 if ( (r = command_exec(id, &m, data)) == -1 ) {
297  m.cmd     = ROAR_CMD_ERROR;
298  m.datalen = 0;
299  ROAR_DBG("clients_check(*): Exec of command faild!");
300 } else {
301  if ( m.cmd == oldcmd ) {
302   m.cmd     = ROAR_CMD_OK;
303   m.datalen = 0;
304  } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
305   m.cmd     = ROAR_CMD_OK;
306   rv        = 1;
307  }
308 }
309
310 roar_send_message(&con, &m, NULL);
311
312 if ( data )
313  free(data);
314
315 ROAR_DBG("clients_check(id=%i) = 0", id);
316 return rv;
317}
318
319int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
320 int i;
321 int fh;
322
323 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
324  if ( g_clients[i] == NULL )
325   continue;
326
327  if ( (fh = g_clients[i]->fh) == -1 )
328   continue;
329
330  if ( g_clients[i]->execed == -1 ) {
331   // TODO: add some code to send a message to the client insetd of the raw data.
332  } else {
333//   streams_check(g_clients[i]->execed);
334   streams_send_mon(g_clients[i]->execed);
335//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
336//    clients_delete(i); // delete client in case we could not write
337  }
338 }
339
340 return -1;
341}
342
343int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
344 int i;
345 int fh;
346
347 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
348  if ( g_clients[i] == NULL )
349   continue;
350
351  if ( (fh = g_clients[i]->fh) == -1 )
352   continue;
353
354  if ( g_clients[i]->execed == -1 ) {
355   // TODO: add some code to send a message to the client insetd of the raw data.
356  } else {
357//   streams_check(g_clients[i]->execed);
358   streams_send_filter(g_clients[i]->execed);
359//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
360//    clients_delete(i); // delete client in case we could not write
361  }
362 }
363
364 return -1;
365}
366
367int client_stream_exec   (int client, int stream) {
368 int i;
369
370 if ( g_clients[client] == NULL )
371  return -1;
372
373 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
374  if ( g_clients[client]->streams[i] == stream ) {
375   g_clients[client]->execed = stream;
376   streams_set_fh(stream, g_clients[client]->fh);
377   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
378   return 0;
379  }
380 }
381
382 return -1;
383}
384
385int client_stream_set_fh (int client, int stream, int fh) {
386 int i;
387
388 if ( g_clients[client] == NULL )
389  return -1;
390
391 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
392  if ( g_clients[client]->streams[i] == stream ) {
393   streams_set_fh(stream, fh);
394   return 0;
395  }
396 }
397
398 return -1;
399}
400
401int client_stream_add    (int client, int stream) {
402 int i;
403
404 if ( g_clients[client] == NULL )
405  return -1;
406
407 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
408  if ( g_clients[client]->streams[i] == -1 ) {
409   g_clients[client]->streams[i] = stream;
410   streams_set_client(stream, client);
411   return 0;
412  }
413 }
414
415 return -1;
416}
417
418int client_stream_delete (int client, int stream) {
419 int i;
420
421 if ( g_clients[client] == NULL )
422  return -1;
423
424 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
425  if ( g_clients[client]->streams[i] == stream ) {
426   g_clients[client]->streams[i] = -1;
427
428   if ( stream == g_clients[client]->execed ) {
429    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
430    clients_delete(client);
431   }
432
433   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
434   return 0;
435  }
436 }
437
438 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
439 return -1;
440}
441
442//ll
Note: See TracBrowser for help on using the repository browser.