source: roaraudio/roard/clients.c @ 4347:52d8fa5ac58e

Last change on this file since 4347:52d8fa5ac58e was 4347:52d8fa5ac58e, checked in by phi, 14 years ago

corrected emit for NOSEND commands...

File size: 18.6 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    ROAR_DBG("clients_check(*): data=%p", data);
508
509    if ( flags[1] & COMMAND_FLAG_OUT_NOSEND ) {
510     roar_notify_core_emit_simple(event, id, -1, -1, -1, -1, NULL, 0);
511    } else {
512     roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0);
513     roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL);
514    }
515
516    if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON )
517     clients_close(id, 1);
518
519   break;
520#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
521#ifdef ROAR_HAVE_H_ESD
522  case ROAR_PROTO_ESOUND:
523    rv = emul_esd_check_client(id, NULL);
524   break;
525#endif
526#endif
527#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
528  case ROAR_PROTO_RPLAY:
529    rv = emul_rplay_check_client(id, NULL);
530   break;
531#endif
532#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
533  case ROAR_PROTO_RSOUND:
534    rv = emul_rsound_check_client(id, NULL);
535    if ( rv == 0 ) { // loop as long as we don't get an error.
536     while (rv == 0)
537      rv = emul_rsound_check_client(id, NULL);
538     rv = 0; // restore
539    } else { // in case of error delete the client
540     if (
541#ifdef EAGAIN
542          errno != EAGAIN      &&
543#endif
544#ifdef EWOULDBLOCK
545          errno != EWOULDBLOCK &&
546#endif
547#ifdef EINTR
548          errno != EINTR       &&
549#endif
550          1 ) {
551      rv = clients_delete(id);
552     } else {
553      rv = 0;
554     }
555    }
556   break;
557#endif
558  default:
559    rv = -1;
560 }
561
562 if ( data != NULL )
563  free(data);
564
565 ROAR_DBG("clients_check(id=%i) = %i", id, rv);
566 return rv;
567}
568
569int clients_send_mon  (struct roar_audio_info * sa, uint32_t pos) {
570 int i;
571// int fh;
572 int j;
573 int keep_going;
574
575 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
576  if ( g_clients[i] == NULL )
577   continue;
578
579  keep_going = 1;
580
581/*
582  if ( (fh = g_clients[i]->fh) == -1 )
583   continue;
584*/
585
586  ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed);
587
588/*
589  if ( g_clients[i]->execed == -1 ) {
590   // TODO: add some code to send a message to the client insetd of the raw data.
591*/
592   for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) {
593    //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) {
594    ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j);
595    if ( ROAR_CLIENT(g_clients[i])->streams[j] != -1 ) {
596     ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, ROAR_CLIENT(g_clients[i])->streams[j]);
597     streams_send_mon(ROAR_CLIENT(g_clients[i])->streams[j]);
598
599     // the client may be deleted here, check if it still exists:
600     if ( g_clients[i] == NULL )
601      keep_going = 0;
602    }
603   }
604/*
605  } else {
606//   streams_check(g_clients[i]->execed);
607   streams_send_mon(g_clients[i]->execed);
608//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
609//    clients_delete(i); // delete client in case we could not write
610  }
611*/
612 }
613
614 // TODO: FIXME: should this really be -1?
615 return -1;
616}
617
618int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) {
619 struct roar_client * c;
620 int i;
621 int fh;
622
623 for (i = 0; i < ROAR_CLIENTS_MAX; i++) {
624  if ( (c = ROAR_CLIENT(g_clients[i])) == NULL )
625   continue;
626
627  if ( (fh = c->fh) == -1 )
628   continue;
629
630  if ( c->execed == -1 ) {
631   // TODO: add some code to send a message to the client insetd of the raw data.
632  } else {
633//   streams_check(g_clients[i]->execed);
634   streams_send_filter(c->execed);
635//   if ( streams_send_mon(g_clients[i]->execed) == -1 )
636//    clients_delete(i); // delete client in case we could not write
637  }
638 }
639
640 return -1;
641}
642
643int client_stream_exec   (int client, int stream) {
644 struct roar_client * c;
645 int i;
646 int fh;
647
648 _CHECK_CID(client);
649
650#if 0
651 if ( g_clients[client] == NULL ) {
652  ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream);
653  return -1;
654 }
655#endif
656
657 c = ROAR_CLIENT(g_clients[client]);
658
659 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
660  if ( c->streams[i] == stream ) {
661   c->execed = stream;
662   if ( streams_is_ready(stream) == 0 ) {
663    streams_set_fh(stream, c->fh);
664    streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR);
665   } else {
666    ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream);
667    if ( (fh = c->fh) != -1 ) {
668     close(fh);
669     c->fh = -1;
670    }
671   }
672   ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream);
673   return 0;
674  }
675 }
676
677 ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream);
678 return -1;
679}
680
681int client_stream_set_fh (int client, int stream, int fh) {
682 int i;
683
684 ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh);
685
686 _CHECK_CID(client);
687
688 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
689  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
690   ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i);
691   return streams_set_fh(stream, fh);
692  }
693 }
694
695 ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh);
696 return -1;
697}
698
699int client_stream_add    (int client, int stream) {
700 int i;
701
702 _CHECK_CID(client);
703
704 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
705  if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) {
706   ROAR_CLIENT(g_clients[client])->streams[i] = stream;
707   streams_set_client(stream, client);
708   return 0;
709  }
710 }
711
712 return -1;
713}
714
715int client_stream_delete (int client, int stream) {
716 int i;
717
718 _CHECK_CID(client);
719
720 for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) {
721  if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) {
722   ROAR_CLIENT(g_clients[client])->streams[i] = -1;
723
724   if ( stream == ROAR_CLIENT(g_clients[client])->execed ) {
725    ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream);
726    clients_delete(client);
727   }
728
729   ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream);
730   return 0;
731  }
732 }
733
734 ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream);
735 return -1;
736}
737
738int client_stream_move   (int client, int stream) {
739 int old_client = streams_get_client(stream);
740
741 ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client);
742
743 if ( old_client != -1 )
744  if ( client_stream_delete(old_client, stream) == -1 )
745   return -1;
746
747 return client_stream_add(client, stream);
748}
749
750
751// notify thingys
752int clients_wait    (int client, struct roar_event * events, size_t num) {
753 struct roar_client_server * cs;
754 size_t i, c;
755
756 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = ?", client, events, (long long unsigned int)num);
757
758 _CHECK_CID(client);
759
760 cs = g_clients[client];
761
762 if ( cs->waits != NULL )
763  return -1;
764
765 cs->waits = roar_mm_malloc((num+1) * sizeof(struct roar_subscriber *));
766
767 if ( cs->waits == NULL )
768  return -1;
769
770 if ( clients_block(client, 0) != 0 )
771  return -1;
772
773 for (i = 0; i < num; i++) {
774#if defined(DEBUG) && 0
775  dbg_notify_cb(NULL, &(events[i]), cs);
776#endif
777  cs->waits[i] = roar_notify_core_subscribe(NULL, &(events[i]), clients_ncb_wait, cs);
778  if ( cs->waits[i] == NULL ) {
779   for (c = 0; c < i; c++)
780    roar_notify_core_unsubscribe(NULL, cs->waits[c]);
781   roar_mm_free(cs->waits);
782   cs->waits = NULL;
783   clients_block(client, 1);
784   return -1;
785  }
786 }
787
788 cs->waits[num] = NULL;
789
790 ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = 0", client, events, (long long unsigned int)num);
791 return 0;
792}
793
794void clients_ncb_wait(struct roar_notify_core * core, struct roar_event * event, void * userdata) {
795 struct roar_client_server * cs = userdata;
796 struct roar_message m;
797 struct roar_connection con;
798 uint16_t * u16 = (uint16_t *) m.data;
799 size_t tmp;
800 size_t i;
801
802 ROAR_DBG("clients_ncb_wait(core=%p, event=%p, userdata=%p) = ?", core, event, userdata);
803
804 for (i = 0; cs->waits[i] != NULL; i++)
805  roar_notify_core_unsubscribe(NULL, cs->waits[i]);
806
807 roar_mm_free(cs->waits);
808 cs->waits = NULL;
809
810 // protocol depended handling...
811 memset(&m, 0, sizeof(m));
812 m.cmd = ROAR_CMD_OK;
813 u16[0] = ROAR_HOST2NET16(0); // Version
814 u16[1] = ROAR_HOST2NET16(0); // flags
815
816 tmp = sizeof(m.data) - 4;
817
818 roar_event_to_blob(event, m.data + 4, &tmp);
819
820 m.datalen = tmp + 4;
821
822 roar_connect_fh(&con, ROAR_CLIENT(cs)->fh);
823 roar_send_message(&con, &m, NULL);
824 // ...end of protocol depended handling.
825
826// clients_block(, 1);
827 // TODO: FIXME: bad hack...
828 cs->blockc--;
829}
830
831//ll
Note: See TracBrowser for help on using the repository browser.