[wpinet] ParallelTcpConnector: Add option to resolve only IPv4 addresses (#7194)

This commit is contained in:
Peter Johnson
2024-10-11 16:42:59 -07:00
committed by GitHub
parent 768fa5f973
commit c6d801d2d6
2 changed files with 12 additions and 6 deletions

View File

@@ -24,10 +24,11 @@ using namespace wpi;
ParallelTcpConnector::ParallelTcpConnector(
wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate,
wpi::Logger& logger, std::function<void(wpi::uv::Tcp& tcp)> connected,
const private_init&)
bool ipv4Only, const private_init&)
: m_loop{loop},
m_logger{logger},
m_reconnectRate{reconnectRate},
m_ipv4Only{ipv4Only},
m_connected{std::move(connected)},
m_reconnectTimer{uv::Timer::Create(loop)} {
if (!m_reconnectTimer) {
@@ -193,7 +194,7 @@ void ParallelTcpConnector::Connect() {
static_cast<void*>(req.get()), server.first, server.second);
addrinfo hints;
std::memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_family = m_ipv4Only ? AF_INET : AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG;

View File

@@ -54,22 +54,26 @@ class ParallelTcpConnector
* @param connected callback function when a connection succeeds; may be
* called multiple times if it does not call Succeeded()
* before returning
* @param ipv4Only true if only IPv4 addresses should be returned; otherwise
* both IPv4 and IPv6 addresses are returned
* @return Parallel connector
*/
static std::shared_ptr<ParallelTcpConnector> Create(
wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate,
wpi::Logger& logger, std::function<void(wpi::uv::Tcp& tcp)> connected) {
wpi::Logger& logger, std::function<void(wpi::uv::Tcp& tcp)> connected,
bool ipv4Only = false) {
if (loop.IsClosing()) {
return nullptr;
}
return std::make_shared<ParallelTcpConnector>(
loop, reconnectRate, logger, std::move(connected), private_init{});
return std::make_shared<ParallelTcpConnector>(loop, reconnectRate, logger,
std::move(connected),
ipv4Only, private_init{});
}
ParallelTcpConnector(wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate,
wpi::Logger& logger,
std::function<void(wpi::uv::Tcp& tcp)> connected,
const private_init&);
bool ipv4Only, const private_init&);
~ParallelTcpConnector();
ParallelTcpConnector(const ParallelTcpConnector&) = delete;
@@ -111,6 +115,7 @@ class ParallelTcpConnector
wpi::uv::Loop& m_loop;
wpi::Logger& m_logger;
wpi::uv::Timer::Time m_reconnectRate;
bool m_ipv4Only;
std::function<void(wpi::uv::Tcp& tcp)> m_connected;
std::shared_ptr<wpi::uv::Timer> m_reconnectTimer;
std::vector<std::pair<std::string, unsigned int>> m_servers;