source: roaraudio/roard/clients.c @ 2706:280ad02bb452

Last change on this file since 2706:280ad02bb452 was 2706:280ad02bb452, checked in by phi, 15 years ago

not only send data to execed stream but all streams of client

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