simplify IXDNSLookup

This commit is contained in:
Benjamin Sergeant
2019-06-26 16:25:07 -07:00
parent e646e53dac
commit 3750781bce
3 changed files with 11 additions and 24 deletions

View File

@ -15,12 +15,13 @@ namespace ix
const int64_t DNSLookup::kDefaultWait = 10; // ms
DNSLookup::DNSLookup(const std::string& hostname, int port, int64_t wait) :
_hostname(hostname),
_port(port),
_wait(wait),
_res(nullptr),
_done(false)
{
setHostname(hostname);
;
}
struct addrinfo* DNSLookup::getAddrInfo(const std::string& hostname,
@ -66,7 +67,7 @@ namespace ix
return nullptr;
}
return getAddrInfo(getHostname(), _port, errMsg);
return getAddrInfo(_hostname, _port, errMsg);
}
struct addrinfo* DNSLookup::resolveAsync(std::string& errMsg,
@ -89,7 +90,9 @@ namespace ix
auto ptr = shared_from_this();
std::weak_ptr<DNSLookup> self(ptr);
_thread = std::thread(&DNSLookup::run, this, self, getHostname(), _port);
int port = _port;
std::string hostname(_hostname);
_thread = std::thread(&DNSLookup::run, this, self, hostname, port);
_thread.detach();
std::unique_lock<std::mutex> lock(_conditionVariableMutex);
@ -123,7 +126,7 @@ namespace ix
return getRes();
}
void DNSLookup::run(std::weak_ptr<DNSLookup> self, const std::string& hostname, int port) // thread runner
void DNSLookup::run(std::weak_ptr<DNSLookup> self, std::string hostname, int port) // thread runner
{
// We don't want to read or write into members variables of an object that could be
// gone, so we use temporary variables (res) or we pass in by copy everything that
@ -142,18 +145,6 @@ namespace ix
}
}
void DNSLookup::setHostname(const std::string& hostname)
{
std::lock_guard<std::mutex> lock(_hostnameMutex);
_hostname = hostname;
}
const std::string& DNSLookup::getHostname()
{
std::lock_guard<std::mutex> lock(_hostnameMutex);
return _hostname;
}
void DNSLookup::setErrMsg(const std::string& errMsg)
{
std::lock_guard<std::mutex> lock(_errMsgMutex);