diff --git a/libs/base/src/ecflow/base/Client.cpp b/libs/base/src/ecflow/base/Client.cpp index b47f2a795..d62c77189 100644 --- a/libs/base/src/ecflow/base/Client.cpp +++ b/libs/base/src/ecflow/base/Client.cpp @@ -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() { @@ -81,9 +81,9 @@ 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 @@ -91,8 +91,8 @@ void Client::start(boost::asio::ip::tcp::resolver::iterator endpoint_iter) { 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 @@ -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 { @@ -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 @@ -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; @@ -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; diff --git a/libs/base/src/ecflow/base/Client.hpp b/libs/base/src/ecflow/base/Client.hpp index da96b3403..c84d6a457 100644 --- a/libs/base/src/ecflow/base/Client.hpp +++ b/libs/base/src/ecflow/base/Client.hpp @@ -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, @@ -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); diff --git a/libs/base/src/ecflow/base/SslClient.cpp b/libs/base/src/ecflow/base/SslClient.cpp index 2751acefa..8475d8b31 100644 --- a/libs/base/src/ecflow/base/SslClient.cpp +++ b/libs/base/src/ecflow/base/SslClient.cpp @@ -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() { @@ -82,9 +82,9 @@ 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 @@ -92,8 +92,8 @@ void SslClient::start(boost::asio::ip::tcp::resolver::iterator endpoint_iter) { 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 @@ -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 { @@ -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 @@ -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; @@ -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; diff --git a/libs/base/src/ecflow/base/SslClient.hpp b/libs/base/src/ecflow/base/SslClient.hpp index 112c478cc..3f3eb8ffb 100644 --- a/libs/base/src/ecflow/base/SslClient.hpp +++ b/libs/base/src/ecflow/base/SslClient.hpp @@ -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, @@ -40,11 +44,11 @@ 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); @@ -52,7 +56,7 @@ class SslClient { 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); diff --git a/libs/server/src/ecflow/server/TcpBaseServer.cpp b/libs/server/src/ecflow/server/TcpBaseServer.cpp index c2ef76fcf..ebdaf2a85 100644 --- a/libs/server/src/ecflow/server/TcpBaseServer.cpp +++ b/libs/server/src/ecflow/server/TcpBaseServer.cpp @@ -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() { diff --git a/libs/udp/src/ecflow/udp/UDPClient.hpp b/libs/udp/src/ecflow/udp/UDPClient.hpp index dac9d6737..9435dc8f9 100644 --- a/libs/udp/src/ecflow/udp/UDPClient.hpp +++ b/libs/udp/src/ecflow/udp/UDPClient.hpp @@ -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 @@ -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();