GNet::SimpleClient Class Reference

A class for making an outgoing connection to a remote server. More...

#include <gsimpleclient.h>

Inheritance diagram for GNet::SimpleClient:

GNet::EventHandler GNet::Connection GNet::SocketProtocolSink GNet::HeapClient GNet::ResolverImp GNet::Client GSmtp::Client GSmtp::RequestClient GSmtp::SpamClient

List of all members.

Public Types

enum  ConnectStatus { Success, Failure, Retry, ImmediateSuccess }
enum  State { Idle, Resolving, Connecting, Connected }
typedef std::string::size_type size_type

Public Member Functions

 SimpleClient (const ResolverInfo &remote_info, const Address &local_interface=Address(0U), bool privileged=false, bool sync_dns=synchronousDnsDefault())
 Constructor.
void connect ()
 Initates a connection to the remote server.
bool connected () const
 Returns true if connected to the peer.
virtual std::pair< bool, AddresslocalAddress () const
 Override from Connection.
virtual std::pair< bool, AddresspeerAddress () const
 Override from Connection.
ResolverInfo resolverInfo () const
 Returns a ResolverInfo structure containing the result of host() and service() name lookup if available.
void updateResolverInfo (const ResolverInfo &)
 Updates the constructor's ResolverInfo object with the given one as long as both objects have the same host and service name.
virtual void readEvent ()
 Final override from GNet::EventHandler.
virtual void writeEvent ()
 Final override from GNet::EventHandler.
bool send (const std::string &data, std::string::size_type offset=0)
 Returns true if all sent, or false if flow control was asserted.

Static Public Member Functions

static bool synchronousDnsDefault ()
 Returns true if DNS queries should normally be synchronous on this platform.

Protected Member Functions

virtual ~SimpleClient ()
 Destructor.
StreamSocketsocket ()
 Returns a reference to the socket. Throws if not connected.
const StreamSocketsocket () const
 Returns a const reference to the socket. Throws if not connected.
virtual void onConnect ()=0
 Called once connected.
virtual void onConnectImp ()
 An alternative to onConnect() for private implementation classes.
virtual void onSendComplete ()=0
 Called when all residual data from send() has been sent.
virtual void onSendImp ()
 Called from within send().
void sslConnect ()
 Starts TLS/SSL client-side negotiation.
std::string logId () const
 Returns a identification string for logging purposes.

Static Protected Member Functions

static bool canRetry (const std::string &reason)
 Parses the given failure reason and returns true if the client can reasonably retry at some later time.

Friends

class ClientResolver


Detailed Description

A class for making an outgoing connection to a remote server.

The class handles name-to-address resolution and connection issues, it reads incoming data and it manages flow-control when sending.

Name-to-address lookup is performed if the supplied ResolverInfo object does not contain an address. This can be done synchronously or asynchronously. The results of the lookup can be obtained via the resolverInfo() method and possibly fed back to the next SimpleClient that connects to the same host/service in order to implement name lookup cacheing (see GNet::ClientPtr). However, most operating systems implement their own name lookup cacheing, so this is not terribly useful in practice.

Definition at line 78 of file gsimpleclient.h.


Member Typedef Documentation

typedef std::string::size_type GNet::SimpleClient::size_type

Definition at line 83 of file gsimpleclient.h.


Member Enumeration Documentation

Enumerator:
Success 
Failure 
Retry 
ImmediateSuccess 

Definition at line 81 of file gsimpleclient.h.

Enumerator:
Idle 
Resolving 
Connecting 
Connected 

Definition at line 82 of file gsimpleclient.h.


Constructor & Destructor Documentation

GNet::SimpleClient::SimpleClient ( const ResolverInfo remote_info,
const Address local_interface = Address(0U),
bool  privileged = false,
bool  sync_dns = synchronousDnsDefault() 
)

Constructor.

The socket is bound with the given local address, but with an arbitrary port number -- the supplied port number is ignored. The local address defaults to the INADDR_ANY address.

If the 'privileged' parameter is true then the local socket is bound with a privileged port number (ie. < 1024), selected at random.

Definition at line 46 of file gsimpleclient.cpp.

References GNet::Monitor::add(), G_DEBUG, and GNet::Monitor::instance().

GNet::SimpleClient::~SimpleClient (  )  [protected, virtual]

Destructor.

Definition at line 58 of file gsimpleclient.cpp.

References GNet::Monitor::instance(), and GNet::Monitor::remove().


Member Function Documentation

void GNet::SimpleClient::connect (  ) 

Initates a connection to the remote server.

This default implementation throws on error, and may call onConnect() before returning.

Definition at line 100 of file gsimpleclient.cpp.

References ClientResolver, Connecting, G_DEBUG, G_WARNING, GNet::ResolverInfo::hasAddress(), Idle, GNet::Resolver::resolve(), Resolving, and GNet::ResolverInfo::str().

Referenced by GNet::ResolverImp::resolveReq().

bool GNet::SimpleClient::connected (  )  const

Returns true if connected to the peer.

Definition at line 184 of file gsimpleclient.cpp.

References Connected.

Referenced by GNet::ResolverImp::resolveReq(), GSmtp::Client::sendMessage(), and GSmtp::Client::sendMessages().

std::pair< bool, GNet::Address > GNet::SimpleClient::localAddress (  )  const [virtual]

Override from Connection.

Returns the local address. Pair.first is false on error. Final override from GNet::Connection.

Implements GNet::Connection.

Definition at line 329 of file gsimpleclient.cpp.

References GNet::Socket::getLocalAddress(), GNet::Address::invalidAddress(), and socket().

std::pair< bool, GNet::Address > GNet::SimpleClient::peerAddress (  )  const [virtual]

Override from Connection.

Returns the peer address. Pair.first is false on error. Final override from GNet::Connection.

Implements GNet::Connection.

Definition at line 337 of file gsimpleclient.cpp.

References GNet::Socket::getPeerAddress(), GNet::Address::invalidAddress(), and socket().

Referenced by GSmtp::Client::onSecure().

GNet::ResolverInfo GNet::SimpleClient::resolverInfo (  )  const

Returns a ResolverInfo structure containing the result of host() and service() name lookup if available.

Definition at line 72 of file gsimpleclient.cpp.

Referenced by GNet::Client::onConnectImp(), and GNet::Client::onConnecting().

bool GNet::SimpleClient::synchronousDnsDefault (  )  [static]

Returns true if DNS queries should normally be synchronous on this platform.

Used to default the relevant constructor parameter.

Definition at line 26 of file gsimpleclient_unix.cpp.

References G::Test::enabled().

void GNet::SimpleClient::updateResolverInfo ( const ResolverInfo update  ) 

Updates the constructor's ResolverInfo object with the given one as long as both objects have the same host and service name.

This is only useful immediately after construction and before re-entering the event loop.

Definition at line 77 of file gsimpleclient.cpp.

References GNet::ResolverInfo::displayString(), G_DEBUG, GNet::ResolverInfo::hasAddress(), GNet::ResolverInfo::host(), and GNet::ResolverInfo::service().

void GNet::SimpleClient::readEvent (  )  [virtual]

Final override from GNet::EventHandler.

Reimplemented from GNet::EventHandler.

Definition at line 317 of file gsimpleclient.cpp.

References G_ASSERT.

void GNet::SimpleClient::writeEvent (  )  [virtual]

bool GNet::SimpleClient::send ( const std::string &  data,
std::string::size_type  offset = 0 
)

Returns true if all sent, or false if flow control was asserted.

Throws on error.

Definition at line 356 of file gsimpleclient.cpp.

References onSendImp().

Referenced by GNet::ResolverImp::onConnect(), GSmtp::RequestClient::onConnect(), and GNet::ResolverImp::resolveReq().

GNet::StreamSocket & GNet::SimpleClient::socket (  )  [protected]

Returns a reference to the socket. Throws if not connected.

Definition at line 86 of file gsimpleclient.cpp.

Referenced by localAddress(), peerAddress(), and writeEvent().

const GNet::StreamSocket & GNet::SimpleClient::socket (  )  const [protected]

Returns a const reference to the socket. Throws if not connected.

Definition at line 93 of file gsimpleclient.cpp.

virtual void GNet::SimpleClient::onConnect (  )  [protected, pure virtual]

Called once connected.

May (unfortunately) be called from within connect().

Implemented in GNet::ResolverImp, GSmtp::RequestClient, GSmtp::Client, and GSmtp::SpamClient.

Referenced by writeEvent().

void GNet::SimpleClient::onConnectImp (  )  [protected, virtual]

An alternative to onConnect() for private implementation classes.

The default implementation does nothing.

Reimplemented in GNet::Client.

Definition at line 352 of file gsimpleclient.cpp.

Referenced by writeEvent().

virtual void GNet::SimpleClient::onSendComplete (  )  [protected, pure virtual]

Called when all residual data from send() has been sent.

Implemented in GNet::ResolverImp, GSmtp::RequestClient, GSmtp::Client, and GSmtp::SpamClient.

Referenced by writeEvent().

void GNet::SimpleClient::onSendImp (  )  [protected, virtual]

Called from within send().

Reimplemented in GNet::Client.

Definition at line 363 of file gsimpleclient.cpp.

Referenced by send().

void GNet::SimpleClient::sslConnect (  )  [protected]

Starts TLS/SSL client-side negotiation.

Definition at line 345 of file gsimpleclient.cpp.

bool GNet::SimpleClient::canRetry ( const std::string &  reason  )  [static, protected]

Parses the given failure reason and returns true if the client can reasonably retry at some later time.

(Not used?)

Definition at line 158 of file gsimpleclient.cpp.

Referenced by GNet::HeapClient::doDelete().

std::string GNet::SimpleClient::logId (  )  const [protected]

Returns a identification string for logging purposes.

Not guaranteed to stay the same during the lifetime of the object.

Definition at line 64 of file gsimpleclient.cpp.

References GNet::ResolverInfo::displayString(), and s.


Friends And Related Function Documentation

friend class ClientResolver [friend]

Definition at line 185 of file gsimpleclient.h.

Referenced by connect().


The documentation for this class was generated from the following files:

Generated on Fri Apr 18 15:56:13 2008 for E-MailRelay by  doxygen 1.5.5