Skip to content

Commit

Permalink
Replace deprecated Boost.ASIO features
Browse files Browse the repository at this point in the history
Re ECFLOW-1997
  • Loading branch information
marcosbento committed Jan 23, 2025
1 parent e21f499 commit 464d451
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 44 deletions.
29 changes: 14 additions & 15 deletions libs/base/src/ecflow/base/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ Client::Client(boost::asio::io_context& io,

// Host name resolution is performed using a resolver, where host and service
// names(or ports) are looked up and converted into one or more end points
boost::asio::ip::tcp::resolver resolver(io);
boost::asio::ip::tcp::resolver::query query(host_, port_);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
auto resolver = resolver_t(io);
auto results = resolver.resolve(host_, port_);
auto endpoints_iterator = results.begin();

// The list of end points obtained could contain both IPv4 and IPv6 end points,
// so a program may try each of them until it finds one that works.
// This keeps the Client program independent of a specific IP version.
start(endpoint_iterator);
start(endpoints_iterator);
}

Client::~Client() {
Expand All @@ -81,18 +81,18 @@ Client::~Client() {
// This function terminates all the actors to shut down the connection. It
// may be called by the user of the client class, or by the class itself in
// response to graceful termination or an unrecoverable error.
void Client::start(boost::asio::ip::tcp::resolver::iterator endpoint_iter) {
void Client::start(endpoints_iterator_t endpoints_iterator) {
// Start the connect actor.
start_connect(endpoint_iter);
start_connect(endpoints_iterator);

// Start the deadline actor. You will note that we're not setting any
// particular deadline here. Instead, the connect and input actors will
// update the deadline prior to each asynchronous operation.
deadline_.async_wait([this](const boost::system::error_code&) { check_deadline(); });
}

bool Client::start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_iterator) {
if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) {
bool Client::start_connect(endpoints_iterator_t endpoints_iterator) {
if (endpoints_iterator != endpoints_iterator_t()) {
#ifdef DEBUG_CLIENT
std::cout << " Client::start_connect: Trying " << endpoint_iterator->endpoint() << "..." << std::endl;
#endif
Expand All @@ -104,10 +104,10 @@ bool Client::start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_ite
// Set a deadline for the connect operation.
deadline_.expires_from_now(boost::posix_time::seconds(timeout_));

boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
auto endpoint = endpoints_iterator->endpoint();
connection_.socket_ll().async_connect(endpoint,
[this, endpoint_iterator](const boost::system::error_code& error) {
this->handle_connect(error, endpoint_iterator);
[this, endpoints_iterator](const boost::system::error_code& error) {
this->handle_connect(error, endpoints_iterator);
});
}
else {
Expand All @@ -117,8 +117,7 @@ bool Client::start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_ite
return true;
}

void Client::handle_connect(const boost::system::error_code& e,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator) {
void Client::handle_connect(const boost::system::error_code& e, endpoints_iterator_t endpoints_iterator) {
#ifdef DEBUG_CLIENT
std::cout << " Client::handle_connect stopped_=" << stopped_ << std::endl;
#endif
Expand All @@ -134,7 +133,7 @@ void Client::handle_connect(const boost::system::error_code& e,
std::cout << " Client::handle_connect: *Connect timeout*: Trying next end point" << std::endl;
#endif
// Try the next available end point.
if (!start_connect(++endpoint_iterator)) {
if (!start_connect(++endpoints_iterator)) {
// Ran out of end points, An error occurred
stop();
std::stringstream ss;
Expand All @@ -159,7 +158,7 @@ void Client::handle_connect(const boost::system::error_code& e,
connection_.socket_ll().close();

// Try the next end point.
if (!start_connect(++endpoint_iterator)) {
if (!start_connect(++endpoints_iterator)) {
// Ran out of end points. An error occurred.
stop();
std::stringstream ss;
Expand Down
10 changes: 7 additions & 3 deletions libs/base/src/ecflow/base/Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@

class Client {
public:
using resolver_t = boost::asio::ip::tcp::resolver;
using endpoints_set_t = resolver_t::results_type;
using endpoints_iterator_t = endpoints_set_t::iterator;

/// Constructor starts the asynchronous connect operation.
Client(boost::asio::io_context& io,
Cmd_ptr cmd_ptr,
Expand All @@ -41,16 +45,16 @@ class Client {
bool handle_server_response(ServerReply&, bool debug) const;

private:
void start(boost::asio::ip::tcp::resolver::iterator);
void start(endpoints_iterator_t endpoint_iter);
void stop();
void check_deadline();

bool start_connect(boost::asio::ip::tcp::resolver::iterator);
bool start_connect(endpoints_iterator_t endpoint_iter);
void start_write();
void start_read();

/// Handle completion of a connect operation.
void handle_connect(const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
void handle_connect(const boost::system::error_code& e, endpoints_iterator_t endpoint_iter);

/// Handle completion of a read operation.
void handle_read(const boost::system::error_code& e);
Expand Down
29 changes: 14 additions & 15 deletions libs/base/src/ecflow/base/SslClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ SslClient::SslClient(boost::asio::io_context& io,

// Host name resolution is performed using a resolver, where host and service
// names(or ports) are looked up and converted into one or more end points
boost::asio::ip::tcp::resolver resolver(io);
boost::asio::ip::tcp::resolver::query query(host_, port_);
boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
auto resolver = resolver_t(io);
auto results = resolver.resolve(host_, port_);
auto endpoints_iterator = results.begin();

// The list of end points obtained could contain both IPv4 and IPv6 end points,
// so a program may try each of them until it finds one that works.
// This keeps the SslClient program independent of a specific IP version.
start(endpoint_iterator);
start(endpoints_iterator);
}

SslClient::~SslClient() {
Expand All @@ -82,18 +82,18 @@ SslClient::~SslClient() {
// This function terminates all the actors to shut down the connection. It
// may be called by the user of the client class, or by the class itself in
// response to graceful termination or an unrecoverable error.
void SslClient::start(boost::asio::ip::tcp::resolver::iterator endpoint_iter) {
void SslClient::start(endpoints_iterator_t endpoints_iterator) {
// Start the connect actor.
start_connect(endpoint_iter);
start_connect(endpoints_iterator);

// Start the deadline actor. You will note that we're not setting any
// particular deadline here. Instead, the connect and input actors will
// update the deadline prior to each asynchronous operation.
deadline_.async_wait([this](const boost::system::error_code&) { check_deadline(); });
}

bool SslClient::start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_iterator) {
if (endpoint_iterator != boost::asio::ip::tcp::resolver::iterator()) {
bool SslClient::start_connect(endpoints_iterator_t endpoints_iterator) {
if (endpoints_iterator != endpoints_iterator_t()) {
#ifdef DEBUG_CLIENT
std::cout << " SslClient::start_connect: Trying " << endpoint_iterator->endpoint() << "..." << std::endl;
#endif
Expand All @@ -105,10 +105,10 @@ bool SslClient::start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_
// Set a deadline for the connect operation.
deadline_.expires_from_now(boost::posix_time::seconds(timeout_));

boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
boost::asio::ip::tcp::endpoint endpoint = *endpoints_iterator;
connection_.socket_ll().async_connect(endpoint,
[this, endpoint_iterator](const boost::system::error_code& error) {
this->handle_connect(error, endpoint_iterator);
[this, endpoints_iterator](const boost::system::error_code& error) {
this->handle_connect(error, endpoints_iterator);
});
}
else {
Expand All @@ -118,8 +118,7 @@ bool SslClient::start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_
return true;
}

void SslClient::handle_connect(const boost::system::error_code& e,
boost::asio::ip::tcp::resolver::iterator endpoint_iterator) {
void SslClient::handle_connect(const boost::system::error_code& e, endpoints_iterator_t endpoints_iterator) {
#ifdef DEBUG_CLIENT
std::cout << " SslClient::handle_connect stopped_=" << stopped_ << std::endl;
#endif
Expand All @@ -135,7 +134,7 @@ void SslClient::handle_connect(const boost::system::error_code& e,
std::cout << " SslClient::handle_connect: *Connect timeout*: Trying next end point" << std::endl;
#endif
// Try the next available end point.
if (!start_connect(++endpoint_iterator)) {
if (!start_connect(++endpoints_iterator)) {
// Ran out of end points, An error occurred
stop();
std::stringstream ss;
Expand All @@ -160,7 +159,7 @@ void SslClient::handle_connect(const boost::system::error_code& e,
connection_.socket_ll().close();

// Try the next end point.
if (!start_connect(++endpoint_iterator)) {
if (!start_connect(++endpoints_iterator)) {
// Ran out of end points. An error occurred.
stop();
std::stringstream ss;
Expand Down
10 changes: 7 additions & 3 deletions libs/base/src/ecflow/base/SslClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@

class SslClient {
public:
using resolver_t = boost::asio::ip::tcp::resolver;
using endpoints_set_t = resolver_t::results_type;
using endpoints_iterator_t = endpoints_set_t::iterator;

/// Constructor starts the asynchronous connect operation.
SslClient(boost::asio::io_context& io,
boost::asio::ssl::context& context,
Expand All @@ -40,19 +44,19 @@ class SslClient {
bool handle_server_response(ServerReply&, bool debug) const;

private:
void start(boost::asio::ip::tcp::resolver::iterator);
void start(endpoints_iterator_t endpoints_iterator);
void stop();
void check_deadline();

bool start_connect(boost::asio::ip::tcp::resolver::iterator);
bool start_connect(endpoints_iterator_t endpoints_iterator);

void start_handshake();
void handle_handshake(const boost::system::error_code& e);
void start_write();
void start_read();

/// Handle completion of a connect operation.
void handle_connect(const boost::system::error_code& e, boost::asio::ip::tcp::resolver::iterator endpoint_iterator);
void handle_connect(const boost::system::error_code& e, endpoints_iterator_t endpoints_iterator);

/// Handle completion of a read operation.
void handle_read(const boost::system::error_code& e);
Expand Down
2 changes: 1 addition & 1 deletion libs/server/src/ecflow/server/TcpBaseServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ void TcpBaseServer::terminate() {
cout << " Server::terminate(): posting call to Server::handle_terminate" << endl;

// Post a call to the stop function so that Server::stop() is safe to call from any thread.
io_.post([this]() { handle_terminate(); });
boost::asio::post(io_, [this]() { handle_terminate(); });
}

void TcpBaseServer::handle_terminate() {
Expand Down
19 changes: 12 additions & 7 deletions libs/udp/src/ecflow/udp/UDPClient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ namespace internal_detail {
*/
class BaseUDPConnection {
public:
BaseUDPConnection(boost::asio::io_context& io,
boost::asio::ip::udp::endpoint server_endpoint,
const std::string& request)
using resolver_t = boost::asio::ip::udp::resolver;
using endpoints_set_t = resolver_t::results_type;
using endpoint_t = endpoints_set_t::endpoint_type;

BaseUDPConnection(boost::asio::io_context& io, endpoint_t server_endpoint, const std::string& request)
: socket_(io),
server_endpoint_(std::move(server_endpoint)) {
// Open socket connection
Expand Down Expand Up @@ -70,14 +72,17 @@ class BaseUDPClient {
using port_t = std::string;
using data_t = std::string;

public:
using resolver_t = internal_detail::BaseUDPConnection::resolver_t;
using endpoints_set_t = resolver_t::results_type;
using endpoints_iterator_t = endpoints_set_t::iterator;

BaseUDPClient(hostname_t host, port_t port) : host_{std::move(host)}, port_{std::move(port)} {}

void send(const data_t& data) {
boost::asio::io_context io;
boost::asio::ip::udp::resolver resolver(io);
boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), host_, port_);
boost::asio::ip::udp::endpoint server_endpoint = *resolver.resolve(query);
auto resolver = resolver_t(io);
auto results = resolver.resolve(boost::asio::ip::udp::v4(), host_, port_);
auto server_endpoint = std::begin(results)->endpoint();

internal_detail::BaseUDPConnection connection(io, server_endpoint, data);
io.run();
Expand Down

0 comments on commit 464d451

Please sign in to comment.