Skip to content

Commit

Permalink
set close() public;
Browse files Browse the repository at this point in the history
add assert for connect;
  • Loading branch information
qicosmos committed Apr 19, 2019
1 parent da76963 commit d0c11da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
7 changes: 6 additions & 1 deletion examples/client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,10 @@ void test_call_with_timeout() {
try {
auto result = client.call<50, person>("get_person");
std::cout << result.name << std::endl;
client.close();
bool r = client.connect();
result = client.call<50, person>("get_person");
std::cout << result.name << std::endl;
}
catch (const std::exception& ex) {
std::cout << ex.what() << std::endl;
Expand All @@ -278,9 +282,10 @@ void test_call_with_timeout() {
}

int main() {
test_call_with_timeout();
test_sync_client();
test_async_client();
test_call_with_timeout();

//test_upload();
//test_download();
//test_performance1();
Expand Down
25 changes: 16 additions & 9 deletions include/rpc_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace rest_rpc {
}

void async_connect() {
assert(port_ != 0);
reset_deadline_timer(connect_timeout_);
auto addr = boost::asio::ip::address::from_string(host_);
socket_.async_connect({ addr, port_ }, [this](const boost::system::error_code& ec) {
Expand Down Expand Up @@ -99,6 +100,7 @@ namespace rest_rpc {
}

bool connect(size_t timeout = 1) {
assert(port_ != 0);
async_connect();
return wait_conn(timeout);
}
Expand Down Expand Up @@ -132,6 +134,20 @@ namespace rest_rpc {
return result;
}

void update_addr(const std::string& host, unsigned short port) {
host_ = host;
port_ = port;
}

void close() {
has_connected_ = false;
if (socket_.is_open()) {
boost::system::error_code ignored_ec;
socket_.shutdown(tcp::socket::shutdown_both, ignored_ec);
socket_.close(ignored_ec);
}
}

void set_error_callback(std::function<void(boost::system::error_code)> f) {
err_cb_ = std::move(f);
}
Expand Down Expand Up @@ -355,15 +371,6 @@ namespace rest_rpc {
});
}

void close() {
has_connected_ = false;
if (socket_.is_open()) {
boost::system::error_code ignored_ec;
socket_.shutdown(tcp::socket::shutdown_both, ignored_ec);
socket_.close(ignored_ec);
}
}

boost::asio::io_service ios_;
tcp::socket socket_;
boost::asio::io_service::work work_;
Expand Down

0 comments on commit d0c11da

Please sign in to comment.