Skip to content

Commit

Permalink
Public-Auto-release: v2.20.3
Browse files Browse the repository at this point in the history
# Changed
* Bugfix: Add timeout for resolver & connect in C++ implementation
  • Loading branch information
blickfeld-lidar committed Jul 25, 2022
1 parent ef96eba commit 1254f5a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
5 changes: 5 additions & 0 deletions doc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ influence the resulting point cloud.

### Removed

## [2.20.3] - 2022.07.25

### Changed
* Bugfix: Add timeout for resolver & connect in C++ implementation

## [2.20.2] - 2022.07.21

### Changed
Expand Down
62 changes: 46 additions & 16 deletions src/scanner_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,7 @@ scanner_connection::scanner_connection(asio::io_context& io_context, string host
socket(io_context)
#endif
{

string port = to_string(default_server_port);
string::size_type colon_pos = hostname.find(":");
if(colon_pos != string::npos) {
port = hostname.substr(colon_pos + 1);
hostname = hostname.substr(0, colon_pos);
}

asio::ip::tcp::resolver resolver(io_context);
socket.connect(*resolver.resolve(hostname, port).begin());
connect(hostname, default_server_port);
}

#ifdef HAVE_OPENSSL
Expand All @@ -54,19 +45,56 @@ scanner_connection::scanner_connection(asio::io_context& io_context, std::string
timeout(timeout),
socket(std::make_shared<asio::ssl::stream<asio::ip::tcp::socket> >(io_context, ssl_context))
{
string port = to_string(default_server_ssl_port);
connect(hostname, default_server_ssl_port);
socket.handshake(asio::ssl::stream_base::client);
}

#endif

void scanner_connection::connect(const std::string full_hostname, const uint32_t default_port) {
// Determine endpoint
string hostname = full_hostname;
string port = to_string(default_port);
string::size_type colon_pos = hostname.find(":");
if(colon_pos != string::npos) {
port = hostname.substr(colon_pos + 1);
hostname = hostname.substr(0, colon_pos);
}

// Prepare synchronous context
io_context.reset();

// Set up sync flags and deadline timer
auto expired = make_shared<bool>(), connected = make_shared<bool>();
*expired = false, *connected = false;
asio::steady_timer timer(io_context);
timer.expires_from_now(timeout);
timer.async_wait([expired, connected](const asio::error_code& ec) {
*expired = !*connected;
});

// Resolve hostname & connect
asio::ip::tcp::resolver resolver(io_context);
socket.connect(*resolver.resolve(hostname, port).begin());
socket.handshake(asio::ssl::stream_base::client);
}
resolver.async_resolve(hostname, port, [this, connected](const asio::error_code& ec, asio::ip::tcp::resolver::iterator endpoint_iterator) {
if (ec) {
throw protocol::CreateErrorConnectionAbort("Failed to resolve hostname. Failed with error: " + ec.message());
}

#endif
socket.async_connect(*endpoint_iterator, [this, connected](const asio::error_code& ec) {
if (ec) {
throw protocol::CreateErrorConnectionAbort("Failed to connect. Failed with error: " + ec.message());
}

*connected = true;
});
});

while(io_context.run_one() && !*connected) {
if (*expired)
throw protocol::CreateErrorConnectionAbort("Connection timeout after " + to_string(timeout.count()) + " seconds");
}
timer.cancel();
}

void scanner_connection::send(const protocol::Request &req) {
auto msg_size = req.ByteSizeLong();
Expand Down Expand Up @@ -94,6 +122,9 @@ void scanner_connection::recv(protocol::Response &resp) {
} else {
std::istream recv_stream(&input_buffer);

// Prepare synchronous context
io_context.reset();

// Set up sync flags and deadline timer
auto expired = make_shared<bool>(), received = make_shared<bool>();
*expired = false, *received = false;
Expand Down Expand Up @@ -126,7 +157,6 @@ void scanner_connection::recv(protocol::Response &resp) {
});

// Make asynchronous read, synchronous
io_context.reset();
while(io_context.run_one() && !*received) {
if (*expired)
throw protocol::CreateErrorConnectionAbort("Timeout after " + to_string(timeout.count()) + " seconds");
Expand Down
1 change: 1 addition & 0 deletions src/scanner_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class scanner_connection: public connection {
protocol::Response async_resp, async_received;
bool async_read_active = false;

void connect(const std::string full_hostname, const uint32_t default_port);
void parse_response(std::istream &recv_stream, protocol::Response &resp);
void async_recv();

Expand Down

0 comments on commit 1254f5a

Please sign in to comment.