VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gheapclient.h
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 /// \file gheapclient.h
19 ///
20 
21 #ifndef G_HEAP_CLIENT_H
22 #define G_HEAP_CLIENT_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gsimpleclient.h"
27 #include "gtimer.h"
28 
29 namespace GNet
30 {
31  class HeapClient ;
32 }
33 
34 /// \class GNet::HeapClient
35 /// A SimpleClient class for client objects that manage their own lifetime on
36 /// the heap.
37 ///
38 /// HeapClients are instantiated on the heap and should be deleted by calling
39 /// their doDelete() method. The doDelete() implementation starts a zero-length
40 /// timer which does "delete this" when it expires, so it is safe to call
41 /// doDelete() from within event callbacks.
42 ///
43 /// This class automatically starts connecting after construction using a
44 /// zero-length timer, so there is no need to call the base class's connect()
45 /// method.
46 ///
47 /// When the event loop delivers an event callback to a HeapClient and the
48 /// HeapClient throws a std::exception back up to the event loop the event loop
49 /// calls the HeapClient again via onException(). The implementation of
50 /// onException() in HeapClient causes the HeapClient to self-destruct.
51 /// As a result, the client code can just throw an exception to terminate
52 /// the connection and delete itself.
53 ///
55 {
56 public:
57  explicit HeapClient( const Location & remote_info ,
58  bool bind_local_address = false ,
59  const Address & local_address = Address::defaultAddress() ,
60  bool sync_dns = synchronousDnsDefault() ,
61  unsigned int secure_connection_timeout = 0U ) ;
62  ///< Constructor. All instances must be on the heap.
63  ///< Initiates the connection via a zero-length timer.
64 
65  void doDelete( const std::string & reason ) ;
66  ///< Calls onDelete() and then does a delayed "delete this".
67 
68  void doDeleteForExit() ;
69  ///< A destructor method that may be called at program
70  ///< termination when the normal doDelete() mechanism has
71  ///< become unusable.
72 
73 protected:
74  virtual ~HeapClient() ;
75  ///< Destructor.
76 
77  virtual void onDelete( const std::string & reason ) = 0 ;
78  ///< Called just before deletion.
79 
80  virtual void onDeleteImp( const std::string & reason ) ;
81  ///< An alternative to onDelete() for private implementation
82  ///< classes. Gets called before onDelete(). The default
83  ///< implementation does nothing.
84 
85  virtual void onConnecting() ;
86  ///< Called just before the connection is initiated.
87  ///< Overridable. The default implementation does nothing.
88 
89  virtual void onException( std::exception & ) g__final override ;
90  ///< Override from GNet::EventExceptionHandler.
91  ///< Calls doDelete(). Note that the exception
92  ///< text is available via onDelete().
93 
94 private:
95  HeapClient( const HeapClient & ) ; // not implemented
96  void operator=( const HeapClient & ) ; // not implemented
97  void onConnectionTimeout() ;
98  void onDeletionTimeout() ;
99  void doDeleteThis() ;
100 
101 private:
102  GNet::Timer<HeapClient> m_connect_timer ;
103  GNet::Timer<HeapClient> m_delete_timer ;
104 } ;
105 
106 #endif
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
virtual void onDelete(const std::string &reason)=0
Called just before deletion.
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
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
static bool synchronousDnsDefault()
Returns true if DNS queries should normally be synchronous on this platform.
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:110
virtual void onConnecting()
Called just before the connection is initiated.
Definition: gheapclient.cpp:86