From faeed90e7e6ba86bd1e5c170ff8ca22c0e3f2abb Mon Sep 17 00:00:00 2001 From: qicosmos Date: Mon, 16 Nov 2020 17:57:24 +0800 Subject: [PATCH] one server one router. --- include/rest_rpc/connection.h | 9 +++++---- include/rest_rpc/router.h | 5 ----- include/rest_rpc/rpc_server.h | 9 +++++---- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/include/rest_rpc/connection.h b/include/rest_rpc/connection.h index aeb839b..97230d1 100644 --- a/include/rest_rpc/connection.h +++ b/include/rest_rpc/connection.h @@ -21,12 +21,13 @@ namespace rest_rpc { class connection : public std::enable_shared_from_this, private asio::noncopyable { public: - connection(boost::asio::io_service& io_service, std::size_t timeout_seconds) + connection(boost::asio::io_service& io_service, std::size_t timeout_seconds, router& router) : socket_(io_service), body_(INIT_BUF_SIZE), timer_(io_service), timeout_seconds_(timeout_seconds), - has_closed_(false) { + has_closed_(false), + router_(router){ } ~connection() { @@ -173,8 +174,7 @@ namespace rest_rpc { if (!ec) { read_head(); if (req_type_ == request_type::req_res) { - router& _router = router::get(); - _router.route(body_.data(), length, this->shared_from_this()); + router_.route(body_.data(), length, this->shared_from_this()); } else if (req_type_ == request_type::sub_pub) { try { @@ -363,6 +363,7 @@ namespace rest_rpc { std::deque write_queue_; std::function)> callback_; + router& router_; }; } // namespace rpc_service } // namespace rest_rpc diff --git a/include/rest_rpc/router.h b/include/rest_rpc/router.h index 7693159..bfc789e 100644 --- a/include/rest_rpc/router.h +++ b/include/rest_rpc/router.h @@ -15,11 +15,6 @@ namespace rest_rpc { class router : asio::noncopyable { public: - static router& get() { - static router instance; - return instance; - } - template void register_handler(std::string const& name, Function f) { return register_nonmember_func(name, std::move(f)); diff --git a/include/rest_rpc/rpc_server.h b/include/rest_rpc/rpc_server.h index 6783017..2b0ed99 100644 --- a/include/rest_rpc/rpc_server.h +++ b/include/rest_rpc/rpc_server.h @@ -65,12 +65,12 @@ namespace rest_rpc { template void register_handler(std::string const& name, const Function& f) { - router::get().register_handler(name, f); + router_.register_handler(name, f); } template void register_handler(std::string const& name, const Function& f, Self* self) { - router::get().register_handler(name, f, self); + router_.register_handler(name, f, self); } void set_conn_timeout_callback(std::function callback) { @@ -94,7 +94,7 @@ namespace rest_rpc { private: void do_accept() { - conn_.reset(new connection(io_service_pool_.get_io_service(), timeout_seconds_)); + conn_.reset(new connection(io_service_pool_.get_io_service(), timeout_seconds_, router_)); conn_->set_callback([this](std::string key, std::string token, std::weak_ptr conn) { std::unique_lock lock(sub_mtx_); sub_map_.emplace(std::move(key) + token, conn); @@ -217,7 +217,8 @@ namespace rest_rpc { std::shared_ptr pub_sub_thread_; bool stop_check_pub_sub_ = false; - ssl_configure ssl_conf_; + ssl_configure ssl_conf_; + router router_; }; } // namespace rpc_service } // namespace rest_rpc