2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2018-07-17 01:06:24 -07:00
|
|
|
|
|
|
|
|
#include "wpi/uv/GetAddrInfo.h"
|
|
|
|
|
|
2021-06-06 16:13:58 -07:00
|
|
|
#include "wpi/SmallString.h"
|
2018-07-17 01:06:24 -07:00
|
|
|
#include "wpi/uv/Loop.h"
|
|
|
|
|
#include "wpi/uv/util.h"
|
|
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
namespace wpi::uv {
|
2018-07-17 01:06:24 -07:00
|
|
|
|
|
|
|
|
GetAddrInfoReq::GetAddrInfoReq() {
|
|
|
|
|
error = [this](Error err) { GetLoop().error(err); };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GetAddrInfo(Loop& loop, const std::shared_ptr<GetAddrInfoReq>& req,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view node, std::string_view service,
|
2018-07-17 01:06:24 -07:00
|
|
|
const addrinfo* hints) {
|
2021-06-06 16:13:58 -07:00
|
|
|
SmallString<128> nodeStr{node};
|
|
|
|
|
SmallString<128> serviceStr{service};
|
2018-07-17 01:06:24 -07:00
|
|
|
int err = uv_getaddrinfo(
|
|
|
|
|
loop.GetRaw(), req->GetRaw(),
|
|
|
|
|
[](uv_getaddrinfo_t* req, int status, addrinfo* res) {
|
|
|
|
|
auto& h = *static_cast<GetAddrInfoReq*>(req->data);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (status < 0) {
|
2018-07-17 01:06:24 -07:00
|
|
|
h.ReportError(status);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2018-07-17 01:06:24 -07:00
|
|
|
h.resolved(*res);
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2018-07-17 01:06:24 -07:00
|
|
|
uv_freeaddrinfo(res);
|
|
|
|
|
h.Release(); // this is always a one-shot
|
|
|
|
|
},
|
2021-06-06 16:13:58 -07:00
|
|
|
node.empty() ? nullptr : nodeStr.c_str(),
|
|
|
|
|
service.empty() ? nullptr : serviceStr.c_str(), hints);
|
2020-12-28 12:58:06 -08:00
|
|
|
if (err < 0) {
|
2018-07-17 01:06:24 -07:00
|
|
|
loop.ReportError(err);
|
2020-12-28 12:58:06 -08:00
|
|
|
} else {
|
2018-07-17 01:06:24 -07:00
|
|
|
req->Keep();
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2018-07-17 01:06:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GetAddrInfo(Loop& loop, std::function<void(const addrinfo&)> callback,
|
2021-06-06 16:13:58 -07:00
|
|
|
std::string_view node, std::string_view service,
|
2018-07-17 01:06:24 -07:00
|
|
|
const addrinfo* hints) {
|
|
|
|
|
auto req = std::make_shared<GetAddrInfoReq>();
|
|
|
|
|
req->resolved.connect(callback);
|
|
|
|
|
GetAddrInfo(loop, req, node, service, hints);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 01:19:59 -08:00
|
|
|
} // namespace wpi::uv
|