[0] | 1 | //clients.c: |
---|
| 2 | |
---|
[668] | 3 | /* |
---|
[5381] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012 |
---|
[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 | |
---|
[4921] | 26 | /* ckport options: |
---|
[4923] | 27 | * ckport: ignore-symbol: roar_event_to_blob of target libroar0 |
---|
[4921] | 28 | */ |
---|
| 29 | |
---|
[0] | 30 | #include "roard.h" |
---|
| 31 | |
---|
[5192] | 32 | // declared 'extern' |
---|
| 33 | struct roar_client_server * g_clients[ROAR_CLIENTS_MAX]; |
---|
| 34 | |
---|
[5567] | 35 | static struct roard_proto_handle __protos[MAX_PROTOS] = { |
---|
| 36 | {.proto = ROAR_PROTO_ROARAUDIO, .lhandle = NULL, .type = ROARD_PROTO_TYPE_BUILDIN, |
---|
| 37 | .impl = {.buildin = 0}}, |
---|
| 38 | #if !defined(ROAR_WITHOUT_DCOMP_EMUL_ESD) && defined(ROAR_HAVE_H_ESD) |
---|
| 39 | {.proto = ROAR_PROTO_ESOUND, .lhandle = NULL, .type = ROARD_PROTO_TYPE_ROARDPROTO, |
---|
| 40 | .impl = {.roardproto = {ROAR_PROTO_ESOUND, ROAR_SUBSYS_WAVEFORM, "EsounD emulation", NULL, NULL, NULL, emul_esd_check_client, NULL, NULL}}}, |
---|
[4678] | 41 | #endif |
---|
| 42 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY |
---|
[5567] | 43 | {.proto = ROAR_PROTO_RPLAY, .lhandle = NULL, .type = ROARD_PROTO_TYPE_ROARDPROTO, |
---|
| 44 | .impl = {.roardproto = {ROAR_PROTO_RPLAY, ROAR_SUBSYS_WAVEFORM, "RPlay emulation", NULL, NULL, NULL, emul_rplay_check_client, NULL, NULL}}}, |
---|
[4678] | 45 | #endif |
---|
[4703] | 46 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_GOPHER |
---|
[5567] | 47 | {.proto = ROAR_PROTO_GOPHER, .lhandle = NULL, .type = ROARD_PROTO_TYPE_ROARDPROTO, |
---|
| 48 | .impl = {.roardproto = {ROAR_PROTO_GOPHER, ROAR_SUBSYS_WAVEFORM, "The Internet Gopher Protocol", NULL, NULL, NULL, emul_gopher_check_client, NULL, |
---|
| 49 | emul_gopher_flushed_client}}}, |
---|
[4703] | 50 | #endif |
---|
[5567] | 51 | {.proto = -1} |
---|
[4678] | 52 | }; |
---|
| 53 | |
---|
[3910] | 54 | #define _CHECK_CID_RET(id,ret) if ( (id) < 0 || (id) > ROAR_CLIENTS_MAX || g_clients[(id)] == NULL ) return (ret) |
---|
| 55 | #define _CHECK_CID(id) _CHECK_CID_RET((id), -1) |
---|
| 56 | |
---|
[0] | 57 | int clients_init (void) { |
---|
| 58 | int i; |
---|
| 59 | |
---|
| 60 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
| 61 | g_clients[i] = NULL; |
---|
| 62 | |
---|
[5567] | 63 | for (i = 0; __protos[i].proto != -1; i++); |
---|
[5275] | 64 | |
---|
| 65 | for (; i < MAX_PROTOS; i++) |
---|
[5567] | 66 | __protos[i].proto = -1; |
---|
[5275] | 67 | |
---|
[0] | 68 | return 0; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | int clients_free (void) { |
---|
| 72 | int i; |
---|
| 73 | |
---|
| 74 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
[3910] | 75 | if ( g_clients[i] != NULL ) |
---|
[0] | 76 | clients_delete(i); |
---|
| 77 | |
---|
| 78 | return 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | int clients_new (void) { |
---|
| 82 | int i; |
---|
| 83 | int s; |
---|
[4326] | 84 | struct roar_client_server * ns; |
---|
[0] | 85 | struct roar_client * n; |
---|
| 86 | |
---|
| 87 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 88 | if ( g_clients[i] == NULL ) { |
---|
[4326] | 89 | ns = roar_mm_malloc(sizeof(struct roar_client_server)); |
---|
| 90 | n = ROAR_CLIENT(ns); |
---|
| 91 | |
---|
| 92 | memset(ns, 0, sizeof(struct roar_client_server)); |
---|
| 93 | |
---|
[0] | 94 | if ( n != NULL ) { |
---|
| 95 | n->pid = -1; |
---|
[437] | 96 | n->uid = -1; |
---|
| 97 | n->gid = -1; |
---|
[0] | 98 | n->fh = -1; |
---|
| 99 | |
---|
| 100 | *n->name = 0; |
---|
| 101 | |
---|
[2614] | 102 | n->proto = ROAR_PROTO_ROARAUDIO; |
---|
| 103 | n->byteorder = ROAR_BYTEORDER_NETWORK; |
---|
[2517] | 104 | |
---|
[346] | 105 | n->acl = NULL; |
---|
| 106 | |
---|
[0] | 107 | n->execed = -1; |
---|
| 108 | for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++) |
---|
| 109 | n->streams[s] = -1; |
---|
| 110 | |
---|
[2815] | 111 | if ( roar_nnode_new(&(n->nnode), ROAR_SOCKET_TYPE_UNKNOWN) == -1 ) { |
---|
[3063] | 112 | roar_mm_free(n); |
---|
[2815] | 113 | return -1; |
---|
| 114 | } |
---|
| 115 | |
---|
[4343] | 116 | ns->blockc = 0; |
---|
| 117 | ns->waits = NULL; |
---|
[4468] | 118 | ns->acclev = ACCLEV_NONE; |
---|
[4343] | 119 | |
---|
[4326] | 120 | g_clients[i] = ns; |
---|
[0] | 121 | |
---|
[4101] | 122 | counters_inc(clients, 1); |
---|
[4346] | 123 | roar_notify_core_emit_snoargs(ROAR_OE_BASICS_NEW, -1, i, ROAR_OT_CLIENT); |
---|
[0] | 124 | ROAR_DBG("clients_new(void) = %i", i); |
---|
| 125 | return i; |
---|
| 126 | } else { |
---|
| 127 | ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno)); |
---|
| 128 | ROAR_ERR("clients_new(void) = -1"); |
---|
| 129 | return -1; |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | return -1; |
---|
| 135 | } |
---|
| 136 | |
---|
[3737] | 137 | int clients_new_from_fh(int fh, int proto, int byteorder, int update_nnode) { |
---|
| 138 | struct roar_client * c; |
---|
| 139 | int client; |
---|
| 140 | |
---|
| 141 | if ( fh == -1 ) |
---|
| 142 | return -1; |
---|
| 143 | |
---|
| 144 | if ( proto != ROAR_PROTO_ROARAUDIO || byteorder != ROAR_BYTEORDER_NETWORK ) |
---|
| 145 | return -1; |
---|
| 146 | |
---|
| 147 | if ( (client = clients_new()) == -1 ) |
---|
| 148 | return -1; |
---|
| 149 | |
---|
| 150 | if ( clients_set_fh(client, fh) == -1 ) { |
---|
| 151 | clients_delete(client); |
---|
| 152 | return -1; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | if ( update_nnode ) { |
---|
| 156 | if ( clients_get(client, &c) != -1 ) { |
---|
| 157 | if ( roar_nnode_free(&(c->nnode)) != -1 ) { |
---|
| 158 | roar_nnode_new_from_fh(&(c->nnode), fh, 1); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | return 0; |
---|
| 164 | } |
---|
| 165 | |
---|
[0] | 166 | int clients_delete (int id) { |
---|
[4394] | 167 | struct roar_client_server * cs; |
---|
[5567] | 168 | const struct roard_proto_handle * proto; |
---|
[0] | 169 | int i; |
---|
[1164] | 170 | int close_client_fh = 1; |
---|
[0] | 171 | |
---|
[2608] | 172 | ROAR_DBG("clients_delete(id=%i) = ?", id); |
---|
| 173 | |
---|
[4394] | 174 | _CHECK_CID(id); |
---|
| 175 | |
---|
| 176 | cs = g_clients[id]; |
---|
| 177 | |
---|
[5567] | 178 | proto = clients_get_protohandle(ROAR_CLIENT(cs)->proto); |
---|
| 179 | if ( proto != NULL ) { |
---|
| 180 | switch (proto->type) { |
---|
| 181 | case ROARD_PROTO_TYPE_BUILDIN: |
---|
| 182 | /* noop */ |
---|
| 183 | break; |
---|
| 184 | case ROARD_PROTO_TYPE_ROARDPROTO: |
---|
| 185 | if ( proto->impl.roardproto.delete_client != NULL ) |
---|
| 186 | proto->impl.roardproto.delete_client(id); |
---|
| 187 | break; |
---|
[5452] | 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
[4394] | 191 | if ( cs->waits != NULL ) { |
---|
| 192 | for (i = 0; cs->waits[i] != NULL; i++) |
---|
| 193 | roar_notify_core_unsubscribe(NULL, cs->waits[i]); |
---|
| 194 | |
---|
| 195 | roar_mm_free(cs->waits); |
---|
| 196 | cs->waits = NULL; |
---|
| 197 | } |
---|
| 198 | |
---|
[4346] | 199 | roar_notify_core_emit_snoargs(ROAR_OE_BASICS_DELETE, -1, id, ROAR_OT_CLIENT); |
---|
| 200 | |
---|
[4101] | 201 | counters_inc(clients, -1); |
---|
| 202 | |
---|
[4395] | 203 | if (ROAR_CLIENT(cs)->execed != -1) { |
---|
[0] | 204 | // return streams_delete(g_clients[id]->execed); |
---|
[4395] | 205 | ROAR_CLIENT(cs)->execed = -1; |
---|
[1164] | 206 | close_client_fh = 0; |
---|
[0] | 207 | } |
---|
| 208 | |
---|
| 209 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
[4395] | 210 | streams_delete(ROAR_CLIENT(cs)->streams[i]); |
---|
[0] | 211 | } |
---|
| 212 | |
---|
[4395] | 213 | if ( ROAR_CLIENT(cs)->fh != -1 && close_client_fh ) |
---|
| 214 | close(ROAR_CLIENT(cs)->fh); |
---|
[0] | 215 | |
---|
[4395] | 216 | roar_nnode_free(&(ROAR_CLIENT(cs)->nnode)); |
---|
[2815] | 217 | |
---|
[4684] | 218 | if ( cs->inbuf != NULL ) |
---|
| 219 | roar_buffer_free(cs->inbuf); |
---|
| 220 | |
---|
| 221 | if ( cs->outbuf != NULL ) |
---|
| 222 | roar_buffer_free(cs->outbuf); |
---|
| 223 | |
---|
[5452] | 224 | if ( cs->protoinst != NULL ) |
---|
| 225 | roar_mm_free(cs->protoinst); |
---|
| 226 | |
---|
[4395] | 227 | roar_mm_free(cs); |
---|
[0] | 228 | g_clients[id] = NULL; |
---|
| 229 | |
---|
| 230 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
| 231 | return 0; |
---|
| 232 | } |
---|
| 233 | |
---|
[3927] | 234 | int clients_close (int id, int nocheck_exec) { |
---|
| 235 | struct roar_client * c; |
---|
| 236 | |
---|
| 237 | ROAR_DBG("clients_close(id=%i) = ?", id); |
---|
| 238 | |
---|
| 239 | _CHECK_CID(id); |
---|
| 240 | |
---|
[4326] | 241 | c = ROAR_CLIENT(g_clients[id]); |
---|
[3927] | 242 | |
---|
| 243 | if ( c->fh == -1 ) { |
---|
| 244 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
| 245 | return 0; |
---|
| 246 | } |
---|
| 247 | |
---|
[4326] | 248 | if (nocheck_exec || c->execed != -1) { |
---|
[3927] | 249 | close(c->fh); |
---|
| 250 | c->fh = -1; |
---|
| 251 | } |
---|
| 252 | |
---|
| 253 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
| 254 | return 0; |
---|
| 255 | } |
---|
| 256 | |
---|
[0] | 257 | int clients_get (int id, struct roar_client ** client) { |
---|
[3910] | 258 | _CHECK_CID(id); |
---|
| 259 | |
---|
[4326] | 260 | *client = ROAR_CLIENT(g_clients[id]); |
---|
[0] | 261 | |
---|
| 262 | if ( *client == NULL ) |
---|
| 263 | return -1; |
---|
| 264 | |
---|
| 265 | return 0; |
---|
| 266 | } |
---|
| 267 | |
---|
[4468] | 268 | int clients_get_server (int id, struct roar_client_server ** client) { |
---|
| 269 | _CHECK_CID(id); |
---|
| 270 | |
---|
| 271 | *client = g_clients[id]; |
---|
| 272 | |
---|
| 273 | if ( *client == NULL ) |
---|
| 274 | return -1; |
---|
| 275 | |
---|
| 276 | return 0; |
---|
| 277 | } |
---|
| 278 | |
---|
[0] | 279 | int clients_set_fh (int id, int fh) { |
---|
[3713] | 280 | struct roar_client * c; |
---|
| 281 | #ifdef SO_PEERCRED |
---|
| 282 | struct ucred cred; |
---|
| 283 | socklen_t cred_len = sizeof(cred); |
---|
| 284 | #endif |
---|
[501] | 285 | |
---|
[3910] | 286 | _CHECK_CID(id); |
---|
| 287 | |
---|
[4326] | 288 | if ( (c = ROAR_CLIENT(g_clients[id])) == NULL ) |
---|
[0] | 289 | return -1; |
---|
| 290 | |
---|
[3713] | 291 | c->fh = fh; |
---|
| 292 | |
---|
[4947] | 293 | if ( fh == -1 ) |
---|
| 294 | return 0; |
---|
| 295 | |
---|
[3713] | 296 | #ifdef SO_PEERCRED |
---|
| 297 | if (getsockopt(fh, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != -1) { |
---|
| 298 | if ( cred.pid != 0 ) { |
---|
| 299 | c->pid = cred.pid; |
---|
| 300 | c->uid = cred.uid; |
---|
| 301 | c->gid = cred.gid; |
---|
| 302 | } |
---|
| 303 | } else { |
---|
| 304 | ROAR_DBG("req_on_identify(): Can't get creds via SO_PEERCRED: %s", strerror(errno)); |
---|
| 305 | } |
---|
| 306 | #elif defined(ROAR_HAVE_GETPEEREID) |
---|
| 307 | if (getpeereid(fh, &(c->uid), &(c->gid)) == -1) { |
---|
| 308 | ROAR_DBG("req_on_identify(): Can't get creds via getpeereid(): %s", strerror(errno)); |
---|
| 309 | } |
---|
| 310 | #endif |
---|
[0] | 311 | |
---|
| 312 | return 0; |
---|
| 313 | } |
---|
| 314 | |
---|
[755] | 315 | int clients_get_fh (int id) { |
---|
[3910] | 316 | _CHECK_CID(id); |
---|
[755] | 317 | |
---|
[4326] | 318 | return ROAR_CLIENT(g_clients[id])->fh; |
---|
[755] | 319 | } |
---|
| 320 | |
---|
[0] | 321 | int clients_set_pid (int id, int pid) { |
---|
[3910] | 322 | _CHECK_CID(id); |
---|
[0] | 323 | |
---|
[4326] | 324 | ROAR_CLIENT(g_clients[id])->pid = pid; |
---|
[0] | 325 | |
---|
| 326 | return 0; |
---|
| 327 | } |
---|
| 328 | |
---|
[439] | 329 | int clients_set_uid (int id, int uid) { |
---|
[3910] | 330 | _CHECK_CID(id); |
---|
[439] | 331 | |
---|
[4326] | 332 | ROAR_CLIENT(g_clients[id])->uid = uid; |
---|
[439] | 333 | |
---|
| 334 | return 0; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | int clients_set_gid (int id, int gid) { |
---|
[3910] | 338 | _CHECK_CID(id); |
---|
[439] | 339 | |
---|
[4326] | 340 | ROAR_CLIENT(g_clients[id])->gid = gid; |
---|
[439] | 341 | |
---|
| 342 | return 0; |
---|
| 343 | } |
---|
| 344 | |
---|
[2517] | 345 | int clients_set_proto (int id, int proto) { |
---|
[2614] | 346 | int byteorder = ROAR_BYTEORDER_UNKNOWN; |
---|
| 347 | |
---|
[3910] | 348 | _CHECK_CID(id); |
---|
[2517] | 349 | |
---|
[2614] | 350 | switch (proto) { |
---|
| 351 | case ROAR_PROTO_ROARAUDIO: |
---|
[2828] | 352 | case ROAR_PROTO_ESOUND: |
---|
[3981] | 353 | case ROAR_PROTO_RPLAY: |
---|
[3255] | 354 | case ROAR_PROTO_SIMPLE: |
---|
[2614] | 355 | byteorder = ROAR_BYTEORDER_NETWORK; |
---|
| 356 | break; |
---|
| 357 | } |
---|
| 358 | |
---|
[4326] | 359 | ROAR_CLIENT(g_clients[id])->proto = proto; |
---|
| 360 | ROAR_CLIENT(g_clients[id])->byteorder = byteorder; |
---|
[2517] | 361 | |
---|
| 362 | return 0; |
---|
| 363 | } |
---|
| 364 | |
---|
[4343] | 365 | int clients_block (int id, int unblock) { |
---|
| 366 | _CHECK_CID(id); |
---|
| 367 | |
---|
| 368 | if ( unblock ) { |
---|
| 369 | g_clients[id]->blockc--; |
---|
| 370 | } else { |
---|
| 371 | g_clients[id]->blockc++; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | return 0; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | |
---|
[498] | 378 | #define MAX_STREAMLESS 8 |
---|
| 379 | |
---|
[0] | 380 | int clients_check_all (void) { |
---|
[1480] | 381 | #ifdef ROAR_HAVE_SELECT |
---|
[4326] | 382 | struct roar_client * c; |
---|
[0] | 383 | struct timeval tv; |
---|
[4686] | 384 | fd_set r, w, e; |
---|
[66] | 385 | int i, j; |
---|
[0] | 386 | int ret; |
---|
| 387 | int fh; |
---|
| 388 | int max_fh = -1; |
---|
[71] | 389 | int have = 0; |
---|
[498] | 390 | struct { |
---|
| 391 | int id; |
---|
| 392 | int fh; |
---|
| 393 | } streamless[MAX_STREAMLESS]; |
---|
| 394 | int have_streamless = 0; |
---|
| 395 | int have_stream; |
---|
[0] | 396 | |
---|
[4815] | 397 | ROAR_DBG("clients_check_all(void) = ?"); |
---|
| 398 | |
---|
[0] | 399 | FD_ZERO(&r); |
---|
[4686] | 400 | FD_ZERO(&w); |
---|
[0] | 401 | FD_ZERO(&e); |
---|
| 402 | |
---|
| 403 | tv.tv_sec = 0; |
---|
| 404 | tv.tv_usec = 1; |
---|
| 405 | |
---|
| 406 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
[4326] | 407 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
[0] | 408 | continue; |
---|
| 409 | |
---|
[4815] | 410 | ROAR_DBG("clients_check_all(void): i(client)=%i", i); |
---|
| 411 | |
---|
[4326] | 412 | if ( (fh = c->fh) != -1 ) { |
---|
[71] | 413 | have++; |
---|
| 414 | |
---|
[610] | 415 | ROAR_DBG("clients_check_all(*): fh=%i", fh); |
---|
| 416 | |
---|
[66] | 417 | FD_SET(fh, &r); |
---|
| 418 | FD_SET(fh, &e); |
---|
| 419 | |
---|
[4686] | 420 | if ( g_clients[i]->outbuf != NULL ) { |
---|
| 421 | FD_SET(fh, &w); |
---|
| 422 | } |
---|
| 423 | |
---|
[66] | 424 | if ( fh > max_fh ) |
---|
| 425 | max_fh = fh; |
---|
[84] | 426 | } |
---|
[0] | 427 | |
---|
[498] | 428 | have_stream = 0; |
---|
| 429 | |
---|
[84] | 430 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
[4815] | 431 | ROAR_DBG("clients_check_all(void): i(client)=%i, j(stream)=%i", i, j); |
---|
[4326] | 432 | if ( (fh = streams_get_fh(c->streams[j])) != -1 ) { |
---|
| 433 | ROAR_DBG("clients_check_all(*): g_clients[i=%i]->streams[j=%i] = %i, fh = %i", i, j, c->streams[j], fh); |
---|
[1504] | 434 | if ( fh > -1 ) { |
---|
| 435 | FD_SET(fh, &r); |
---|
[0] | 436 | |
---|
[1504] | 437 | if ( fh > max_fh ) |
---|
| 438 | max_fh = fh; |
---|
[3579] | 439 | } else if ( fh == -2 ) { |
---|
[4326] | 440 | streams_check(c->streams[j]); |
---|
[1504] | 441 | } |
---|
[498] | 442 | |
---|
| 443 | have_stream = 1; |
---|
[66] | 444 | } |
---|
[84] | 445 | //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh); |
---|
[66] | 446 | } |
---|
| 447 | |
---|
[498] | 448 | if ( !have_stream && have_streamless < MAX_STREAMLESS ) { |
---|
| 449 | streamless[have_streamless ].id = i; |
---|
[4326] | 450 | if ( (streamless[have_streamless++].fh = c->fh) == -1 ) |
---|
[498] | 451 | have_streamless--; |
---|
| 452 | } |
---|
[0] | 453 | } |
---|
| 454 | |
---|
[4815] | 455 | ROAR_DBG("clients_check_all(void): max_fh=%i", max_fh); |
---|
| 456 | |
---|
[256] | 457 | if ( max_fh == -1 ) |
---|
| 458 | return 0; |
---|
| 459 | |
---|
[4686] | 460 | if ( (ret = select(max_fh + 1, &r, &w, &e, &tv)) < 1 ) { |
---|
[71] | 461 | return ret < 0 ? ret : have; |
---|
[0] | 462 | } |
---|
| 463 | |
---|
| 464 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
[4326] | 465 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
[0] | 466 | continue; |
---|
| 467 | |
---|
[4326] | 468 | if ( (fh = c->fh) != -1 ) { |
---|
[66] | 469 | if ( FD_ISSET(fh, &r) ) { |
---|
[4326] | 470 | if ( c->execed == -1 ) { |
---|
[66] | 471 | clients_check(i); |
---|
[4326] | 472 | if ( g_clients[i] != NULL && ROAR_CLIENT(g_clients[i])->execed != -1 ) { |
---|
[1834] | 473 | FD_CLR(fh, &r); |
---|
| 474 | } |
---|
[255] | 475 | /* |
---|
[66] | 476 | } else { |
---|
| 477 | streams_check(g_clients[i]->execed); |
---|
[255] | 478 | */ |
---|
[66] | 479 | } |
---|
| 480 | } |
---|
[4686] | 481 | if ( FD_ISSET(fh, &w) ) { |
---|
| 482 | clients_flush(i); |
---|
| 483 | } |
---|
[0] | 484 | |
---|
[84] | 485 | if ( FD_ISSET(fh, &e) ) { |
---|
[66] | 486 | clients_delete(i); |
---|
[84] | 487 | continue; |
---|
| 488 | } |
---|
| 489 | } |
---|
| 490 | |
---|
[4326] | 491 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
[84] | 492 | continue; |
---|
| 493 | |
---|
| 494 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
[1611] | 495 | ROAR_DBG("clients_check_all(*): D: client=%i, stream=%i, g_clients[i=%i] = %p", i, j, i, g_clients[i]); |
---|
[136] | 496 | if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?) |
---|
| 497 | break; |
---|
[1611] | 498 | |
---|
| 499 | //ROAR_WARN("clients_check_all(*): client=%i: client exists", i); |
---|
[4326] | 500 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: id=%i", i, j, c->streams[j]); |
---|
[1611] | 501 | |
---|
[4326] | 502 | if ( (fh = streams_get_fh(c->streams[j])) != -1 ) { |
---|
[1611] | 503 | ROAR_DBG("clients_check_all(*): client=%i, stream=%i: fh=%i", i, j, fh); |
---|
[3579] | 504 | if ( fh > -1 && FD_ISSET(fh, &r) ) { |
---|
[4326] | 505 | streams_check(c->streams[j]); |
---|
[66] | 506 | } |
---|
[0] | 507 | } |
---|
| 508 | } |
---|
| 509 | } |
---|
| 510 | |
---|
[498] | 511 | if ( have_streamless ) { |
---|
| 512 | FD_ZERO(&r); |
---|
[4686] | 513 | FD_ZERO(&w); |
---|
[498] | 514 | |
---|
| 515 | tv.tv_sec = 0; |
---|
| 516 | tv.tv_usec = 1; |
---|
| 517 | |
---|
| 518 | max_fh = -1; |
---|
| 519 | |
---|
| 520 | for (i = 0; i < have_streamless; i++) { |
---|
[4326] | 521 | if ( g_clients[j = streamless[i].id] == NULL ) |
---|
[607] | 522 | continue; |
---|
| 523 | |
---|
[4326] | 524 | if ( ROAR_CLIENT(g_clients[j])->execed != -1 ) |
---|
[601] | 525 | continue; |
---|
| 526 | |
---|
[498] | 527 | fh = streamless[i].fh; |
---|
| 528 | |
---|
| 529 | ROAR_DBG("clients_check_all(void): fh=%i", fh); |
---|
| 530 | FD_SET(fh, &r); |
---|
| 531 | |
---|
[4688] | 532 | if ( g_clients[j]->outbuf != NULL ) { |
---|
[4686] | 533 | FD_SET(fh, &w); |
---|
| 534 | } |
---|
| 535 | |
---|
[498] | 536 | if ( fh > max_fh ) |
---|
| 537 | max_fh = fh; |
---|
| 538 | } |
---|
| 539 | |
---|
[4686] | 540 | if ( (ret = select(max_fh + 1, &r, &w, NULL, &tv)) < 0 ) { |
---|
[498] | 541 | return ret; |
---|
| 542 | } |
---|
| 543 | |
---|
| 544 | for (i = 0; i < have_streamless; i++) { |
---|
| 545 | if ( FD_ISSET(streamless[i].fh, &r) ) { |
---|
| 546 | clients_check(streamless[i].id); |
---|
| 547 | } |
---|
[4686] | 548 | if ( FD_ISSET(streamless[i].fh, &w) ) { |
---|
| 549 | clients_flush(streamless[i].id); |
---|
| 550 | } |
---|
[498] | 551 | } |
---|
| 552 | } |
---|
| 553 | |
---|
[71] | 554 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
| 555 | return have; |
---|
[1480] | 556 | #else |
---|
[4815] | 557 | ROAR_DBG("clients_check_all(void) = -1"); |
---|
[1480] | 558 | return -1; |
---|
| 559 | #endif |
---|
[0] | 560 | } |
---|
| 561 | |
---|
| 562 | int clients_check (int id) { |
---|
[4326] | 563 | struct roar_client * c; |
---|
[0] | 564 | struct roar_message m; |
---|
| 565 | struct roar_connection con; |
---|
[5190] | 566 | struct roar_vio_calls vio; |
---|
[5155] | 567 | struct roar_error_state errstate; |
---|
[5567] | 568 | const struct roard_proto_handle * proto; |
---|
[5155] | 569 | int command_error; |
---|
[0] | 570 | char * data = NULL; |
---|
| 571 | int oldcmd; |
---|
| 572 | int r; |
---|
[498] | 573 | int rv = 0; |
---|
[3926] | 574 | uint32_t flags[2] = {COMMAND_FLAG_NONE, COMMAND_FLAG_NONE}; |
---|
[4325] | 575 | uint32_t event; |
---|
[0] | 576 | |
---|
[4713] | 577 | ROAR_DBG("clients_check(id=%i) = ?", id); |
---|
| 578 | |
---|
[3910] | 579 | _CHECK_CID(id); |
---|
| 580 | |
---|
[4326] | 581 | c = ROAR_CLIENT(g_clients[id]); |
---|
| 582 | |
---|
| 583 | if ( c->fh == -1 ) |
---|
[0] | 584 | return -1; |
---|
| 585 | |
---|
[4326] | 586 | roar_connect_fh(&con, c->fh); |
---|
[0] | 587 | |
---|
[4713] | 588 | ROAR_DBG("clients_check(id=%i): c->proto=%i", id, c->proto); |
---|
| 589 | |
---|
[4326] | 590 | switch (c->proto) { |
---|
[2517] | 591 | case ROAR_PROTO_ROARAUDIO: |
---|
| 592 | r = roar_recv_message(&con, &m, &data); |
---|
[0] | 593 | |
---|
[2517] | 594 | if ( r == -1 ) { // should we drop the client? |
---|
| 595 | clients_delete(id); |
---|
| 596 | return -1; |
---|
| 597 | } |
---|
[0] | 598 | |
---|
[4325] | 599 | event = ROAR_NOTIFY_CMD2EVENT(m.cmd); |
---|
| 600 | |
---|
[2517] | 601 | oldcmd = m.cmd; |
---|
[0] | 602 | |
---|
[5155] | 603 | roar_err_store(&errstate); |
---|
| 604 | roar_err_clear_all(); |
---|
| 605 | r = command_exec(id, &m, &data, flags); |
---|
| 606 | roar_err_update(); |
---|
| 607 | command_error = roar_error; |
---|
| 608 | roar_err_restore(&errstate); |
---|
| 609 | |
---|
| 610 | if ( r == -1 ) { |
---|
| 611 | // use command_error to create an error frame here as soon as this is supported. |
---|
[2517] | 612 | m.cmd = ROAR_CMD_ERROR; |
---|
| 613 | m.datalen = 0; |
---|
| 614 | ROAR_DBG("clients_check(*): Exec of command faild!"); |
---|
| 615 | } else { |
---|
| 616 | if ( m.cmd == oldcmd ) { |
---|
| 617 | m.cmd = ROAR_CMD_OK; |
---|
| 618 | m.datalen = 0; |
---|
| 619 | } else if ( m.cmd == ROAR_CMD_OK_STOP ) { |
---|
| 620 | m.cmd = ROAR_CMD_OK; |
---|
| 621 | rv = 1; |
---|
| 622 | } |
---|
| 623 | } |
---|
[4298] | 624 | ROAR_DBG("clients_check(*): data=%p", data); |
---|
| 625 | |
---|
[4347] | 626 | if ( flags[1] & COMMAND_FLAG_OUT_NOSEND ) { |
---|
| 627 | roar_notify_core_emit_simple(event, id, -1, -1, -1, -1, NULL, 0); |
---|
| 628 | } else { |
---|
| 629 | roar_notify_core_emit_simple(event, id, -1, -1, m.cmd, -1, NULL, 0); |
---|
[4343] | 630 | roar_send_message(&con, &m, flags[1] & COMMAND_FLAG_OUT_LONGDATA ? data : NULL); |
---|
[4347] | 631 | } |
---|
[3928] | 632 | |
---|
| 633 | if ( flags[1] & COMMAND_FLAG_OUT_CLOSECON ) |
---|
| 634 | clients_close(id, 1); |
---|
| 635 | |
---|
[2517] | 636 | break; |
---|
[4020] | 637 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
[3684] | 638 | case ROAR_PROTO_RSOUND: |
---|
[3820] | 639 | rv = emul_rsound_check_client(id, NULL); |
---|
[3825] | 640 | if ( rv == 0 ) { // loop as long as we don't get an error. |
---|
| 641 | while (rv == 0) |
---|
| 642 | rv = emul_rsound_check_client(id, NULL); |
---|
| 643 | rv = 0; // restore |
---|
| 644 | } else { // in case of error delete the client |
---|
[4131] | 645 | if ( |
---|
| 646 | #ifdef EAGAIN |
---|
[4153] | 647 | errno != EAGAIN && |
---|
[4131] | 648 | #endif |
---|
| 649 | #ifdef EWOULDBLOCK |
---|
[4153] | 650 | errno != EWOULDBLOCK && |
---|
[4131] | 651 | #endif |
---|
| 652 | #ifdef EINTR |
---|
[4153] | 653 | errno != EINTR && |
---|
[4131] | 654 | #endif |
---|
[4153] | 655 | 1 ) { |
---|
[4131] | 656 | rv = clients_delete(id); |
---|
| 657 | } else { |
---|
| 658 | rv = 0; |
---|
| 659 | } |
---|
[3825] | 660 | } |
---|
[3684] | 661 | break; |
---|
| 662 | #endif |
---|
[2517] | 663 | default: |
---|
| 664 | rv = -1; |
---|
[5567] | 665 | proto = clients_get_protohandle(c->proto); |
---|
| 666 | if ( proto != NULL ) { |
---|
| 667 | roar_vio_open_fh_socket(&vio, clients_get_fh(id)); |
---|
| 668 | if ( proto->lhandle != NULL ) |
---|
| 669 | roar_dl_context_restore(proto->lhandle); |
---|
| 670 | |
---|
| 671 | switch (proto->type) { |
---|
| 672 | case ROARD_PROTO_TYPE_BUILDIN: |
---|
| 673 | ROAR_WARN("clients_check(id=%i): proto(%i) marked as buildin but isn't. BAD.", id, proto->proto); |
---|
| 674 | break; |
---|
| 675 | case ROARD_PROTO_TYPE_ROARDPROTO: |
---|
| 676 | if ( proto->impl.roardproto.check_client != NULL ) |
---|
| 677 | rv = proto->impl.roardproto.check_client(id, &vio); |
---|
| 678 | break; |
---|
[4678] | 679 | } |
---|
[5567] | 680 | |
---|
| 681 | if ( proto->lhandle != NULL ) |
---|
| 682 | roar_dl_context_store(proto->lhandle); |
---|
[4678] | 683 | } |
---|
[0] | 684 | } |
---|
| 685 | |
---|
[4297] | 686 | if ( data != NULL ) |
---|
[5295] | 687 | roar_mm_free(data); |
---|
[0] | 688 | |
---|
[2517] | 689 | ROAR_DBG("clients_check(id=%i) = %i", id, rv); |
---|
[498] | 690 | return rv; |
---|
[0] | 691 | } |
---|
| 692 | |
---|
[4686] | 693 | int clients_flush (int id) { |
---|
[5567] | 694 | struct roar_vio_calls vio; |
---|
| 695 | struct roar_client_server * cs; |
---|
| 696 | struct roar_client * c; |
---|
| 697 | const struct roard_proto_handle * p; |
---|
[4686] | 698 | size_t len; |
---|
| 699 | ssize_t ret; |
---|
| 700 | void * buf; |
---|
[5567] | 701 | int rv; |
---|
[4686] | 702 | |
---|
| 703 | _CHECK_CID(id); |
---|
| 704 | |
---|
| 705 | c = ROAR_CLIENT(cs = g_clients[id]); |
---|
| 706 | |
---|
[5567] | 707 | p = clients_get_protohandle(c->proto); |
---|
[4686] | 708 | |
---|
| 709 | if ( p == NULL ) |
---|
| 710 | return -1; |
---|
| 711 | |
---|
| 712 | roar_vio_open_fh_socket(&vio, clients_get_fh(id)); |
---|
| 713 | |
---|
[5567] | 714 | switch (p->type) { |
---|
| 715 | case ROARD_PROTO_TYPE_BUILDIN: |
---|
| 716 | /* noop */ |
---|
| 717 | break; |
---|
| 718 | case ROARD_PROTO_TYPE_ROARDPROTO: |
---|
| 719 | if ( p->impl.roardproto.flush_client != NULL ) { |
---|
| 720 | if ( p->lhandle != NULL ) |
---|
| 721 | roar_dl_context_restore(p->lhandle); |
---|
| 722 | rv = p->impl.roardproto.flush_client(id, &vio); |
---|
| 723 | if ( p->lhandle != NULL ) |
---|
| 724 | roar_dl_context_store(p->lhandle); |
---|
| 725 | return rv; |
---|
| 726 | } |
---|
| 727 | break; |
---|
[4686] | 728 | } |
---|
| 729 | |
---|
| 730 | if ( roar_buffer_get_len(cs->outbuf, &len) == -1 ) |
---|
| 731 | return -1; |
---|
| 732 | |
---|
| 733 | if ( roar_buffer_get_data(cs->outbuf, &buf) == -1 ) |
---|
| 734 | return -1; |
---|
| 735 | |
---|
| 736 | ret = roar_vio_write(&vio, buf, len); |
---|
| 737 | |
---|
| 738 | if ( ret < 1 ) { |
---|
| 739 | clients_delete(id); |
---|
| 740 | return -1; |
---|
| 741 | } |
---|
| 742 | |
---|
| 743 | if ( ret == len ) { |
---|
[5242] | 744 | if ( roar_buffer_next(&(cs->outbuf)) == -1 ) { |
---|
| 745 | clients_delete(id); |
---|
| 746 | return -1; |
---|
| 747 | } |
---|
[4686] | 748 | } else { |
---|
| 749 | if ( roar_buffer_set_offset(cs->outbuf, ret) == -1 ) { |
---|
| 750 | clients_delete(id); |
---|
| 751 | return -1; |
---|
| 752 | } |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | if ( cs->outbuf == NULL ) { |
---|
[5567] | 756 | switch (p->type) { |
---|
| 757 | case ROARD_PROTO_TYPE_BUILDIN: |
---|
| 758 | /* noop */ |
---|
| 759 | break; |
---|
| 760 | case ROARD_PROTO_TYPE_ROARDPROTO: |
---|
| 761 | if ( p->impl.roardproto.flushed_client != NULL ) { |
---|
| 762 | if ( p->lhandle != NULL ) |
---|
| 763 | roar_dl_context_restore(p->lhandle); |
---|
| 764 | rv = p->impl.roardproto.flushed_client(id, &vio); |
---|
| 765 | if ( p->lhandle != NULL ) |
---|
| 766 | roar_dl_context_store(p->lhandle); |
---|
| 767 | return rv; |
---|
| 768 | } |
---|
| 769 | break; |
---|
[4686] | 770 | } |
---|
| 771 | } |
---|
| 772 | |
---|
| 773 | return 0; |
---|
| 774 | } |
---|
| 775 | |
---|
[0] | 776 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
| 777 | int i; |
---|
[934] | 778 | // int fh; |
---|
| 779 | int j; |
---|
[2715] | 780 | int keep_going; |
---|
[0] | 781 | |
---|
| 782 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 783 | if ( g_clients[i] == NULL ) |
---|
| 784 | continue; |
---|
| 785 | |
---|
[2715] | 786 | keep_going = 1; |
---|
| 787 | |
---|
[934] | 788 | /* |
---|
[0] | 789 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 790 | continue; |
---|
[934] | 791 | */ |
---|
[0] | 792 | |
---|
[4326] | 793 | ROAR_DBG("clients_send_mon(*): client=%i, execed=%i", i, ROAR_CLIENT(g_clients[i])->execed); |
---|
[1905] | 794 | |
---|
[2706] | 795 | /* |
---|
[0] | 796 | if ( g_clients[i]->execed == -1 ) { |
---|
| 797 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
[2706] | 798 | */ |
---|
[2715] | 799 | for (j = 0; keep_going && j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
[934] | 800 | //if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
[2715] | 801 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> ?", i, j); |
---|
[4326] | 802 | if ( ROAR_CLIENT(g_clients[i])->streams[j] != -1 ) { |
---|
| 803 | ROAR_DBG("clients_send_mon(*): client=%i, stream=%i -> %i", i, j, ROAR_CLIENT(g_clients[i])->streams[j]); |
---|
| 804 | streams_send_mon(ROAR_CLIENT(g_clients[i])->streams[j]); |
---|
[2715] | 805 | |
---|
| 806 | // the client may be deleted here, check if it still exists: |
---|
| 807 | if ( g_clients[i] == NULL ) |
---|
| 808 | keep_going = 0; |
---|
[1905] | 809 | } |
---|
[934] | 810 | } |
---|
[2706] | 811 | /* |
---|
[0] | 812 | } else { |
---|
| 813 | // streams_check(g_clients[i]->execed); |
---|
| 814 | streams_send_mon(g_clients[i]->execed); |
---|
| 815 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 816 | // clients_delete(i); // delete client in case we could not write |
---|
| 817 | } |
---|
[2706] | 818 | */ |
---|
[0] | 819 | } |
---|
| 820 | |
---|
[1902] | 821 | // TODO: FIXME: should this really be -1? |
---|
[0] | 822 | return -1; |
---|
| 823 | } |
---|
| 824 | |
---|
| 825 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
[4326] | 826 | struct roar_client * c; |
---|
[0] | 827 | int i; |
---|
| 828 | int fh; |
---|
| 829 | |
---|
| 830 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
[4326] | 831 | if ( (c = ROAR_CLIENT(g_clients[i])) == NULL ) |
---|
[0] | 832 | continue; |
---|
| 833 | |
---|
[4326] | 834 | if ( (fh = c->fh) == -1 ) |
---|
[0] | 835 | continue; |
---|
| 836 | |
---|
[4326] | 837 | if ( c->execed == -1 ) { |
---|
[0] | 838 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 839 | } else { |
---|
| 840 | // streams_check(g_clients[i]->execed); |
---|
[4326] | 841 | streams_send_filter(c->execed); |
---|
[0] | 842 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 843 | // clients_delete(i); // delete client in case we could not write |
---|
| 844 | } |
---|
| 845 | } |
---|
| 846 | |
---|
| 847 | return -1; |
---|
| 848 | } |
---|
| 849 | |
---|
[5301] | 850 | int clients_add_output (int id, struct roar_buffer ** buf) { |
---|
[4686] | 851 | struct roar_client_server * cs; |
---|
| 852 | |
---|
| 853 | _CHECK_CID(id); |
---|
| 854 | cs = g_clients[id]; |
---|
| 855 | |
---|
| 856 | if ( cs->outbuf == NULL ) { |
---|
[5301] | 857 | cs->outbuf = *buf; |
---|
[4686] | 858 | } else { |
---|
[5301] | 859 | return roar_buffer_moveinto(cs->outbuf, buf); |
---|
[4686] | 860 | } |
---|
| 861 | |
---|
| 862 | return 0; |
---|
| 863 | } |
---|
| 864 | |
---|
[4679] | 865 | // proto support |
---|
[5567] | 866 | const struct roard_proto_handle * clients_get_protohandle(const int proto) { |
---|
| 867 | size_t i; |
---|
| 868 | |
---|
| 869 | if ( proto < 0 ) { |
---|
| 870 | roar_err_set(ROAR_ERROR_INVAL); |
---|
| 871 | return NULL; |
---|
| 872 | } |
---|
| 873 | |
---|
| 874 | for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) |
---|
| 875 | if ( __protos[i].proto == proto ) |
---|
| 876 | return &(__protos[i]); |
---|
| 877 | |
---|
| 878 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 879 | return NULL; |
---|
| 880 | } |
---|
| 881 | |
---|
[5312] | 882 | int clients_register_proto(struct roard_proto * proto, struct roar_dl_lhandle * lhandle) { |
---|
[5567] | 883 | const size_t len = sizeof(__protos)/sizeof(*__protos); |
---|
[4679] | 884 | size_t i; |
---|
| 885 | |
---|
| 886 | if ( proto == NULL ) |
---|
| 887 | return -1; |
---|
| 888 | |
---|
[5567] | 889 | for (i = 0; __protos[i].proto != -1; i++); |
---|
[4679] | 890 | |
---|
| 891 | // i is now at pos of current EOS entry. |
---|
| 892 | |
---|
| 893 | // test if we have space for one more entry: |
---|
| 894 | if ( (i+1) >= len ) |
---|
| 895 | return -1; |
---|
| 896 | |
---|
[5567] | 897 | memcpy(&(__protos[i].impl.roardproto), proto, sizeof(__protos[i].impl.roardproto)); |
---|
| 898 | |
---|
| 899 | __protos[i].impl.roardproto.lhandle = lhandle; |
---|
[4679] | 900 | |
---|
[5567] | 901 | __protos[i].proto = proto->proto; |
---|
| 902 | __protos[i].type = ROARD_PROTO_TYPE_ROARDPROTO; |
---|
| 903 | __protos[i].lhandle = lhandle; |
---|
[5312] | 904 | |
---|
[4679] | 905 | return 0; |
---|
| 906 | } |
---|
| 907 | |
---|
[5275] | 908 | int clients_unregister_proto(int proto) { |
---|
| 909 | size_t i; |
---|
| 910 | |
---|
| 911 | if ( proto < 0 ) { |
---|
| 912 | roar_err_set(ROAR_ERROR_RANGE); |
---|
| 913 | return -1; |
---|
| 914 | } |
---|
| 915 | |
---|
[5567] | 916 | for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) { |
---|
| 917 | if ( __protos[i].proto == proto ) { |
---|
| 918 | memset(&(__protos[i]), 0, sizeof(*__protos)); |
---|
| 919 | __protos[i].proto = -1; |
---|
[5275] | 920 | return 0; |
---|
| 921 | } |
---|
| 922 | } |
---|
| 923 | |
---|
| 924 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 925 | return -1; |
---|
| 926 | } |
---|
| 927 | |
---|
| 928 | void print_protolist (enum output_format format) { |
---|
[5567] | 929 | struct roard_proto_handle * p; |
---|
[5275] | 930 | char subsys[7] = " "; |
---|
[5567] | 931 | const char * description; |
---|
[5275] | 932 | size_t i; |
---|
| 933 | |
---|
| 934 | switch (format) { |
---|
| 935 | case FORMAT_NATIVE: |
---|
| 936 | printf(" Protocol Flag Subsys - Description\n"); |
---|
| 937 | printf("------------------------------------------------------\n"); |
---|
| 938 | printf(" roar WM LRX - RoarAudio native protocol\n"); |
---|
| 939 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
| 940 | printf(" simple WM LRX - PulseAudio simple protocol\n"); |
---|
| 941 | #endif |
---|
| 942 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
| 943 | printf(" rsound W - RSound emulation\n"); |
---|
| 944 | #endif |
---|
| 945 | break; |
---|
| 946 | case FORMAT_WIKI: |
---|
| 947 | printf("||=Protocol =||=Flag =||=Subsys =||=Description =||\n"); |
---|
| 948 | printf("||roar || ||WM LRX ||RoarAudio native protocol ||\n"); |
---|
| 949 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
| 950 | printf("||simple || ||WM LRX ||PulseAudio simple protocol ||\n"); |
---|
| 951 | #endif |
---|
| 952 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
| 953 | printf("||rsound || ||W ||RSound emulation ||\n"); |
---|
| 954 | #endif |
---|
| 955 | break; |
---|
| 956 | case FORMAT_CSV: |
---|
| 957 | printf("Protocol,Flag,Subsys,Description\n"); |
---|
| 958 | printf("roar,,WM LRX,RoarAudio native protocol\n"); |
---|
| 959 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE |
---|
| 960 | printf("simple,,WM LRX,PulseAudio simple protocol\n"); |
---|
| 961 | #endif |
---|
| 962 | #ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND |
---|
| 963 | printf("rsound,,W,RSound emulation\n"); |
---|
| 964 | #endif |
---|
| 965 | break; |
---|
| 966 | default: |
---|
| 967 | roar_err_set(ROAR_ERROR_NOTSUP); |
---|
| 968 | return; |
---|
| 969 | } |
---|
| 970 | |
---|
[5567] | 971 | for (i = 0; i < (sizeof(__protos)/sizeof(*__protos)); i++) { |
---|
| 972 | p = &(__protos[i]); |
---|
[5275] | 973 | if ( p->proto == -1 ) |
---|
| 974 | continue; |
---|
| 975 | |
---|
| 976 | strncpy(subsys, " ", 6); |
---|
[5567] | 977 | description = "(none)"; |
---|
[5275] | 978 | |
---|
[5567] | 979 | if ( p->type == ROARD_PROTO_TYPE_ROARDPROTO ) { |
---|
| 980 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_WAVEFORM ) |
---|
| 981 | subsys[0] = 'W'; |
---|
| 982 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_MIDI ) |
---|
| 983 | subsys[1] = 'M'; |
---|
| 984 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_CB ) |
---|
| 985 | subsys[2] = 'C'; |
---|
| 986 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_LIGHT ) |
---|
| 987 | subsys[3] = 'L'; |
---|
| 988 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_RAW ) |
---|
| 989 | subsys[4] = 'R'; |
---|
| 990 | if ( p->impl.roardproto.subsystems & ROAR_SUBSYS_COMPLEX ) |
---|
| 991 | subsys[5] = 'X'; |
---|
| 992 | |
---|
| 993 | description = p->impl.roardproto.description; |
---|
| 994 | } |
---|
[5275] | 995 | |
---|
| 996 | switch (format) { |
---|
| 997 | case FORMAT_NATIVE: |
---|
[5567] | 998 | printf(" %-13s %s - %s\n", roar_proto2str(p->proto), subsys, description); |
---|
[5275] | 999 | break; |
---|
| 1000 | case FORMAT_WIKI: |
---|
[5567] | 1001 | printf("||%s || ||%s ||%s ||\n", roar_proto2str(p->proto), subsys, description); |
---|
[5275] | 1002 | break; |
---|
| 1003 | case FORMAT_CSV: |
---|
[5567] | 1004 | printf("%s,,%s,%s\n", roar_proto2str(p->proto), subsys, description); |
---|
[5275] | 1005 | break; |
---|
| 1006 | } |
---|
| 1007 | } |
---|
| 1008 | } |
---|
[4679] | 1009 | |
---|
[0] | 1010 | int client_stream_exec (int client, int stream) { |
---|
[4326] | 1011 | struct roar_client * c; |
---|
[0] | 1012 | int i; |
---|
[3920] | 1013 | int fh; |
---|
[0] | 1014 | |
---|
[3910] | 1015 | _CHECK_CID(client); |
---|
| 1016 | |
---|
| 1017 | #if 0 |
---|
[1901] | 1018 | if ( g_clients[client] == NULL ) { |
---|
| 1019 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not exist", client, stream); |
---|
[0] | 1020 | return -1; |
---|
[1901] | 1021 | } |
---|
[3910] | 1022 | #endif |
---|
[0] | 1023 | |
---|
[4326] | 1024 | c = ROAR_CLIENT(g_clients[client]); |
---|
| 1025 | |
---|
[0] | 1026 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
[4326] | 1027 | if ( c->streams[i] == stream ) { |
---|
| 1028 | c->execed = stream; |
---|
[3921] | 1029 | if ( streams_is_ready(stream) == 0 ) { |
---|
[4326] | 1030 | streams_set_fh(stream, c->fh); |
---|
[3920] | 1031 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR); |
---|
| 1032 | } else { |
---|
[4254] | 1033 | ROAR_DBG("client_stream_exec(client=%i, stream=%i): fh=?", client, stream); |
---|
[4326] | 1034 | if ( (fh = c->fh) != -1 ) { |
---|
[3925] | 1035 | close(fh); |
---|
[4326] | 1036 | c->fh = -1; |
---|
[3925] | 1037 | } |
---|
[3920] | 1038 | } |
---|
[2815] | 1039 | ROAR_DBG("client_stream_exec(client=%i, stream=%i) = 0", client, stream); |
---|
[0] | 1040 | return 0; |
---|
| 1041 | } |
---|
| 1042 | } |
---|
| 1043 | |
---|
[1901] | 1044 | ROAR_WARN("client_stream_exec(client=%i, stream=%i) = -1 // client does not own stream", client, stream); |
---|
[0] | 1045 | return -1; |
---|
| 1046 | } |
---|
| 1047 | |
---|
[78] | 1048 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
| 1049 | int i; |
---|
| 1050 | |
---|
[4240] | 1051 | ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i) = ?", client, stream, fh); |
---|
| 1052 | |
---|
[3910] | 1053 | _CHECK_CID(client); |
---|
[78] | 1054 | |
---|
| 1055 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
[4326] | 1056 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) { |
---|
[4240] | 1057 | ROAR_DBG("client_stream_set_fh(client=%i, stream=%i, fh=%i): stream found, index %i", client, stream, fh, i); |
---|
[4229] | 1058 | return streams_set_fh(stream, fh); |
---|
[78] | 1059 | } |
---|
| 1060 | } |
---|
| 1061 | |
---|
[4240] | 1062 | ROAR_WARN("client_stream_set_fh(client=%i, stream=%i, fh=%i) = -1 // client does not own stream", client, stream, fh); |
---|
[78] | 1063 | return -1; |
---|
| 1064 | } |
---|
| 1065 | |
---|
[0] | 1066 | int client_stream_add (int client, int stream) { |
---|
| 1067 | int i; |
---|
| 1068 | |
---|
[3910] | 1069 | _CHECK_CID(client); |
---|
[0] | 1070 | |
---|
| 1071 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
[4326] | 1072 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == -1 ) { |
---|
| 1073 | ROAR_CLIENT(g_clients[client])->streams[i] = stream; |
---|
[0] | 1074 | streams_set_client(stream, client); |
---|
| 1075 | return 0; |
---|
| 1076 | } |
---|
| 1077 | } |
---|
| 1078 | |
---|
| 1079 | return -1; |
---|
| 1080 | } |
---|
| 1081 | |
---|
| 1082 | int client_stream_delete (int client, int stream) { |
---|
| 1083 | int i; |
---|
| 1084 | |
---|
[3910] | 1085 | _CHECK_CID(client); |
---|
[0] | 1086 | |
---|
| 1087 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
[4326] | 1088 | if ( ROAR_CLIENT(g_clients[client])->streams[i] == stream ) { |
---|
| 1089 | ROAR_CLIENT(g_clients[client])->streams[i] = -1; |
---|
[0] | 1090 | |
---|
[4326] | 1091 | if ( stream == ROAR_CLIENT(g_clients[client])->execed ) { |
---|
[0] | 1092 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
| 1093 | clients_delete(client); |
---|
| 1094 | } |
---|
| 1095 | |
---|
| 1096 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
| 1097 | return 0; |
---|
| 1098 | } |
---|
| 1099 | } |
---|
| 1100 | |
---|
| 1101 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
| 1102 | return -1; |
---|
| 1103 | } |
---|
| 1104 | |
---|
[767] | 1105 | int client_stream_move (int client, int stream) { |
---|
| 1106 | int old_client = streams_get_client(stream); |
---|
| 1107 | |
---|
[771] | 1108 | ROAR_DBG("client_stream_move(client=%i, stream=%i): old_client = %i", client, stream, old_client); |
---|
| 1109 | |
---|
[767] | 1110 | if ( old_client != -1 ) |
---|
| 1111 | if ( client_stream_delete(old_client, stream) == -1 ) |
---|
| 1112 | return -1; |
---|
| 1113 | |
---|
| 1114 | return client_stream_add(client, stream); |
---|
| 1115 | } |
---|
| 1116 | |
---|
[4343] | 1117 | |
---|
| 1118 | // notify thingys |
---|
| 1119 | int clients_wait (int client, struct roar_event * events, size_t num) { |
---|
| 1120 | struct roar_client_server * cs; |
---|
| 1121 | size_t i, c; |
---|
| 1122 | |
---|
| 1123 | ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = ?", client, events, (long long unsigned int)num); |
---|
| 1124 | |
---|
| 1125 | _CHECK_CID(client); |
---|
| 1126 | |
---|
| 1127 | cs = g_clients[client]; |
---|
| 1128 | |
---|
| 1129 | if ( cs->waits != NULL ) |
---|
| 1130 | return -1; |
---|
| 1131 | |
---|
| 1132 | cs->waits = roar_mm_malloc((num+1) * sizeof(struct roar_subscriber *)); |
---|
| 1133 | |
---|
| 1134 | if ( cs->waits == NULL ) |
---|
| 1135 | return -1; |
---|
| 1136 | |
---|
| 1137 | if ( clients_block(client, 0) != 0 ) |
---|
| 1138 | return -1; |
---|
| 1139 | |
---|
| 1140 | for (i = 0; i < num; i++) { |
---|
| 1141 | #if defined(DEBUG) && 0 |
---|
| 1142 | dbg_notify_cb(NULL, &(events[i]), cs); |
---|
| 1143 | #endif |
---|
| 1144 | cs->waits[i] = roar_notify_core_subscribe(NULL, &(events[i]), clients_ncb_wait, cs); |
---|
| 1145 | if ( cs->waits[i] == NULL ) { |
---|
| 1146 | for (c = 0; c < i; c++) |
---|
| 1147 | roar_notify_core_unsubscribe(NULL, cs->waits[c]); |
---|
| 1148 | roar_mm_free(cs->waits); |
---|
| 1149 | cs->waits = NULL; |
---|
| 1150 | clients_block(client, 1); |
---|
| 1151 | return -1; |
---|
| 1152 | } |
---|
| 1153 | } |
---|
| 1154 | |
---|
| 1155 | cs->waits[num] = NULL; |
---|
| 1156 | |
---|
| 1157 | ROAR_DBG("clients_wait(client=%i, events=%p, num=%llu) = 0", client, events, (long long unsigned int)num); |
---|
| 1158 | return 0; |
---|
| 1159 | } |
---|
| 1160 | |
---|
| 1161 | void clients_ncb_wait(struct roar_notify_core * core, struct roar_event * event, void * userdata) { |
---|
| 1162 | struct roar_client_server * cs = userdata; |
---|
| 1163 | struct roar_message m; |
---|
| 1164 | struct roar_connection con; |
---|
| 1165 | uint16_t * u16 = (uint16_t *) m.data; |
---|
| 1166 | size_t tmp; |
---|
| 1167 | size_t i; |
---|
| 1168 | |
---|
| 1169 | ROAR_DBG("clients_ncb_wait(core=%p, event=%p, userdata=%p) = ?", core, event, userdata); |
---|
| 1170 | |
---|
| 1171 | for (i = 0; cs->waits[i] != NULL; i++) |
---|
| 1172 | roar_notify_core_unsubscribe(NULL, cs->waits[i]); |
---|
| 1173 | |
---|
| 1174 | roar_mm_free(cs->waits); |
---|
| 1175 | cs->waits = NULL; |
---|
| 1176 | |
---|
| 1177 | // protocol depended handling... |
---|
| 1178 | memset(&m, 0, sizeof(m)); |
---|
| 1179 | m.cmd = ROAR_CMD_OK; |
---|
| 1180 | u16[0] = ROAR_HOST2NET16(0); // Version |
---|
| 1181 | u16[1] = ROAR_HOST2NET16(0); // flags |
---|
| 1182 | |
---|
| 1183 | tmp = sizeof(m.data) - 4; |
---|
| 1184 | |
---|
| 1185 | roar_event_to_blob(event, m.data + 4, &tmp); |
---|
| 1186 | |
---|
| 1187 | m.datalen = tmp + 4; |
---|
| 1188 | |
---|
| 1189 | roar_connect_fh(&con, ROAR_CLIENT(cs)->fh); |
---|
| 1190 | roar_send_message(&con, &m, NULL); |
---|
| 1191 | // ...end of protocol depended handling. |
---|
| 1192 | |
---|
| 1193 | // clients_block(, 1); |
---|
| 1194 | // TODO: FIXME: bad hack... |
---|
| 1195 | cs->blockc--; |
---|
| 1196 | } |
---|
| 1197 | |
---|
[4480] | 1198 | |
---|
| 1199 | // acclev: |
---|
| 1200 | static struct { |
---|
| 1201 | const enum roard_client_acclev acclev; |
---|
| 1202 | const char * name; |
---|
| 1203 | } _g_acclevs[] = { |
---|
| 1204 | {ACCLEV_NONE, "none" }, |
---|
| 1205 | {ACCLEV_IDENTED, "idented"}, |
---|
| 1206 | {ACCLEV_CONCTL, "conctl" }, |
---|
| 1207 | {ACCLEV_GUEST, "guest" }, |
---|
| 1208 | {ACCLEV_USER, "user" }, |
---|
| 1209 | {ACCLEV_PWRUSER, "pwruser"}, |
---|
| 1210 | {ACCLEV_ALL, "all" }, |
---|
| 1211 | {-1, NULL} |
---|
| 1212 | }; |
---|
| 1213 | |
---|
| 1214 | enum roard_client_acclev clients_str2acclev(const char * acclev) { |
---|
| 1215 | int i; |
---|
| 1216 | |
---|
| 1217 | for (i = 0; _g_acclevs[i].name != NULL; i++) |
---|
| 1218 | if ( !strcasecmp(_g_acclevs[i].name, acclev) ) |
---|
| 1219 | return _g_acclevs[i].acclev; |
---|
| 1220 | |
---|
[5146] | 1221 | return ACCLEV_ERROR; |
---|
[4480] | 1222 | } |
---|
| 1223 | |
---|
| 1224 | const char * clients_acclev2str(const enum roard_client_acclev acclev) { |
---|
| 1225 | int i; |
---|
| 1226 | |
---|
| 1227 | for (i = 0; _g_acclevs[i].name != NULL; i++) |
---|
| 1228 | if ( _g_acclevs[i].acclev == acclev ) |
---|
| 1229 | return _g_acclevs[i].name; |
---|
| 1230 | |
---|
| 1231 | return NULL; |
---|
| 1232 | } |
---|
| 1233 | |
---|
[0] | 1234 | //ll |
---|