refactor
This commit is contained in:
@ -38,7 +38,6 @@ private:
|
|||||||
std::unique_ptr<httplib::Server> server_;
|
std::unique_ptr<httplib::Server> server_;
|
||||||
std::unique_ptr<std::thread> server_thread_;
|
std::unique_ptr<std::thread> server_thread_;
|
||||||
std::mutex start_stop_mutex_;
|
std::mutex start_stop_mutex_;
|
||||||
std::atomic<bool> started_ = false;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
[[nodiscard]] auto check_authorization(const httplib::Request &req) -> bool;
|
[[nodiscard]] auto check_authorization(const httplib::Request &req) -> bool;
|
||||||
|
@ -137,7 +137,10 @@ void server::start() {
|
|||||||
REPERTORY_USES_FUNCTION_NAME();
|
REPERTORY_USES_FUNCTION_NAME();
|
||||||
|
|
||||||
mutex_lock lock(start_stop_mutex_);
|
mutex_lock lock(start_stop_mutex_);
|
||||||
if (not started_) {
|
if (server_thread_) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event_system::instance().raise<service_started>("server");
|
event_system::instance().raise<service_started>("server");
|
||||||
server_ = std::make_unique<httplib::Server>();
|
server_ = std::make_unique<httplib::Server>();
|
||||||
|
|
||||||
@ -176,24 +179,25 @@ void server::start() {
|
|||||||
|
|
||||||
server_thread_ = std::make_unique<std::thread>(
|
server_thread_ = std::make_unique<std::thread>(
|
||||||
[this]() { server_->listen("127.0.0.1", config_.get_api_port()); });
|
[this]() { server_->listen("127.0.0.1", config_.get_api_port()); });
|
||||||
|
|
||||||
started_ = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void server::stop() {
|
void server::stop() {
|
||||||
if (started_) {
|
unique_mutex_lock lock(start_stop_mutex_);
|
||||||
mutex_lock l(start_stop_mutex_);
|
if (not server_thread_) {
|
||||||
if (started_) {
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event_system::instance().raise<service_shutdown_begin>("server");
|
event_system::instance().raise<service_shutdown_begin>("server");
|
||||||
|
|
||||||
server_->stop();
|
server_->stop();
|
||||||
server_thread_->join();
|
|
||||||
server_thread_.reset();
|
|
||||||
|
|
||||||
started_ = false;
|
std::unique_ptr<std::thread> thread{nullptr};
|
||||||
|
std::swap(thread, server_thread_);
|
||||||
|
lock.unlock();
|
||||||
|
|
||||||
|
thread->join();
|
||||||
|
thread.reset();
|
||||||
|
|
||||||
event_system::instance().raise<service_shutdown_end>("server");
|
event_system::instance().raise<service_shutdown_end>("server");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
} // namespace repertory
|
} // namespace repertory
|
||||||
|
@ -142,6 +142,7 @@ void polling::stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
event_system::instance().raise<service_shutdown_begin>("polling");
|
event_system::instance().raise<service_shutdown_begin>("polling");
|
||||||
|
|
||||||
stop_requested_ = true;
|
stop_requested_ = true;
|
||||||
|
|
||||||
tasks::instance().stop();
|
tasks::instance().stop();
|
||||||
|
Reference in New Issue
Block a user