Skip to content

Commit

Permalink
Refactor socket_type and socket_endpoint enums
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 21, 2024
1 parent 6768e7d commit b7c6261
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 80 deletions.
54 changes: 27 additions & 27 deletions nano/core_test/bootstrap.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions nano/core_test/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ TEST (socket_timeout, write)
// create a client socket and send lots of data to fill the socket queue on the local and remote side
// eventually, the all tcp queues should fill up and async_write will not be able to progress
// and the timeout should kick in and close the socket, which will cause the async_write to return an error
auto socket = std::make_shared<nano::transport::socket> (*node, nano::transport::socket::endpoint_type_t::client, 1024 * 64); // socket with a max queue size much larger than OS buffers
auto socket = std::make_shared<nano::transport::socket> (*node, nano::transport::socket_endpoint::client, 1024 * 64); // socket with a max queue size much larger than OS buffers
std::atomic<bool> done = false;
boost::system::error_code ec;
socket->async_connect (acceptor.local_endpoint (), [&socket, &ec, &done] (boost::system::error_code const & ec_a) {
Expand Down Expand Up @@ -804,7 +804,7 @@ TEST (socket_timeout, write_overlapped)
// create a client socket and send lots of data to fill the socket queue on the local and remote side
// eventually, the all tcp queues should fill up and async_write will not be able to progress
// and the timeout should kick in and close the socket, which will cause the async_write to return an error
auto socket = std::make_shared<nano::transport::socket> (*node, nano::transport::socket::endpoint_type_t::client, 1024 * 64); // socket with a max queue size much larger than OS buffers
auto socket = std::make_shared<nano::transport::socket> (*node, nano::transport::socket_endpoint::client, 1024 * 64); // socket with a max queue size much larger than OS buffers
std::atomic<bool> done = false;
boost::system::error_code ec;
socket->async_connect (acceptor.local_endpoint (), [&socket, &ec, &done] (boost::system::error_code const & ec_a) {
Expand Down
19 changes: 14 additions & 5 deletions nano/node/transport/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* socket
*/

nano::transport::socket::socket (nano::node & node_a, endpoint_type_t endpoint_type_a, std::size_t max_queue_size_a) :
nano::transport::socket::socket (nano::node & node_a, nano::transport::socket_endpoint endpoint_type_a, std::size_t max_queue_size_a) :
send_queue{ max_queue_size_a },
strand{ node_a.io_ctx.get_executor () },
tcp_socket{ node_a.io_ctx },
Expand Down Expand Up @@ -47,7 +47,7 @@ void nano::transport::socket::start ()
void nano::transport::socket::async_connect (nano::tcp_endpoint const & endpoint_a, std::function<void (boost::system::error_code const &)> callback_a)
{
debug_assert (callback_a);
debug_assert (endpoint_type () == endpoint_type_t::client);
debug_assert (endpoint_type () == socket_endpoint::client);

start ();
set_default_timeout ();
Expand Down Expand Up @@ -248,7 +248,7 @@ void nano::transport::socket::ongoing_checkup ()
auto condition_to_disconnect{ false };

// if this is a server socket, and no data is received for silent_connection_tolerance_time seconds then disconnect
if (this_l->endpoint_type () == endpoint_type_t::server && (now - this_l->last_receive_time_or_init) > static_cast<uint64_t> (this_l->silent_connection_tolerance_time.count ()))
if (this_l->endpoint_type () == socket_endpoint::server && (now - this_l->last_receive_time_or_init) > static_cast<uint64_t> (this_l->silent_connection_tolerance_time.count ()))
{
this_l->node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_silent_connection_drop, nano::stat::dir::in);

Expand All @@ -258,7 +258,7 @@ void nano::transport::socket::ongoing_checkup ()
// if there is no activity for timeout seconds then disconnect
if ((now - this_l->last_completion_time_or_init) > this_l->timeout)
{
this_l->node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, this_l->endpoint_type () == endpoint_type_t::server ? nano::stat::dir::in : nano::stat::dir::out);
this_l->node.stats.inc (nano::stat::type::tcp, nano::stat::detail::tcp_io_timeout_drop, this_l->endpoint_type () == socket_endpoint::server ? nano::stat::dir::in : nano::stat::dir::out);

condition_to_disconnect = true;
}
Expand Down Expand Up @@ -462,7 +462,16 @@ std::size_t network_prefix)
return counted_connections;
}

std::string_view nano::transport::to_string (nano::transport::socket::type_t type)
/*
*
*/

std::string_view nano::transport::to_string (socket_type type)
{
return magic_enum::enum_name (type);
}

std::string_view nano::transport::to_string (socket_endpoint type)
{
return magic_enum::enum_name (type);
}
80 changes: 45 additions & 35 deletions nano/node/transport/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,26 @@ enum class buffer_drop_policy
no_socket_drop
};

enum class socket_type
{
undefined,
bootstrap,
realtime,
realtime_response_server // special type for tcp channel response server
};

std::string_view to_string (socket_type);

enum class socket_endpoint
{
server, // Socket was created by accepting an incoming connection
client, // Socket was created by initiating an outgoing connection
};

std::string_view to_string (socket_endpoint);

/** Socket class for tcp clients and newly accepted connections */
class socket final : public std::enable_shared_from_this<nano::transport::socket>
class socket final : public std::enable_shared_from_this<socket>
{
friend class tcp_server;
friend class tcp_channels;
Expand All @@ -49,66 +67,60 @@ class socket final : public std::enable_shared_from_this<nano::transport::socket
public:
static std::size_t constexpr default_max_queue_size = 128;

enum class type_t
{
undefined,
bootstrap,
realtime,
realtime_response_server // special type for tcp channel response server
};

enum class endpoint_type_t
{
server,
client
};

/**
* Constructor
* @param node Owning node
* @param endpoint_type_a The endpoint's type: either server or client
*/
explicit socket (nano::node & node, endpoint_type_t endpoint_type_a = endpoint_type_t::client, std::size_t max_queue_size = default_max_queue_size);
public:
explicit socket (nano::node &, nano::transport::socket_endpoint = socket_endpoint::client, std::size_t max_queue_size = default_max_queue_size);
~socket ();

void start ();

void async_connect (boost::asio::ip::tcp::endpoint const &, std::function<void (boost::system::error_code const &)>);
void async_read (std::shared_ptr<std::vector<uint8_t>> const &, std::size_t, std::function<void (boost::system::error_code const &, std::size_t)>);
void async_write (nano::shared_const_buffer const &, std::function<void (boost::system::error_code const &, std::size_t)> callback = {}, nano::transport::traffic_type = nano::transport::traffic_type::generic);
void async_connect (
boost::asio::ip::tcp::endpoint const & endpoint,
std::function<void (boost::system::error_code const &)> callback);

void async_read (
std::shared_ptr<std::vector<uint8_t>> const & buffer,
std::size_t size,
std::function<void (boost::system::error_code const &, std::size_t)> callback);

void async_write (
nano::shared_const_buffer const &,
std::function<void (boost::system::error_code const &, std::size_t)> callback = {},
traffic_type = traffic_type::generic);

void close ();

boost::asio::ip::tcp::endpoint remote_endpoint () const;
boost::asio::ip::tcp::endpoint local_endpoint () const;

/** Returns true if the socket has timed out */
bool has_timed_out () const;
/** This can be called to change the maximum idle time, e.g. based on the type of traffic detected. */
void set_default_timeout_value (std::chrono::seconds);
std::chrono::seconds get_default_timeout_value () const;
void set_timeout (std::chrono::seconds);

bool max (nano::transport::traffic_type = nano::transport::traffic_type::generic) const;
bool full (nano::transport::traffic_type = nano::transport::traffic_type::generic) const;
bool max (nano::transport::traffic_type = traffic_type::generic) const;
bool full (nano::transport::traffic_type = traffic_type::generic) const;

type_t type () const
nano::transport::socket_type type () const
{
return type_m;
};
void type_set (type_t type_a)
void type_set (nano::transport::socket_type type_a)
{
type_m = type_a;
}
endpoint_type_t endpoint_type () const
nano::transport::socket_endpoint endpoint_type () const
{
return endpoint_type_m;
}
bool is_realtime_connection () const
{
return type () == nano::transport::socket::type_t::realtime || type () == nano::transport::socket::type_t::realtime_response_server;
return type () == socket_type::realtime || type () == socket_type::realtime_response_server;
}
bool is_bootstrap_connection () const
{
return type () == nano::transport::socket::type_t::bootstrap;
return type () == socket_type::bootstrap;
}
bool is_closed () const
{
Expand Down Expand Up @@ -201,8 +213,8 @@ class socket final : public std::enable_shared_from_this<nano::transport::socket
void read_impl (std::shared_ptr<std::vector<uint8_t>> const & data_a, std::size_t size_a, std::function<void (boost::system::error_code const &, std::size_t)> callback_a);

private:
type_t type_m{ type_t::undefined };
endpoint_type_t endpoint_type_m;
nano::transport::socket_type type_m{ socket_type::undefined };
nano::transport::socket_endpoint endpoint_type_m;

public:
std::size_t const max_queue_size;
Expand All @@ -211,8 +223,6 @@ class socket final : public std::enable_shared_from_this<nano::transport::socket
virtual void operator() (nano::object_stream &) const;
};

std::string_view to_string (socket::type_t type);

using address_socket_mmap = std::multimap<boost::asio::ip::address, std::weak_ptr<socket>>;

namespace socket_functions
Expand Down
6 changes: 3 additions & 3 deletions nano/node/transport/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ void nano::transport::tcp_channels::process_message (nano::message const & messa
temporary_channel->set_node_id (node_id_a);
temporary_channel->set_network_version (message_a.header.version_using);
temporary_channel->temporary = true;
debug_assert (type_a == nano::transport::socket::type_t::realtime || type_a == nano::transport::socket::type_t::realtime_response_server);
debug_assert (type_a == nano::transport::socket_type::realtime || type_a == nano::transport::socket_type::realtime_response_server);
// Don't insert temporary channels for response_server
if (type_a == nano::transport::socket::type_t::realtime)
if (type_a == nano::transport::socket_type::realtime)
{
insert (temporary_channel, socket_a, nullptr);
}
Expand Down Expand Up @@ -767,7 +767,7 @@ void nano::transport::tcp_channels::start_tcp_receive_node_id (std::shared_ptr<n
auto response_server = std::make_shared<nano::transport::tcp_server> (socket_l, node_l);
node_l->network.tcp_channels.insert (channel_a, socket_l, response_server);
// Listen for possible responses
response_server->socket->type_set (nano::transport::socket::type_t::realtime_response_server);
response_server->socket->type_set (nano::transport::socket_type::realtime_response_server);
response_server->remote_node_id = channel_a->get_node_id ();
response_server->start ();
});
Expand Down
2 changes: 1 addition & 1 deletion nano/node/transport/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void nano::transport::tcp_listener::on_connection (std::function<bool (std::shar
}

// Prepare new connection
auto new_connection = std::make_shared<nano::transport::socket> (this_l->node, socket::endpoint_type_t::server);
auto new_connection = std::make_shared<nano::transport::socket> (this_l->node, socket_endpoint::server);
this_l->acceptor.async_accept (new_connection->tcp_socket, new_connection->remote,
boost::asio::bind_executor (this_l->strand,
[this_l, new_connection, cbk = std::move (callback)] (boost::system::error_code const & ec_a) mutable {
Expand Down
14 changes: 7 additions & 7 deletions nano/node/transport/tcp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ nano::transport::tcp_server::~tcp_server ()

node->logger.debug (nano::log::type::tcp_server, "Exiting TCP server ({})", fmt::streamed (remote_endpoint));

if (socket->type () == nano::transport::socket::type_t::bootstrap)
if (socket->type () == nano::transport::socket_type::bootstrap)
{
--node->tcp_listener->bootstrap_count;
}
else if (socket->type () == nano::transport::socket::type_t::realtime)
else if (socket->type () == nano::transport::socket_type::realtime)
{
--node->tcp_listener->realtime_count;

Expand Down Expand Up @@ -612,13 +612,13 @@ bool nano::transport::tcp_server::to_bootstrap_connection ()
{
return false;
}
if (socket->type () != nano::transport::socket::type_t::undefined)
if (socket->type () != nano::transport::socket_type::undefined)
{
return false;
}

++node->tcp_listener->bootstrap_count;
socket->type_set (nano::transport::socket::type_t::bootstrap);
socket->type_set (nano::transport::socket_type::bootstrap);

node->logger.debug (nano::log::type::tcp_server, "Switched to bootstrap mode ({})", fmt::streamed (remote_endpoint));

Expand All @@ -636,14 +636,14 @@ bool nano::transport::tcp_server::to_realtime_connection (nano::account const &
{
return false;
}
if (socket->type () != nano::transport::socket::type_t::undefined)
if (socket->type () != nano::transport::socket_type::undefined)
{
return false;
}

remote_node_id = node_id;
++node->tcp_listener->realtime_count;
socket->type_set (nano::transport::socket::type_t::realtime);
socket->type_set (nano::transport::socket_type::realtime);

node->logger.debug (nano::log::type::tcp_server, "Switched to realtime mode ({})", fmt::streamed (remote_endpoint));

Expand All @@ -652,7 +652,7 @@ bool nano::transport::tcp_server::to_realtime_connection (nano::account const &

bool nano::transport::tcp_server::is_undefined_connection () const
{
return socket->type () == nano::transport::socket::type_t::undefined;
return socket->type () == nano::transport::socket_type::undefined;
}

bool nano::transport::tcp_server::is_bootstrap_connection () const
Expand Down

0 comments on commit b7c6261

Please sign in to comment.