From bc717e86312c5ba28c948b5b82e8da57e460517c Mon Sep 17 00:00:00 2001 From: qicosmos Date: Fri, 20 Aug 2021 21:00:33 +0800 Subject: [PATCH] add a method: on networking error --- examples/server/main.cpp | 6 +++++- include/rest_rpc/connection.h | 34 +++++++++++++++++++++++++++++++--- include/rest_rpc/rpc_server.h | 11 +++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/examples/server/main.cpp b/examples/server/main.cpp index f4c095c..7ec938d 100644 --- a/examples/server/main.cpp +++ b/examples/server/main.cpp @@ -140,7 +140,11 @@ int main() { std::string token, std::string val) { server.publish(std::move(key), std::move(val)); }); - + server.set_network_err_callback( + [](std::shared_ptr conn, std::string reason) { + std::cout << "remote client address: " << conn->remote_address() + << " networking error, reason: " << reason << "\n"; + }); std::thread thd([&server] { person p{1, "tom", 20}; while (true) { diff --git a/include/rest_rpc/connection.h b/include/rest_rpc/connection.h index 973f22e..0f10082 100644 --- a/include/rest_rpc/connection.h +++ b/include/rest_rpc/connection.h @@ -96,6 +96,11 @@ public: callback_ = std::move(callback); } + void on_network_error(std::function, + std::string)> &on_net_err) { + on_net_err_ = &on_net_err; + } + void init_ssl_context(const ssl_configure &ssl_conf) { #ifdef CINATRA_ENABLE_SSL unsigned long ssl_options = boost::asio::ssl::context::default_workarounds | @@ -136,7 +141,9 @@ private: async_read_head( [this, self](boost::system::error_code ec, std::size_t length) { if (!socket_.is_open()) { - // LOG(INFO) << "socket already closed"; + if (on_net_err_) { + (*on_net_err_)(self, "socket already closed"); + } return; } @@ -160,10 +167,16 @@ private: read_head(); } else { print("invalid body len"); + if (on_net_err_) { + (*on_net_err_)(self, "invalid body len"); + } close(); } } else { print(ec); + if (on_net_err_) { + (*on_net_err_)(self, ec.message()); + } close(); } }); @@ -176,7 +189,9 @@ private: cancel_timer(); if (!socket_.is_open()) { - // LOG(INFO) << "socket already closed"; + if (on_net_err_) { + (*on_net_err_)(self, "socket already closed"); + } return; } @@ -194,10 +209,15 @@ private: this->shared_from_this()); } catch (const std::exception &ex) { print(ex); + if (on_net_err_) { + (*on_net_err_)(self, ex.what()); + } } } } else { - // LOG(INFO) << ec.message(); + if (on_net_err_) { + (*on_net_err_)(self, ec.message()); + } } }); } @@ -221,6 +241,9 @@ private: void on_write(boost::system::error_code ec, std::size_t length) { if (ec) { print(ec); + if (on_net_err_) { + (*on_net_err_)(shared_from_this(), ec.message()); + } close(false); return; } @@ -245,6 +268,9 @@ private: [this, self](const boost::system::error_code &error) { if (error) { print(error); + if (on_net_err_) { + (*on_net_err_)(self, error.message()); + } close(); return; } @@ -385,6 +411,8 @@ private: std::deque write_queue_; std::function)> callback_; + std::function, std::string)> *on_net_err_ = + nullptr; router &router_; nonstd::any user_data_; }; diff --git a/include/rest_rpc/rpc_server.h b/include/rest_rpc/rpc_server.h index 685facb..207b1a3 100644 --- a/include/rest_rpc/rpc_server.h +++ b/include/rest_rpc/rpc_server.h @@ -79,6 +79,12 @@ public: conn_timeout_callback_ = std::move(callback); } + void set_network_err_callback( + std::function, std::string /*reason*/)> + on_net_err) { + on_net_err_callback_ = std::move(on_net_err); + } + template void publish(const std::string &key, T data) { publish(key, "", std::move(data)); } @@ -105,6 +111,9 @@ private: token_list_.emplace(std::move(token)); } }); + if (on_net_err_callback_) { + conn_->on_network_error(on_net_err_callback_); + } acceptor_.async_accept(conn_->socket(), [this](boost::system::error_code ec) { @@ -214,6 +223,8 @@ private: std::condition_variable cv_; std::function conn_timeout_callback_; + std::function, std::string)> + on_net_err_callback_ = nullptr; std::unordered_multimap> sub_map_; std::set token_list_; std::mutex sub_mtx_;