diff --git a/nano/node/transport/channel.cpp b/nano/node/transport/channel.cpp index ea470f2ec..ff80fdb72 100644 --- a/nano/node/transport/channel.cpp +++ b/nano/node/transport/channel.cpp @@ -16,8 +16,7 @@ nano::transport::channel::channel (nano::node & node_a) : bool nano::transport::channel::send (nano::message const & message, nano::transport::traffic_type traffic_type, callback_t callback) { - auto buffer = message.to_shared_const_buffer (); - bool sent = send_buffer (buffer, traffic_type, std::move (callback)); + bool sent = send_impl (message, traffic_type, std::move (callback)); node.stats.inc (sent ? nano::stat::type::message : nano::stat::type::drop, to_stat_detail (message.type ()), nano::stat::dir::out, /* aggregate all */ true); return sent; } diff --git a/nano/node/transport/channel.hpp b/nano/node/transport/channel.hpp index 5911f1249..cafb830bc 100644 --- a/nano/node/transport/channel.hpp +++ b/nano/node/transport/channel.hpp @@ -120,7 +120,7 @@ public: std::shared_ptr owner () const; protected: - virtual bool send_buffer (nano::shared_const_buffer const &, nano::transport::traffic_type, callback_t) = 0; + virtual bool send_impl (nano::message const &, nano::transport::traffic_type, callback_t) = 0; protected: nano::node & node; diff --git a/nano/node/transport/fake.cpp b/nano/node/transport/fake.cpp index 81d3fec7a..2da2b6e29 100644 --- a/nano/node/transport/fake.cpp +++ b/nano/node/transport/fake.cpp @@ -14,8 +14,9 @@ nano::transport::fake::channel::channel (nano::node & node) : /** * The send function behaves like a null device, it throws the data away and returns success. */ -bool nano::transport::fake::channel::send_buffer (nano::shared_const_buffer const & buffer, nano::transport::traffic_type traffic_type, nano::transport::channel::callback_t callback) +bool nano::transport::fake::channel::send_impl (nano::message const & message, nano::transport::traffic_type traffic_type, nano::transport::channel::callback_t callback) { + auto buffer = message.to_shared_const_buffer (); auto size = buffer.size (); if (callback) { diff --git a/nano/node/transport/fake.hpp b/nano/node/transport/fake.hpp index f503b2bd5..2ddec4a90 100644 --- a/nano/node/transport/fake.hpp +++ b/nano/node/transport/fake.hpp @@ -50,7 +50,7 @@ namespace transport } protected: - bool send_buffer (nano::shared_const_buffer const &, nano::transport::traffic_type, nano::transport::channel::callback_t) override; + bool send_impl (nano::message const &, nano::transport::traffic_type, nano::transport::channel::callback_t) override; private: nano::endpoint endpoint; diff --git a/nano/node/transport/inproc.cpp b/nano/node/transport/inproc.cpp index 78a4be990..5858dc16e 100644 --- a/nano/node/transport/inproc.cpp +++ b/nano/node/transport/inproc.cpp @@ -18,8 +18,10 @@ nano::transport::inproc::channel::channel (nano::node & node, nano::node & desti * Send the buffer to the peer and call the callback function when done. The call never fails. * Note that the inbound message visitor will be called before the callback because it is called directly whereas the callback is spawned in the background. */ -bool nano::transport::inproc::channel::send_buffer (nano::shared_const_buffer const & buffer, nano::transport::traffic_type traffic_type, nano::transport::channel::callback_t callback) +bool nano::transport::inproc::channel::send_impl (nano::message const & message, nano::transport::traffic_type traffic_type, nano::transport::channel::callback_t callback) { + auto buffer = message.to_shared_const_buffer (); + std::size_t offset{ 0 }; auto const buffer_read_fn = [&offset, buffer_v = buffer.to_bytes ()] (std::shared_ptr> const & data_a, std::size_t size_a, std::function callback_a) { debug_assert (buffer_v.size () >= (offset + size_a)); diff --git a/nano/node/transport/inproc.hpp b/nano/node/transport/inproc.hpp index dfe932e77..37b3c4495 100644 --- a/nano/node/transport/inproc.hpp +++ b/nano/node/transport/inproc.hpp @@ -40,7 +40,7 @@ namespace transport } protected: - bool send_buffer (nano::shared_const_buffer const &, nano::transport::traffic_type, nano::transport::channel::callback_t) override; + bool send_impl (nano::message const &, nano::transport::traffic_type, nano::transport::channel::callback_t) override; private: nano::node & destination; diff --git a/nano/node/transport/tcp_channel.cpp b/nano/node/transport/tcp_channel.cpp index 47ed452fe..ca69f8d80 100644 --- a/nano/node/transport/tcp_channel.cpp +++ b/nano/node/transport/tcp_channel.cpp @@ -76,20 +76,27 @@ bool nano::transport::tcp_channel::max (nano::transport::traffic_type traffic_ty return queue.max (traffic_type); } -bool nano::transport::tcp_channel::send_buffer (nano::shared_const_buffer const & buffer, nano::transport::traffic_type type, nano::transport::channel::callback_t callback) +bool nano::transport::tcp_channel::send_impl (nano::message const & message, nano::transport::traffic_type type, nano::transport::channel::callback_t callback) { + auto buffer = message.to_shared_const_buffer (); + nano::unique_lock lock{ mutex }; if (!queue.full (type)) { queue.push (type, { buffer, callback }); lock.unlock (); + node.stats.inc (nano::stat::type::tcp_channel, nano::stat::detail::queued, nano::stat::dir::out); node.stats.inc (nano::stat::type::tcp_channel_queued, to_stat_detail (type), nano::stat::dir::out); - sending_task.notify (); + + sending_task.notify (); // Wake up the sending task + return true; } else { + lock.unlock (); + node.stats.inc (nano::stat::type::tcp_channel, nano::stat::detail::drop, nano::stat::dir::out); node.stats.inc (nano::stat::type::tcp_channel_drop, to_stat_detail (type), nano::stat::dir::out); } diff --git a/nano/node/transport/tcp_channel.hpp b/nano/node/transport/tcp_channel.hpp index fc64ed902..c48d079b6 100644 --- a/nano/node/transport/tcp_channel.hpp +++ b/nano/node/transport/tcp_channel.hpp @@ -66,7 +66,7 @@ public: std::string to_string () const override; protected: - bool send_buffer (nano::shared_const_buffer const &, nano::transport::traffic_type, nano::transport::channel::callback_t) override; + bool send_impl (nano::message const &, nano::transport::traffic_type, nano::transport::channel::callback_t) override; private: void start ();