diff --git a/test_client/async_client.hpp b/test_client/async_client.hpp index 2a7b1c4..3b55aca 100644 --- a/test_client/async_client.hpp +++ b/test_client/async_client.hpp @@ -9,12 +9,18 @@ using namespace rest_rpc::rpc_service; class async_client : private boost::noncopyable { public: async_client(const std::string& host, unsigned short port) : socket_(ios_), work_(ios_), - connect_timer_(ios_), host_(host), port_(port) { + deadline_(ios_), host_(host), port_(port) { + thd_ = std::make_shared([this] { + ios_.run(); + }); } - void set_timeout(int milliseconds) { - assert(milliseconds > 0); - timeout_ = milliseconds; + ~async_client() { + stop(); + } + + void set_connect_timeout(size_t seconds) { + timeout_ = seconds; } void set_reconnect_count(int reconnect_count) { @@ -41,23 +47,28 @@ public: } else { has_connected_ = true; + deadline_.cancel(); std::cout << "connect "<< host_<<" "<join(); + thd_ = nullptr; + } + } + private: void reset_connect_timer() { - connect_timer_.expires_from_now(boost::posix_time::milliseconds(timeout_)); - connect_timer_.async_wait([this](const boost::system::error_code& ec) { + deadline_.expires_from_now(boost::posix_time::seconds((long)timeout_)); + deadline_.async_wait([this](const boost::system::error_code& ec) { if (!ec) { socket_.close(); } @@ -68,11 +79,12 @@ private: boost::asio::io_service ios_; tcp::socket socket_; boost::asio::io_service::work work_; + std::shared_ptr thd_ = nullptr; std::string host_; unsigned short port_; - int timeout_=1000;//ms + int timeout_=1;//s int reconnect_cnt_ = -1; - boost::asio::deadline_timer connect_timer_; + boost::asio::deadline_timer deadline_; }; \ No newline at end of file diff --git a/test_client/main.cpp b/test_client/main.cpp index 8a4c74a..092974a 100644 --- a/test_client/main.cpp +++ b/test_client/main.cpp @@ -1,5 +1,6 @@ #include #include "test_client.hpp" +#include "async_client.hpp" #include "../codec.h" using namespace rest_rpc; @@ -61,7 +62,7 @@ void test_get_person_name() { test_client client(io_service); client.connect("127.0.0.1", "9000"); - auto result = client.call("get_person_name", person{1, "tom", 20}); + auto result = client.call("get_person_name", person{ 1, "tom", 20 }); std::cout << result << std::endl; } catch (const std::exception& e) { @@ -83,7 +84,16 @@ void test_get_person() { } } +void test_async_client() { + async_client client("127.0.0.1", 9000); + client.connect(); + + std::string str; + std::cin >> str; +} + int main() { + test_async_client(); test_get_person(); test_get_person_name(); test_hello(); diff --git a/test_client/test_client.vcxproj b/test_client/test_client.vcxproj index d355e2c..9255cf1 100644 --- a/test_client/test_client.vcxproj +++ b/test_client/test_client.vcxproj @@ -120,6 +120,7 @@ + diff --git a/test_client/test_client.vcxproj.filters b/test_client/test_client.vcxproj.filters index 8832584..afcbb63 100644 --- a/test_client/test_client.vcxproj.filters +++ b/test_client/test_client.vcxproj.filters @@ -18,6 +18,9 @@ 头文件 + + 头文件 +