VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gclient.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 gclient.h
19 ///
20 
21 #ifndef G_CLIENT_H
22 #define G_CLIENT_H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "gheapclient.h"
27 #include "glinebuffer.h"
28 #include "gtimer.h"
29 #include "gslot.h"
30 
31 namespace GNet
32 {
33  class ClientTimer ;
34  class Client ;
35 }
36 
37 /// \class GNet::Client
38 /// A HeapClient class that adds slot/signal signalling, connection/response
39 /// timeouts, and input line buffering.
40 ///
41 /// The following pure virtual functions must be implemented by derived
42 /// classes: onConnect(), onReceive(), onDelete(), and onSendComplete().
43 ///
44 /// The intention is that derived classes use the virtual functions for
45 /// event notification while the slot/signal event notifications are used
46 /// by the containing object (in particular so that the container is
47 /// informed when the client object deletes itself).
48 ///
49 class GNet::Client : public HeapClient
50 {
51 public:
52  explicit Client( const Location & remote_info , unsigned int connection_timeout = 0U ,
53  unsigned int response_timeout = 0U , unsigned int secure_connection_timeout = 0U ,
54  const std::string & eol = std::string("\n") ,
55  bool bind_local_address = false ,
56  const Address & local_address = Address::defaultAddress() ,
57  bool sync_dns = synchronousDnsDefault() ) ;
58  ///< Constructor.
59 
61  ///< Returns a signal that indicates that client processing
62  ///< is complete.
63  ///<
64  ///< The first signal parameter is a failure reason, or the
65  ///< empty string on success. The second parameter gives some
66  ///< idea of whether the connection can be retried later.
67 
69  ///< Returns a signal that indicates that something interesting
70  ///< has happened.
71  ///<
72  ///< The first signal parameter is one of "connecting",
73  ///< "connected", "sending", "failed" (on error) or "done"
74  ///< (for normal termination). Derived clases may emit
75  ///< other events over this channel.
76 
78  ///< Returns a signal that indicates that the client
79  ///< has successfully connected to the server.
80 
82  ///< Returns a signal that indicates that the security
83  ///< layer has been successfully established.
84 
85 protected:
86  virtual ~Client() ;
87  ///< Destructor.
88 
89  virtual bool onReceive( const std::string & ) = 0 ;
90  ///< Called when a complete line is received from the peer.
91  ///< The implementation should return false if it needs
92  ///< to stop further onReceive() calls being generated
93  ///< from data already received and buffered, if for
94  ///< example the object has just deleted itself.
95 
96  void clearInput() ;
97  ///< Clears any pending input from the server.
98 
99  virtual void onDeleteImp( const std::string & reason ) override ;
100  ///< Override from GNet::HeapClient.
101 
102  virtual void onConnectImp() override ;
103  ///< Override from GNet::SimpleClient.
104 
105  virtual void onData( const char * , SimpleClient::size_type ) override ;
106  ///< Override from GNet::SocketProtocolSink.
107 
108  virtual void onConnecting() override ;
109  ///< Override from GNet::HeapClient.
110 
111  virtual void onSendImp() override ;
112  ///< Override from GNet::SimpleClient.
113 
114 private:
115  Client( const Client& ) ; // not implemented
116  void operator=( const Client& ) ; // not implemented
117  void onConnectionTimeout() ;
118  void onResponseTimeout() ;
119 
120 private:
121  G::Slot::Signal1<std::string> m_done_signal ;
123  G::Slot::Signal0 m_connected_signal ;
124  G::Slot::Signal0 m_secure_signal ;
125  unsigned int m_connection_timeout ;
126  unsigned int m_response_timeout ;
127  GNet::Timer<Client> m_connection_timer ;
128  GNet::Timer<Client> m_response_timer ;
129  GNet::LineBuffer m_line_buffer ;
130 } ;
131 
132 #endif
G::Slot::Signal0 & connectedSignal()
Returns a signal that indicates that the client has successfully connected to the server...
Definition: gclient.cpp:95
virtual void onConnecting() override
Override from GNet::HeapClient.
Definition: gclient.cpp:46
virtual void onDeleteImp(const std::string &reason) override
Override from GNet::HeapClient.
Definition: gclient.cpp:61
virtual void onConnectImp() override
Override from GNet::SimpleClient.
Definition: gclient.cpp:51
The GNet::Address class encapsulates a TCP/UDP transport address.
Definition: gaddress.h:55
G::Slot::Signal2< std::string, std::string > & eventSignal()
Returns a signal that indicates that something interesting has happened.
Definition: gclient.cpp:90
virtual ~Client()
Destructor.
Definition: gclient.cpp:42
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
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
void clearInput()
Clears any pending input from the server.
Definition: gclient.cpp:121
virtual bool onReceive(const std::string &)=0
Called when a complete line is received from the peer.
A SimpleClient class for client objects that manage their own lifetime on the heap.
Definition: gheapclient.h:54
G::Slot::Signal1< std::string > & doneSignal()
Returns a signal that indicates that client processing is complete.
Definition: gclient.cpp:85
A signal class for zero-parameter callbacks.
Definition: gslot.h:193
static bool synchronousDnsDefault()
Returns true if DNS queries should normally be synchronous on this platform.
A class which does line buffering.
Definition: glinebuffer.h:52
Client(const Location &remote_info, unsigned int connection_timeout=0U, unsigned int response_timeout=0U, unsigned int secure_connection_timeout=0U, const std::string &eol=std::string("\n"), bool bind_local_address=false, const Address &local_address=Address::defaultAddress(), bool sync_dns=synchronousDnsDefault())
Constructor.
Definition: gclient.cpp:25
G::Slot::Signal0 & secureSignal()
Returns a signal that indicates that the security layer has been successfully established.
Definition: gclient.cpp:100
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:110
virtual void onSendImp() override
Override from GNet::SimpleClient.
Definition: gclient.cpp:69
A HeapClient class that adds slot/signal signalling, connection/response timeouts, and input line buffering.
Definition: gclient.h:49
virtual void onData(const char *, SimpleClient::size_type) override
Override from GNet::SocketProtocolSink.
Definition: gclient.cpp:105