source: roaraudio/roard/clients.c @ 4343:a67cbb88fbe0

Last change on this file since 4343:a67cbb88fbe0 was 4343:a67cbb88fbe0, checked in by phi, 14 years ago

added support for wait command

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