E-MailRelay
gclient.h
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 /// \file gclient.h
19 ///
20 
21 #ifndef G_NET_CLIENT__H
22 #define G_NET_CLIENT__H
23 
24 #include "gdef.h"
25 #include "gheapclient.h"
26 #include "glinebuffer.h"
27 #include "gtimer.h"
28 #include "gslot.h"
29 
30 namespace GNet
31 {
32  class ClientTimer ;
33  class Client ;
34 }
35 
36 /// \class GNet::Client
37 /// A HeapClient class that adds slot/signal signalling, connection/response
38 /// timeouts, and input line buffering.
39 ///
40 /// The following pure virtual functions must be implemented by derived
41 /// classes: onConnect(), onReceive(), onDelete(), and onSendComplete().
42 ///
43 /// The intention is that derived classes use the virtual functions for
44 /// event notification while the slot/signal event notifications are used
45 /// by the containing object (in particular so that the container is
46 /// informed when the client object deletes itself).
47 ///
48 class GNet::Client : public HeapClient
49 {
50 public:
51  explicit Client( const Location & remote_info , unsigned int connection_timeout = 0U ,
52  unsigned int response_timeout = 0U , unsigned int secure_connection_timeout = 0U ,
53  LineBufferConfig = LineBufferConfig() , bool bind_local_address = false ,
54  const Address & local_address = Address::defaultAddress() ,
55  bool sync_dns = synchronousDnsDefault() ) ;
56  ///< Constructor. The connection is initiated asynchronously.
57 
59  ///< Returns a signal that indicates that client processing
60  ///< is complete. The signal parameter is a failure reason,
61  ///< or the empty string on success.
62 
64  ///< Returns a signal that indicates that something interesting
65  ///< has happened.
66  ///<
67  ///< The first signal parameter is one of "connecting",
68  ///< "connected", and either "failed" (on error) or "done"
69  ///< (for normal termination). The "done/failed" event is
70  ///< emitted just before the doneSignal(). Derived clases
71  ///< may emit other events over this channel.
72 
74  ///< Returns a signal that indicates that the client
75  ///< has successfully connected to the server.
76 
77 protected:
78  virtual ~Client() ;
79  ///< Destructor.
80 
81  virtual bool onReceive( const char * line_data , size_t line_size , size_t eol_size ) = 0 ;
82  ///< Called when a complete line is received from the peer.
83  ///< The implementation should return false if it needs
84  ///< to stop further onReceive() calls being generated
85  ///< from data already received and buffered, if for
86  ///< example the object has just deleted itself.
87 
88  void clearInput() ;
89  ///< Clears the line buffer. This discards any data that has
90  ///< been received from the server but not delivered as a
91  ///< complete line via onReceive().
92 
93  std::string lineBufferEndOfLine() const ;
94  ///< Returns the line buffer end-of-line string. Returns
95  ///< the empty string if auto-detecting and not yet
96  ///< auto-detected.
97 
98  virtual void onDeleteImp( const std::string & reason ) override ;
99  ///< Override from GNet::HeapClient.
100 
101  virtual void onConnectImp() override ;
102  ///< Override from GNet::SimpleClient.
103 
104  virtual void onData( const char * , SimpleClient::size_type ) override ;
105  ///< Override from GNet::SocketProtocolSink.
106 
107  virtual void onConnecting() override ;
108  ///< Override from GNet::HeapClient.
109 
110  virtual void onSendImp() override ;
111  ///< Override from GNet::SimpleClient.
112 
113 private:
114  Client( const Client& ) ; // not implemented
115  void operator=( const Client& ) ; // not implemented
116  void onConnectionTimeout() ;
117  void onResponseTimeout() ;
118 
119 private:
120  G::Slot::Signal1<std::string> m_done_signal ;
122  G::Slot::Signal0 m_connected_signal ;
123  G::Slot::Signal0 m_secure_signal ;
124  unsigned int m_connection_timeout ;
125  unsigned int m_response_timeout ;
126  GNet::Timer<Client> m_connection_timer ;
127  GNet::Timer<Client> m_response_timer ;
128  GNet::LineBuffer m_line_buffer ;
129 } ;
130 
131 #endif
std::string lineBufferEndOfLine() const
Returns the line buffer end-of-line string.
Definition: gclient.cpp:115
G::Slot::Signal0 & connectedSignal()
Returns a signal that indicates that the client has successfully connected to the server...
Definition: gclient.cpp:94
virtual void onConnecting() override
Override from GNet::HeapClient.
Definition: gclient.cpp:46
virtual bool onReceive(const char *line_data, size_t line_size, size_t eol_size)=0
Called when a complete line is received from the peer.
Network classes.
Definition: gaddress.h:29
virtual void onDeleteImp(const std::string &reason) override
Override from GNet::HeapClient.
Definition: gclient.cpp:60
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:54
G::Slot::Signal2< std::string, std::string > & eventSignal()
Returns a signal that indicates that something interesting has happened.
Definition: gclient.cpp:89
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:50
static Address defaultAddress()
Returns a default address, being the IPv4 wildcard address with a zero port number.
A configuration structure for GNet::LineBuffer.
Definition: glinebuffer.h:174
void clearInput()
Clears the line buffer.
Definition: gclient.cpp:120
A SimpleClient class for client objects that manage their own lifetime on the heap.
Definition: gheapclient.h:53
G::Slot::Signal1< std::string > & doneSignal()
Returns a signal that indicates that client processing is complete.
Definition: gclient.cpp:84
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 that does line buffering, supporting auto-detection of line endings and fixed-size extraction...
Definition: glinebuffer.h:53
A timer class template in which the timeout is delivered to the specified method. ...
Definition: gtimer.h:115
virtual void onSendImp() override
Override from GNet::SimpleClient.
Definition: gclient.cpp:68
Client(const Location &remote_info, unsigned int connection_timeout=0U, unsigned int response_timeout=0U, unsigned int secure_connection_timeout=0U, LineBufferConfig=LineBufferConfig(), bool bind_local_address=false, const Address &local_address=Address::defaultAddress(), bool sync_dns=synchronousDnsDefault())
Constructor. The connection is initiated asynchronously.
Definition: gclient.cpp:25
A HeapClient class that adds slot/signal signalling, connection/response timeouts, and input line buffering.
Definition: gclient.h:48
virtual void onData(const char *, SimpleClient::size_type) override
Override from GNet::SocketProtocolSink.
Definition: gclient.cpp:99