-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
nano::transport::channel
to a dedicated file (#4155)
- Loading branch information
1 parent
b07fa46
commit 159be4f
Showing
12 changed files
with
265 additions
and
233 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <nano/node/common.hpp> | ||
#include <nano/node/node.hpp> | ||
#include <nano/node/transport/channel.hpp> | ||
#include <nano/node/transport/transport.hpp> | ||
|
||
#include <boost/asio/ip/address.hpp> | ||
#include <boost/asio/ip/address_v4.hpp> | ||
#include <boost/asio/ip/address_v6.hpp> | ||
#include <boost/format.hpp> | ||
|
||
nano::transport::channel::channel (nano::node & node_a) : | ||
node{ node_a } | ||
{ | ||
set_network_version (node_a.network_params.network.protocol_version); | ||
} | ||
|
||
void nano::transport::channel::send (nano::message & message_a, std::function<void (boost::system::error_code const &, std::size_t)> const & callback_a, nano::buffer_drop_policy drop_policy_a, nano::bandwidth_limit_type limiter_type) | ||
{ | ||
auto buffer (message_a.to_shared_const_buffer ()); | ||
auto detail = nano::to_stat_detail (message_a.header.type); | ||
auto is_droppable_by_limiter = drop_policy_a == nano::buffer_drop_policy::limiter; | ||
auto should_pass (node.outbound_limiter.should_pass (buffer.size (), limiter_type)); | ||
if (!is_droppable_by_limiter || should_pass) | ||
{ | ||
send_buffer (buffer, callback_a, drop_policy_a); | ||
node.stats.inc (nano::stat::type::message, detail, nano::stat::dir::out); | ||
} | ||
else | ||
{ | ||
if (callback_a) | ||
{ | ||
node.background ([callback_a] () { | ||
callback_a (boost::system::errc::make_error_code (boost::system::errc::not_supported), 0); | ||
}); | ||
} | ||
|
||
node.stats.inc (nano::stat::type::drop, detail, nano::stat::dir::out); | ||
if (node.config.logging.network_packet_logging ()) | ||
{ | ||
node.logger.always_log (boost::str (boost::format ("%1% of size %2% dropped") % nano::to_string (detail) % buffer.size ())); | ||
} | ||
} | ||
} | ||
|
||
void nano::transport::channel::set_peering_endpoint (nano::endpoint endpoint) | ||
{ | ||
nano::lock_guard<nano::mutex> lock{ channel_mutex }; | ||
peering_endpoint = endpoint; | ||
} | ||
|
||
nano::endpoint nano::transport::channel::get_peering_endpoint () const | ||
{ | ||
nano::unique_lock<nano::mutex> lock{ channel_mutex }; | ||
if (peering_endpoint) | ||
{ | ||
return *peering_endpoint; | ||
} | ||
else | ||
{ | ||
lock.unlock (); | ||
return get_endpoint (); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
#pragma once | ||
|
||
#include <nano/lib/locks.hpp> | ||
#include <nano/lib/stats.hpp> | ||
#include <nano/node/bandwidth_limiter.hpp> | ||
#include <nano/node/common.hpp> | ||
#include <nano/node/messages.hpp> | ||
#include <nano/node/socket.hpp> | ||
|
||
#include <boost/asio/ip/network_v6.hpp> | ||
|
||
namespace nano::transport | ||
{ | ||
enum class transport_type : uint8_t | ||
{ | ||
undefined = 0, | ||
tcp = 1, | ||
loopback = 2, | ||
fake = 3 | ||
}; | ||
|
||
class channel | ||
{ | ||
public: | ||
explicit channel (nano::node &); | ||
virtual ~channel () = default; | ||
|
||
virtual std::size_t hash_code () const = 0; | ||
virtual bool operator== (nano::transport::channel const &) const = 0; | ||
void send (nano::message & message_a, std::function<void (boost::system::error_code const &, std::size_t)> const & callback_a = nullptr, nano::buffer_drop_policy policy_a = nano::buffer_drop_policy::limiter, nano::bandwidth_limit_type = nano::bandwidth_limit_type::standard); | ||
// TODO: investigate clang-tidy warning about default parameters on virtual/override functions | ||
// | ||
virtual void send_buffer (nano::shared_const_buffer const &, std::function<void (boost::system::error_code const &, std::size_t)> const & = nullptr, nano::buffer_drop_policy = nano::buffer_drop_policy::limiter) = 0; | ||
virtual std::string to_string () const = 0; | ||
virtual nano::endpoint get_endpoint () const = 0; | ||
virtual nano::tcp_endpoint get_tcp_endpoint () const = 0; | ||
virtual nano::transport::transport_type get_type () const = 0; | ||
virtual bool max () | ||
{ | ||
return false; | ||
} | ||
virtual bool alive () const | ||
{ | ||
return true; | ||
} | ||
|
||
std::chrono::steady_clock::time_point get_last_bootstrap_attempt () const | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
return last_bootstrap_attempt; | ||
} | ||
|
||
void set_last_bootstrap_attempt (std::chrono::steady_clock::time_point const time_a) | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
last_bootstrap_attempt = time_a; | ||
} | ||
|
||
std::chrono::steady_clock::time_point get_last_packet_received () const | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
return last_packet_received; | ||
} | ||
|
||
void set_last_packet_received (std::chrono::steady_clock::time_point const time_a) | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
last_packet_received = time_a; | ||
} | ||
|
||
std::chrono::steady_clock::time_point get_last_packet_sent () const | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
return last_packet_sent; | ||
} | ||
|
||
void set_last_packet_sent (std::chrono::steady_clock::time_point const time_a) | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
last_packet_sent = time_a; | ||
} | ||
|
||
boost::optional<nano::account> get_node_id_optional () const | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
return node_id; | ||
} | ||
|
||
nano::account get_node_id () const | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
if (node_id.is_initialized ()) | ||
{ | ||
return node_id.get (); | ||
} | ||
else | ||
{ | ||
return 0; | ||
} | ||
} | ||
|
||
void set_node_id (nano::account node_id_a) | ||
{ | ||
nano::lock_guard<nano::mutex> lk (channel_mutex); | ||
node_id = node_id_a; | ||
} | ||
|
||
uint8_t get_network_version () const | ||
{ | ||
return network_version; | ||
} | ||
|
||
void set_network_version (uint8_t network_version_a) | ||
{ | ||
network_version = network_version_a; | ||
} | ||
|
||
nano::endpoint get_peering_endpoint () const; | ||
void set_peering_endpoint (nano::endpoint endpoint); | ||
|
||
mutable nano::mutex channel_mutex; | ||
|
||
private: | ||
std::chrono::steady_clock::time_point last_bootstrap_attempt{ std::chrono::steady_clock::time_point () }; | ||
std::chrono::steady_clock::time_point last_packet_received{ std::chrono::steady_clock::now () }; | ||
std::chrono::steady_clock::time_point last_packet_sent{ std::chrono::steady_clock::now () }; | ||
boost::optional<nano::account> node_id{ boost::none }; | ||
std::atomic<uint8_t> network_version{ 0 }; | ||
std::optional<nano::endpoint> peering_endpoint{}; | ||
|
||
protected: | ||
nano::node & node; | ||
}; | ||
} | ||
|
||
namespace std | ||
{ | ||
template <> | ||
struct hash<::nano::transport::channel> | ||
{ | ||
std::size_t operator() (::nano::transport::channel const & channel_a) const | ||
{ | ||
return channel_a.hash_code (); | ||
} | ||
}; | ||
template <> | ||
struct equal_to<std::reference_wrapper<::nano::transport::channel const>> | ||
{ | ||
bool operator() (std::reference_wrapper<::nano::transport::channel const> const & lhs, std::reference_wrapper<::nano::transport::channel const> const & rhs) const | ||
{ | ||
return lhs.get () == rhs.get (); | ||
} | ||
}; | ||
} | ||
|
||
namespace boost | ||
{ | ||
template <> | ||
struct hash<::nano::transport::channel> | ||
{ | ||
std::size_t operator() (::nano::transport::channel const & channel_a) const | ||
{ | ||
std::hash<::nano::transport::channel> hash; | ||
return hash (channel_a); | ||
} | ||
}; | ||
template <> | ||
struct hash<std::reference_wrapper<::nano::transport::channel const>> | ||
{ | ||
std::size_t operator() (std::reference_wrapper<::nano::transport::channel const> const & channel_a) const | ||
{ | ||
std::hash<::nano::transport::channel> hash; | ||
return hash (channel_a.get ()); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.