E-MailRelay
gclient.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 // gclient.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gclient.h"
23 #include "gstr.h"
24 
25 GNet::Client::Client( const Location & remote_info , unsigned int connection_timeout ,
26  unsigned int response_timeout , unsigned int secure_connection_timeout ,
27  LineBufferConfig eol , bool bind_local_address , const Address & local_address ,
28  bool sync_dns ) :
29  HeapClient(remote_info,bind_local_address,local_address,sync_dns,secure_connection_timeout) ,
30  m_done_signal(true) ,
31  m_connected_signal(true) ,
32  m_connection_timeout(connection_timeout) ,
33  m_response_timeout(response_timeout) ,
34  m_connection_timer(*this,&Client::onConnectionTimeout,*this) ,
35  m_response_timer(*this,&Client::onResponseTimeout,*this) ,
36  m_line_buffer(eol)
37 {
38  if( connection_timeout != 0U )
39  m_connection_timer.startTimer( connection_timeout ) ;
40 }
41 
43 {
44 }
45 
47 {
48  m_event_signal.emit( "connecting" , remoteLocation().displayString() ) ;
49 }
50 
52 {
53  if( m_connection_timeout != 0U )
54  m_connection_timer.cancelTimer() ;
55 
56  m_connected_signal.emit() ;
57  m_event_signal.emit( "connected" , remoteLocation().address().displayString() + " " + remoteLocation().name() ) ;
58 }
59 
60 void GNet::Client::onDeleteImp( const std::string & reason )
61 {
62  m_connection_timer.cancelTimer() ;
63  m_response_timer.cancelTimer() ;
64  m_event_signal.emit( reason.empty() ? "done" : "failed" , reason ) ;
65  m_done_signal.emit( reason ) ;
66 }
67 
69 {
70  if( m_response_timeout != 0U )
71  m_response_timer.startTimer( m_response_timeout ) ;
72 }
73 
74 void GNet::Client::onConnectionTimeout()
75 {
76  doDelete( "connection timeout after " + G::Str::fromUInt(m_connection_timeout) + "s connecting to " + remoteLocation().displayString() ) ;
77 }
78 
79 void GNet::Client::onResponseTimeout()
80 {
81  doDelete( "response timeout after " + G::Str::fromUInt(m_response_timeout) + "s talking to " + remoteLocation().displayString() ) ;
82 }
83 
85 {
86  return m_done_signal ;
87 }
88 
90 {
91  return m_event_signal ;
92 }
93 
95 {
96  return m_connected_signal ;
97 }
98 
99 void GNet::Client::onData( const char * p , SimpleClient::size_type n )
100 {
101  m_line_buffer.add( p , n ) ;
102 
103  LineBufferIterator iter( m_line_buffer ) ;
104  for( bool first = true ; iter.more() ; first = false )
105  {
106  if( first && m_response_timeout != 0U )
107  m_response_timer.cancelTimer() ;
108 
109  bool ok = onReceive( iter.lineData() , iter.lineSize() , iter.eolSize() ) ;
110  if( !ok )
111  break ;
112  }
113 }
114 
116 {
117  return m_line_buffer.eol() ;
118 }
119 
121 {
122  LineBufferIterator iter( m_line_buffer ) ;
123  while( iter.more() )
124  ; // no-op to discard each line
125 }
126 
127 /// \file gclient.cpp
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 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
const char * lineData() const
See LineBuffer::lineData().
Definition: glinebuffer.h:276
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
Syntactic sugar for calling GNet::LineBuffer iteration methods.
Definition: glinebuffer.h:138
size_t eolSize() const
See LineBuffer::eolSize().
Definition: glinebuffer.h:288
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 std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
Definition: gstr.cpp:353
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
size_t lineSize() const
See LineBuffer::lineSize().
Definition: glinebuffer.h:282
A signal class for zero-parameter callbacks.
Definition: gslot.h:193
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
bool more()
See LineBuffer::more().
Definition: glinebuffer.h:258