source: roaraudio/roard/clients.c @ 498:0c066d1a1842

Last change on this file since 498:0c066d1a1842 was 498:0c066d1a1842, checked in by phi, 16 years ago

added simple code to speed up non-stream connections

File size: 8.3 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 if ( g_clients[id] == NULL )
98  return -1;
99
100 g_clients[id]->fh = fh;
101
102 return 0;
103}
104
105int clients_set_pid   (int id, int    pid) {
106 if ( g_clients[id] == NULL )
107  return -1;
108
109 g_clients[id]->pid = pid;
110
111 return 0;
112}
113
114int clients_set_uid   (int id, int    uid) {
115 if ( g_clients[id] == NULL )
116  return -1;
117
118 g_clients[id]->uid = uid;
119
120 return 0;
121}
122
123int clients_set_gid   (int id, int    gid) {
124 if ( g_clients[id] == NULL )
125  return -1;
126
127 g_clients[id]->gid = gid;
128
129 return 0;
130}
131
132#define MAX_STREAMLESS 8
133
134int clients_check_all (void) {
135 struct timeval tv;
136 fd_set r, e;
137 int i, j;
138 int ret;
139 int fh;
140 int max_fh = -1;
141 int have = 0;
142 struct {
143  int id;
144  int fh;
145 } streamless[MAX_STREAMLESS];
146 int have_streamless = 0;
147 int have_stream;
148
149 FD_ZERO(&r);
150 FD_ZERO(&e);
151
152 tv.tv_sec  = 0;
153 tv.tv_usec = 1;
154
155 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
156  if ( g_clients[i] == NULL )
157   continue;
158
159  if ( (fh = g_clients[i]->fh) != -1 ) {
160   have++;
161
162   FD_SET(fh, &r);
163   FD_SET(fh, &e);
164
165   if ( fh > max_fh )
166    max_fh = fh;
167  }
168
169  have_stream = 0;
170
171  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
172   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
173    FD_SET(fh, &r);
174
175    if ( fh > max_fh )
176     max_fh = fh;
177
178    have_stream = 1;
179   }
180   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
181  }
182
183  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
184   streamless[have_streamless  ].id = i;
185   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
186    have_streamless--;
187  }
188 }
189
190 if ( max_fh == -1 )
191  return 0;
192
193 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
194  return ret < 0 ? ret : have;
195 }
196
197 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
198  if ( g_clients[i] == NULL )
199   continue;
200
201  if ( (fh = g_clients[i]->fh) != -1 ) {
202   if ( FD_ISSET(fh, &r) ) {
203    if ( g_clients[i]->execed == -1 ) {
204     clients_check(i);
205/*
206    } else {
207     streams_check(g_clients[i]->execed);
208*/
209    }
210   }
211
212   if ( FD_ISSET(fh, &e) ) {
213    clients_delete(i);
214    continue;
215   }
216  }
217
218  if ( g_clients[i] == NULL )
219   continue;
220
221  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
222   //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]);
223   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
224    break;
225   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
226    if ( FD_ISSET(fh, &r) ) {
227     streams_check(g_clients[i]->streams[j]);
228    }
229   }
230  }
231 }
232
233 if ( have_streamless ) {
234   FD_ZERO(&r);
235
236   tv.tv_sec  = 0;
237   tv.tv_usec = 1;
238
239   max_fh = -1;
240
241   for (i = 0; i < have_streamless; i++) {
242    fh = streamless[i].fh;
243
244    ROAR_DBG("clients_check_all(void): fh=%i", fh);
245    FD_SET(fh, &r);
246
247    if ( fh > max_fh )
248     max_fh = fh;
249   }
250
251   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
252    return ret;
253   }
254
255   for (i = 0; i < have_streamless; i++) {
256    if ( FD_ISSET(streamless[i].fh, &r) ) {
257     clients_check(streamless[i].id);
258    }
259   }
260 }
261
262 ROAR_DBG("clients_check_all(void) = %i // have value", have);
263 return have;
264}
265
266int clients_check     (int id) {
267 struct roar_message    m;
268 struct roar_connection con;
269 char * data = NULL;
270 int oldcmd;
271 int r;
272 int rv = 0;
273
274 if ( g_clients[id] == NULL )
275  return -1;
276 if ( g_clients[id]->fh == -1 )
277  return -1;
278
279 con.fh = g_clients[id]->fh;
280
281 r = roar_recv_message(&con, &m, &data);
282
283 if ( r == -1 ) { // should we drop the client?
284  clients_delete(id);
285  return -1;
286 }
287
288 roar_debug_message_print(&m);
289
290 oldcmd = m.cmd;
291
292 if ( (r = command_exec(id, &m, data)) == -1 ) {
293  m.cmd     = ROAR_CMD_ERROR;
294  m.datalen = 0;
295  ROAR_DBG("clients_check(*): Exec of command faild!");
296 } else {
297  if ( m.cmd == oldcmd ) {
298   m.cmd     = ROAR_CMD_OK;
299   m.datalen = 0;
300  } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
301   m.cmd     = ROAR_CMD_OK;
302   rv        = 1;
303  }
304 }
305
306 roar_send_message(&con, &m, NULL);
307
308 if ( data )
309  free(data);
310
311 ROAR_DBG("clients_check(id=%i) = 0", id);
312 return rv;
313}
314
315int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
316 int i;
317 int fh;
318
319 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
320  if ( g_clients[i] == NULL )
321   continue;
322
323  if ( (fh = g_clients[i]->fh) == -1 )
324   continue;
325
326  if ( g_clients[i]->execed == -1 ) {
327   // TODO: add some code to send a message to the client insetd of the raw data.
328  } else {
329//   streams_check(g_clients[i]->execed);
330   streams_send_mon(g_clients[i]->execed);
331//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
332//    clients_delete(i); // delete client in case we could not write
333  }
334 }
335
336 return -1;
337}
338
339int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
340 int i;
341 int fh;
342
343 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
344  if ( g_clients[i] == NULL )
345   continue;
346
347  if ( (fh = g_clients[i]->fh) == -1 )
348   continue;
349
350  if ( g_clients[i]->execed == -1 ) {
351   // TODO: add some code to send a message to the client insetd of the raw data.
352  } else {
353//   streams_check(g_clients[i]->execed);
354   streams_send_filter(g_clients[i]->execed);
355//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
356//    clients_delete(i); // delete client in case we could not write
357  }
358 }
359
360 return -1;
361}
362
363int client_stream_exec   (int client, int stream) {
364 int i;
365
366 if ( g_clients[client] == NULL )
367  return -1;
368
369 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
370  if ( g_clients[client]->streams[i] == stream ) {
371   g_clients[client]->execed = stream;
372   streams_set_fh(stream, g_clients[client]->fh);
373   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
374   return 0;
375  }
376 }
377
378 return -1;
379}
380
381int client_stream_set_fh (int client, int stream, int fh) {
382 int i;
383
384 if ( g_clients[client] == NULL )
385  return -1;
386
387 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
388  if ( g_clients[client]->streams[i] == stream ) {
389   streams_set_fh(stream, fh);
390   return 0;
391  }
392 }
393
394 return -1;
395}
396
397int client_stream_add    (int client, int stream) {
398 int i;
399
400 if ( g_clients[client] == NULL )
401  return -1;
402
403 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
404  if ( g_clients[client]->streams[i] == -1 ) {
405   g_clients[client]->streams[i] = stream;
406   streams_set_client(stream, client);
407   return 0;
408  }
409 }
410
411 return -1;
412}
413
414int client_stream_delete (int client, int stream) {
415 int i;
416
417 if ( g_clients[client] == NULL )
418  return -1;
419
420 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
421  if ( g_clients[client]->streams[i] == stream ) {
422   g_clients[client]->streams[i] = -1;
423
424   if ( stream == g_clients[client]->execed ) {
425    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
426    clients_delete(client);
427   }
428
429   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
430   return 0;
431  }
432 }
433
434 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
435 return -1;
436}
437
438//ll
Note: See TracBrowser for help on using the repository browser.