source: roaraudio/roard/clients.c @ 3255:970b81dc7c18

Last change on this file since 3255:970b81dc7c18 was 3255:970b81dc7c18, checked in by phi, 14 years ago

got PA simple protocol basicly to work

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