Skip to content

Commit

Permalink
LAST PACKET SENT
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 12, 2024
1 parent 2ffb79e commit 5a3a1b1
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 39 deletions.
84 changes: 51 additions & 33 deletions nano/node/transport/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,48 +49,66 @@ bool nano::transport::channel_tcp::operator== (nano::transport::channel const &

void nano::transport::channel_tcp::send_buffer (nano::shared_const_buffer const & buffer_a, std::function<void (boost::system::error_code const &, std::size_t)> const & callback_a, nano::transport::buffer_drop_policy policy_a, nano::transport::traffic_type traffic_type)
{
if (auto socket_l = socket.lock ())
auto socket = this->socket.lock ();
if (!socket)
{
if (!socket_l->max (traffic_type) || (policy_a == nano::transport::buffer_drop_policy::no_socket_drop && !socket_l->full (traffic_type)))
if (callback_a)
{
socket_l->async_write (
buffer_a,
[endpoint_a = socket_l->remote_endpoint (), node = std::weak_ptr<nano::node> (node.shared ()), callback_a] (boost::system::error_code const & ec, std::size_t size_a) {
if (auto node_l = node.lock ())
{
if (ec == boost::system::errc::host_unreachable)
{
node_l->stats.inc (nano::stat::type::error, nano::stat::detail::unreachable_host, nano::stat::dir::out);
}
if (callback_a)
{
callback_a (ec, size_a);
}
}
},
traffic_type);
node.background ([callback_a] () {
callback_a (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0);
});
}
return;
}

auto should_drop = [&] () {
if (policy_a == nano::transport::buffer_drop_policy::no_socket_drop)
{
return socket->full (traffic_type);
}
else
{
if (policy_a == nano::transport::buffer_drop_policy::no_socket_drop)
{
node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_write_no_socket_drop, nano::stat::dir::out);
}
else
{
node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_write_drop, nano::stat::dir::out);
}
if (callback_a)
return socket->max (traffic_type);
}
};

if (!should_drop ())
{
socket->async_write (
buffer_a,
[this_s = shared_from_this (), node = std::weak_ptr<nano::node>{ node.shared () }, callback_a] (boost::system::error_code const & ec, std::size_t size_a) {
if (auto node_l = node.lock ())
{
callback_a (boost::system::errc::make_error_code (boost::system::errc::no_buffer_space), 0);
if (ec == boost::system::errc::host_unreachable)
{
node_l->stats.inc (nano::stat::type::error, nano::stat::detail::unreachable_host, nano::stat::dir::out);
}
else
{
this_s->set_last_packet_sent (std::chrono::steady_clock::now ());
}
if (callback_a)
{
callback_a (ec, size_a);
}
}
}
},
traffic_type);
}
else if (callback_a)
else
{
node.background ([callback_a] () {
callback_a (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0);
});
if (policy_a == nano::transport::buffer_drop_policy::no_socket_drop)
{
node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_write_no_socket_drop, nano::stat::dir::out);
}
else
{
node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_write_drop, nano::stat::dir::out);
}
if (callback_a)
{
callback_a (boost::system::errc::make_error_code (boost::system::errc::no_buffer_space), 0);
}
}
}

Expand Down
16 changes: 10 additions & 6 deletions nano/node/transport/tcp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace transport
class tcp_server;
class tcp_channels;

class channel_tcp final : public nano::transport::channel
class channel_tcp final : public nano::transport::channel, public std::enable_shared_from_this<channel_tcp>
{
friend class nano::transport::tcp_channels;

Expand All @@ -36,18 +36,22 @@ namespace transport
nano::transport::channel_tcp & operator= (nano::transport::channel_tcp const &) = delete;
nano::transport::channel_tcp & operator= (nano::transport::channel_tcp &&) = delete;

std::string to_string () const override;
std::size_t hash_code () const override;
bool operator== (nano::transport::channel const &) const override;

// TODO: investigate clang-tidy warning about default parameters on virtual/override functions//
void send_buffer (nano::shared_const_buffer const &, std::function<void (boost::system::error_code const &, std::size_t)> const & = nullptr, nano::transport::buffer_drop_policy = nano::transport::buffer_drop_policy::limiter, nano::transport::traffic_type = nano::transport::traffic_type::generic) override;

std::string to_string () const override;
bool operator== (nano::transport::channel_tcp const & other_a) const
{
return &node == &other_a.node && socket.lock () == other_a.socket.lock ();
}

// TODO: investigate clang-tidy warning about default parameters on virtual/override functions//
void send_buffer (nano::shared_const_buffer const &, std::function<void (boost::system::error_code const &, std::size_t)> const & = nullptr, nano::transport::buffer_drop_policy = nano::transport::buffer_drop_policy::limiter, nano::transport::traffic_type = nano::transport::traffic_type::generic) override;

/*
* Underlying socket
*/
std::weak_ptr<nano::transport::socket> socket;

/* Mark for temporary channels. Usually remote ports of these channels are ephemeral and received from incoming connections to server.
If remote part has open listening port, temporary channel will be replaced with direct connection to listening port soon. But if other side is behing NAT or firewall this connection can be pemanent. */
std::atomic<bool> temporary{ false };
Expand Down

0 comments on commit 5a3a1b1

Please sign in to comment.