From 1acb3311b161094a79b0745147a15e79fee1c05d Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Wed, 22 Jan 2025 13:12:18 -0600 Subject: [PATCH] refactor --- .../include/rpc/server/server.hpp | 1 - .../librepertory/src/rpc/server/server.cpp | 110 +++++++++--------- repertory/librepertory/src/utils/polling.cpp | 1 + 3 files changed, 58 insertions(+), 54 deletions(-) diff --git a/repertory/librepertory/include/rpc/server/server.hpp b/repertory/librepertory/include/rpc/server/server.hpp index c8e82912..48735e10 100644 --- a/repertory/librepertory/include/rpc/server/server.hpp +++ b/repertory/librepertory/include/rpc/server/server.hpp @@ -38,7 +38,6 @@ private: std::unique_ptr server_; std::unique_ptr server_thread_; std::mutex start_stop_mutex_; - std::atomic started_ = false; private: [[nodiscard]] auto check_authorization(const httplib::Request &req) -> bool; diff --git a/repertory/librepertory/src/rpc/server/server.cpp b/repertory/librepertory/src/rpc/server/server.cpp index 1424c5f4..ff2d218d 100644 --- a/repertory/librepertory/src/rpc/server/server.cpp +++ b/repertory/librepertory/src/rpc/server/server.cpp @@ -137,63 +137,67 @@ void server::start() { REPERTORY_USES_FUNCTION_NAME(); mutex_lock lock(start_stop_mutex_); - if (not started_) { - event_system::instance().raise("server"); - server_ = std::make_unique(); - - server_->set_exception_handler([](const httplib::Request &req, - httplib::Response &res, - std::exception_ptr ep) { - json data = {{"path", req.path}}; - - try { - std::rethrow_exception(ep); - } catch (std::exception &e) { - data["error"] = e.what() ? e.what() : "unknown error"; - utils::error::raise_error(function_name, e, - "failed request: " + req.path); - } catch (...) { - data["error"] = "unknown error"; - utils::error::raise_error(function_name, "unknown error", - "failed request: " + req.path); - } - - res.set_content(data.dump(), "application/json"); - res.status = 500; - }); - - server_->set_pre_routing_handler( - [this](auto &&req, auto &&res) -> httplib::Server::HandlerResponse { - if (check_authorization(req)) { - return httplib::Server::HandlerResponse::Unhandled; - } - - res.status = 401; - return httplib::Server::HandlerResponse::Handled; - }); - - initialize(*server_); - - server_thread_ = std::make_unique( - [this]() { server_->listen("127.0.0.1", config_.get_api_port()); }); - - started_ = true; + if (server_thread_) { + return; } + + event_system::instance().raise("server"); + server_ = std::make_unique(); + + server_->set_exception_handler([](const httplib::Request &req, + httplib::Response &res, + std::exception_ptr ep) { + json data = {{"path", req.path}}; + + try { + std::rethrow_exception(ep); + } catch (std::exception &e) { + data["error"] = e.what() ? e.what() : "unknown error"; + utils::error::raise_error(function_name, e, + "failed request: " + req.path); + } catch (...) { + data["error"] = "unknown error"; + utils::error::raise_error(function_name, "unknown error", + "failed request: " + req.path); + } + + res.set_content(data.dump(), "application/json"); + res.status = 500; + }); + + server_->set_pre_routing_handler( + [this](auto &&req, auto &&res) -> httplib::Server::HandlerResponse { + if (check_authorization(req)) { + return httplib::Server::HandlerResponse::Unhandled; + } + + res.status = 401; + return httplib::Server::HandlerResponse::Handled; + }); + + initialize(*server_); + + server_thread_ = std::make_unique( + [this]() { server_->listen("127.0.0.1", config_.get_api_port()); }); } void server::stop() { - if (started_) { - mutex_lock l(start_stop_mutex_); - if (started_) { - event_system::instance().raise("server"); - - server_->stop(); - server_thread_->join(); - server_thread_.reset(); - - started_ = false; - event_system::instance().raise("server"); - } + unique_mutex_lock lock(start_stop_mutex_); + if (not server_thread_) { + return; } + + event_system::instance().raise("server"); + + server_->stop(); + + std::unique_ptr thread{nullptr}; + std::swap(thread, server_thread_); + lock.unlock(); + + thread->join(); + thread.reset(); + + event_system::instance().raise("server"); } } // namespace repertory diff --git a/repertory/librepertory/src/utils/polling.cpp b/repertory/librepertory/src/utils/polling.cpp index 57c0899a..fbda7798 100644 --- a/repertory/librepertory/src/utils/polling.cpp +++ b/repertory/librepertory/src/utils/polling.cpp @@ -142,6 +142,7 @@ void polling::stop() { } event_system::instance().raise("polling"); + stop_requested_ = true; tasks::instance().stop();