[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 | |
---|
[3737] | 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 | |
---|
[0] | 122 | int clients_delete (int id) { |
---|
| 123 | int i; |
---|
[1164] | 124 | int close_client_fh = 1; |
---|
[0] | 125 | |
---|
[2608] | 126 | ROAR_DBG("clients_delete(id=%i) = ?", id); |
---|
| 127 | |
---|
[0] | 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; |
---|
[1164] | 134 | close_client_fh = 0; |
---|
[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 | |
---|
[1164] | 141 | if ( g_clients[id]->fh != -1 && close_client_fh ) |
---|
[0] | 142 | close(g_clients[id]->fh); |
---|
| 143 | |
---|
[2815] | 144 | roar_nnode_free(&(g_clients[id]->nnode)); |
---|
| 145 | |
---|
[3063] | 146 | roar_mm_free(g_clients[id]); |
---|
[0] | 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) { |
---|
[3713] | 163 | struct roar_client * c; |
---|
| 164 | #ifdef SO_PEERCRED |
---|
| 165 | struct ucred cred; |
---|
| 166 | socklen_t cred_len = sizeof(cred); |
---|
| 167 | #endif |
---|
[501] | 168 | |
---|
[3713] | 169 | if ( (c = g_clients[id]) == NULL ) |
---|
[0] | 170 | return -1; |
---|
| 171 | |
---|
[3713] | 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 |
---|
[0] | 189 | |
---|
| 190 | return 0; |
---|
| 191 | } |
---|
| 192 | |
---|
[755] | 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 | |
---|
[0] | 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 | |
---|
[439] | 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 | |
---|
[2517] | 227 | int clients_set_proto (int id, int proto) { |
---|
[2614] | 228 | int byteorder = ROAR_BYTEORDER_UNKNOWN; |
---|
| 229 | |
---|
[2517] | 230 | if ( g_clients[id] == NULL ) |
---|
| 231 | return -1; |
---|
| 232 | |
---|
[2614] | 233 | switch (proto) { |
---|
| 234 | case ROAR_PROTO_ROARAUDIO: |
---|
[2828] | 235 | case ROAR_PROTO_ESOUND: |
---|
[3255] | 236 | case ROAR_PROTO_SIMPLE: |
---|
[2614] | 237 | byteorder = ROAR_BYTEORDER_NETWORK; |
---|
| 238 | break; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | g_clients[id]->proto = proto; |
---|
| 242 | g_clients[id]->byteorder = byteorder; |
---|
[2517] | 243 | |
---|
| 244 | return 0; |
---|
| 245 | } |
---|
| 246 | |
---|
[498] | 247 | #define MAX_STREAMLESS 8 |
---|
| 248 | |
---|
[0] | 249 | int clients_check_all (void) { |
---|
[1480] | 250 | #ifdef ROAR_HAVE_SELECT |
---|
[0] | 251 | struct timeval tv; |
---|
| 252 | fd_set r, e; |
---|
[66] | 253 | int i, j; |
---|
[0] | 254 | int ret; |
---|
| 255 | int fh; |
---|
| 256 | int max_fh = -1; |
---|
[71] | 257 | int have = 0; |
---|
[498] | 258 | struct { |
---|
| 259 | int id; |
---|
| 260 | int fh; |
---|
| 261 | } streamless[MAX_STREAMLESS]; |
---|
| 262 | int have_streamless = 0; |
---|
| 263 | int have_stream; |
---|
[0] | 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 | |
---|
[66] | 275 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
[71] | 276 | have++; |
---|
| 277 | |
---|
[610] | 278 | ROAR_DBG("clients_check_all(*): fh=%i", fh); |
---|
| 279 | |
---|
[66] | 280 | FD_SET(fh, &r); |
---|
| 281 | FD_SET(fh, &e); |
---|
| 282 | |
---|
| 283 | if ( fh > max_fh ) |
---|
| 284 | max_fh = fh; |
---|
[84] | 285 | } |
---|
[0] | 286 | |
---|
[498] | 287 | have_stream = 0; |
---|
| 288 | |
---|
[84] | 289 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 290 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
[2598] | 291 | ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, g_clients[i]->streams[j], fh); |
---|
[1504] | 292 | if ( fh > -1 ) { |
---|
| 293 | FD_SET(fh, &r); |
---|
[0] | 294 | |
---|
[1504] | 295 | if ( fh > max_fh ) |
---|
| 296 | max_fh = fh; |
---|
[3579] | 297 | } else if ( fh == -2 ) { |
---|
| 298 | streams_check(g_clients[i]->streams[j]); |
---|
[1504] | 299 | } |
---|
[498] | 300 | |
---|
| 301 | have_stream = 1; |
---|
[66] | 302 | } |
---|
[84] | 303 | //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh); |
---|
[66] | 304 | } |
---|
| 305 | |
---|
[498] | 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 | } |
---|
[0] | 311 | } |
---|
| 312 | |
---|
[256] | 313 | if ( max_fh == -1 ) |
---|
| 314 | return 0; |
---|
| 315 | |
---|
[0] | 316 | if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) { |
---|
[71] | 317 | return ret < 0 ? ret : have; |
---|
[0] | 318 | } |
---|
| 319 | |
---|
| 320 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 321 | if ( g_clients[i] == NULL ) |
---|
| 322 | continue; |
---|
| 323 | |
---|
[66] | 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); |
---|
[1835] | 328 | if ( g_clients[i] != NULL && g_clients[i]->execed != -1 ) { |
---|
[1834] | 329 | FD_CLR(fh, &r); |
---|
| 330 | } |
---|
[255] | 331 | /* |
---|
[66] | 332 | } else { |
---|
| 333 | streams_check(g_clients[i]->execed); |
---|
[255] | 334 | */ |
---|
[66] | 335 | } |
---|
| 336 | } |
---|
[0] | 337 | |
---|
[84] | 338 | if ( FD_ISSET(fh, &e) ) { |
---|
[66] | 339 | clients_delete(i); |
---|
[84] | 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++) { |
---|
[1611] | 348 | ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]); |
---|
[136] | 349 | if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?) |
---|
| 350 | break; |
---|
[1611] | 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 | |
---|
[84] | 355 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
[1611] | 356 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh); |
---|
[3579] | 357 | if ( fh > -1 && FD_ISSET(fh, &r) ) { |
---|
[84] | 358 | streams_check(g_clients[i]->streams[j]); |
---|
[66] | 359 | } |
---|
[0] | 360 | } |
---|
| 361 | } |
---|
| 362 | } |
---|
| 363 | |
---|
[498] | 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++) { |
---|
[607] | 373 | if ( ! g_clients[j = streamless[i].id] ) |
---|
| 374 | continue; |
---|
| 375 | |
---|
| 376 | if ( g_clients[j]->execed != -1 ) |
---|
[601] | 377 | continue; |
---|
| 378 | |
---|
[498] | 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 | |
---|
[71] | 399 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
| 400 | return have; |
---|
[1480] | 401 | #else |
---|
| 402 | return -1; |
---|
| 403 | #endif |
---|
[0] | 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; |
---|
[498] | 412 | int rv = 0; |
---|
[0] | 413 | |
---|
| 414 | if ( g_clients[id] == NULL ) |
---|
| 415 | return -1; |
---|
| 416 | if ( g_clients[id]->fh == -1 ) |
---|
| 417 | return -1; |
---|
| 418 | |
---|
[1662] | 419 | roar_connect_fh(&con, g_clients[id]->fh); |
---|
[0] | 420 | |
---|
[2517] | 421 | switch (g_clients[id]->proto) { |
---|
| 422 | case ROAR_PROTO_ROARAUDIO: |
---|
| 423 | r = roar_recv_message(&con, &m, &data); |
---|
[0] | 424 | |
---|
[2517] | 425 | if ( r == -1 ) { // should we drop the client? |
---|
| 426 | clients_delete(id); |
---|
| 427 | return -1; |
---|
| 428 | } |
---|
[0] | 429 | |
---|
[2517] | 430 | roar_debug_message_print(&m); |
---|
[0] | 431 | |
---|
[2517] | 432 | oldcmd = m.cmd; |
---|
[0] | 433 | |
---|
[2517] | 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; |
---|
[2546] | 450 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD |
---|
[2791] | 451 | #ifdef ROAR_HAVE_H_ESD |
---|
[2523] | 452 | case ROAR_PROTO_ESOUND: |
---|
| 453 | rv = emul_esd_check_client(id, NULL); |
---|
| 454 | break; |
---|
[2546] | 455 | #endif |
---|
| 456 | #endif |
---|
[3684] | 457 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD |
---|
| 458 | case ROAR_PROTO_RSOUND: |
---|
| 459 | rv = clients_delete(id); |
---|
| 460 | break; |
---|
| 461 | #endif |
---|
[2517] | 462 | default: |
---|
| 463 | rv = -1; |
---|
[0] | 464 | } |
---|
| 465 | |
---|
| 466 | if ( data ) |
---|
| 467 | free(data); |
---|
| 468 | |
---|
[2517] | 469 | ROAR_DBG("clients_check(id=%i) = %i", id, rv); |
---|
[498] | 470 | return rv; |
---|
[0] | 471 | } |
---|
| 472 | |
---|
| 473 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
| 474 | int i; |
---|
[934] | 475 | // int fh; |
---|
| 476 | int j; |
---|
[2715] | 477 | int keep_going; |
---|
[0] | 478 | |
---|
| 479 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 480 | if ( g_clients[i] == NULL ) |
---|
| 481 | continue; |
---|
| 482 | |
---|
[2715] | 483 | keep_going = 1; |
---|
| 484 | |
---|
[934] | 485 | /* |
---|
[0] | 486 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 487 | continue; |
---|
[934] | 488 | */ |
---|
[0] | 489 | |
---|
[1905] | 490 | ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, g_clients[i]->execed); |
---|
| 491 | |
---|
[2706] | 492 | /* |
---|
[0] | 493 | if ( g_clients[i]->execed == -1 ) { |
---|
| 494 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
[2706] | 495 | */ |
---|
[2715] | 496 | for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
[934] | 497 | //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
[2715] | 498 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j); |
---|
[1905] | 499 | if ( g_clients[i]->streams[j] != -1 ) { |
---|
| 500 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, g_clients[i]->streams[j]); |
---|
[934] | 501 | streams_send_mon(g_clients[i]->streams[j]); |
---|
[2715] | 502 | |
---|
| 503 | // the client may be deleted here, check if it still exists: |
---|
| 504 | if ( g_clients[i] == NULL ) |
---|
| 505 | keep_going = 0; |
---|
[1905] | 506 | } |
---|
[934] | 507 | } |
---|
[2706] | 508 | /* |
---|
[0] | 509 | } else { |
---|
| 510 | // streams_check(g_clients[i]->execed); |
---|
| 511 | streams_send_mon(g_clients[i]->execed); |
---|
| 512 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 513 | // clients_delete(i); // delete client in case we could not write |
---|
| 514 | } |
---|
[2706] | 515 | */ |
---|
[0] | 516 | } |
---|
| 517 | |
---|
[1902] | 518 | // TODO: FIXME: should this really be -1? |
---|
[0] | 519 | return -1; |
---|
| 520 | } |
---|
| 521 | |
---|
| 522 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
| 523 | int i; |
---|
| 524 | int fh; |
---|
| 525 | |
---|
| 526 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 527 | if ( g_clients[i] == NULL ) |
---|
| 528 | continue; |
---|
| 529 | |
---|
| 530 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 531 | continue; |
---|
| 532 | |
---|
| 533 | if ( g_clients[i]->execed == -1 ) { |
---|
| 534 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 535 | } else { |
---|
| 536 | // streams_check(g_clients[i]->execed); |
---|
| 537 | streams_send_filter(g_clients[i]->execed); |
---|
| 538 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 539 | // clients_delete(i); // delete client in case we could not write |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | return -1; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | int client_stream_exec (int client, int stream) { |
---|
| 547 | int i; |
---|
| 548 | |
---|
[1901] | 549 | if ( g_clients[client] == NULL ) { |
---|
| 550 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream); |
---|
[0] | 551 | return -1; |
---|
[1901] | 552 | } |
---|
[0] | 553 | |
---|
| 554 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 555 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 556 | g_clients[client]->execed = stream; |
---|
| 557 | streams_set_fh(stream, g_clients[client]->fh); |
---|
[378] | 558 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR); |
---|
[2815] | 559 | ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream); |
---|
[0] | 560 | return 0; |
---|
| 561 | } |
---|
| 562 | } |
---|
| 563 | |
---|
[1901] | 564 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream); |
---|
[0] | 565 | return -1; |
---|
| 566 | } |
---|
| 567 | |
---|
[78] | 568 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
| 569 | int i; |
---|
| 570 | |
---|
| 571 | if ( g_clients[client] == NULL ) |
---|
| 572 | return -1; |
---|
| 573 | |
---|
| 574 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 575 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 576 | streams_set_fh(stream, fh); |
---|
| 577 | return 0; |
---|
| 578 | } |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | return -1; |
---|
| 582 | } |
---|
| 583 | |
---|
[0] | 584 | int client_stream_add (int client, int stream) { |
---|
| 585 | int i; |
---|
| 586 | |
---|
| 587 | if ( g_clients[client] == NULL ) |
---|
| 588 | return -1; |
---|
| 589 | |
---|
| 590 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 591 | if ( g_clients[client]->streams[i] == -1 ) { |
---|
| 592 | g_clients[client]->streams[i] = stream; |
---|
| 593 | streams_set_client(stream, client); |
---|
| 594 | return 0; |
---|
| 595 | } |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | return -1; |
---|
| 599 | } |
---|
| 600 | |
---|
| 601 | int client_stream_delete (int client, int stream) { |
---|
| 602 | int i; |
---|
| 603 | |
---|
| 604 | if ( g_clients[client] == NULL ) |
---|
| 605 | return -1; |
---|
| 606 | |
---|
| 607 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 608 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 609 | g_clients[client]->streams[i] = -1; |
---|
| 610 | |
---|
| 611 | if ( stream == g_clients[client]->execed ) { |
---|
| 612 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
| 613 | clients_delete(client); |
---|
| 614 | } |
---|
| 615 | |
---|
| 616 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
| 617 | return 0; |
---|
| 618 | } |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
| 622 | return -1; |
---|
| 623 | } |
---|
| 624 | |
---|
[767] | 625 | int client_stream_move (int client, int stream) { |
---|
| 626 | int old_client = streams_get_client(stream); |
---|
| 627 | |
---|
[771] | 628 | ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client); |
---|
| 629 | |
---|
[767] | 630 | if ( old_client != -1 ) |
---|
| 631 | if ( client_stream_delete(old_client, stream) == -1 ) |
---|
| 632 | return -1; |
---|
| 633 | |
---|
| 634 | return client_stream_add(client, stream); |
---|
| 635 | } |
---|
| 636 | |
---|
[0] | 637 | //ll |
---|