| 493 | } |
| 494 | |
| 495 | s32 ConsoleServer::run_input_thread() |
| 496 | { |
| 497 | while (!_thread_exit) { |
| 498 | // Wait for input from one of the sockets in _active_socket_set. |
| 499 | _read_socket_set = _active_socket_set; |
| 500 | SelectResult ret = _read_socket_set.select(100); |
| 501 | if (ret.error == SelectResult::GENERIC_ERROR) { |
| 502 | return -1; |
| 503 | } else if (ret.error == SelectResult::TIMEOUT) { |
| 504 | continue; |
| 505 | } |
| 506 | |
| 507 | FileBuffer fb(*_input_write); |
| 508 | BinaryWriter bw(fb); |
| 509 | // Read data from all clients that are ready. |
| 510 | const u32 num_sockets = _read_socket_set.num(); |
| 511 | for (u32 ii = 0; ii < num_sockets; ++ii) { |
| 512 | TCPSocket cur_socket = _read_socket_set.get(ii); |
| 513 | |
| 514 | // Skip if socket is not ready for reading. |
| 515 | if (_read_socket_set.isset(&cur_socket) == false) |
| 516 | continue; |
| 517 | |
| 518 | // If ready socket is the one listening for incoming connections. |
| 519 | if (cur_socket == _server) { |
| 520 | // Accept the incoming connection. |
| 521 | TCPSocket client; |
| 522 | AcceptResult ar = _server.accept_nonblock(client); |
| 523 | if (ar.error == AcceptResult::SUCCESS) { |
| 524 | console_server_internal::add_client(*this, client); |
| 525 | _active_socket_set.set(&client); |
| 526 | _client_connected.post(); |
| 527 | } |
| 528 | |
| 529 | if (_thread_exit) |
| 530 | break; |
| 531 | } else { // Check if any other socket is ready for reading. |
| 532 | u32 msg_len = 0; |
| 533 | ReadResult rr = cur_socket.read(&msg_len, 4); |
| 534 | |
| 535 | if (rr.error != ReadResult::SUCCESS) { |
| 536 | console_server_internal::remove_client_by_socket(*this, cur_socket); |
| 537 | _active_socket_set.clr(&cur_socket); |
| 538 | cur_socket.close(); |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | const u32 client_id = console_server_internal::get_client_id(*this, cur_socket); |
| 543 | |
| 544 | // Add client header and message length. |
| 545 | bw.write(client_id); |
| 546 | bw.write(msg_len); |
| 547 | |
| 548 | // Read message. |
| 549 | u32 num_read; |
| 550 | for (num_read = 0; num_read < msg_len;) { |
| 551 | char buf[4096]; |
| 552 | const u32 num_pending = min(u32(sizeof(buf)), msg_len - num_read); |
no test coverage detected