Skip to content

Commit c6d801d

Browse files
authored
[wpinet] ParallelTcpConnector: Add option to resolve only IPv4 addresses (#7194)
1 parent 768fa5f commit c6d801d

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

wpinet/src/main/native/cpp/ParallelTcpConnector.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@ using namespace wpi;
2424
ParallelTcpConnector::ParallelTcpConnector(
2525
wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate,
2626
wpi::Logger& logger, std::function<void(wpi::uv::Tcp& tcp)> connected,
27-
const private_init&)
27+
bool ipv4Only, const private_init&)
2828
: m_loop{loop},
2929
m_logger{logger},
3030
m_reconnectRate{reconnectRate},
31+
m_ipv4Only{ipv4Only},
3132
m_connected{std::move(connected)},
3233
m_reconnectTimer{uv::Timer::Create(loop)} {
3334
if (!m_reconnectTimer) {
@@ -193,7 +194,7 @@ void ParallelTcpConnector::Connect() {
193194
static_cast<void*>(req.get()), server.first, server.second);
194195
addrinfo hints;
195196
std::memset(&hints, 0, sizeof(hints));
196-
hints.ai_family = AF_UNSPEC;
197+
hints.ai_family = m_ipv4Only ? AF_INET : AF_UNSPEC;
197198
hints.ai_socktype = SOCK_STREAM;
198199
hints.ai_protocol = IPPROTO_TCP;
199200
hints.ai_flags = AI_NUMERICSERV | AI_ADDRCONFIG;

wpinet/src/main/native/include/wpinet/ParallelTcpConnector.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,26 @@ class ParallelTcpConnector
5454
* @param connected callback function when a connection succeeds; may be
5555
* called multiple times if it does not call Succeeded()
5656
* before returning
57+
* @param ipv4Only true if only IPv4 addresses should be returned; otherwise
58+
* both IPv4 and IPv6 addresses are returned
5759
* @return Parallel connector
5860
*/
5961
static std::shared_ptr<ParallelTcpConnector> Create(
6062
wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate,
61-
wpi::Logger& logger, std::function<void(wpi::uv::Tcp& tcp)> connected) {
63+
wpi::Logger& logger, std::function<void(wpi::uv::Tcp& tcp)> connected,
64+
bool ipv4Only = false) {
6265
if (loop.IsClosing()) {
6366
return nullptr;
6467
}
65-
return std::make_shared<ParallelTcpConnector>(
66-
loop, reconnectRate, logger, std::move(connected), private_init{});
68+
return std::make_shared<ParallelTcpConnector>(loop, reconnectRate, logger,
69+
std::move(connected),
70+
ipv4Only, private_init{});
6771
}
6872

6973
ParallelTcpConnector(wpi::uv::Loop& loop, wpi::uv::Timer::Time reconnectRate,
7074
wpi::Logger& logger,
7175
std::function<void(wpi::uv::Tcp& tcp)> connected,
72-
const private_init&);
76+
bool ipv4Only, const private_init&);
7377
~ParallelTcpConnector();
7478

7579
ParallelTcpConnector(const ParallelTcpConnector&) = delete;
@@ -111,6 +115,7 @@ class ParallelTcpConnector
111115
wpi::uv::Loop& m_loop;
112116
wpi::Logger& m_logger;
113117
wpi::uv::Timer::Time m_reconnectRate;
118+
bool m_ipv4Only;
114119
std::function<void(wpi::uv::Tcp& tcp)> m_connected;
115120
std::shared_ptr<wpi::uv::Timer> m_reconnectTimer;
116121
std::vector<std::pair<std::string, unsigned int>> m_servers;

0 commit comments

Comments
 (0)