source: roaraudio/roard/clients.c @ 4346:0dbee113ccd0

Last change on this file since 4346:0dbee113ccd0 was 4346:0dbee113ccd0, checked in by phi, 14 years ago

added ROAR_OE_BASICS_NEW/ROAR_OE_BASICS_DELETE for clients and streams

File size: 18.5 KB
Line 
1//clients.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#define _CHECK_CID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_CLIENTS_MAX || g_clients[(id)] == NULL ) return (ret)
29#define _CHECK_CID(id)         _CHECK_CID_RET((id), -1)
30
31int clients_init (void) {
32 int i;
33
34 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
35  g_clients[i] = NULL;
36
37 return 0;
38}
39
40int clients_free (void) {
41 int i;
42
43 for (i = 0; i < ROAR_CLIENTS_MAX; i++)
44  if ( g_clients[i] != NULL )
45   clients_delete(i);
46
47 return 0;
48}
49
50int clients_new (void) {
51 int i;
52 int s;
53 struct roar_client_server * ns;
54 struct roar_client * n;
55
56 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
57  if ( g_clients[i] == NULL ) {
58   ns = roar_mm_malloc(sizeof(struct roar_client_server));
59   n = ROAR_CLIENT(ns);
60
61   memset(ns, 0, sizeof(struct roar_client_server));
62
63   if ( n != NULL ) {
64    n->pid    = -1;
65    n->uid    = -1;
66    n->gid    = -1;
67    n->fh     = -1;
68
69    *n->name = 0;
70    *n->host = 0;
71
72    n->proto     = ROAR_PROTO_ROARAUDIO;
73    n->byteorder = ROAR_BYTEORDER_NETWORK;
74
75    n->acl   = NULL;
76
77    n->execed = -1;
78    for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++)
79     n->streams[s] = -1;
80
81    if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) {
82     roar_mm_free(n);
83     return -1;
84    }
85
86    ns->blockc   = 0;
87    ns->waits    = NULL;
88
89    g_clients[i] = ns;
90
91    counters_inc(clients, 1);
92    roar_notify_core_emit_snoargs(ROAR_OE_BASICS_NEW, -1, i, ROAR_OT_CLIENT);
93    ROAR_DBG("clients_new(void) = %i", i);
94    return i;
95   } else {
96    ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno));
97    ROAR_ERR("clients_new(void) = -1");
98    return -1;
99   }
100  }
101 }
102
103 return -1;
104}
105
106int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) {
107 struct roar_client * c;
108 int client;
109
110 if ( fh == -1 )
111  return -1;
112
113 if ( proto != ROAR_PROTO_ROARAUDIO || byteorder != ROAR_BYTEORDER_NETWORK )
114  return -1;
115
116 if ( (client = clients_new()) == -1 )
117  return -1;
118
119 if ( clients_set_fh(client, fh) == -1 ) {
120  clients_delete(client);
121  return -1;
122 }
123
124 if ( update_nnode ) {
125  if ( clients_get(client, &c) != -1 ) {
126   if ( roar_nnode_free(&(c->nnode)) != -1 ) {
127    roar_nnode_new_from_fh(&(c->nnode), fh, 1);
128   }
129  }
130 }
131
132 return 0;
133}
134
135int clients_delete (int id) {
136 int i;
137 int close_client_fh = 1;
138
139 ROAR_DBG("clients_delete(id=%i) = ?", id);
140
141 roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_CLIENT);
142
143 counters_inc(clients, -1);
144
145 _CHECK_CID(id);
146
147 if (ROAR_CLIENT(g_clients[id])->execed != -1) {
148//  return streams_delete(g_clients[id]->execed);
149  ROAR_CLIENT(g_clients[id])->execed = -1;
150  close_client_fh = 0;
151 }
152
153 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
154  streams_delete(ROAR_CLIENT(g_clients[id])->streams[i]);
155 }
156
157 if ( ROAR_CLIENT(g_clients[id])->fh != -1 && close_client_fh )
158  close(ROAR_CLIENT(g_clients[id])->fh);
159
160 roar_nnode_free(&(ROAR_CLIENT(g_clients[id])->nnode));
161
162 roar_mm_free(g_clients[id]);
163 g_clients[id] = NULL;
164
165 ROAR_DBG("clients_delete(id=%i) = 0", id);
166 return 0;
167}
168
169int clients_close      (int id, int nocheck_exec) {
170 struct roar_client * c;
171
172 ROAR_DBG("clients_close(id=%i) = ?", id);
173
174 _CHECK_CID(id);
175
176 c = ROAR_CLIENT(g_clients[id]);
177
178 if ( c->fh == -1 ) {
179  ROAR_DBG("clients_delete(id=%i) = 0", id);
180  return 0;
181 }
182
183 if (nocheck_exec || c->execed != -1) {
184  close(c->fh);
185  c->fh = -1;
186 }
187
188 ROAR_DBG("clients_delete(id=%i) = 0", id);
189 return 0;
190}
191
192int clients_get       (int id, struct roar_client ** client) {
193 _CHECK_CID(id);
194
195 *client = ROAR_CLIENT(g_clients[id]);
196
197 if ( *client == NULL )
198  return -1;
199
200 return 0;
201}
202
203int clients_set_fh    (int id, int    fh) {
204 struct roar_client * c;
205#ifdef SO_PEERCRED
206 struct ucred cred;
207 socklen_t cred_len = sizeof(cred);
208#endif
209
210 _CHECK_CID(id);
211
212 if ( (c = ROAR_CLIENT(g_clients[id])) == NULL )
213  return -1;
214
215 c->fh = fh;
216
217#ifdef SO_PEERCRED
218 if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) {
219  if ( cred.pid != 0 ) {
220   c->pid = cred.pid;
221   c->uid = cred.uid;
222   c->gid = cred.gid;
223  }
224 } else {
225  ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno));
226 }
227#elif defined(ROAR_HAVE_GETPEEREID)
228 if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) {
229  ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno));
230 }
231#endif
232
233 return 0;
234}
235
236int clients_get_fh    (int id) {
237 _CHECK_CID(id);
238
239 return ROAR_CLIENT(g_clients[id])->fh;
240}
241
242int clients_set_pid   (int id, int    pid) {
243 _CHECK_CID(id);
244
245 ROAR_CLIENT(g_clients[id])->pid = pid;
246
247 return 0;
248}
249
250int clients_set_uid   (int id, int    uid) {
251 _CHECK_CID(id);
252
253 ROAR_CLIENT(g_clients[id])->uid = uid;
254
255 return 0;
256}
257
258int clients_set_gid   (int id, int    gid) {
259 _CHECK_CID(id);
260
261 ROAR_CLIENT(g_clients[id])->gid = gid;
262
263 return 0;
264}
265
266int clients_set_proto (int id, int    proto) {
267 int byteorder = ROAR_BYTEORDER_UNKNOWN;
268
269 _CHECK_CID(id);
270
271 switch (proto) {
272  case ROAR_PROTO_ROARAUDIO:
273  case ROAR_PROTO_ESOUND:
274  case ROAR_PROTO_RPLAY:
275  case ROAR_PROTO_SIMPLE:
276    byteorder = ROAR_BYTEORDER_NETWORK;
277   break;
278 }
279
280 ROAR_CLIENT(g_clients[id])->proto     = proto;
281 ROAR_CLIENT(g_clients[id])->byteorder = byteorder;
282
283 return 0;
284}
285
286int clients_block      (int id, int unblock) {
287 _CHECK_CID(id);
288
289 if ( unblock ) {
290  g_clients[id]->blockc--;
291 } else {
292  g_clients[id]->blockc++;
293 }
294
295 return 0;
296}
297
298
299#define MAX_STREAMLESS 8
300
301int clients_check_all (void) {
302#ifdef ROAR_HAVE_SELECT
303 struct roar_client * c;
304 struct timeval tv;
305 fd_set r, e;
306 int i, j;
307 int ret;
308 int fh;
309 int max_fh = -1;
310 int have = 0;
311 struct {
312  int id;
313  int fh;
314 } streamless[MAX_STREAMLESS];
315 int have_streamless = 0;
316 int have_stream;
317
318 FD_ZERO(&r);
319 FD_ZERO(&e);
320
321 tv.tv_sec  = 0;
322 tv.tv_usec = 1;
323
324 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
325  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
326   continue;
327
328  if ( (fh = c->fh) != -1 ) {
329   have++;
330
331   ROAR_DBG("clients_check_all(*): fh=%i", fh);
332
333   FD_SET(fh, &r);
334   FD_SET(fh, &e);
335
336   if ( fh > max_fh )
337    max_fh = fh;
338  }
339
340  have_stream = 0;
341
342  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
343   if ( (fh = streams_get_fh(c->streams[j])) != -1 ) {
344    ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, c->streams[j], fh);
345    if ( fh > -1 ) {
346     FD_SET(fh, &r);
347
348     if ( fh > max_fh )
349      max_fh = fh;
350    } else if ( fh == -2 ) {
351     streams_check(c->streams[j]);
352    }
353
354    have_stream = 1;
355   }
356   //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh);
357  }
358
359  if ( !have_stream && have_streamless < MAX_STREAMLESS ) {
360   streamless[have_streamless  ].id = i;
361   if ( (streamless[have_streamless++].fh = c->fh) == -1 )
362    have_streamless--;
363  }
364 }
365
366 if ( max_fh == -1 )
367  return 0;
368
369 if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) {
370  return ret < 0 ? ret : have;
371 }
372
373 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
374  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
375   continue;
376
377  if ( (fh = c->fh) != -1 ) {
378   if ( FD_ISSET(fh, &r) ) {
379    if ( c->execed == -1 ) {
380     clients_check(i);
381     if ( g_clients[i] != NULL && ROAR_CLIENT(g_clients[i])->execed != -1 ) {
382      FD_CLR(fh, &r);
383     }
384/*
385    } else {
386     streams_check(g_clients[i]->execed);
387*/
388    }
389   }
390
391   if ( FD_ISSET(fh, &e) ) {
392    clients_delete(i);
393    continue;
394   }
395  }
396
397  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
398   continue;
399
400  for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
401   ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]);
402   if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?)
403    break;
404
405   //ROAR_WARN("clients_check_all(*): client=%i: client exists", i);
406   ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, c->streams[j]);
407
408   if ( (fh = streams_get_fh(c->streams[j])) != -1 ) {
409    ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh);
410    if ( fh > -1 && FD_ISSET(fh, &r) ) {
411     streams_check(c->streams[j]);
412    }
413   }
414  }
415 }
416
417 if ( have_streamless ) {
418   FD_ZERO(&r);
419
420   tv.tv_sec  = 0;
421   tv.tv_usec = 1;
422
423   max_fh = -1;
424
425   for (i = 0; i < have_streamless; i++) {
426    if ( g_clients[j = streamless[i].id] == NULL )
427     continue;
428
429    if ( ROAR_CLIENT(g_clients[j])->execed != -1 )
430     continue;
431
432    fh = streamless[i].fh;
433
434    ROAR_DBG("clients_check_all(void): fh=%i", fh);
435    FD_SET(fh, &r);
436
437    if ( fh > max_fh )
438     max_fh = fh;
439   }
440
441   if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) {
442    return ret;
443   }
444
445   for (i = 0; i < have_streamless; i++) {
446    if ( FD_ISSET(streamless[i].fh, &r) ) {
447     clients_check(streamless[i].id);
448    }
449   }
450 }
451
452 ROAR_DBG("clients_check_all(void) = %i // have value", have);
453 return have;
454#else
455 return -1;
456#endif
457}
458
459int clients_check     (int id) {
460 struct roar_client   * c;
461 struct roar_message    m;
462 struct roar_connection con;
463 char * data = NULL;
464 int oldcmd;
465 int r;
466 int rv = 0;
467 uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE};
468 uint32_t event;
469
470 _CHECK_CID(id);
471
472 c = ROAR_CLIENT(g_clients[id]);
473
474 if ( c->fh == -1 )
475  return -1;
476
477 roar_connect_fh(&con, c->fh);
478
479 switch (c->proto) {
480  case ROAR_PROTO_ROARAUDIO:
481    r = roar_recv_message(&con, &m, &data);
482
483    if ( r == -1 ) { // should we drop the client?
484     clients_delete(id);
485     return -1;
486    }
487
488    event = ROAR_NOTIFY_CMD2EVENT(m.cmd);
489
490    roar_debug_message_print(&m);
491
492    oldcmd = m.cmd;
493
494    if ( (r = command_exec(id, &m, &data, flags)) == -1 ) {
495     m.cmd     = ROAR_CMD_ERROR;
496     m.datalen = 0;
497     ROAR_DBG("clients_check(*): Exec of command faild!");
498    } else {
499     if ( m.cmd == oldcmd ) {
500      m.cmd     = ROAR_CMD_OK;
501      m.datalen = 0;
502     } else if ( m.cmd == ROAR_CMD_OK_STOP ) {
503      m.cmd     = ROAR_CMD_OK;
504      rv        = 1;
505     }
506    }
507
508    roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0);
509
510    ROAR_DBG("clients_check(*): data=%p", data);
511
512    if ( !(flags[1] & COMMAND_FLAG_OUT_NOSEND) )
513     roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL);
514
515    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
516     clients_close(id, 1);
517
518   break;
519#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
520#ifdef ROAR_HAVE_H_ESD
521  case ROAR_PROTO_ESOUND:
522    rv = emul_esd_check_client(id, NULL);
523   break;
524#endif
525#endif
526#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
527  case ROAR_PROTO_RPLAY:
528    rv = emul_rplay_check_client(id, NULL);
529   break;
530#endif
531#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
532  case ROAR_PROTO_RSOUND:
533    rv = emul_rsound_check_client(id, NULL);
534    if ( rv == 0 ) { // loop as long as we don't get an error.
535     while (rv == 0)
536      rv = emul_rsound_check_client(id, NULL);
537     rv = 0; // restore
538    } else { // in case of error delete the client
539     if (
540#ifdef EAGAIN
541          errno != EAGAIN      &&
542#endif
543#ifdef EWOULDBLOCK
544          errno != EWOULDBLOCK &&
545#endif
546#ifdef EINTR
547          errno != EINTR       &&
548#endif
549          1 ) {
550      rv = clients_delete(id);
551     } else {
552      rv = 0;
553     }
554    }
555   break;
556#endif
557  default:
558    rv = -1;
559 }
560
561 if ( data != NULL )
562  free(data);
563
564 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
565 return rv;
566}
567
568int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
569 int i;
570// int fh;
571 int j;
572 int keep_going;
573
574 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
575  if ( g_clients[i] == NULL )
576   continue;
577
578  keep_going = 1;
579
580/*
581  if ( (fh = g_clients[i]->fh) == -1 )
582   continue;
583*/
584
585  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed);
586
587/*
588  if ( g_clients[i]->execed == -1 ) {
589   // TODO: add some code to send a message to the client insetd of the raw data.
590*/
591   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
592    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
593    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
594    if ( ROAR_CLIENT(g_clients[i])->streams[j] != -1 ) {
595     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, ROAR_CLIENT(g_clients[i])->streams[j]);
596     streams_send_mon(ROAR_CLIENT(g_clients[i])->streams[j]);
597
598     // the client may be deleted here, check if it still exists:
599     if ( g_clients[i] == NULL )
600      keep_going = 0;
601    }
602   }
603/*
604  } else {
605//   streams_check(g_clients[i]->execed);
606   streams_send_mon(g_clients[i]->execed);
607//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
608//    clients_delete(i); // delete client in case we could not write
609  }
610*/
611 }
612
613 // TODO: FIXME: should this really be -1?
614 return -1;
615}
616
617int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
618 struct roar_client * c;
619 int i;
620 int fh;
621
622 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
623  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
624   continue;
625
626  if ( (fh = c->fh) == -1 )
627   continue;
628
629  if ( c->execed == -1 ) {
630   // TODO: add some code to send a message to the client insetd of the raw data.
631  } else {
632//   streams_check(g_clients[i]->execed);
633   streams_send_filter(c->execed);
634//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
635//    clients_delete(i); // delete client in case we could not write
636  }
637 }
638
639 return -1;
640}
641
642int client_stream_exec   (int client, int stream) {
643 struct roar_client * c;
644 int i;
645 int fh;
646
647 _CHECK_CID(client);
648
649#if 0
650 if ( g_clients[client] == NULL ) {
651  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
652  return -1;
653 }
654#endif
655
656 c = ROAR_CLIENT(g_clients[client]);
657
658 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
659  if ( c->streams[i] == stream ) {
660   c->execed = stream;
661   if ( streams_is_ready(stream) == 0 ) {
662    streams_set_fh(stream, c->fh);
663    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
664   } else {
665    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream);
666    if ( (fh = c->fh) != -1 ) {
667     close(fh);
668     c->fh = -1;
669    }
670   }
671   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
672   return 0;
673  }
674 }
675
676 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
677 return -1;
678}
679
680int client_stream_set_fh (int client, int stream, int fh) {
681 int i;
682
683 ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh);
684
685 _CHECK_CID(client);
686
687 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
688  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
689   ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i);
690   return streams_set_fh(stream, fh);
691  }
692 }
693
694 ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh);
695 return -1;
696}
697
698int client_stream_add    (int client, int stream) {
699 int i;
700
701 _CHECK_CID(client);
702
703 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
704  if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) {
705   ROAR_CLIENT(g_clients[client])->streams[i] = stream;
706   streams_set_client(stream, client);
707   return 0;
708  }
709 }
710
711 return -1;
712}
713
714int client_stream_delete (int client, int stream) {
715 int i;
716
717 _CHECK_CID(client);
718
719 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
720  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
721   ROAR_CLIENT(g_clients[client])->streams[i] = -1;
722
723   if ( stream == ROAR_CLIENT(g_clients[client])->execed ) {
724    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
725    clients_delete(client);
726   }
727
728   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
729   return 0;
730  }
731 }
732
733 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
734 return -1;
735}
736
737int client_stream_move   (int client, int stream) {
738 int old_client = streams_get_client(stream);
739
740 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
741
742 if ( old_client != -1 )
743  if ( client_stream_delete(old_client, stream) == -1 )
744   return -1;
745
746 return client_stream_add(client, stream);
747}
748
749
750// notify thingys
751int clients_wait    (int client, struct roar_event * events, size_t num) {
752 struct roar_client_server * cs;
753 size_t i, c;
754
755 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = ?", client, events, (long long unsigned int)num);
756
757 _CHECK_CID(client);
758
759 cs = g_clients[client];
760
761 if ( cs->waits != NULL )
762  return -1;
763
764 cs->waits = roar_mm_malloc((num+1) * sizeof(struct roar_subscriber *));
765
766 if ( cs->waits == NULL )
767  return -1;
768
769 if ( clients_block(client, 0) != 0 )
770  return -1;
771
772 for (i = 0; i < num; i++) {
773#if defined(DEBUG) && 0
774  dbg_notify_cb(NULL, &(events[i]), cs);
775#endif
776  cs->waits[i] = roar_notify_core_subscribe(NULL, &(events[i]), clients_ncb_wait, cs);
777  if ( cs->waits[i] == NULL ) {
778   for (c = 0; c < i; c++)
779    roar_notify_core_unsubscribe(NULL, cs->waits[c]);
780   roar_mm_free(cs->waits);
781   cs->waits = NULL;
782   clients_block(client, 1);
783   return -1;
784  }
785 }
786
787 cs->waits[num] = NULL;
788
789 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = 0", client, events, (long long unsigned int)num);
790 return 0;
791}
792
793void clients_ncb_wait(struct roar_notify_core * core, struct roar_event * event, void * userdata) {
794 struct roar_client_server * cs = userdata;
795 struct roar_message m;
796 struct roar_connection con;
797 uint16_t * u16 = (uint16_t *) m.data;
798 size_t tmp;
799 size_t i;
800
801 ROAR_DBG("clients_ncb_wait(core=%p, event=%p, userdata=%p) = ?", core, event, userdata);
802
803 for (i = 0; cs->waits[i] != NULL; i++)
804  roar_notify_core_unsubscribe(NULL, cs->waits[i]);
805
806 roar_mm_free(cs->waits);
807 cs->waits = NULL;
808
809 // protocol depended handling...
810 memset(&m, 0, sizeof(m));
811 m.cmd = ROAR_CMD_OK;
812 u16[0] = ROAR_HOST2NET16(0); // Version
813 u16[1] = ROAR_HOST2NET16(0); // flags
814
815 tmp = sizeof(m.data) - 4;
816
817 roar_event_to_blob(event, m.data + 4, &tmp);
818
819 m.datalen = tmp + 4;
820
821 roar_connect_fh(&con, ROAR_CLIENT(cs)->fh);
822 roar_send_message(&con, &m, NULL);
823 // ...end of protocol depended handling.
824
825// clients_block(, 1);
826 // TODO: FIXME: bad hack...
827 cs->blockc--;
828}
829
830//ll
Note: See TracBrowser for help on using the repository browser.