VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gheapclient.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
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 "gnet.h"
23 #include "gheapclient.h"
24 #include "gdebug.h"
25 
26 GNet::HeapClient::HeapClient( const Location & remote_info ,
27  bool bind_local_address , const Address & local_address , bool sync_dns ,
28  unsigned int secure_connection_timeout ) :
29  SimpleClient(remote_info,bind_local_address,local_address,sync_dns,secure_connection_timeout) ,
30  m_connect_timer(*this,&HeapClient::onConnectionTimeout,*static_cast<EventHandler*>(this)) ,
31  m_delete_timer(*this,&HeapClient::onDeletionTimeout,*static_cast<EventHandler*>(this))
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 
59 {
60  delete this ;
61 }
62 
63 void GNet::HeapClient::doDelete( const std::string & reason )
64 {
65  m_connect_timer.cancelTimer() ;
66  m_delete_timer.startTimer( 0U ) ; // before the callbacks, in case they throw
67  onDeleteImp( reason ) ; // first -- for 'internal' library classes
68  onDelete( reason ) ; // second -- for 'external' client classes
69 }
70 
71 void GNet::HeapClient::doDeleteThis()
72 {
73  delete this ;
74 }
75 
76 void GNet::HeapClient::onException( std::exception & e )
77 {
78  G_DEBUG( "GNet::HeapClient::onException: exception: " << e.what() ) ;
79  doDelete( e.what() ) ;
80 }
81 
82 void GNet::HeapClient::onDeleteImp( const std::string & )
83 {
84 }
85 
87 {
88 }
89 
90 /// \file gheapclient.cpp
virtual ~HeapClient()
Destructor.
Definition: gheapclient.cpp:36
A class for making an outgoing connection to a remote server, with support for socket-level protocols...
Definition: gsimpleclient.h:60
virtual void onDeleteImp(const std::string &reason)
An alternative to onDelete() for private implementation classes.
Definition: gheapclient.cpp:82
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:26
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:55
virtual void onException(std::exception &) g__final override
Override from GNet::EventExceptionHandler.
Definition: gheapclient.cpp:76
void doDelete(const std::string &reason)
Calls onDelete() and then does a delayed "delete this".
Definition: gheapclient.cpp:63
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:51
A SimpleClient class for client objects that manage their own lifetime on the heap.
Definition: gheapclient.h:54
void doDeleteForExit()
A destructor method that may be called at program termination when the normal doDelete() mechanism ha...
Definition: gheapclient.cpp:58
A base class for classes that handle asynchronous events from the event loop.
Definition: geventhandler.h:78
virtual void onConnecting()
Called just before the connection is initiated.
Definition: gheapclient.cpp:86