51 static void start(
ResolverImp * , FutureEvent::handle_type ) ;
57 static size_t count() ;
63 virtual void onFutureEvent()
override ;
67 typedef ResolverFuture::Pair Pair ;
69 unique_ptr<FutureEvent> m_future_event ;
73 G::threading::thread_type m_thread ;
75 static size_t m_instance_count ;
78 size_t GNet::ResolverImp::m_instance_count = 0U ;
81 m_resolver(&resolver) ,
84 m_location(location) ,
85 m_future(location.host(),location.service(),location.family(),location.dgram(),true) ,
86 m_thread(
ResolverImp::start,this,m_future_event->handle()) ,
92 GNet::ResolverImp::~ResolverImp()
94 m_timer.cancelTimer() ;
95 if( m_thread.joinable() )
97 G_WARNING(
"ResolverImp::dtor: waiting for getaddrinfo thread to complete" ) ;
103 size_t GNet::ResolverImp::count()
105 return m_instance_count ;
108 void GNet::ResolverImp::start( ResolverImp * This , FutureEvent::handle_type handle )
113 This->m_future.run() ;
122 void GNet::ResolverImp::onFutureEvent()
124 G_DEBUG(
"GNet::ResolverImp::onFutureEvent: future event: ptr=" << m_resolver ) ;
125 G_ASSERT( m_busy ) ;
if( !m_busy ) return ;
126 if( m_resolver ==
nullptr ) return ;
130 Resolver * resolver = m_resolver ;
131 m_resolver = nullptr ;
132 m_timer.startTimer( 0U ) ;
134 Pair result = m_future.get() ;
135 if( !m_future.error() )
136 m_location.update( result.first , result.second ) ;
138 G_DEBUG(
"GNet::ResolverImp::onFutureEvent: [" << m_future.reason() <<
"][" << m_location.displayString() <<
"]" ) ;
139 resolver->done( m_future.reason() , m_location ) ;
142 void GNet::ResolverImp::doDelete()
144 m_resolver = nullptr ;
145 m_timer.startTimer( 0U ) ;
148 void GNet::ResolverImp::onTimeout()
151 m_timer.startTimer( 1U ) ;
159 m_callback(callback) ,
167 const size_t sanity_limit = 50U ;
168 if( m_imp.get() !=
nullptr && ResolverImp::count() < sanity_limit )
171 G_DEBUG(
"GNet::Resolver::dtor: releasing still-busy thread: " << ResolverImp::count() ) ;
180 typedef ResolverFuture::Pair Pair ;
181 G_DEBUG(
"GNet::Resolver::resolve: resolve request [" << location.
displayString() <<
"] (" << location.
family() <<
")" ) ;
184 Pair result = future.get() ;
187 G_DEBUG(
"GNet::Resolver::resolve: resolve error [" << future.reason() <<
"]" ) ;
188 return future.reason() ;
192 G_DEBUG(
"GNet::Resolver::resolve: resolve result [" << result.first.displayString() <<
"][" << result.second <<
"]" ) ;
193 location.
update( result.first , result.second ) ;
194 return std::string() ;
198 GNet::Resolver::AddressList
GNet::Resolver::resolve(
const std::string & host ,
const std::string & service ,
int family ,
bool dgram )
201 G_DEBUG(
"GNet::Resolver::resolve: resolve-request [" << host <<
"/" << service <<
"/" << (family==AF_UNSPEC?
"ip":(family==AF_INET?
"ipv4":
"ipv6")) <<
"]" ) ;
206 G_DEBUG(
"GNet::Resolver::resolve: resolve result: list of " << list.size() ) ;
214 if( busy() )
throw BusyError() ;
215 G_DEBUG(
"GNet::Resolver::start: resolve start [" << location.
displayString() <<
"]" ) ;
216 m_imp.reset(
new ResolverImp(*
this,m_eh,location) ) ;
219 void GNet::Resolver::done( std::string error ,
Location location )
222 G_DEBUG(
"GNet::Resolver::done: resolve done: error=[" << error <<
"] location=[" << location.
displayString() <<
"]" ) ;
224 m_callback.onResolved( error , location ) ;
229 return m_imp.get() != nullptr ;
234 if( G::threading::works() )
240 G_DEBUG(
"GNet::Resolver::async: not multi-threaded: using synchronous domain name lookup");
247 GNet::Resolver::Callback::~Callback()
void start(const Location &)
Starts asynchronous name-to-address resolution.
A callback interface for GNet::FutureEvent.
An object that hooks into the event loop and calls back to the client code.
A class for synchronous or asynchronous network name to address resolution.
std::string displayString() const
Returns a string representation for logging and debug.
An interface used for GNet::Resolver callbacks.
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events...
A class that holds a host/service name pair and the preferred address family (if any), and also the results of a name-to-address lookup, ie.
bool dgram() const
Returns true if the name resolution should be specifically for datagram sockets.
virtual bool running() const =0
Returns true if called from within run().
void update(const Address &address, const std::string &canonical_name)
Updates the address and canonical name, typically after doing a name lookup on host() and service()...
std::string service() const
Returns the remote service name, as passed in to the constructor.
Pair get()
Returns the resolved address/name pair.
static bool async()
Returns true if the resolver supports asynchronous operation.
static bool send(handle_type handle) g__noexcept
Pokes an event into the main event loop so that the FutureEventHandler callback is called asynchronou...
void run()
Does the name resolution.
bool busy() const
Returns true if there is a pending resolve request.
static std::string resolve(Location &)
Does synchronous name resolution.
Resolver(Callback &, ExceptionHandler &)
Constructor taking a callback interface reference.
int family() const
Returns the preferred name resolution address family, or AF_UNSPEC.
A 'future' object for asynchronous name resolution, in practice holding just enough state for a singl...
A timer class template in which the timeout is delivered to the specified method. ...
A private "pimple" implementation class used by GNet::Resolver to do asynchronous name resolution...
std::string host() const
Returns the remote host name, as passed in to the constructor.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.