Skip to content

Commit

Permalink
Remove client_socket class in line with removed server_socket class. …
Browse files Browse the repository at this point in the history
…Removing virtual functions from the nano::socket class and marking it as final.
  • Loading branch information
clemahieu committed Jan 9, 2024
1 parent 335d5bc commit 32ba010
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 59 deletions.
6 changes: 3 additions & 3 deletions nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ TEST (node, port_mapping)
TEST (tcp_listener, tcp_node_id_handshake)
{
nano::test::system system (1);
auto socket (std::make_shared<nano::transport::client_socket> (*system.nodes[0]));
auto socket (std::make_shared<nano::transport::socket> (*system.nodes[0]));
auto bootstrap_endpoint (system.nodes[0]->tcp_listener->endpoint ());
auto cookie (system.nodes[0]->network.syn_cookies.assign (nano::transport::map_tcp_to_endpoint (bootstrap_endpoint)));
ASSERT_TRUE (cookie);
Expand Down Expand Up @@ -684,7 +684,7 @@ TEST (tcp_listener, DISABLED_tcp_listener_timeout_empty)
{
nano::test::system system (1);
auto node0 (system.nodes[0]);
auto socket (std::make_shared<nano::transport::client_socket> (*node0));
auto socket (std::make_shared<nano::transport::socket> (*node0));
std::atomic<bool> connected (false);
socket->async_connect (node0->tcp_listener->endpoint (), [&connected] (boost::system::error_code const & ec) {
ASSERT_FALSE (ec);
Expand All @@ -707,7 +707,7 @@ TEST (tcp_listener, tcp_listener_timeout_node_id_handshake)
{
nano::test::system system (1);
auto node0 (system.nodes[0]);
auto socket (std::make_shared<nano::transport::client_socket> (*node0));
auto socket (std::make_shared<nano::transport::socket> (*node0));
auto cookie (node0->network.syn_cookies.assign (nano::transport::map_tcp_to_endpoint (node0->tcp_listener->endpoint ())));
ASSERT_TRUE (cookie);
nano::node_id_handshake::query_payload query{ *cookie };
Expand Down
20 changes: 10 additions & 10 deletions nano/core_test/request_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TEST (request_aggregator, one)
.build_shared ();
std::vector<std::pair<nano::block_hash, nano::root>> request;
request.emplace_back (send1->hash (), send1->root ());
auto client = std::make_shared<nano::transport::client_socket> (node);
auto client = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel = std::make_shared<nano::transport::channel_tcp> (node, client);
node.aggregator.add (dummy_channel, request);
ASSERT_EQ (1, node.aggregator.size ());
Expand Down Expand Up @@ -98,7 +98,7 @@ TEST (request_aggregator, one_update)
ASSERT_EQ (nano::process_result::progress, node.ledger.process (node.store.tx_begin_write (), *receive1).code);
std::vector<std::pair<nano::block_hash, nano::root>> request;
request.emplace_back (send2->hash (), send2->root ());
auto client = std::make_shared<nano::transport::client_socket> (node);
auto client = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel = std::make_shared<nano::transport::channel_tcp> (node, client);
node.aggregator.add (dummy_channel, request);
request.clear ();
Expand Down Expand Up @@ -165,7 +165,7 @@ TEST (request_aggregator, two)
std::vector<std::pair<nano::block_hash, nano::root>> request;
request.emplace_back (send2->hash (), send2->root ());
request.emplace_back (receive1->hash (), receive1->root ());
auto client = std::make_shared<nano::transport::client_socket> (node);
auto client = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel = std::make_shared<nano::transport::channel_tcp> (node, client);
// Process both blocks
node.aggregator.add (dummy_channel, request);
Expand Down Expand Up @@ -289,7 +289,7 @@ TEST (request_aggregator, split)
election->force_confirm ();
ASSERT_TIMELY (5s, max_vbh + 2 == node.ledger.cache.cemented_count);
ASSERT_EQ (max_vbh + 1, request.size ());
auto client = std::make_shared<nano::transport::client_socket> (node);
auto client = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel = std::make_shared<nano::transport::channel_tcp> (node, client);
node.aggregator.add (dummy_channel, request);
ASSERT_EQ (1, node.aggregator.size ());
Expand Down Expand Up @@ -330,7 +330,7 @@ TEST (request_aggregator, channel_lifetime)
request.emplace_back (send1->hash (), send1->root ());
{
// The aggregator should extend the lifetime of the channel
auto client = std::make_shared<nano::transport::client_socket> (node);
auto client = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel = std::make_shared<nano::transport::channel_tcp> (node, client);
node.aggregator.add (dummy_channel, request);
}
Expand Down Expand Up @@ -361,11 +361,11 @@ TEST (request_aggregator, channel_update)
request.emplace_back (send1->hash (), send1->root ());
std::weak_ptr<nano::transport::channel> channel1_w;
{
auto client1 = std::make_shared<nano::transport::client_socket> (node);
auto client1 = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel1 = std::make_shared<nano::transport::channel_tcp> (node, client1);
channel1_w = dummy_channel1;
node.aggregator.add (dummy_channel1, request);
auto client2 = std::make_shared<nano::transport::client_socket> (node);
auto client2 = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel2 = std::make_shared<nano::transport::channel_tcp> (node, client2);
// The aggregator then hold channel2 and drop channel1
node.aggregator.add (dummy_channel2, request);
Expand Down Expand Up @@ -399,7 +399,7 @@ TEST (request_aggregator, channel_max_queue)
ASSERT_EQ (nano::process_result::progress, node.ledger.process (node.store.tx_begin_write (), *send1).code);
std::vector<std::pair<nano::block_hash, nano::root>> request;
request.emplace_back (send1->hash (), send1->root ());
auto client = std::make_shared<nano::transport::client_socket> (node);
auto client = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel = std::make_shared<nano::transport::channel_tcp> (node, client);
node.aggregator.add (dummy_channel, request);
node.aggregator.add (dummy_channel, request);
Expand Down Expand Up @@ -427,7 +427,7 @@ TEST (request_aggregator, unique)
ASSERT_EQ (nano::process_result::progress, node.ledger.process (node.store.tx_begin_write (), *send1).code);
std::vector<std::pair<nano::block_hash, nano::root>> request;
request.emplace_back (send1->hash (), send1->root ());
auto client = std::make_shared<nano::transport::client_socket> (node);
auto client = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel = std::make_shared<nano::transport::channel_tcp> (node, client);
node.aggregator.add (dummy_channel, request);
node.aggregator.add (dummy_channel, request);
Expand Down Expand Up @@ -474,7 +474,7 @@ TEST (request_aggregator, cannot_vote)
request.emplace_back (send2->hash (), send2->root ());
// Incorrect hash, correct root
request.emplace_back (1, send2->root ());
auto client = std::make_shared<nano::transport::client_socket> (node);
auto client = std::make_shared<nano::transport::socket> (node);
std::shared_ptr<nano::transport::channel> dummy_channel = std::make_shared<nano::transport::channel_tcp> (node, client);
node.aggregator.add (dummy_channel, request);
ASSERT_EQ (1, node.aggregator.size ());
Expand Down
52 changes: 26 additions & 26 deletions nano/core_test/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ TEST (socket, max_connections)

// start 3 clients, 2 will persist but 1 will be dropped

auto client1 = std::make_shared<nano::transport::client_socket> (*node);
auto client1 = std::make_shared<nano::transport::socket> (*node);
client1->async_connect (dst_endpoint, connect_handler);

auto client2 = std::make_shared<nano::transport::client_socket> (*node);
auto client2 = std::make_shared<nano::transport::socket> (*node);
client2->async_connect (dst_endpoint, connect_handler);

auto client3 = std::make_shared<nano::transport::client_socket> (*node);
auto client3 = std::make_shared<nano::transport::socket> (*node);
client3->async_connect (dst_endpoint, connect_handler);

auto get_tcp_accept_failures = [&node] () {
Expand All @@ -72,10 +72,10 @@ TEST (socket, max_connections)

server_sockets[0].reset ();

auto client4 = std::make_shared<nano::transport::client_socket> (*node);
auto client4 = std::make_shared<nano::transport::socket> (*node);
client4->async_connect (dst_endpoint, connect_handler);

auto client5 = std::make_shared<nano::transport::client_socket> (*node);
auto client5 = std::make_shared<nano::transport::socket> (*node);
client5->async_connect (dst_endpoint, connect_handler);

ASSERT_TIMELY_EQ (5s, get_tcp_accept_failures (), 2);
Expand All @@ -89,13 +89,13 @@ TEST (socket, max_connections)
server_sockets[1].reset ();
server_sockets[2].reset ();

auto client6 = std::make_shared<nano::transport::client_socket> (*node);
auto client6 = std::make_shared<nano::transport::socket> (*node);
client6->async_connect (dst_endpoint, connect_handler);

auto client7 = std::make_shared<nano::transport::client_socket> (*node);
auto client7 = std::make_shared<nano::transport::socket> (*node);
client7->async_connect (dst_endpoint, connect_handler);

auto client8 = std::make_shared<nano::transport::client_socket> (*node);
auto client8 = std::make_shared<nano::transport::socket> (*node);
client8->async_connect (dst_endpoint, connect_handler);

ASSERT_TIMELY_EQ (5s, get_tcp_accept_failures (), 3);
Expand Down Expand Up @@ -144,7 +144,7 @@ TEST (socket, max_connections_per_ip)

for (auto idx = 0; idx < max_ip_connections + 1; ++idx)
{
auto client = std::make_shared<nano::transport::client_socket> (*node);
auto client = std::make_shared<nano::transport::socket> (*node);
client->async_connect (dst_endpoint, connect_handler);
client_list.push_back (client);
}
Expand Down Expand Up @@ -199,13 +199,13 @@ TEST (socket, count_subnetwork_connections)
auto address5 = boost::asio::ip::make_address ("a41d:b7b3::"); // out of the network prefix
auto address6 = boost::asio::ip::make_address ("a41d:b7b3::1"); // out of the network prefix

auto connection0 = std::make_shared<nano::transport::client_socket> (*node);
auto connection1 = std::make_shared<nano::transport::client_socket> (*node);
auto connection2 = std::make_shared<nano::transport::client_socket> (*node);
auto connection3 = std::make_shared<nano::transport::client_socket> (*node);
auto connection4 = std::make_shared<nano::transport::client_socket> (*node);
auto connection5 = std::make_shared<nano::transport::client_socket> (*node);
auto connection6 = std::make_shared<nano::transport::client_socket> (*node);
auto connection0 = std::make_shared<nano::transport::socket> (*node);
auto connection1 = std::make_shared<nano::transport::socket> (*node);
auto connection2 = std::make_shared<nano::transport::socket> (*node);
auto connection3 = std::make_shared<nano::transport::socket> (*node);
auto connection4 = std::make_shared<nano::transport::socket> (*node);
auto connection5 = std::make_shared<nano::transport::socket> (*node);
auto connection6 = std::make_shared<nano::transport::socket> (*node);

nano::transport::address_socket_mmap connections_per_address;
connections_per_address.emplace (address0, connection0);
Expand Down Expand Up @@ -264,7 +264,7 @@ TEST (socket, max_connections_per_subnetwork)

for (auto idx = 0; idx < max_subnetwork_connections + 1; ++idx)
{
auto client = std::make_shared<nano::transport::client_socket> (*node);
auto client = std::make_shared<nano::transport::socket> (*node);
client->async_connect (dst_endpoint, connect_handler);
client_list.push_back (client);
}
Expand Down Expand Up @@ -324,7 +324,7 @@ TEST (socket, disabled_max_peers_per_ip)

for (auto idx = 0; idx < max_ip_connections + 1; ++idx)
{
auto client = std::make_shared<nano::transport::client_socket> (*node);
auto client = std::make_shared<nano::transport::socket> (*node);
client->async_connect (dst_endpoint, connect_handler);
client_list.push_back (client);
}
Expand Down Expand Up @@ -372,7 +372,7 @@ TEST (socket, disconnection_of_silent_connections)
boost::asio::ip::tcp::endpoint dst_endpoint{ boost::asio::ip::address_v6::loopback (), listener->endpoint ().port () };

// Instantiates a client to simulate an incoming connection.
auto client_socket = std::make_shared<nano::transport::client_socket> (*node);
auto client_socket = std::make_shared<nano::transport::socket> (*node);
std::atomic<bool> connected{ false };
// Opening a connection that will be closed because it remains silent during the tolerance time.
client_socket->async_connect (dst_endpoint, [client_socket, &connected] (boost::system::error_code const & ec_a) {
Expand Down Expand Up @@ -420,7 +420,7 @@ TEST (socket, drop_policy)
return true;
});

auto client = std::make_shared<nano::transport::client_socket> (*node);
auto client = std::make_shared<nano::transport::socket> (*node);
nano::transport::channel_tcp channel{ *node, client };
nano::test::counted_completion write_completion (static_cast<unsigned> (total_message_count));

Expand Down Expand Up @@ -521,7 +521,7 @@ TEST (socket, concurrent_writes)
std::vector<std::shared_ptr<nano::transport::socket>> clients;
for (unsigned i = 0; i < client_count; i++)
{
auto client = std::make_shared<nano::transport::client_socket> (*node);
auto client = std::make_shared<nano::transport::socket> (*node);
clients.push_back (client);
client->async_connect (boost::asio::ip::tcp::endpoint (boost::asio::ip::address_v4::loopback (), listener->endpoint ().port ()),
[&connection_count_completion] (boost::system::error_code const & ec_a) {
Expand Down Expand Up @@ -587,7 +587,7 @@ TEST (socket_timeout, connect)
boost::asio::ip::tcp::endpoint endpoint (boost::asio::ip::make_address_v6 ("::ffff:10.255.254.253"), 1234);

// create a client socket and try to connect to the IP address that wil not respond
auto socket = std::make_shared<nano::transport::client_socket> (*node);
auto socket = std::make_shared<nano::transport::socket> (*node);
std::atomic<bool> done = false;
boost::system::error_code ec;
socket->async_connect (endpoint, [&ec, &done] (boost::system::error_code const & ec_a) {
Expand Down Expand Up @@ -628,7 +628,7 @@ TEST (socket_timeout, read)
});

// create a client socket to connect and call async_read, which should time out
auto socket = std::make_shared<nano::transport::client_socket> (*node);
auto socket = std::make_shared<nano::transport::socket> (*node);
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 @@ -676,7 +676,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::client_socket> (*node, 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_type_t::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 @@ -731,7 +731,7 @@ TEST (socket_timeout, read_overlapped)
});

// create a client socket to connect and call async_read twice, the second call should time out
auto socket = std::make_shared<nano::transport::client_socket> (*node);
auto socket = std::make_shared<nano::transport::socket> (*node);
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 @@ -790,7 +790,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::client_socket> (*node, 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_type_t::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
2 changes: 1 addition & 1 deletion nano/node/bootstrap/bootstrap_connections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ std::shared_ptr<nano::bootstrap_client> nano::bootstrap_connections::find_connec
void nano::bootstrap_connections::connect_client (nano::tcp_endpoint const & endpoint_a, bool push_front)
{
++connections_count;
auto socket (std::make_shared<nano::transport::client_socket> (node));
auto socket (std::make_shared<nano::transport::socket> (node));
auto this_l (shared_from_this ());
socket->async_connect (endpoint_a,
[this_l, socket, endpoint_a, push_front] (boost::system::error_code const & ec) {
Expand Down
22 changes: 4 additions & 18 deletions nano/node/transport/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum class buffer_drop_policy
};

/** Socket class for tcp clients and newly accepted connections */
class socket : public std::enable_shared_from_this<nano::transport::socket>
class socket final : public std::enable_shared_from_this<nano::transport::socket>
{
friend class tcp_server;
friend class tcp_channels;
Expand Down Expand Up @@ -67,16 +67,16 @@ class socket : public std::enable_shared_from_this<nano::transport::socket>
* @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, std::size_t max_queue_size = default_max_queue_size);
virtual ~socket ();
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);
~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);

virtual void close ();
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 */
Expand Down Expand Up @@ -218,18 +218,4 @@ namespace socket_functions
boost::asio::ip::address last_ipv6_subnet_address (boost::asio::ip::address_v6 const &, std::size_t);
std::size_t count_subnetwork_connections (nano::transport::address_socket_mmap const &, boost::asio::ip::address_v6 const &, std::size_t);
}

/** Socket class for TCP clients */
class client_socket final : public socket
{
public:
/**
* Constructor
* @param node_a Owning node
*/
explicit client_socket (nano::node & node_a, std::size_t max_queue_size = default_max_queue_size) :
socket{ node_a, endpoint_type_t::client, max_queue_size }
{
}
};
}
2 changes: 1 addition & 1 deletion nano/node/transport/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ void nano::transport::tcp_channels::update (nano::tcp_endpoint const & endpoint_

void nano::transport::tcp_channels::start_tcp (nano::endpoint const & endpoint_a)
{
auto socket = std::make_shared<nano::transport::client_socket> (node);
auto socket = std::make_shared<nano::transport::socket> (node);
std::weak_ptr<nano::transport::socket> socket_w (socket);
auto channel (std::make_shared<nano::transport::channel_tcp> (node, socket_w));
std::weak_ptr<nano::node> node_w (node.shared ());
Expand Down

0 comments on commit 32ba010

Please sign in to comment.