IXWebSocket/ixwebsocket/IXDNSLookup.h

66 lines
2.0 KiB
C
Raw Permalink Normal View History

2018-12-15 01:28:11 +01:00
/*
* IXDNSLookup.h
* Author: Benjamin Sergeant
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
*
* Resolve a hostname+port to a struct addrinfo obtained with getaddrinfo
2018-12-15 01:28:11 +01:00
* Does this in a background thread so that it can be cancelled, since
* getaddrinfo is a blocking call, and we don't want to block the main thread on Mobile.
*/
#pragma once
#include "IXCancellationRequest.h"
#include <atomic>
2019-08-13 19:59:18 +02:00
#include <memory>
2019-06-27 04:12:48 +02:00
#include <mutex>
2018-12-15 02:49:42 +01:00
#include <set>
2019-05-30 17:46:50 +02:00
#include <string>
2018-12-15 01:28:11 +01:00
struct addrinfo;
namespace ix
2018-12-15 01:28:11 +01:00
{
class DNSLookup : public std::enable_shared_from_this<DNSLookup>
2019-05-30 17:46:50 +02:00
{
2018-12-15 01:28:11 +01:00
public:
2019-05-30 17:46:50 +02:00
DNSLookup(const std::string& hostname, int port, int64_t wait = DNSLookup::kDefaultWait);
~DNSLookup() = default;
2018-12-15 01:28:11 +01:00
struct addrinfo* resolve(std::string& errMsg,
const CancellationRequest& isCancellationRequested,
bool cancellable = true);
2018-12-15 01:28:11 +01:00
private:
struct addrinfo* resolveCancellable(std::string& errMsg,
const CancellationRequest& isCancellationRequested);
struct addrinfo* resolveUnCancellable(std::string& errMsg,
const CancellationRequest& isCancellationRequested);
2018-12-15 01:28:11 +01:00
static struct addrinfo* getAddrInfo(const std::string& hostname,
2018-12-15 01:28:11 +01:00
int port,
std::string& errMsg);
2019-06-27 01:25:07 +02:00
void run(std::weak_ptr<DNSLookup> self, std::string hostname, int port); // thread runner
void setErrMsg(const std::string& errMsg);
const std::string& getErrMsg();
void setRes(struct addrinfo* addr);
struct addrinfo* getRes();
2018-12-15 01:28:11 +01:00
std::string _hostname;
int _port;
2018-12-23 23:14:38 +01:00
int64_t _wait;
2019-06-27 04:12:48 +02:00
const static int64_t kDefaultWait;
2018-12-15 01:28:11 +01:00
struct addrinfo* _res;
std::mutex _resMutex;
std::string _errMsg;
std::mutex _errMsgMutex;
2018-12-15 01:28:11 +01:00
std::atomic<bool> _done;
};
2019-05-30 17:46:50 +02:00
} // namespace ix