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