mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[wpinet] ParallelTcpConnector: Add option to resolve only IPv4 addresses (#7194)
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user