one server one router.

This commit is contained in:
qicosmos
2020-11-16 17:57:24 +08:00
parent 4db62f0511
commit faeed90e7e
3 changed files with 10 additions and 13 deletions
+5 -4
View File
@@ -21,12 +21,13 @@ namespace rest_rpc {
class connection : public std::enable_shared_from_this<connection>, private asio::noncopyable { class connection : public std::enable_shared_from_this<connection>, private asio::noncopyable {
public: 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), : socket_(io_service),
body_(INIT_BUF_SIZE), body_(INIT_BUF_SIZE),
timer_(io_service), timer_(io_service),
timeout_seconds_(timeout_seconds), timeout_seconds_(timeout_seconds),
has_closed_(false) { has_closed_(false),
router_(router){
} }
~connection() { ~connection() {
@@ -173,8 +174,7 @@ namespace rest_rpc {
if (!ec) { if (!ec) {
read_head(); read_head();
if (req_type_ == request_type::req_res) { if (req_type_ == request_type::req_res) {
router& _router = router::get(); router_.route<connection>(body_.data(), length, this->shared_from_this());
_router.route<connection>(body_.data(), length, this->shared_from_this());
} }
else if (req_type_ == request_type::sub_pub) { else if (req_type_ == request_type::sub_pub) {
try { try {
@@ -363,6 +363,7 @@ namespace rest_rpc {
std::deque<message_type> write_queue_; std::deque<message_type> write_queue_;
std::function<void(std::string, std::string, std::weak_ptr<connection>)> callback_; std::function<void(std::string, std::string, std::weak_ptr<connection>)> callback_;
router& router_;
}; };
} // namespace rpc_service } // namespace rpc_service
} // namespace rest_rpc } // namespace rest_rpc
-5
View File
@@ -15,11 +15,6 @@ namespace rest_rpc {
class router : asio::noncopyable { class router : asio::noncopyable {
public: public:
static router& get() {
static router instance;
return instance;
}
template<ExecMode model, typename Function> template<ExecMode model, typename Function>
void register_handler(std::string const& name, Function f) { void register_handler(std::string const& name, Function f) {
return register_nonmember_func<model>(name, std::move(f)); return register_nonmember_func<model>(name, std::move(f));
+5 -4
View File
@@ -65,12 +65,12 @@ namespace rest_rpc {
template<ExecMode model = ExecMode::sync, typename Function> template<ExecMode model = ExecMode::sync, typename Function>
void register_handler(std::string const& name, const Function& f) { void register_handler(std::string const& name, const Function& f) {
router::get().register_handler<model>(name, f); router_.register_handler<model>(name, f);
} }
template<ExecMode model = ExecMode::sync, typename Function, typename Self> template<ExecMode model = ExecMode::sync, typename Function, typename Self>
void register_handler(std::string const& name, const Function& f, Self* self) { void register_handler(std::string const& name, const Function& f, Self* self) {
router::get().register_handler<model>(name, f, self); router_.register_handler<model>(name, f, self);
} }
void set_conn_timeout_callback(std::function<void(int64_t)> callback) { void set_conn_timeout_callback(std::function<void(int64_t)> callback) {
@@ -94,7 +94,7 @@ namespace rest_rpc {
private: private:
void do_accept() { 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<connection> conn) { conn_->set_callback([this](std::string key, std::string token, std::weak_ptr<connection> conn) {
std::unique_lock<std::mutex> lock(sub_mtx_); std::unique_lock<std::mutex> lock(sub_mtx_);
sub_map_.emplace(std::move(key) + token, conn); sub_map_.emplace(std::move(key) + token, conn);
@@ -217,7 +217,8 @@ namespace rest_rpc {
std::shared_ptr<std::thread> pub_sub_thread_; std::shared_ptr<std::thread> pub_sub_thread_;
bool stop_check_pub_sub_ = false; bool stop_check_pub_sub_ = false;
ssl_configure ssl_conf_; ssl_configure ssl_conf_;
router router_;
}; };
} // namespace rpc_service } // namespace rpc_service
} // namespace rest_rpc } // namespace rest_rpc