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