Add SIGINT and SIGTERM handle

This commit is contained in:
2024-05-08 15:04:46 +02:00
parent 01448a9f23
commit 81f3cebe20
2 changed files with 21 additions and 4 deletions

1
.gitignore vendored
View File

@@ -125,3 +125,4 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
.idea

View File

@@ -1,6 +1,16 @@
#include "MyTCPClient.h"
#include <atomic>
#include <csignal>
std::atomic<bool> shouldStop = false;
void signalHandler( int signum ) {
shouldStop = true;
}
int main(int argc, char* argv[]) {
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
int port = 8080;
if (argc > 1) {
port = std::stoi(argv[1]);
@@ -8,12 +18,18 @@ int main(int argc, char* argv[]) {
MyTCPClient client("127.0.0.1", port);
client.start();
try{
client.start();
client.sendMessage("servo_moteur;strat;ready;1");
while(!client.shouldStop()){
usleep(100'000);
}
while (!client.shouldStop()) {
usleep(100'000);
client.sendMessage("servo_moteur;strat;ready;1");
}
catch (const std::exception& e){
std::cerr << e.what() << std::endl;
return 1;
}
return 0;