E-MailRelay
gheapclient.cpp
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 // gheapclient.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gheapclient.h"
23 #include "gdebug.h"
24 
25 GNet::HeapClient::HeapClient( const Location & remote_info ,
26  bool bind_local_address , const Address & local_address , bool sync_dns ,
27  unsigned int secure_connection_timeout ) :
28  SimpleClient(*this,remote_info,bind_local_address,local_address,sync_dns,secure_connection_timeout) ,
29  m_connect_timer(*this,&HeapClient::onConnectionTimeout,*this) ,
30  m_delete_timer(*this,&HeapClient::onDeletionTimeout,*this) ,
31  m_finished(false)
32 {
33  m_connect_timer.startTimer( 0U ) ;
34 }
35 
37 {
38 }
39 
40 void GNet::HeapClient::onConnectionTimeout()
41 {
42  onConnecting() ;
43  connect() ; // base class
44 }
45 
46 void GNet::HeapClient::onDeletionTimeout()
47 {
48  try
49  {
50  doDeleteThis() ;
51  }
52  catch( std::exception & e ) // never gets here
53  {
54  G_ERROR( "HeapClientTimer::onTimeout: exception: " << e.what() ) ;
55  }
56 }
57 
58 void GNet::HeapClient::doDelete( const std::string & reason )
59 {
60  m_connect_timer.cancelTimer() ;
61  m_delete_timer.startTimer( 0U ) ; // before the callbacks, in case they throw
62  onDeleteImp( reason ) ; // first -- for 'internal' library classes (see GNet::Client)
63  onDelete( reason ) ; // second -- for 'external' client classes (eg. GSmtp::Client)
64 }
65 
66 void GNet::HeapClient::doDeleteThis()
67 {
68  delete this ;
69 }
70 
71 void GNet::HeapClient::onException( std::exception & e )
72 {
73  std::string reason = e.what() ;
74  G_DEBUG( "GNet::HeapClient::onException: reason=[" << reason << "] finished=" << (m_finished?1:0) ) ;
75  if( m_finished ) reason.clear() ;
76  doDelete( reason ) ;
77 }
78 
79 void GNet::HeapClient::onDeleteImp( const std::string & )
80 {
81 }
82 
84 {
85 }
86 
88 {
89  m_finished = true ;
90 }
91 
92 /// \file gheapclient.cpp
virtual ~HeapClient()
Destructor.
Definition: gheapclient.cpp:36
void finish()
Indicates that the last data has been sent and the client is expecting a peer disconnect.
Definition: gheapclient.cpp:87
A class for making an outgoing connection to a remote server, with support for socket-level protocols...
Definition: gsimpleclient.h:61
virtual void onDeleteImp(const std::string &reason)
An alternative to onDelete() for derived classes in the GNet namespace (in practice GNet::Client)...
Definition: gheapclient.cpp:79
HeapClient(const Location &remote_info, bool bind_local_address=false, const Address &local_address=Address::defaultAddress(), bool sync_dns=synchronousDnsDefault(), unsigned int secure_connection_timeout=0U)
Constructor.
Definition: gheapclient.cpp:25
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:54
virtual void onException(std::exception &) g__final override
Override from GNet::ExceptionHandler.
Definition: gheapclient.cpp:71
void doDelete(const std::string &reason)
Calls onDelete() and then does a delayed "delete this".
Definition: gheapclient.cpp:58
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:50
A SimpleClient class for client objects that manage their own lifetime on the heap.
Definition: gheapclient.h:53
virtual void onConnecting()
Called just before the connection is initiated.
Definition: gheapclient.cpp:83