Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allocate callbacks for rai::socket on the heap #1273

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions rai/node/bootstrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,6 @@ node (node_a)
{
}

void rai::socket::async_connect (rai::tcp_endpoint const & endpoint_a, std::function<void(boost::system::error_code const &)> callback_a)
{
auto this_l (shared_from_this ());
start ();
socket_m.async_connect (endpoint_a, [this_l, callback_a](boost::system::error_code const & ec) {
this_l->stop ();
callback_a (ec);
});
}

void rai::socket::async_read (std::shared_ptr<std::vector<uint8_t>> buffer_a, size_t size_a, std::function<void(boost::system::error_code const &, size_t)> callback_a)
{
assert (size_a <= buffer_a->size ());
auto this_l (shared_from_this ());
start ();
boost::asio::async_read (socket_m, boost::asio::buffer (buffer_a->data (), size_a), [this_l, callback_a](boost::system::error_code const & ec, size_t size_a) {
this_l->stop ();
callback_a (ec, size_a);
});
}

void rai::socket::async_write (std::shared_ptr<std::vector<uint8_t>> buffer_a, std::function<void(boost::system::error_code const &, size_t)> callback_a)
{
auto this_l (shared_from_this ());
start ();
boost::asio::async_write (socket_m, boost::asio::buffer (buffer_a->data (), buffer_a->size ()), [this_l, callback_a, buffer_a](boost::system::error_code const & ec, size_t size_a) {
this_l->stop ();
callback_a (ec, size_a);
});
}

void rai::socket::start (std::chrono::steady_clock::time_point timeout_a)
{
auto ticket_l (++ticket);
Expand Down
37 changes: 34 additions & 3 deletions rai/node/bootstrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,40 @@ class socket : public std::enable_shared_from_this<rai::socket>
{
public:
socket (std::shared_ptr<rai::node>);
void async_connect (rai::tcp_endpoint const &, std::function<void(boost::system::error_code const &)>);
void async_read (std::shared_ptr<std::vector<uint8_t>>, size_t, std::function<void(boost::system::error_code const &, size_t)>);
void async_write (std::shared_ptr<std::vector<uint8_t>>, std::function<void(boost::system::error_code const &, size_t)>);
// F should be a function with signature void(boost::system::error_code const &)
template <typename F>
void async_connect (rai::tcp_endpoint const & endpoint_a, F callback_a)
{
auto this_l (shared_from_this ());
start ();
socket_m.async_connect (endpoint_a, [this_l, callback_a](boost::system::error_code const & ec) {
this_l->stop ();
callback_a (ec);
});
}
// F should be a function with signature void(boost::system::error_code const &, size_t)
template <typename F>
void async_read (std::shared_ptr<std::vector<uint8_t>> buffer_a, size_t size_a, F callback_a)
{
assert (size_a <= buffer_a->size ());
auto this_l (shared_from_this ());
start ();
boost::asio::async_read (socket_m, boost::asio::buffer (buffer_a->data (), size_a), [this_l, callback_a](boost::system::error_code const & ec, size_t size_a) {
this_l->stop ();
callback_a (ec, size_a);
});
}
// F should be a function with signature void(boost::system::error_code const &, size_t)
template <typename F>
void async_write (std::shared_ptr<std::vector<uint8_t>> buffer_a, F callback_a)
{
auto this_l (shared_from_this ());
start ();
boost::asio::async_write (socket_m, boost::asio::buffer (buffer_a->data (), buffer_a->size ()), [this_l, callback_a, buffer_a](boost::system::error_code const & ec, size_t size_a) {
this_l->stop ();
callback_a (ec, size_a);
});
}
void start (std::chrono::steady_clock::time_point = std::chrono::steady_clock::now () + std::chrono::seconds (5));
void stop ();
void close ();
Expand Down