gclient.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2001-2013 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 "gnet.h"
23 #include "gclient.h"
24 
25 GNet::Client::Client( const ResolverInfo & remote_info , unsigned int connection_timeout ,
26  unsigned int response_timeout , unsigned int secure_connection_timeout ,
27  const std::string & eol , const Address & local_interface ,
28  bool privileged , bool sync_dns ) :
29  HeapClient(remote_info,local_interface,privileged,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" , resolverInfo().displayString() ) ;
49 }
50 
52 {
53  if( m_connection_timeout != 0U )
54  m_connection_timer.cancelTimer() ;
55 
56  m_connected_signal.emit() ;
57 
58  m_event_signal.emit( "connected" , resolverInfo().address().displayString() + " " + resolverInfo().name() ) ;
59 }
60 
61 void GNet::Client::onDeleteImp( const std::string & reason , bool retry )
62 {
63  m_connection_timer.cancelTimer() ;
64  m_response_timer.cancelTimer() ;
65  m_event_signal.emit( reason.empty() ? "done" : "failed" , reason ) ;
66  m_done_signal.emit( reason , retry ) ;
67 }
68 
70 {
71  if( m_response_timeout != 0U )
72  m_response_timer.startTimer( m_response_timeout ) ;
73 }
74 
75 void GNet::Client::onConnectionTimeout()
76 {
77  doDelete( "connection timeout" ) ;
78 }
79 
80 void GNet::Client::onResponseTimeout()
81 {
82  doDelete( "response timeout" ) ;
83 }
84 
86 {
87  return m_done_signal ;
88 }
89 
91 {
92  return m_event_signal ;
93 }
94 
96 {
97  return m_connected_signal ;
98 }
99 
101 {
102  return m_secure_signal ;
103 }
104 
106 {
107  m_line_buffer.add(p,n) ;
108 
109  bool first = true ;
110  while( m_line_buffer.more() )
111  {
112  if( first && m_response_timeout != 0U )
113  m_response_timer.cancelTimer() ;
114  first = false ;
115 
116  bool ok = onReceive( m_line_buffer.line() ) ;
117  if( !ok )
118  break ;
119  }
120 }
121 
123 {
124  while( m_line_buffer.more() )
125  m_line_buffer.discard() ;
126 }
127 
Client(const ResolverInfo &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"), const Address &local_interface=Address(0U), bool privileged=false, bool sync_dns=synchronousDnsDefault())
Constructor.
Definition: gclient.cpp:25
std::string::size_type size_type
Definition: gsimpleclient.h:86
virtual void onSendImp()
Final override from GNet::SimpleClient.
Definition: gclient.cpp:69
G::Signal0 & connectedSignal()
Returns a signal that incidcates that the client has successfully connected to the server...
Definition: gclient.cpp:95
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
G::Signal2< std::string, bool > & doneSignal()
Returns a signal that indicates that client processing is complete.
Definition: gclient.cpp:85
A class that holds a host/service name pair and optionally the results of a name-to-address lookup...
Definition: gresolverinfo.h:48
virtual void onDeleteImp(const std::string &, bool)
Override from GNet::HeapClient.
Definition: gclient.cpp:61
G::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
virtual void onData(const char *, SimpleClient::size_type)
Final override from GNet::SocketProtocolSink.
Definition: gclient.cpp:105
void clearInput()
Clears any pending input from the server.
Definition: gclient.cpp:122
A SimpleClient class for client objects that manage their own lifetime on the heap.
Definition: gheapclient.h:54
Part of the slot/signal system.
Definition: gslot.h:138
G::Signal0 & secureSignal()
Returns a signal that incidcates that the security layer has been successfully established.
Definition: gclient.cpp:100
virtual void onConnectImp()
Final override from GNet::SimpleClient.
Definition: gclient.cpp:51
A HeapClient class that adds slot/signal signalling, connection/response timeouts, and input line buffering.
Definition: gclient.h:51
virtual void onConnecting()
Final override from GNet::HeapClient.
Definition: gclient.cpp:46