Skip to content

Commit

Permalink
Pull request #21: Replace Boost lambdas with standard lambdas (ECFLOW…
Browse files Browse the repository at this point in the history
…-1964)

Merge in ECFLOW/ecflow from feature/ECFLOW-1864_replace_boost_lambdas to develop

* commit 'c2e13be632135f1c92b49c1226f7169b9c277e56':
  Ensure correct buffer size for inbound data (ECFLOW-1964)
  Replace use of Boost lambdas with standard lambdas (ECFLOW-1964)
  • Loading branch information
marcosbento authored and iainrussell committed Mar 17, 2023
2 parents ce3a346 + c2e13be commit 95f1353
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 99 deletions.
4 changes: 1 addition & 3 deletions ACore/src/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
#include <string>
#include <vector>

#include <boost/lambda/lambda.hpp>

#include "DurationTimer.hpp"

namespace ecf {
Expand Down Expand Up @@ -217,7 +215,7 @@ std::string stringize_f(Functor const& f) {
f(out);
return out.str();
}
#define STRINGIZE(EXPRESSION) (ecf::stringize_f(boost::lambda::_1 << EXPRESSION))
#define STRINGIZE(EXPRESSION) (ecf::stringize_f([&](std::ostringstream& os) { os << EXPRESSION; }))
#define LOG(level, EXPRESSION) ecf::log(level, STRINGIZE(EXPRESSION))
#define LOG_ASSERT(expr, EXPRESSION) \
((expr) ? (static_cast<void>(0)) : ecf::log_assert(#expr, __FILE__, __LINE__, STRINGIZE(EXPRESSION)))
Expand Down
4 changes: 1 addition & 3 deletions ANode/src/ExprParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <utility>

#include <boost/algorithm/string/trim.hpp>
#include <boost/cast.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/spirit/include/classic.hpp>
#include <boost/spirit/include/classic_actor.hpp>
Expand All @@ -49,9 +50,6 @@
#include "Log.hpp"
#include "NodeAttr.hpp"
#include "Str.hpp"
#include "boost/cast.hpp"
#include "boost/lambda/bind.hpp"
#include "boost/lambda/lambda.hpp"

// Reference
// ‘*’ Zero or more
Expand Down
12 changes: 6 additions & 6 deletions Base/src/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include <sstream>
#include <stdexcept>

#include <boost/bind.hpp>

#include "StcCmd.hpp"

#ifdef DEBUG_PERF
Expand Down Expand Up @@ -111,8 +109,10 @@ bool Client::start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_ite
deadline_.expires_from_now(boost::posix_time::seconds(timeout_));

boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
connection_.socket_ll().async_connect(
endpoint, boost::bind(&Client::handle_connect, this, boost::asio::placeholders::error, endpoint_iterator));
connection_.socket_ll().async_connect(endpoint,
[this, endpoint_iterator](const boost::system::error_code& error) {
this->handle_connect(error, endpoint_iterator);
});
}
else {
// ran out of end points
Expand Down Expand Up @@ -194,7 +194,7 @@ void Client::start_write() {
deadline_.expires_from_now(boost::posix_time::seconds(timeout_));

connection_.async_write(outbound_request_,
boost::bind(&Client::handle_write, this, boost::asio::placeholders::error));
[this](const boost::system::error_code& error) { this->handle_write(error); });
}

void Client::handle_write(const boost::system::error_code& e) {
Expand Down Expand Up @@ -238,7 +238,7 @@ void Client::start_read() {
deadline_.expires_from_now(boost::posix_time::seconds(timeout_));

connection_.async_read(inbound_response_,
boost::bind(&Client::handle_read, this, boost::asio::placeholders::error));
[this](const boost::system::error_code& error) { this->handle_read(error); });
}

void Client::handle_read(const boost::system::error_code& e) {
Expand Down
54 changes: 24 additions & 30 deletions Base/src/Connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <sstream>

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/tuple/tuple.hpp>

#include "Serialization.hpp"

Expand Down Expand Up @@ -68,8 +66,7 @@ class connection {
catch (const std::exception& ae) {
// Unable to decode data. Something went wrong, inform the caller.
log_archive_error("Connection::async_write, exception ", ae, outbound_data_);
boost::system::error_code error(boost::asio::error::invalid_argument);
boost::asio::post(socket_.get_executor(), boost::bind(handler, error));
boost::asio::post(socket_.get_executor(), [handler]() { handler(boost::asio::error::invalid_argument); });
return;
}

Expand All @@ -82,8 +79,7 @@ class connection {
if (!header_stream || header_stream.str().size() != header_length) {
// Something went wrong, inform the caller.
log_error("Connection::async_write, could not format header");
boost::system::error_code error(boost::asio::error::invalid_argument);
boost::asio::post(socket_.get_executor(), boost::bind(handler, error));
boost::asio::post(socket_.get_executor(), [handler]() { handler(boost::asio::error::invalid_argument); });
return;
}
outbound_header_ = header_stream.str();
Expand All @@ -101,7 +97,10 @@ class connection {
buffers.reserve(2);
buffers.emplace_back(boost::asio::buffer(outbound_header_));
buffers.emplace_back(boost::asio::buffer(outbound_data_));
boost::asio::async_write(socket_, buffers, handler);
boost::asio::async_write(
socket_, buffers, [handler](const boost::system::error_code& error, std::size_t bytes_transferred) {
handler(error);
});

#ifdef DEBUG_CONNECTION
std::cout << " END\n";
Expand All @@ -119,22 +118,19 @@ class connection {
std::cout << "CLIENT: Connection::async_read\n";
#endif

// Issue a read operation to read exactly the number of bytes in a header.
void (connection::*f)(const boost::system::error_code&, T&, boost::tuple<Handler>) =
&connection::handle_read_header<T, Handler>;

// Issue a read operation to read exactly the number of bytes in a header
boost::asio::async_read(
socket_,
boost::asio::buffer(inbound_header_),
boost::bind(f, this, boost::asio::placeholders::error, boost::ref(t), boost::make_tuple(handler)));
[this, &t, handler](const boost::system::error_code& error, std::size_t bytes_transferred) {
this->handle_read_header(error, t, handler);
});
}

private:
/// Handle a completed read of a message header. The handler is passed using
/// a tuple since boost::bind seems to have trouble binding a function object
/// created using boost::bind as a parameter.
/// Handle a completed read of a message header.
template <typename T, typename Handler>
void handle_read_header(const boost::system::error_code& e, T& t, boost::tuple<Handler> handler) {
void handle_read_header(const boost::system::error_code& e, T& t, Handler handler) {
#ifdef DEBUG_CONNECTION
if (Ecf::server())
std::cout << "SERVER: Connection::handle_read_header\n";
Expand All @@ -144,7 +140,7 @@ class connection {
<< "' # this size of payload in hex\n";
#endif
if (e) {
boost::get<0>(handler)(e);
handler(e);
}
else {
// Determine the length of the serialized data.
Expand All @@ -156,25 +152,24 @@ class connection {
std::string err =
"Connection::handle_read_header: invalid header : " + std::string(inbound_header_, header_length);
log_error(err.c_str());
boost::system::error_code error(boost::asio::error::invalid_argument);
boost::get<0>(handler)(error);
handler(boost::asio::error::invalid_argument);
return;
}

// Start an asynchronous call to receive the data.
inbound_data_.resize(inbound_data_size);
void (connection::*f)(const boost::system::error_code&, T&, boost::tuple<Handler>) =
&connection::handle_read_data<T, Handler>;

boost::asio::async_read(socket_,
boost::asio::buffer(inbound_data_),
boost::bind(f, this, boost::asio::placeholders::error, boost::ref(t), handler));
boost::asio::async_read(
socket_,
boost::asio::buffer(inbound_data_),
[this, &t, handler](const boost::system::error_code& error, std::size_t bytes_transferred) {
this->handle_read_data(error, t, handler);
});
}
}

/// Handle a completed read of message data.
template <typename T, typename Handler>
void handle_read_data(const boost::system::error_code& e, T& t, boost::tuple<Handler> handler) {
void handle_read_data(const boost::system::error_code& e, T& t, Handler handler) {
#ifdef DEBUG_CONNECTION
if (Ecf::server())
std::cout << "SERVER: Connection::handle_read_data\n";
Expand All @@ -183,7 +178,7 @@ class connection {
#endif

if (e) {
boost::get<0>(handler)(e);
handler(e);
}
else {
// Extract the data structure from the data just received.
Expand All @@ -198,13 +193,12 @@ class connection {
}
catch (std::exception& e) {
log_archive_error("Connection::handle_read_data, Unable to decode data :", e, archive_data);
boost::system::error_code error(boost::asio::error::invalid_argument);
boost::get<0>(handler)(error);
handler(boost::asio::error::invalid_argument);
return;
}

// Inform caller that data has been received ok.
boost::get<0>(handler)(e);
handler(e);
}
}

Expand Down
13 changes: 7 additions & 6 deletions Base/src/Openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,17 @@
//
// Description :
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
#include "Openssl.hpp"

#include <cassert>
#include <cstdlib> // getenv
#include <stdexcept>

// #include <iostream>

#include <boost/bind.hpp>
#include <boost/filesystem/operations.hpp>

#include "File.hpp"
#include "Host.hpp"
#include "Openssl.hpp"
#include "Str.hpp"
#include "boost/filesystem/operations.hpp"

using namespace std;
namespace fs = boost::filesystem;
Expand Down Expand Up @@ -126,7 +124,10 @@ void Openssl::init_for_server() {
ssl_context_->set_options(boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use);
// this must be done before loading any keys. as below
ssl_context_->set_password_callback(boost::bind(&Openssl::get_password, this));
ssl_context_->set_password_callback(
[this](std::size_t size, boost::asio::ssl::context_base::password_purpose purpose) {
return this->get_password();
});
ssl_context_->use_certificate_chain_file(crt());
ssl_context_->use_private_key_file(key(), boost::asio::ssl::context::pem);
ssl_context_->use_tmp_dh_file(pem());
Expand Down
13 changes: 6 additions & 7 deletions Base/src/SslClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

#include <stdexcept>

#include <boost/bind.hpp>

#include "ErrorCmd.hpp"
#include "StcCmd.hpp"

Expand Down Expand Up @@ -111,9 +109,10 @@ bool SslClient::start_connect(boost::asio::ip::tcp::resolver::iterator endpoint_
deadline_.expires_from_now(boost::posix_time::seconds(timeout_));

boost::asio::ip::tcp::endpoint endpoint = *endpoint_iterator;
connection_.socket_ll().async_connect(
endpoint,
boost::bind(&SslClient::handle_connect, this, boost::asio::placeholders::error, endpoint_iterator));
connection_.socket_ll().async_connect(endpoint,
[this, endpoint_iterator](const boost::system::error_code& error) {
this->handle_connect(error, endpoint_iterator);
});
}
else {
// ran out of end points
Expand Down Expand Up @@ -226,7 +225,7 @@ void SslClient::start_write() {
deadline_.expires_from_now(boost::posix_time::seconds(timeout_));

connection_.async_write(outbound_request_,
boost::bind(&SslClient::handle_write, this, boost::asio::placeholders::error));
[this](const boost::system::error_code& error) { this->handle_write(error); });
}

void SslClient::handle_write(const boost::system::error_code& e) {
Expand Down Expand Up @@ -270,7 +269,7 @@ void SslClient::start_read() {
deadline_.expires_from_now(boost::posix_time::seconds(timeout_));

connection_.async_read(inbound_response_,
boost::bind(&SslClient::handle_read, this, boost::asio::placeholders::error));
[this](const boost::system::error_code& error) { this->handle_write(error); });
}

void SslClient::handle_read(const boost::system::error_code& e) {
Expand Down
4 changes: 3 additions & 1 deletion Base/src/ssl_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ ssl_connection::ssl_connection(boost::asio::io_service& io_service, boost::asio:
// if (Ecf::server())
// socket_.set_verify_mode(boost::asio::ssl::verify_peer|boost::asio::ssl::verify_fail_if_no_peer_cert); else
// socket_.set_verify_mode(boost::asio::ssl::verify_peer);
socket_.set_verify_callback(boost::bind(&ssl_connection::verify_certificate, this, _1, _2));
socket_.set_verify_callback([this](bool preverified, boost::asio::ssl::verify_context& ctx) {
return this->verify_certificate(preverified, ctx);
});
}

bool ssl_connection::verify_certificate(bool preverified, boost::asio::ssl::verify_context& ctx) {
Expand Down
Loading

0 comments on commit 95f1353

Please sign in to comment.