VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gresolver.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file gresolver.h
19 ///
20 
21 #ifndef G_RESOLVER_H
22 #define G_RESOLVER_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "glocation.h"
27 #include "geventhandler.h"
28 #include "gexception.h"
29 #include "gaddress.h"
30 #include <vector>
31 
32 namespace GNet
33 {
34  class Resolver ;
35  class ResolverImp ;
36 }
37 
38 /// \class GNet::Resolver
39 /// A class for synchronous or asynchronous network name to address resolution.
40 /// The implementation uses getaddrinfo() at its core. Consider using
41 /// GNet::ResolverService rather than this class.
42 ///
44 {
45 public:
46  typedef std::vector<Address> AddressList ;
47  G_EXCEPTION( Error , "asynchronous resolver error" ) ;
48  G_EXCEPTION( BusyError , "asynchronous resolver still busy" ) ;
49  struct Callback : public EventExceptionHandler /// An interface used for GNet::Resolver callbacks.
50  {
51  virtual ~Callback() ;
52  virtual void onResolved( std::string error , Location ) = 0 ;
53  ///< Called on completion of GNet::Resolver name resolution.
54  } ;
55 
56  explicit Resolver( Callback & ) ;
57  ///< Constructor taking a callback interface reference.
58  ///< The callback's onException() method is called if an exception is
59  ///< thrown out of Callback::onResolved() -- see GNet::HeapClient.
60 
61  ~Resolver() ;
62  ///< Destructor.
63 
64  void start( const Location & ) ;
65  ///< Starts asynchronous name-to-address resolution.
66  ///< Precondition: async() && !busy()
67 
68  static std::string resolve( Location & ) ;
69  ///< Does syncronous name resolution. Fills in the
70  ///< name and address fields of the supplied Location
71  ///< structure. The returned error string is zero length
72  ///< on success.
73 
74  static AddressList resolve( const std::string & host , const std::string & service , int family = AF_UNSPEC , bool dgram = false ) ;
75  ///< Does synchronous name resolution returning a list
76  ///< of addresses. Errors are not reported. The empty
77  ///< list is returned on error.
78 
79  static bool async() ;
80  ///< Returns true if the resolver supports asynchronous operation.
81  ///< If it doesnt then start() will always throw.
82 
83  bool busy() const ;
84  ///< Returns true if there is a pending resolve request.
85 
86 private:
87  void operator=( const Resolver & ) ; // not implemented
88  Resolver( const Resolver & ) ; // not implemented
89  friend class GNet::ResolverImp ;
90  void done( const std::string & , const Location & ) ;
91 
92 private:
93  Callback & m_callback ;
94  bool m_busy ;
95  unique_ptr<ResolverImp> m_imp ;
96 } ;
97 
98 #endif
~Resolver()
Destructor.
Definition: gresolver.cpp:372
void start(const Location &)
Starts asynchronous name-to-address resolution.
Definition: gresolver.cpp:418
An abstract interface for handling exceptions thrown out of event-loop callbacks (socket events and t...
Definition: geventhandler.h:44
A class for synchronous or asynchronous network name to address resolution.
Definition: gresolver.h:43
An interface used for GNet::Resolver callbacks.
Definition: gresolver.h:49
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.
Definition: glocation.h:51
Resolver(Callback &)
Constructor taking a callback interface reference.
Definition: gresolver.cpp:365
virtual void onResolved(std::string error, Location)=0
Called on completion of GNet::Resolver name resolution.
static bool async()
Returns true if the resolver supports asynchronous operation.
Definition: gresolver.cpp:441
bool busy() const
Returns true if there is a pending resolve request.
Definition: gresolver.cpp:436
static std::string resolve(Location &)
Does syncronous name resolution.
Definition: gresolver.cpp:385
A private "pimple" implementation class used by GNet::Resolver to do asynchronous name resolution...
Definition: gresolver.cpp:251