E-MailRelay
gresolverfuture.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
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 gresolverfuture.h
19 ///
20 
21 #ifndef G_NET_RESOLVER_FUTURE__H
22 #define G_NET_RESOLVER_FUTURE__H
23 
24 #include "gdef.h"
25 #include "gaddress.h"
26 #include <utility>
27 #include <vector>
28 #include <string>
29 
30 namespace GNet
31 {
32  class ResolverFuture ;
33 }
34 
35 /// \class GNet::ResolverFuture
36 /// A 'future' object for asynchronous name resolution, in practice holding
37 /// just enough state for a single call to getaddrinfo().
38 ///
39 /// The run() method can be called from a worker thread and the results
40 /// collected by the main thread with get() once the run() has finished.
41 ///
42 /// Signalling completion from worker thread to main thread is out of scope
43 /// for this class; see GNet::FutureEvent.
44 ///
45 /// Eg:
46 /// \code
47 ///
48 /// ResolverFuture f( "example.com" , "smtp" , AF_INET , false ) ;
49 /// std::thread t( &ResolverFuture::run , f ) ;
50 /// ...
51 /// t.join() ;
52 /// Address a = f.get().first ;
53 /// if( f.error() ) throw std::runtime_error( f.reason() ) ;
54 /// \endcode
55 ///
57 {
58 public:
59  typedef std::pair<Address,std::string> Pair ;
60  typedef std::vector<Address> List ;
61 
62  ResolverFuture( const std::string & host , const std::string & service ,
63  int family , bool dgram , bool for_async_hint = false ) ;
64  ///< Constructor for resolving the given host and service names.
65 
66  ~ResolverFuture() ;
67  ///< Destructor.
68 
69  void run() ;
70  ///< Does the name resolution.
71 
72  Pair get() ;
73  ///< Returns the resolved address/name pair.
74 
75  void get( List & ) ;
76  ///< Returns the list of resolved addresses.
77 
78  bool error() const ;
79  ///< Returns true if name resolution failed or no suitable
80  ///< address was returned. Use after get().
81 
82  std::string reason() const ;
83  ///< Returns the reason for the error().
84  ///< Precondition: error()
85 
86 private:
87  ResolverFuture( const ResolverFuture & ) ;
88  void operator=( const ResolverFuture & ) ;
89  std::string failure() const ;
90  bool fetch( List & ) const ;
91  bool fetch( Pair & ) const ;
92  bool failed() const ;
93  std::string none() const ;
94  std::string ipvx() const ;
95 
96 private:
97  bool m_numeric_service ;
98  int m_socktype ;
99  std::string m_host ;
100  const char * m_host_p ;
101  std::string m_service ;
102  const char * m_service_p ;
103  int m_family ;
104  struct addrinfo m_ai_hint ;
105  bool m_test_mode ;
106  int m_rc ;
107  struct addrinfo * m_ai ;
108  std::string m_reason ;
109 } ;
110 
111 #endif
ResolverFuture(const std::string &host, const std::string &service, int family, bool dgram, bool for_async_hint=false)
Constructor for resolving the given host and service names.
Network classes.
Definition: gaddress.h:29
std::string reason() const
Returns the reason for the error().
bool error() const
Returns true if name resolution failed or no suitable address was returned.
~ResolverFuture()
Destructor.
void run()
Does the name resolution.
A 'future' object for asynchronous name resolution, in practice holding just enough state for a singl...