From 90e7ed973f08c06681b1a82ae84854680275005c Mon Sep 17 00:00:00 2001 From: ackimixs Date: Thu, 23 May 2024 16:17:51 +0200 Subject: [PATCH] non blocking cin --- example/client.example.cpp | 82 ++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 38 deletions(-) diff --git a/example/client.example.cpp b/example/client.example.cpp index f72438c..e9d115f 100644 --- a/example/client.example.cpp +++ b/example/client.example.cpp @@ -5,72 +5,78 @@ #include #include #include +#include +#include +#include +#include +#include +#include std::atomic shouldStop = false; -void signalHandler( int signum ) { +void signalHandler(int signum) { shouldStop = true; } -void messageHandler(TCPClient& client) { - std::string message; +void userInputHandler(const std::shared_ptr& client) { + std::string input; - while (!shouldStop && !client.shouldStop()) { - std::getline(std::cin, message); + while (!client->shouldStop() && !shouldStop) { + fd_set read_fds; + FD_ZERO(&read_fds); + FD_SET(STDIN_FILENO, &read_fds); - if (message == "quit") { - client.stop(); + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 1000000; // 100 milliseconds + + int result = select(STDIN_FILENO + 1, &read_fds, nullptr, nullptr, &timeout); + + if (result > 0 && FD_ISSET(STDIN_FILENO, &read_fds)) { + std::string line; + std::getline(std::cin, line); + if (!line.empty()) { + client->sendMessage(line); + } + } else if (result < 0) { + // Some other error occurred + std::cerr << "Error reading input" << std::endl; break; } - if (message == "ready") { - client.sendMessage("lidar;strat;ready;1"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client.sendMessage("aruco;strat;ready;1"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client.sendMessage("arduino;strat;ready;1"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client.sendMessage("tirette;strat;ready;1"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client.sendMessage("servo_moteur;strat;ready;1"); - } else if (message == "pong") { - client.sendMessage("lidar;ihm;pong;1"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client.sendMessage("aruco;ihm;pong;1"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client.sendMessage("arduino;ihm;pong;1"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client.sendMessage("tirette;ihm;pong;1"); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - client.sendMessage("servo_moteur;ihm;pong;1"); - } else { - client.sendMessage(message); - } } } int main(int argc, char* argv[]) { signal(SIGINT, signalHandler); + int flags = fcntl(STDIN_FILENO, F_GETFL, 0); + fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); + CLParser clParser(argc, argv); int port = clParser.getOption("port", 8080); - auto host = clParser.getOption("host", "127.0.0.1"); - bool loggerMode = clParser.hasOption("logger"); - TCPClient client(host, port); // Replace "127.0.0.1" with the IP address of your server and 8080 with the port number + std::shared_ptr client = std::make_shared(host, port); - client.start(); + client->start(); + + // Start the user input handling thread + std::thread inputThread; if (!loggerMode) { - std::thread messageThread(messageHandler, std::ref(client)); - messageThread.detach(); + inputThread = std::thread(userInputHandler, client); + inputThread.detach(); } - while (!client.shouldStop() && !shouldStop) { + while (!client->shouldStop() && !shouldStop) { usleep(500'000); } + if (inputThread.joinable()) { + inputThread.join(); + } + return 0; -} \ No newline at end of file +}