Use private udns context for each DNSClient

This commit is contained in:
Torsten Dreyer
2016-11-20 12:35:43 +01:00
parent 73f57bbbd8
commit 9088f41352
2 changed files with 24 additions and 18 deletions
+19 -13
View File
@@ -22,7 +22,7 @@
// //
#include "DNSClient.hxx" #include "DNSClient.hxx"
#include "udns.h" #include <udns.h>
#include <time.h> #include <time.h>
#include <simgear/debug/logstream.hxx> #include <simgear/debug/logstream.hxx>
@@ -33,16 +33,21 @@ namespace DNS {
class Client::ClientPrivate { class Client::ClientPrivate {
public: public:
ClientPrivate() { ClientPrivate() {
if (dns_init(NULL, 0) < 0) ctx = dns_new(NULL);
if (dns_init(ctx, 0) < 0)
SG_LOG(SG_IO, SG_ALERT, "Can't init udns library" ); SG_LOG(SG_IO, SG_ALERT, "Can't init udns library" );
if( dns_open(NULL) < 0 ) if( dns_open(ctx) < 0 )
SG_LOG(SG_IO, SG_ALERT, "Can't open udns context" ); SG_LOG(SG_IO, SG_ALERT, "Can't open udns context" );
} }
~ClientPrivate() { ~ClientPrivate() {
dns_close(NULL); dns_close(ctx);
dns_free(ctx);
} }
struct dns_ctx * ctx;
}; };
Request::Request( const std::string & dn ) : Request::Request( const std::string & dn ) :
@@ -111,10 +116,10 @@ static void dnscbSRV(struct dns_ctx *ctx, struct dns_rr_srv *result, void *data)
r->setComplete(); r->setComplete();
} }
void SRVRequest::submit() void SRVRequest::submit( Client * client )
{ {
// if service is defined, pass service and protocol // if service is defined, pass service and protocol
if (!dns_submit_srv(NULL, getDn().c_str(), _service.empty() ? NULL : _service.c_str(), _service.empty() ? NULL : _protocol.c_str(), 0, dnscbSRV, this )) { if (!dns_submit_srv(client->d->ctx, getDn().c_str(), _service.empty() ? NULL : _service.c_str(), _service.empty() ? NULL : _protocol.c_str(), 0, dnscbSRV, this )) {
SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn()); SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn());
return; return;
} }
@@ -148,10 +153,10 @@ static void dnscbTXT(struct dns_ctx *ctx, struct dns_rr_txt *result, void *data)
r->setComplete(); r->setComplete();
} }
void TXTRequest::submit() void TXTRequest::submit( Client * client )
{ {
// protocol and service an already encoded in DN so pass in NULL for both // protocol and service an already encoded in DN so pass in NULL for both
if (!dns_submit_txt(NULL, getDn().c_str(), DNS_C_IN, 0, dnscbTXT, this )) { if (!dns_submit_txt(client->d->ctx, getDn().c_str(), DNS_C_IN, 0, dnscbTXT, this )) {
SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn()); SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn());
return; return;
} }
@@ -196,9 +201,9 @@ static void dnscbNAPTR(struct dns_ctx *ctx, struct dns_rr_naptr *result, void *d
r->setComplete(); r->setComplete();
} }
void NAPTRRequest::submit() void NAPTRRequest::submit( Client * client )
{ {
if (!dns_submit_naptr(NULL, getDn().c_str(), 0, dnscbNAPTR, this )) { if (!dns_submit_naptr(client->d->ctx, getDn().c_str(), 0, dnscbNAPTR, this )) {
SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn()); SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn());
return; return;
} }
@@ -217,15 +222,16 @@ Client::Client() :
void Client::makeRequest(const Request_ptr& r) void Client::makeRequest(const Request_ptr& r)
{ {
r->submit(); r->submit(this);
} }
void Client::update(int waitTimeout) void Client::update(int waitTimeout)
{ {
time_t now = time(NULL); time_t now = time(NULL);
if( dns_timeouts( NULL, waitTimeout, now ) < 0 ) if( dns_timeouts( d->ctx, waitTimeout, now ) < 0 )
return; return;
dns_ioevent(NULL, now);
dns_ioevent(d->ctx, now);
} }
} // of namespace DNS } // of namespace DNS
+5 -5
View File
@@ -39,6 +39,7 @@ namespace simgear
namespace DNS namespace DNS
{ {
class Client;
class Request : public SGReferenced class Request : public SGReferenced
{ {
public: public:
@@ -50,7 +51,7 @@ public:
bool isTimeout() const; bool isTimeout() const;
void setComplete( bool b = true ) { _complete = b; } void setComplete( bool b = true ) { _complete = b; }
virtual void submit() = 0; virtual void submit( Client * client) = 0;
std::string cname; std::string cname;
std::string qname; std::string qname;
@@ -68,7 +69,7 @@ class NAPTRRequest : public Request
{ {
public: public:
NAPTRRequest( const std::string & dn ); NAPTRRequest( const std::string & dn );
virtual void submit(); virtual void submit( Client * client );
struct NAPTR : SGReferenced { struct NAPTR : SGReferenced {
int order; int order;
@@ -91,7 +92,7 @@ class SRVRequest : public Request
public: public:
SRVRequest( const std::string & dn ); SRVRequest( const std::string & dn );
SRVRequest( const std::string & dn, const string & service, const string & protocol ); SRVRequest( const std::string & dn, const string & service, const string & protocol );
virtual void submit(); virtual void submit( Client * client );
struct SRV : SGReferenced { struct SRV : SGReferenced {
int priority; int priority;
@@ -111,7 +112,7 @@ class TXTRequest : public Request
{ {
public: public:
TXTRequest( const std::string & dn ); TXTRequest( const std::string & dn );
virtual void submit(); virtual void submit( Client * client );
typedef std::vector<string> TXT_list; typedef std::vector<string> TXT_list;
typedef std::map<std::string,std::string> TXT_Attribute_map; typedef std::map<std::string,std::string> TXT_Attribute_map;
@@ -131,7 +132,6 @@ public:
// void cancelRequest(const Request_ptr& r, std::string reason = std::string()); // void cancelRequest(const Request_ptr& r, std::string reason = std::string());
private:
class ClientPrivate; class ClientPrivate;
std::auto_ptr<ClientPrivate> d; std::auto_ptr<ClientPrivate> d;
}; };