simplify IXDNSLookup
This commit is contained in:
parent
e646e53dac
commit
3750781bce
@ -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);
|
||||
|
@ -42,10 +42,7 @@ namespace ix
|
||||
int port,
|
||||
std::string& errMsg);
|
||||
|
||||
void run(std::weak_ptr<DNSLookup> self, const std::string& hostname, int port); // thread runner
|
||||
|
||||
void setHostname(const std::string& hostname);
|
||||
const std::string& getHostname();
|
||||
void run(std::weak_ptr<DNSLookup> self, std::string hostname, int port); // thread runner
|
||||
|
||||
void setErrMsg(const std::string& errMsg);
|
||||
const std::string& getErrMsg();
|
||||
@ -54,7 +51,6 @@ namespace ix
|
||||
struct addrinfo* getRes();
|
||||
|
||||
std::string _hostname;
|
||||
std::mutex _hostnameMutex;
|
||||
int _port;
|
||||
|
||||
int64_t _wait;
|
||||
|
@ -253,14 +253,14 @@ namespace ix
|
||||
bool Socket::writeBytes(const std::string& str,
|
||||
const CancellationRequest& isCancellationRequested)
|
||||
{
|
||||
char* buffer = const_cast<char*>(str.c_str());
|
||||
int offset = 0;
|
||||
int len = (int) str.size();
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (isCancellationRequested && isCancellationRequested()) return false;
|
||||
|
||||
ssize_t ret = send(buffer, len);
|
||||
ssize_t ret = send((char*)&str[offset], len);
|
||||
|
||||
// We wrote some bytes, as needed, all good.
|
||||
if (ret > 0)
|
||||
@ -271,7 +271,7 @@ namespace ix
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer += ret;
|
||||
offset += ret;
|
||||
len -= ret;
|
||||
continue;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user