Skip to content

Commit

Permalink
feat: use stdin instead of signal to stop
Browse files Browse the repository at this point in the history
  • Loading branch information
neko-para committed Oct 24, 2023
1 parent b1eee44 commit 2230e3b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions source/MaaRpc/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>
#include <mutex>
#include <string>
#include <thread>

#include "MaaRpc/MaaRpc.h"

Expand All @@ -13,16 +14,6 @@ bool mi = false;
bool quiet = false;
bool quit = false;

void sig_handler(int)
{
if (!quiet) {
std::cout << "Quit from interrupt" << std::endl;
}
quit = true;
std::unique_lock<std::mutex> lock(mutex);
cv.notify_all();
}

int main(int argc, char* argv[])
{
std::string host = "localhost";
Expand Down Expand Up @@ -57,7 +48,6 @@ int main(int argc, char* argv[])
}
std::string server_address(host + ":" + std::to_string(port));
MaaRpcStart(server_address.c_str());
signal(SIGINT, sig_handler);

std::unique_lock<std::mutex> lock(mutex);
if (!quiet) {
Expand All @@ -68,6 +58,15 @@ int main(int argc, char* argv[])
std::cout << "Server listening on " << server_address << std::endl;
}
}

std::thread wait_enter([]() {
std::string row;
std::getline(std::cin, row);
quit = true;
std::unique_lock<std::mutex> lock(mutex);
cv.notify_all();
});

cv.wait(lock, []() { return quit; });

if (mi) {
Expand All @@ -84,5 +83,6 @@ int main(int argc, char* argv[])
else {
std::cout << "Exit" << std::endl;
}
wait_enter.join();
return 0;
}

0 comments on commit 2230e3b

Please sign in to comment.