[0] | 1 | //clients.c: |
---|
| 2 | |
---|
| 3 | #include "roard.h" |
---|
| 4 | |
---|
| 5 | int clients_init (void) { |
---|
| 6 | int i; |
---|
| 7 | |
---|
| 8 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
| 9 | g_clients[i] = NULL; |
---|
| 10 | |
---|
| 11 | return 0; |
---|
| 12 | } |
---|
| 13 | |
---|
| 14 | int clients_free (void) { |
---|
| 15 | int i; |
---|
| 16 | |
---|
| 17 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) |
---|
| 18 | if ( g_clients[i] ) |
---|
| 19 | clients_delete(i); |
---|
| 20 | |
---|
| 21 | return 0; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | int clients_new (void) { |
---|
| 25 | int i; |
---|
| 26 | int s; |
---|
| 27 | struct roar_client * n; |
---|
| 28 | |
---|
| 29 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 30 | if ( g_clients[i] == NULL ) { |
---|
| 31 | n = malloc(sizeof(struct roar_client)); |
---|
| 32 | if ( n != NULL ) { |
---|
| 33 | n->pid = -1; |
---|
[437] | 34 | n->uid = -1; |
---|
| 35 | n->gid = -1; |
---|
[0] | 36 | n->fh = -1; |
---|
| 37 | |
---|
| 38 | *n->name = 0; |
---|
| 39 | *n->host = 0; |
---|
| 40 | |
---|
[346] | 41 | n->acl = NULL; |
---|
| 42 | |
---|
[0] | 43 | n->execed = -1; |
---|
| 44 | for (s = 0; s < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; s++) |
---|
| 45 | n->streams[s] = -1; |
---|
| 46 | |
---|
| 47 | g_clients[i] = n; |
---|
| 48 | |
---|
| 49 | ROAR_DBG("clients_new(void) = %i", i); |
---|
| 50 | return i; |
---|
| 51 | } else { |
---|
| 52 | ROAR_ERR("clients_new(void): Can not alloc memory for new client: %s", strerror(errno)); |
---|
| 53 | ROAR_ERR("clients_new(void) = -1"); |
---|
| 54 | return -1; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | return -1; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | int clients_delete (int id) { |
---|
| 63 | int i; |
---|
| 64 | |
---|
| 65 | if ( g_clients[id] == NULL ) |
---|
| 66 | return -1; |
---|
| 67 | |
---|
| 68 | if (g_clients[id]->execed != -1) { |
---|
| 69 | // return streams_delete(g_clients[id]->execed); |
---|
| 70 | g_clients[id]->execed = -1; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 74 | streams_delete(g_clients[id]->streams[i]); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | if ( g_clients[id]->fh != -1 ) |
---|
| 78 | close(g_clients[id]->fh); |
---|
| 79 | |
---|
| 80 | free(g_clients[id]); |
---|
| 81 | g_clients[id] = NULL; |
---|
| 82 | |
---|
| 83 | ROAR_DBG("clients_delete(id=%i) = 0", id); |
---|
| 84 | return 0; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | int clients_get (int id, struct roar_client ** client) { |
---|
| 88 | *client = g_clients[id]; |
---|
| 89 | |
---|
| 90 | if ( *client == NULL ) |
---|
| 91 | return -1; |
---|
| 92 | |
---|
| 93 | return 0; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | int clients_set_fh (int id, int fh) { |
---|
[501] | 97 | |
---|
[0] | 98 | if ( g_clients[id] == NULL ) |
---|
| 99 | return -1; |
---|
| 100 | |
---|
| 101 | g_clients[id]->fh = fh; |
---|
| 102 | |
---|
| 103 | return 0; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | int clients_set_pid (int id, int pid) { |
---|
| 107 | if ( g_clients[id] == NULL ) |
---|
| 108 | return -1; |
---|
| 109 | |
---|
| 110 | g_clients[id]->pid = pid; |
---|
| 111 | |
---|
| 112 | return 0; |
---|
| 113 | } |
---|
| 114 | |
---|
[439] | 115 | int clients_set_uid (int id, int uid) { |
---|
| 116 | if ( g_clients[id] == NULL ) |
---|
| 117 | return -1; |
---|
| 118 | |
---|
| 119 | g_clients[id]->uid = uid; |
---|
| 120 | |
---|
| 121 | return 0; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | int clients_set_gid (int id, int gid) { |
---|
| 125 | if ( g_clients[id] == NULL ) |
---|
| 126 | return -1; |
---|
| 127 | |
---|
| 128 | g_clients[id]->gid = gid; |
---|
| 129 | |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
[498] | 133 | #define MAX_STREAMLESS 8 |
---|
| 134 | |
---|
[0] | 135 | int clients_check_all (void) { |
---|
| 136 | struct timeval tv; |
---|
| 137 | fd_set r, e; |
---|
[66] | 138 | int i, j; |
---|
[0] | 139 | int ret; |
---|
| 140 | int fh; |
---|
| 141 | int max_fh = -1; |
---|
[71] | 142 | int have = 0; |
---|
[498] | 143 | struct { |
---|
| 144 | int id; |
---|
| 145 | int fh; |
---|
| 146 | } streamless[MAX_STREAMLESS]; |
---|
| 147 | int have_streamless = 0; |
---|
| 148 | int have_stream; |
---|
[0] | 149 | |
---|
| 150 | FD_ZERO(&r); |
---|
| 151 | FD_ZERO(&e); |
---|
| 152 | |
---|
| 153 | tv.tv_sec = 0; |
---|
| 154 | tv.tv_usec = 1; |
---|
| 155 | |
---|
| 156 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 157 | if ( g_clients[i] == NULL ) |
---|
| 158 | continue; |
---|
| 159 | |
---|
[66] | 160 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
[71] | 161 | have++; |
---|
| 162 | |
---|
[66] | 163 | FD_SET(fh, &r); |
---|
| 164 | FD_SET(fh, &e); |
---|
| 165 | |
---|
| 166 | if ( fh > max_fh ) |
---|
| 167 | max_fh = fh; |
---|
[84] | 168 | } |
---|
[0] | 169 | |
---|
[498] | 170 | have_stream = 0; |
---|
| 171 | |
---|
[84] | 172 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 173 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
| 174 | FD_SET(fh, &r); |
---|
[0] | 175 | |
---|
[84] | 176 | if ( fh > max_fh ) |
---|
| 177 | max_fh = fh; |
---|
[498] | 178 | |
---|
| 179 | have_stream = 1; |
---|
[66] | 180 | } |
---|
[84] | 181 | //printf("D: client=%i, stream=%i, fh=%i\n", i, j, fh); |
---|
[66] | 182 | } |
---|
| 183 | |
---|
[498] | 184 | if ( !have_stream && have_streamless < MAX_STREAMLESS ) { |
---|
| 185 | streamless[have_streamless ].id = i; |
---|
| 186 | if ( (streamless[have_streamless++].fh = g_clients[i]->fh) == -1 ) |
---|
| 187 | have_streamless--; |
---|
| 188 | } |
---|
[0] | 189 | } |
---|
| 190 | |
---|
[256] | 191 | if ( max_fh == -1 ) |
---|
| 192 | return 0; |
---|
| 193 | |
---|
[0] | 194 | if ( (ret = select(max_fh + 1, &r, NULL, &e, &tv)) < 1 ) { |
---|
[71] | 195 | return ret < 0 ? ret : have; |
---|
[0] | 196 | } |
---|
| 197 | |
---|
| 198 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 199 | if ( g_clients[i] == NULL ) |
---|
| 200 | continue; |
---|
| 201 | |
---|
[66] | 202 | if ( (fh = g_clients[i]->fh) != -1 ) { |
---|
| 203 | if ( FD_ISSET(fh, &r) ) { |
---|
| 204 | if ( g_clients[i]->execed == -1 ) { |
---|
| 205 | clients_check(i); |
---|
[255] | 206 | /* |
---|
[66] | 207 | } else { |
---|
| 208 | streams_check(g_clients[i]->execed); |
---|
[255] | 209 | */ |
---|
[66] | 210 | } |
---|
| 211 | } |
---|
[0] | 212 | |
---|
[84] | 213 | if ( FD_ISSET(fh, &e) ) { |
---|
[66] | 214 | clients_delete(i); |
---|
[84] | 215 | continue; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | if ( g_clients[i] == NULL ) |
---|
| 220 | continue; |
---|
| 221 | |
---|
| 222 | for (j = 0; j < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; j++) { |
---|
| 223 | //printf("D: client=%i, stream=%i, g_clients[i=%i] = %p\n", i, j, i, g_clients[i]); |
---|
[136] | 224 | if ( g_clients[i] == NULL ) // streams_check() bellow can delete our client (why?) |
---|
| 225 | break; |
---|
[84] | 226 | if ( (fh = streams_get_fh(g_clients[i]->streams[j])) != -1 ) { |
---|
| 227 | if ( FD_ISSET(fh, &r) ) { |
---|
| 228 | streams_check(g_clients[i]->streams[j]); |
---|
[66] | 229 | } |
---|
[0] | 230 | } |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | |
---|
[498] | 234 | if ( have_streamless ) { |
---|
| 235 | FD_ZERO(&r); |
---|
| 236 | |
---|
| 237 | tv.tv_sec = 0; |
---|
| 238 | tv.tv_usec = 1; |
---|
| 239 | |
---|
| 240 | max_fh = -1; |
---|
| 241 | |
---|
| 242 | for (i = 0; i < have_streamless; i++) { |
---|
| 243 | fh = streamless[i].fh; |
---|
| 244 | |
---|
| 245 | ROAR_DBG("clients_check_all(void): fh=%i", fh); |
---|
| 246 | FD_SET(fh, &r); |
---|
| 247 | |
---|
| 248 | if ( fh > max_fh ) |
---|
| 249 | max_fh = fh; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | if ( (ret = select(max_fh + 1, &r, NULL, NULL, &tv)) < 0 ) { |
---|
| 253 | return ret; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | for (i = 0; i < have_streamless; i++) { |
---|
| 257 | if ( FD_ISSET(streamless[i].fh, &r) ) { |
---|
| 258 | clients_check(streamless[i].id); |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | } |
---|
| 262 | |
---|
[71] | 263 | ROAR_DBG("clients_check_all(void) = %i // have value", have); |
---|
| 264 | return have; |
---|
[0] | 265 | } |
---|
| 266 | |
---|
| 267 | int clients_check (int id) { |
---|
| 268 | struct roar_message m; |
---|
| 269 | struct roar_connection con; |
---|
| 270 | char * data = NULL; |
---|
| 271 | int oldcmd; |
---|
| 272 | int r; |
---|
[498] | 273 | int rv = 0; |
---|
[0] | 274 | |
---|
| 275 | if ( g_clients[id] == NULL ) |
---|
| 276 | return -1; |
---|
| 277 | if ( g_clients[id]->fh == -1 ) |
---|
| 278 | return -1; |
---|
| 279 | |
---|
| 280 | con.fh = g_clients[id]->fh; |
---|
| 281 | |
---|
| 282 | r = roar_recv_message(&con, &m, &data); |
---|
| 283 | |
---|
| 284 | if ( r == -1 ) { // should we drop the client? |
---|
| 285 | clients_delete(id); |
---|
| 286 | return -1; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | roar_debug_message_print(&m); |
---|
| 290 | |
---|
| 291 | oldcmd = m.cmd; |
---|
| 292 | |
---|
| 293 | if ( (r = command_exec(id, &m, data)) == -1 ) { |
---|
| 294 | m.cmd = ROAR_CMD_ERROR; |
---|
| 295 | m.datalen = 0; |
---|
| 296 | ROAR_DBG("clients_check(*): Exec of command faild!"); |
---|
| 297 | } else { |
---|
| 298 | if ( m.cmd == oldcmd ) { |
---|
| 299 | m.cmd = ROAR_CMD_OK; |
---|
| 300 | m.datalen = 0; |
---|
[498] | 301 | } else if ( m.cmd == ROAR_CMD_OK_STOP ) { |
---|
| 302 | m.cmd = ROAR_CMD_OK; |
---|
| 303 | rv = 1; |
---|
[0] | 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | roar_send_message(&con, &m, NULL); |
---|
| 308 | |
---|
| 309 | if ( data ) |
---|
| 310 | free(data); |
---|
| 311 | |
---|
| 312 | ROAR_DBG("clients_check(id=%i) = 0", id); |
---|
[498] | 313 | return rv; |
---|
[0] | 314 | } |
---|
| 315 | |
---|
| 316 | int clients_send_mon (struct roar_audio_info * sa, uint32_t pos) { |
---|
| 317 | int i; |
---|
| 318 | int fh; |
---|
| 319 | |
---|
| 320 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 321 | if ( g_clients[i] == NULL ) |
---|
| 322 | continue; |
---|
| 323 | |
---|
| 324 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 325 | continue; |
---|
| 326 | |
---|
| 327 | if ( g_clients[i]->execed == -1 ) { |
---|
| 328 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 329 | } else { |
---|
| 330 | // streams_check(g_clients[i]->execed); |
---|
| 331 | streams_send_mon(g_clients[i]->execed); |
---|
| 332 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 333 | // clients_delete(i); // delete client in case we could not write |
---|
| 334 | } |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | return -1; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | int clients_send_filter(struct roar_audio_info * sa, uint32_t pos) { |
---|
| 341 | int i; |
---|
| 342 | int fh; |
---|
| 343 | |
---|
| 344 | for (i = 0; i < ROAR_CLIENTS_MAX; i++) { |
---|
| 345 | if ( g_clients[i] == NULL ) |
---|
| 346 | continue; |
---|
| 347 | |
---|
| 348 | if ( (fh = g_clients[i]->fh) == -1 ) |
---|
| 349 | continue; |
---|
| 350 | |
---|
| 351 | if ( g_clients[i]->execed == -1 ) { |
---|
| 352 | // TODO: add some code to send a message to the client insetd of the raw data. |
---|
| 353 | } else { |
---|
| 354 | // streams_check(g_clients[i]->execed); |
---|
| 355 | streams_send_filter(g_clients[i]->execed); |
---|
| 356 | // if ( streams_send_mon(g_clients[i]->execed) == -1 ) |
---|
| 357 | // clients_delete(i); // delete client in case we could not write |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | return -1; |
---|
| 362 | } |
---|
| 363 | |
---|
| 364 | int client_stream_exec (int client, int stream) { |
---|
| 365 | int i; |
---|
| 366 | |
---|
| 367 | if ( g_clients[client] == NULL ) |
---|
| 368 | return -1; |
---|
| 369 | |
---|
| 370 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 371 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 372 | g_clients[client]->execed = stream; |
---|
| 373 | streams_set_fh(stream, g_clients[client]->fh); |
---|
[378] | 374 | streams_set_socktype(stream, ROAR_SOCKET_TYPE_GENSTR); |
---|
[0] | 375 | return 0; |
---|
| 376 | } |
---|
| 377 | } |
---|
| 378 | |
---|
| 379 | return -1; |
---|
| 380 | } |
---|
| 381 | |
---|
[78] | 382 | int client_stream_set_fh (int client, int stream, int fh) { |
---|
| 383 | int i; |
---|
| 384 | |
---|
| 385 | if ( g_clients[client] == NULL ) |
---|
| 386 | return -1; |
---|
| 387 | |
---|
| 388 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 389 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 390 | streams_set_fh(stream, fh); |
---|
| 391 | return 0; |
---|
| 392 | } |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | return -1; |
---|
| 396 | } |
---|
| 397 | |
---|
[0] | 398 | int client_stream_add (int client, int stream) { |
---|
| 399 | int i; |
---|
| 400 | |
---|
| 401 | if ( g_clients[client] == NULL ) |
---|
| 402 | return -1; |
---|
| 403 | |
---|
| 404 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 405 | if ( g_clients[client]->streams[i] == -1 ) { |
---|
| 406 | g_clients[client]->streams[i] = stream; |
---|
| 407 | streams_set_client(stream, client); |
---|
| 408 | return 0; |
---|
| 409 | } |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | return -1; |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | int client_stream_delete (int client, int stream) { |
---|
| 416 | int i; |
---|
| 417 | |
---|
| 418 | if ( g_clients[client] == NULL ) |
---|
| 419 | return -1; |
---|
| 420 | |
---|
| 421 | for (i = 0; i < ROAR_CLIENTS_MAX_STREAMS_PER_CLIENT; i++) { |
---|
| 422 | if ( g_clients[client]->streams[i] == stream ) { |
---|
| 423 | g_clients[client]->streams[i] = -1; |
---|
| 424 | |
---|
| 425 | if ( stream == g_clients[client]->execed ) { |
---|
| 426 | ROAR_DBG("client_stream_delete(client=%i, stream=%i): stream is execed one, deleting client!", client, stream); |
---|
| 427 | clients_delete(client); |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = 0", client, stream); |
---|
| 431 | return 0; |
---|
| 432 | } |
---|
| 433 | } |
---|
| 434 | |
---|
| 435 | ROAR_DBG("client_stream_delete(client=%i, stream=%i) = -1", client, stream); |
---|
| 436 | return -1; |
---|
| 437 | } |
---|
| 438 | |
---|
| 439 | //ll |
---|