Cleanup nano::transport::establish_tcp

Removes usage of start_tcp callback and replace it with simple polling for an established connection.
This commit is contained in:
clemahieu 2021-09-11 14:07:35 +01:00
commit ce29ba079f
No known key found for this signature in database
GPG key ID: 43708520C8DFB938

View file

@ -8,27 +8,19 @@
#include <chrono> #include <chrono>
#include <future> #include <future>
using namespace std::chrono_literals;
std::shared_ptr<nano::transport::channel_tcp> nano::establish_tcp (nano::system & system, nano::node & node, nano::endpoint const & endpoint) std::shared_ptr<nano::transport::channel_tcp> nano::establish_tcp (nano::system & system, nano::node & node, nano::endpoint const & endpoint)
{ {
using namespace std::chrono_literals;
debug_assert (node.network.endpoint () != endpoint && "Establishing TCP to self is not allowed"); debug_assert (node.network.endpoint () != endpoint && "Establishing TCP to self is not allowed");
std::shared_ptr<nano::transport::channel_tcp> result; std::shared_ptr<nano::transport::channel_tcp> result;
debug_assert (!node.flags.disable_tcp_realtime); debug_assert (!node.flags.disable_tcp_realtime);
std::promise<std::shared_ptr<nano::transport::channel>> promise; node.network.tcp_channels.start_tcp (endpoint);
auto callback = [&promise] (std::shared_ptr<nano::transport::channel> channel_a) { promise.set_value (channel_a); }; auto error = system.poll_until_true (2s, [&result, &node, &endpoint] {
auto future = promise.get_future (); result = node.network.tcp_channels.find_channel (nano::transport::map_endpoint_to_tcp (endpoint));
node.network.tcp_channels.start_tcp (endpoint, callback); return result != nullptr;
auto error = system.poll_until_true (2s, [&future] { return future.wait_for (0s) == std::future_status::ready; }); });
if (!error)
{
auto channel = future.get ();
EXPECT_NE (nullptr, channel);
if (channel)
{
result = node.network.tcp_channels.find_channel (channel->get_tcp_endpoint ());
}
}
return result; return result;
} }