source: roaraudio/roard/clients.c @ 2614:11dce7d724c4

Last change on this file since 2614:11dce7d724c4 was 2614:11dce7d724c4, checked in by phi, 15 years ago

set byte order on server side

File size: 11.9 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 }
180
181 g_clients[id]->proto     = proto;
182 g_clients[id]->byteorder = byteorder;
183
184 return 0;
185}
186
187#define MAX_STREAMLESS 8
188
189int clients_check_all (void) {
190#ifdef ROAR_HAVE_SELECT
191 struct timeval tv;
192 fd_set r, e;
193 int i, j;
194 int ret;
195 int fh;
196 int max_fh = -1;
197 int have = 0;
198 struct {
199  int id;
200  int fh;
201 } streamless[MAX_STREAMLESS];
202 int have_streamless = 0;
203 int have_stream;
204
205 FD_ZERO(&r);
206 FD_ZERO(&e);
207
208 tv.tv_sec  = 0;
209 tv.tv_usec = 1;
210
211 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
212  if ( g_clients[i] == NULL )
213   continue;
214
215  if ( (fh = g_clients[i]->fh) != -1 ) {
216   have++;
217
218   ROAR_DBG("clients_check_all(*): fh=%i", fh);
219
220   FD_SET(fh, &r);
221   FD_SET(fh, &e);
222
223   if ( fh > max_fh )
224    max_fh = fh;
225  }
226
227  have_stream = 0;
228
229  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
230   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
231    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh);
232    if ( fh > -1 ) {
233     FD_SET(fh, &r);
234
235     if ( fh > max_fh )
236      max_fh = fh;
237    }
238
239    have_stream = 1;
240   }
241   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
242  }
243
244  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
245   streamless[have_streamless  ].id = i;
246   if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 )
247    have_streamless--;
248  }
249 }
250
251 if ( max_fh == -1 )
252  return 0;
253
254 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
255  return ret < 0 ? ret : have;
256 }
257
258 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
259  if ( g_clients[i] == NULL )
260   continue;
261
262  if ( (fh = g_clients[i]->fh) != -1 ) {
263   if ( FD_ISSET(fh, &r) ) {
264    if ( g_clients[i]->execed == -1 ) {
265     clients_check(i);
266     if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) {
267      FD_CLR(fh, &r);
268     }
269/*
270    } else {
271     streams_check(g_clients[i]->execed);
272*/
273    }
274   }
275
276   if ( FD_ISSET(fh, &e) ) {
277    clients_delete(i);
278    continue;
279   }
280  }
281
282  if ( g_clients[i] == NULL )
283   continue;
284
285  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
286   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
287   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
288    break;
289
290   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
291   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, g_clients[i]->streams[j]);
292
293   if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
294    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
295    if ( fh == -2 ) {
296     streams_check(g_clients[i]->streams[j]);
297    } else if ( FD_ISSET(fh, &r) ) {
298     streams_check(g_clients[i]->streams[j]);
299    }
300   }
301  }
302 }
303
304 if ( have_streamless ) {
305   FD_ZERO(&r);
306
307   tv.tv_sec  = 0;
308   tv.tv_usec = 1;
309
310   max_fh = -1;
311
312   for (i = 0; i < have_streamless; i++) {
313    if ( ! g_clients[j = streamless[i].id] )
314     continue;
315
316    if ( g_clients[j]->execed != -1 )
317     continue;
318
319    fh = streamless[i].fh;
320
321    ROAR_DBG("clients_check_all(void): fh=%i", fh);
322    FD_SET(fh, &r);
323
324    if ( fh > max_fh )
325     max_fh = fh;
326   }
327
328   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
329    return ret;
330   }
331
332   for (i = 0; i < have_streamless; i++) {
333    if ( FD_ISSET(streamless[i].fh, &r) ) {
334     clients_check(streamless[i].id);
335    }
336   }
337 }
338
339 ROAR_DBG("clients_check_all(void) = %i // have value", have);
340 return have;
341#else
342 return -1;
343#endif
344}
345
346int clients_check     (int id) {
347 struct roar_message    m;
348 struct roar_connection con;
349 char * data = NULL;
350 int oldcmd;
351 int r;
352 int rv = 0;
353
354 if ( g_clients[id] == NULL )
355  return -1;
356 if ( g_clients[id]->fh == -1 )
357  return -1;
358
359 roar_connect_fh(&con, g_clients[id]->fh);
360
361 switch (g_clients[id]->proto) {
362  case ROAR_PROTO_ROARAUDIO:
363    r = roar_recv_message(&con, &m, &data);
364
365    if ( r == -1 ) { // should we drop the client?
366     clients_delete(id);
367     return -1;
368    }
369
370    roar_debug_message_print(&m);
371
372    oldcmd = m.cmd;
373
374    if ( (r = command_exec(id, &m, data)) == -1 ) {
375     m.cmd     = ROAR_CMD_ERROR;
376     m.datalen = 0;
377     ROAR_DBG("clients_check(*): Exec of command faild!");
378    } else {
379     if ( m.cmd == oldcmd ) {
380      m.cmd     = ROAR_CMD_OK;
381      m.datalen = 0;
382     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
383      m.cmd     = ROAR_CMD_OK;
384      rv        = 1;
385     }
386    }
387
388    roar_send_message(&con, &m, NULL);
389   break;
390#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
391#ifdef ROAR_HAVE_ESD
392  case ROAR_PROTO_ESOUND:
393    rv = emul_esd_check_client(id, NULL);
394   break;
395#endif
396#endif
397  default:
398    rv = -1;
399 }
400
401 if ( data )
402  free(data);
403
404 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
405 return rv;
406}
407
408int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
409 int i;
410// int fh;
411 int j;
412
413 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
414  if ( g_clients[i] == NULL )
415   continue;
416
417/*
418  if ( (fh = g_clients[i]->fh) == -1 )
419   continue;
420*/
421
422  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed);
423
424  if ( g_clients[i]->execed == -1 ) {
425   // TODO: add some code to send a message to the client insetd of the raw data.
426   for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
427    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
428    if ( g_clients[i]->streams[j] != -1 ) {
429     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]);
430     streams_send_mon(g_clients[i]->streams[j]);
431    }
432   }
433  } else {
434//   streams_check(g_clients[i]->execed);
435   streams_send_mon(g_clients[i]->execed);
436//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
437//    clients_delete(i); // delete client in case we could not write
438  }
439 }
440
441 // TODO: FIXME: should this really be -1?
442 return -1;
443}
444
445int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
446 int i;
447 int fh;
448
449 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
450  if ( g_clients[i] == NULL )
451   continue;
452
453  if ( (fh = g_clients[i]->fh) == -1 )
454   continue;
455
456  if ( g_clients[i]->execed == -1 ) {
457   // TODO: add some code to send a message to the client insetd of the raw data.
458  } else {
459//   streams_check(g_clients[i]->execed);
460   streams_send_filter(g_clients[i]->execed);
461//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
462//    clients_delete(i); // delete client in case we could not write
463  }
464 }
465
466 return -1;
467}
468
469int client_stream_exec   (int client, int stream) {
470 int i;
471
472 if ( g_clients[client] == NULL ) {
473  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
474  return -1;
475 }
476
477 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
478  if ( g_clients[client]->streams[i] == stream ) {
479   g_clients[client]->execed = stream;
480   streams_set_fh(stream, g_clients[client]->fh);
481   streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
482   return 0;
483  }
484 }
485
486 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
487 return -1;
488}
489
490int client_stream_set_fh (int client, int stream, int fh) {
491 int i;
492
493 if ( g_clients[client] == NULL )
494  return -1;
495
496 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
497  if ( g_clients[client]->streams[i] == stream ) {
498   streams_set_fh(stream, fh);
499   return 0;
500  }
501 }
502
503 return -1;
504}
505
506int client_stream_add    (int client, int stream) {
507 int i;
508
509 if ( g_clients[client] == NULL )
510  return -1;
511
512 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
513  if ( g_clients[client]->streams[i] == -1 ) {
514   g_clients[client]->streams[i] = stream;
515   streams_set_client(stream, client);
516   return 0;
517  }
518 }
519
520 return -1;
521}
522
523int client_stream_delete (int client, int stream) {
524 int i;
525
526 if ( g_clients[client] == NULL )
527  return -1;
528
529 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
530  if ( g_clients[client]->streams[i] == stream ) {
531   g_clients[client]->streams[i] = -1;
532
533   if ( stream == g_clients[client]->execed ) {
534    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
535    clients_delete(client);
536   }
537
538   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
539   return 0;
540  }
541 }
542
543 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
544 return -1;
545}
546
547int client_stream_move   (int client, int stream) {
548 int old_client = streams_get_client(stream);
549
550 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
551
552 if ( old_client != -1 )
553  if ( client_stream_delete(old_client, stream) == -1 )
554   return -1;
555
556 return client_stream_add(client, stream);
557}
558
559//ll
Note: See TracBrowser for help on using the repository browser.