IXDNSLookup. Uses weak pointer + smart_ptr + shared_from_this instead of static sets + mutex to handle object going away before dns lookup has resolved

This commit is contained in:
Benjamin Sergeant
2019-06-19 00:43:59 -07:00
parent dbd62b8622
commit b26e9d0338
6 changed files with 28 additions and 52 deletions

View File

@ -16,16 +16,17 @@
#include <set>
#include <string>
#include <thread>
#include <memory>
struct addrinfo;
namespace ix
{
class DNSLookup
class DNSLookup : public std::enable_shared_from_this<DNSLookup>
{
public:
DNSLookup(const std::string& hostname, int port, int64_t wait = DNSLookup::kDefaultWait);
~DNSLookup();
~DNSLookup() = default;
struct addrinfo* resolve(std::string& errMsg,
const CancellationRequest& isCancellationRequested,
@ -41,7 +42,7 @@ namespace ix
int port,
std::string& errMsg);
void run(uint64_t id, const std::string& hostname, int port); // thread runner
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();
@ -69,11 +70,6 @@ namespace ix
std::condition_variable _condition;
std::mutex _conditionVariableMutex;
uint64_t _id;
static std::atomic<uint64_t> _nextId;
static std::set<uint64_t> _activeJobs;
static std::mutex _activeJobsMutex;
const static int64_t kDefaultWait;
};
} // namespace ix