diff --git a/examples/client/main.cpp b/examples/client/main.cpp index 723f7a5..f581ea8 100644 --- a/examples/client/main.cpp +++ b/examples/client/main.cpp @@ -108,8 +108,7 @@ void test_get_person() { void test_async_client() { rpc_client client("127.0.0.1", 9000); - client.async_connect(); - bool r = client.wait_conn(1); + bool r = client.connect(); if (!r) { std::cout << "connect timeout" << std::endl; return; @@ -141,8 +140,7 @@ void test_async_client() { void test_upload() { rpc_client client("127.0.0.1", 9000); - client.async_connect(); - bool r = client.wait_conn(1); + bool r = client.connect(1); if (!r) { std::cout << "connect timeout" << std::endl; return; @@ -181,8 +179,7 @@ void test_upload() { void test_download() { rpc_client client("127.0.0.1", 9000); - client.async_connect(); - bool r = client.wait_conn(1); + bool r = client.connect(1); if (!r) { std::cout << "connect timeout" << std::endl; return; @@ -312,13 +309,33 @@ void test_call_with_timeout() { void test_connect() { rpc_client client; - client.set_error_callback([&client](boost::system::error_code ex) { - client.async_reconnect(); - }); - + client.enable_auto_reconnect(); //automatic reconnect bool r = client.connect("127.0.0.1", 9000); - if (!r) { - client.async_reconnect(); + int count = 0; + while (true) { + if (client.has_connected()) { + std::cout << "connected ok\n"; + break; + } + else { + std::cout << "connected failed: "<< count++<<"\n"; + } + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + + { + rpc_client client; + bool r = client.connect("127.0.0.1", 9000); + int count = 0; + while (true) { + if (client.connect()) { + std::cout << "connected ok\n"; + break; + } + else { + std::cout << "connected failed: " << count++ << "\n"; + } + } } std::string str; @@ -477,7 +494,8 @@ void test_threads() { std::cin >> str; } -int main() { +int main() { + test_connect(); test_callback(); test_echo(); test_sync_client(); diff --git a/include/rpc_client.hpp b/include/rpc_client.hpp index 557eba8..ec0af89 100644 --- a/include/rpc_client.hpp +++ b/include/rpc_client.hpp @@ -78,50 +78,6 @@ namespace rest_rpc { reconnect_cnt_ = reconnect_count; } - void set_wait_timeout(size_t seconds) { - wait_timeout_ = seconds; - } - - void async_connect() { - assert(port_ != 0); - auto addr = boost::asio::ip::address::from_string(host_); - socket_.async_connect({ addr, port_ }, [this](const boost::system::error_code& ec) { - if (has_connected_) { - return; - } - - if (ec) { - //std::cout << ec.message() << std::endl; - - has_connected_ = false; - - if (reconnect_cnt_ == 0) { - return; - } - - if (reconnect_cnt_ > 0) { - reconnect_cnt_--; - } - - async_reconnect(); - } - else { - //std::cout<<"connected ok"< 201402L template @@ -278,10 +242,6 @@ namespace rest_rpc { write(cb_id, std::move(ret)); } - bool has_connected() const { - return has_connected_; - } - void stop() { if (thd_ != nullptr) { ios_.stop(); @@ -291,6 +251,46 @@ namespace rest_rpc { } private: + void async_connect() { + assert(port_ != 0); + auto addr = boost::asio::ip::address::from_string(host_); + socket_.async_connect({ addr, port_ }, [this](const boost::system::error_code& ec) { + if (has_connected_) { + return; + } + + if (ec) { + //std::cout << ec.message() << std::endl; + + has_connected_ = false; + + if (reconnect_cnt_ == 0) { + return; + } + + if (reconnect_cnt_ > 0) { + reconnect_cnt_--; + } + + async_reconnect(); + } + else { + //std::cout<<"connected ok"<; void reset_deadline_timer(size_t timeout) { deadline_.expires_from_now(std::chrono::seconds(timeout)); @@ -329,9 +329,7 @@ namespace rest_rpc { if (ec) { has_connected_ = false; close(); - if (err_cb_) { - err_cb_(ec); - } + error_callback(ec); return; } @@ -382,7 +380,7 @@ namespace rest_rpc { //LOG(INFO) << ec.message(); has_connected_ = false; close(); - if (err_cb_) err_cb_(ec); + error_callback(ec); } }); } @@ -409,9 +407,7 @@ namespace rest_rpc { //LOG(INFO) << ec.message(); has_connected_ = false; close(); - if (err_cb_) { - err_cb_(ec); - } + error_callback(ec); } }); } @@ -526,6 +522,22 @@ namespace rest_rpc { bool has_timeout_ = false; }; + void error_callback(const boost::system::error_code& ec) { + if (err_cb_) { + err_cb_(ec); + } + + if (enable_reconnect_) { + async_connect(); + } + } + + void set_default_error_cb() { + err_cb_ = [this](boost::system::error_code){ + async_connect(); + }; + } + boost::asio::io_service ios_; asio::ip::tcp::socket socket_; boost::asio::io_service::work work_; @@ -533,8 +545,7 @@ namespace rest_rpc { std::string host_; unsigned short port_ = 0; - size_t connect_timeout_ = 2000;//s - size_t wait_timeout_ = 2;//s + size_t connect_timeout_ = 1000;//s int reconnect_cnt_ = -1; std::atomic_bool has_connected_ = { false }; std::mutex conn_mtx_; @@ -548,6 +559,7 @@ namespace rest_rpc { std::mutex write_mtx_; uint64_t fu_id_ = 0; std::function err_cb_; + bool enable_reconnect_ = false; std::unordered_map>> future_map_; std::unordered_map> callback_map_;