gsimpleclient.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 // ===
20 
21 #ifndef G_SIMPLE_CLIENT_H
22 #define G_SIMPLE_CLIENT_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gaddress.h"
27 #include "gmemory.h"
28 #include "gconnection.h"
29 #include "gexception.h"
30 #include "geventhandler.h"
31 #include "gresolver.h"
32 #include "gresolverinfo.h"
33 #include "gsocket.h"
34 #include "gsocketprotocol.h"
35 #include "gevent.h"
36 #include <string>
37 
39 namespace GNet
40 {
41  class SimpleClient ;
42  class ClientResolver ;
43 }
44 
49 {
50 private:
51  SimpleClient & m_client ;
52 
53 public:
54  explicit ClientResolver( SimpleClient & ) ;
56 
57  void resolveCon( bool success , const Address &address , std::string reason ) ;
59 
60 private:
61  ClientResolver( const ClientResolver & ) ;
62  void operator=( const ClientResolver & ) ;
63 } ;
64 
82 {
83 public:
86  G_EXCEPTION( DnsError , "dns error" ) ;
87  G_EXCEPTION( ConnectError , "connect failure" ) ;
88  G_EXCEPTION( SocksError , "socks error" ) ;
89  G_EXCEPTION( NotConnected , "socket not connected" ) ;
91 
92  SimpleClient( const ResolverInfo & remote_info ,
93  const Address & local_address = Address(0U) ,
94  bool privileged = false ,
95  bool sync_dns = synchronousDnsDefault() ,
96  unsigned int secure_connection_timeout = 0U ) ;
108 
109  void connect() ;
117 
118  bool connected() const ;
120 
121  virtual std::pair<bool,Address> localAddress() const ;
125 
126  virtual std::pair<bool,Address> peerAddress() const ;
130 
131  virtual std::string peerCertificate() const ;
134 
135  ResolverInfo resolverInfo() const ;
139 
140  static bool synchronousDnsDefault() ;
144 
145  void updateResolverInfo( const ResolverInfo & ) ;
151 
152  virtual void readEvent() ;
154 
155  virtual void writeEvent() ;
157 
158  bool send( const std::string & data , std::string::size_type offset = 0 ) ;
161 
162 protected:
163  virtual ~SimpleClient() ;
165 
166  StreamSocket & socket() ;
168 
169  const StreamSocket & socket() const ;
171 
172  virtual void onConnect() = 0 ;
175 
176  virtual void onConnectImp() ;
179 
180  virtual void onSendComplete() = 0 ;
182 
183  virtual void onSendImp() ;
185 
186  void sslConnect() ;
188 
189  static bool canRetry( const std::string & reason ) ;
193 
194  std::string logId() const ;
198 
199 private:
200  friend class ClientResolver ;
201  void resolveCon( bool , const Address & , std::string ) ;
202 
203 private:
204  SimpleClient( const SimpleClient& ) ; // not implemented
205  void operator=( const SimpleClient& ) ; // not implemented
206  void close() ;
207  static unsigned int getRandomPort() ;
208  bool startConnecting() ;
209  bool localBind( Address ) ;
210  ConnectStatus connectCore( Address remote_address , std::string *error_p ) ;
211  void setState( State ) ;
212  void immediateConnection() ;
213  void sendSocksRequest() ;
214  bool readSocksResponse() ;
215  void logFlowControlAsserted() const ;
216  void logFlowControlReleased() const ;
217 
218 private:
219  std::auto_ptr<ClientResolver> m_resolver ;
220  std::auto_ptr<StreamSocket> m_s ;
221  std::auto_ptr<SocketProtocol> m_sp ;
222  ResolverInfo m_remote ;
223  Address m_local_address ;
224  bool m_privileged ;
225  State m_state ;
226  bool m_sync_dns ;
227  unsigned int m_secure_connection_timeout ;
228 } ;
229 
230 #endif
virtual void onConnect()=0
Called once connected.
std::string::size_type size_type
Definition: gsimpleclient.h:86
A class for making an outgoing connection to a remote server, with support for socket-level protocols...
Definition: gsimpleclient.h:81
bool send(const std::string &data, std::string::size_type offset=0)
Returns true if all sent, or false if flow control was asserted.
Network classes.
void sslConnect()
Starts TLS/SSL client-side negotiation.
A resolver class which calls SimpleClient::resolveCon() when done.
Definition: gsimpleclient.h:48
virtual std::pair< bool, Address > peerAddress() const
Override from Connection.
virtual std::string peerCertificate() const
Returns the peer's TLS certificate.
A class for asynchronous TCP name-to-address resolution.
Definition: gresolver.h:45
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
A class that holds a host/service name pair and optionally the results of a name-to-address lookup...
Definition: gresolverinfo.h:48
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
virtual void writeEvent()
Final override from GNet::EventHandler.
bool connected() const
Returns true if connected to the peer.
virtual void onConnectImp()
An alternative to onConnect() for private implementation classes.
ClientResolver(SimpleClient &)
Constructor.
SimpleClient(const ResolverInfo &remote_info, const Address &local_address=Address(0U), bool privileged=false, bool sync_dns=synchronousDnsDefault(), unsigned int secure_connection_timeout=0U)
Constructor.
void updateResolverInfo(const ResolverInfo &)
Updates the constructor's ResolverInfo object with the given one as long as both objects have the sam...
static bool canRetry(const std::string &reason)
Parses the given failure reason and returns true if the client can reasonably retry at some later tim...
virtual void onSendComplete()=0
Called when all residual data from send() has been sent.
void connect()
Initates a connection to the remote server.
A derivation of Socket for a stream socket.
Definition: gsocket.h:270
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
std::string logId() const
Returns a identification string for logging purposes.
void resolveCon(bool success, const Address &address, std::string reason)
From Resolver.
static bool synchronousDnsDefault()
Returns true if DNS queries should normally be synchronous on this platform.
virtual void onSendImp()
Called from within send().
An interface which provides address information for a network connection.
Definition: gconnection.h:39
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
virtual ~SimpleClient()
Destructor.
ResolverInfo resolverInfo() const
Returns a ResolverInfo structure containing the result of host() and service() name lookup if availab...
virtual std::pair< bool, Address > localAddress() const
Override from Connection.
virtual void readEvent()
Final override from GNet::EventHandler.
An interface used by GNet::SocketProtocol to deliver data from a socket.
StreamSocket & socket()
Returns a reference to the socket. Throws if not connected.