- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
48 class GNet::ClientResolver : public GNet::Resolver
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
81 class GNet::SimpleClient : public GNet::EventHandler , public GNet::Connection , public GNet::SocketProtocolSink
82 {
83 public:
84 enum ConnectStatus { Success , Failure , Retry , ImmediateSuccess } ;
85 enum State { Idle , Resolving , Connecting , Connected , Socksing } ;
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" ) ;
90 typedef std::string::size_type size_type ;
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
GNet::SimpleClient::onConnect
virtual void onConnect()=0
Called once connected.
GNet::SimpleClient::ImmediateSuccess
Definition: gsimpleclient.h:84
GNet::SimpleClient::Success
Definition: gsimpleclient.h:84
GNet::SimpleClient::Connecting
Definition: gsimpleclient.h:85
GNet::SimpleClient::size_type
std::string::size_type size_type
Definition: gsimpleclient.h:86
GNet::SimpleClient
A class for making an outgoing connection to a remote server, with support for socket-level protocols...
Definition: gsimpleclient.h:81
GNet::SimpleClient::send
bool send(const std::string &data, std::string::size_type offset=0)
Returns true if all sent, or false if flow control was asserted.
Definition: gsimpleclient.cpp:404
GNet
Network classes.
GNet::SimpleClient::sslConnect
void sslConnect()
Starts TLS/SSL client-side negotiation.
Definition: gsimpleclient.cpp:393
GNet::ClientResolver
A resolver class which calls SimpleClient::resolveCon() when done.
Definition: gsimpleclient.h:48
gresolverinfo.h
GNet::SimpleClient::peerAddress
virtual std::pair< bool, Address > peerAddress() const
Override from Connection.
Definition: gsimpleclient.cpp:380
GNet::SimpleClient::peerCertificate
virtual std::string peerCertificate() const
Returns the peer's TLS certificate.
Definition: gsimpleclient.cpp:388
GNet::SimpleClient::ConnectStatus
ConnectStatus
Definition: gsimpleclient.h:84
gsocketprotocol.h
GNet::Resolver
A class for asynchronous TCP name-to-address resolution.
Definition: gresolver.h:45
GNet::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
GNet::ResolverInfo
A class that holds a host/service name pair and optionally the results of a name-to-address lookup...
Definition: gresolverinfo.h:48
md5::size_type
std::string::size_type size_type
A std::size_t type.
Definition: md5.h:43
GNet::SimpleClient::writeEvent
virtual void writeEvent()
Final override from GNet::EventHandler.
Definition: gsimpleclient.cpp:314
GNet::SimpleClient::connected
bool connected() const
Returns true if connected to the peer.
Definition: gsimpleclient.cpp:188
GNet::SimpleClient::Resolving
Definition: gsimpleclient.h:85
GNet::SimpleClient::onConnectImp
virtual void onConnectImp()
An alternative to onConnect() for private implementation classes.
Definition: gsimpleclient.cpp:400
GNet::ClientResolver::ClientResolver
ClientResolver(SimpleClient &)
Constructor.
Definition: gsimpleclient.cpp:457
GNet::SimpleClient::SimpleClient
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.
Definition: gsimpleclient.cpp:47
GNet::SimpleClient::updateResolverInfo
void updateResolverInfo(const ResolverInfo &)
Updates the constructor's ResolverInfo object with the given one as long as both objects have the sam...
Definition: gsimpleclient.cpp:80
GNet::SimpleClient::Socksing
Definition: gsimpleclient.h:85
GNet::SimpleClient::canRetry
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...
Definition: gsimpleclient.cpp:161
GNet::SimpleClient::onSendComplete
virtual void onSendComplete()=0
Called when all residual data from send() has been sent.
GNet::SimpleClient::connect
void connect()
Initates a connection to the remote server.
Definition: gsimpleclient.cpp:103
GNet::StreamSocket
A derivation of Socket for a stream socket.
Definition: gsocket.h:270
geventhandler.h
gevent.h
GNet::EventHandler
A base class for classes that handle asynchronous socket events.
Definition: geventhandler.h:54
GNet::SimpleClient::logId
std::string logId() const
Returns a identification string for logging purposes.
Definition: gsimpleclient.cpp:67
gsocket.h
GNet::SimpleClient::Connected
Definition: gsimpleclient.h:85
GNet::ClientResolver::resolveCon
void resolveCon(bool success, const Address &address, std::string reason)
From Resolver.
Definition: gsimpleclient.cpp:463
GNet::SimpleClient::synchronousDnsDefault
static bool synchronousDnsDefault()
Returns true if DNS queries should normally be synchronous on this platform.
Definition: gsimpleclient_unix.cpp:26
GNet::SimpleClient::onSendImp
virtual void onSendImp()
Called from within send().
Definition: gsimpleclient.cpp:411
gexception.h
GNet::Connection
An interface which provides address information for a network connection.
Definition: gconnection.h:39
G_EXCEPTION
#define G_EXCEPTION(class_name, description)
define as a function rather than a type if optimising for size
Definition: gexception.h:93
GNet::SimpleClient::~SimpleClient
virtual ~SimpleClient()
Destructor.
Definition: gsimpleclient.cpp:61
GNet::SimpleClient::resolverInfo
ResolverInfo resolverInfo() const
Returns a ResolverInfo structure containing the result of host() and service() name lookup if availab...
Definition: gsimpleclient.cpp:75
GNet::SimpleClient::Retry
Definition: gsimpleclient.h:84
gaddress.h
GNet::SimpleClient::localAddress
virtual std::pair< bool, Address > localAddress() const
Override from Connection.
Definition: gsimpleclient.cpp:372
GNet::SimpleClient::readEvent
virtual void readEvent()
Final override from GNet::EventHandler.
Definition: gsimpleclient.cpp:347
GNet::SimpleClient::Failure
Definition: gsimpleclient.h:84
GNet::SocketProtocolSink
An interface used by GNet::SocketProtocol to deliver data from a socket.
Definition: gsocketprotocol.h:112
gmemory.h
GNet::SimpleClient::State
State
Definition: gsimpleclient.h:85
GNet::SimpleClient::socket
StreamSocket & socket()
Returns a reference to the socket. Throws if not connected.
Definition: gsimpleclient.cpp:89
gconnection.h
GNet::SimpleClient::Idle
Definition: gsimpleclient.h:85
gresolver.h