void consume_command() // runs in its own thread { try { while (true) { ClientCommand command {queue.get()}; // will block here unless there are still command objects in the queue shared_ptr<Socket> client {command.get_client()}; shared_ptr<Player> player {command.get_player()}; try { m_g->handleCommand(player, command.get_cmd()); // TODO handle command here //*client << player->get_name() << ", you wrote: '" << command.get_cmd() << "', but I'll ignore that for now.\r\n" << machiavelli::prompt; } catch (const exception& ex) { cerr << "*** exception in consumer thread for player " << player->get_name() << ": " << ex.what() << '\n'; if (client->is_open()) { client->write("Sorry, something went wrong during handling of your request.\r\n"); } } catch (...) { cerr << "*** exception in consumer thread for player " << player->get_name() << '\n'; if (client->is_open()) { client->write("Sorry, something went wrong during handling of your request.\r\n"); } } } } catch (...) { cerr << "consume_command crashed\n"; } }
void ServerController::consume_command() { is_consuming = true; while (is_consuming) { ClientCommand command; client_queue.get(command); std::shared_ptr<Socket> client = command.get_client(); if (client) { game_controller->consume_command(command, client); } else std::cerr << "Client has disappeared" << "\n"; } }