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 {
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<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) {
try {
@@ -363,6 +363,7 @@ namespace rest_rpc {
std::deque<message_type> write_queue_;
std::function<void(std::string, std::string, std::weak_ptr<connection>)> callback_;
router& router_;
};
} // namespace rpc_service
} // namespace rest_rpc
-5
View File
@@ -15,11 +15,6 @@ namespace rest_rpc {
class router : asio::noncopyable {
public:
static router& get() {
static router instance;
return instance;
}
template<ExecMode model, typename Function>
void register_handler(std::string const& name, Function 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>
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>
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) {
@@ -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<connection> conn) {
std::unique_lock<std::mutex> lock(sub_mtx_);
sub_map_.emplace(std::move(key) + token, conn);
@@ -217,7 +217,8 @@ namespace rest_rpc {
std::shared_ptr<std::thread> 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