support quit with ctrl c

private
This commit is contained in:
qicosmos
2022-01-27 08:37:49 +08:00
parent 987a306092
commit 0f60021c67
2 changed files with 49 additions and 26 deletions
+6 -5
View File
@@ -145,9 +145,11 @@ int main() {
std::cout << "remote client address: " << conn->remote_address()
<< " networking error, reason: " << reason << "\n";
});
std::thread thd([&server] {
bool stop = false;
std::thread thd([&server, &stop] {
person p{1, "tom", 20};
while (true) {
while (!stop) {
server.publish("key", "hello subscriber");
auto list = server.get_token_list();
for (auto &token : list) {
@@ -159,7 +161,6 @@ int main() {
});
server.run();
std::string str;
std::cin >> str;
stop = true;
thd.join();
}
+43 -21
View File
@@ -19,11 +19,18 @@ public:
size_t check_seconds = 10)
: io_service_pool_(size), acceptor_(io_service_pool_.get_io_service(),
tcp::endpoint(tcp::v4(), port)),
timeout_seconds_(timeout_seconds), check_seconds_(check_seconds) {
timeout_seconds_(timeout_seconds), check_seconds_(check_seconds),
signals_(io_service_pool_.get_io_service()) {
do_accept();
check_thread_ = std::make_shared<std::thread>([this] { clean(); });
pub_sub_thread_ =
std::make_shared<std::thread>([this] { clean_sub_pub(); });
signals_.add(SIGINT);
signals_.add(SIGTERM);
#if defined(SIGQUIT)
signals_.add(SIGQUIT);
#endif // defined(SIGQUIT)
do_await_stop();
}
rpc_server(unsigned short port, size_t size, ssl_configure ssl_conf,
@@ -37,26 +44,7 @@ public:
#endif
}
~rpc_server() {
{
std::unique_lock<std::mutex> lock(mtx_);
stop_check_ = true;
cv_.notify_all();
}
check_thread_->join();
{
std::unique_lock<std::mutex> lock(sub_mtx_);
stop_check_pub_sub_ = true;
sub_cv_.notify_all();
}
pub_sub_thread_->join();
io_service_pool_.stop();
if (thd_) {
thd_->join();
}
}
~rpc_server() { stop(); }
void async_run() {
thd_ = std::make_shared<std::thread>([this] { io_service_pool_.run(); });
@@ -207,6 +195,37 @@ private:
return std::make_shared<std::string>(buf.data(), buf.size());
}
void do_await_stop() {
signals_.async_wait(
[this](std::error_code /*ec*/, int /*signo*/) { stop(); });
}
void stop() {
if (has_stoped_) {
return;
}
{
std::unique_lock<std::mutex> lock(mtx_);
stop_check_ = true;
cv_.notify_all();
}
check_thread_->join();
{
std::unique_lock<std::mutex> lock(sub_mtx_);
stop_check_pub_sub_ = true;
sub_cv_.notify_all();
}
pub_sub_thread_->join();
io_service_pool_.stop();
if (thd_) {
thd_->join();
}
has_stoped_ = true;
}
io_service_pool io_service_pool_;
tcp::acceptor acceptor_;
std::shared_ptr<connection> conn_;
@@ -221,6 +240,8 @@ private:
bool stop_check_ = false;
std::condition_variable cv_;
asio::signal_set signals_;
std::function<void(int64_t)> conn_timeout_callback_;
std::function<void(std::shared_ptr<connection>, std::string)>
on_net_err_callback_ = nullptr;
@@ -234,6 +255,7 @@ private:
ssl_configure ssl_conf_;
router router_;
std::atomic_bool has_stoped_ = {false};
};
} // namespace rpc_service
} // namespace rest_rpc