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