implement DnsTask and async WFDnsResolver

This commit is contained in:
kedixa
2021-06-01 21:43:12 +08:00
parent 233e2ed843
commit 76624fd6fb
33 changed files with 3532 additions and 386 deletions
+1 -1
View File
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.6)
project(algorithm)
set(SRC
DNSRoutine.cc
DnsRoutine.cc
)
add_library(${PROJECT_NAME} OBJECT ${SRC})
@@ -25,11 +25,11 @@
#include <stdio.h>
#include <string.h>
#include <string>
#include "DNSRoutine.h"
#include "DnsRoutine.h"
#define PORT_STR_MAX 5
DNSOutput::DNSOutput(DNSOutput&& move)
DnsOutput::DnsOutput(DnsOutput&& move)
{
error_ = move.error_;
addrinfo_ = move.addrinfo_;
@@ -38,7 +38,7 @@ DNSOutput::DNSOutput(DNSOutput&& move)
move.addrinfo_ = NULL;
}
DNSOutput& DNSOutput::operator= (DNSOutput&& move)
DnsOutput& DnsOutput::operator= (DnsOutput&& move)
{
if (this != &move)
{
@@ -55,7 +55,7 @@ DNSOutput& DNSOutput::operator= (DNSOutput&& move)
return *this;
}
void DNSRoutine::run_local_path(const std::string& path, DNSOutput *out)
void DnsRoutine::run_local_path(const std::string& path, DnsOutput *out)
{
struct sockaddr_un *sun = NULL;
@@ -85,7 +85,7 @@ void DNSRoutine::run_local_path(const std::string& path, DNSOutput *out)
out->error_ = EAI_SYSTEM;
}
void DNSRoutine::run(const DNSInput *in, DNSOutput *out)
void DnsRoutine::run(const DnsInput *in, DnsOutput *out)
{
if (!in->host_.empty() && in->host_[0] == '/')
{
@@ -23,17 +23,17 @@
#include <netdb.h>
#include <string>
class DNSInput
class DnsInput
{
public:
DNSInput():
DnsInput():
port_(0)
{}
//move constructor
DNSInput(DNSInput&& move) = default;
DnsInput(DnsInput&& move) = default;
//move operator
DNSInput& operator= (DNSInput &&move) = default;
DnsInput& operator= (DnsInput &&move) = default;
void reset(const std::string& host, unsigned short port)
{
@@ -48,32 +48,32 @@ protected:
std::string host_;
unsigned short port_;
friend class DNSRoutine;
friend class DnsRoutine;
};
class DNSOutput
class DnsOutput
{
public:
DNSOutput():
DnsOutput():
error_(0),
addrinfo_(NULL)
{}
~DNSOutput()
~DnsOutput()
{
if (addrinfo_)
freeaddrinfo(addrinfo_);
}
//move constructor
DNSOutput(DNSOutput&& move);
DnsOutput(DnsOutput&& move);
//move operator
DNSOutput& operator= (DNSOutput&& move);
DnsOutput& operator= (DnsOutput&& move);
int get_error() const { return error_; }
const struct addrinfo *get_addrinfo() const { return addrinfo_; }
//if DONOT want DNSOutput release addrinfo, use move_addrinfo in callback
//if DONOT want DnsOutput release addrinfo, use move_addrinfo in callback
struct addrinfo *move_addrinfo()
{
struct addrinfo *p = addrinfo_;
@@ -85,19 +85,27 @@ protected:
int error_;
struct addrinfo *addrinfo_;
friend class DNSRoutine;
friend class DnsRoutine;
};
class DNSRoutine
class DnsRoutine
{
public:
static void run(const DNSInput *in, DNSOutput *out);
static void run(const DnsInput *in, DnsOutput *out);
static void create(DnsOutput *out, int error, struct addrinfo *ai)
{
if (out->addrinfo_)
freeaddrinfo(out->addrinfo_);
out->error_ = error;
out->addrinfo_ = ai;
}
private:
static void run_local_path(const std::string& path, DNSOutput *out);
static void run_local_path(const std::string& path, DnsOutput *out);
};
//new WFDNSTask(queue, executor, dns_routine, callback)
//new WFDnsTask(queue, executor, dns_routine, callback)
//if donot want freeaddrinfo, please std::move output in callback
#endif