mirror of
https://github.com/qicosmos/rest_rpc.git
synced 2026-08-29 16:40:48 +08:00
uodate
This commit is contained in:
@@ -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<std::thread>([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_<<" "<<port_ << std::endl;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void run() {
|
||||
ios_.run();
|
||||
}
|
||||
|
||||
bool has_connected() const {
|
||||
return has_connected_;
|
||||
}
|
||||
|
||||
void stop() {
|
||||
if (thd_ != nullptr) {
|
||||
ios_.stop();
|
||||
thd_->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<std::thread> 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_;
|
||||
};
|
||||
+11
-1
@@ -1,5 +1,6 @@
|
||||
#include <iostream>
|
||||
#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<std::string>("get_person_name", person{1, "tom", 20});
|
||||
auto result = client.call<std::string>("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();
|
||||
|
||||
@@ -120,6 +120,7 @@
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="async_client.hpp" />
|
||||
<ClInclude Include="test_client.hpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -18,6 +18,9 @@
|
||||
<ClInclude Include="test_client.hpp">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="async_client.hpp">
|
||||
<Filter>头文件</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp">
|
||||
|
||||
Reference in New Issue
Block a user