VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gclient.cpp
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 // gclient.cpp
19 //
20 
21 #include "gdef.h"
22 #include "gnet.h"
23 #include "gclient.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  const std::string & 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,*static_cast<EventHandler*>(this)) ,
35  m_response_timer(*this,&Client::onResponseTimeout,*static_cast<EventHandler*>(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 
58  m_event_signal.emit( "connected" , remoteLocation().address().displayString() + " " + remoteLocation().name() ) ;
59 }
60 
61 void GNet::Client::onDeleteImp( const std::string & reason )
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 ) ;
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 
105 void GNet::Client::onData( const char * p , SimpleClient::size_type n )
106 {
107  m_line_buffer.add(p,n) ;
108 
109  LineBufferIterator iter( m_line_buffer ) ;
110  for( bool first = true ; iter.more() ; first = false )
111  {
112  if( first && m_response_timeout != 0U )
113  m_response_timer.cancelTimer() ;
114 
115  bool ok = onReceive( iter.line() ) ;
116  if( !ok )
117  break ;
118  }
119 }
120 
122 {
123  LineBufferIterator iter( m_line_buffer ) ;
124  while( iter.more() )
125  ; // no-op to discard each line
126 }
127 
128 /// \file gclient.cpp
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
An iterator class for GNet::LineBuffer that extracts complete lines.
Definition: glinebuffer.h:102
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
void clearInput()
Clears any pending input from the server.
Definition: gclient.cpp:121
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 base class for classes that handle asynchronous events from the event loop.
Definition: geventhandler.h:78
A signal class for zero-parameter callbacks.
Definition: gslot.h:193
const std::string & line() const
Returns the current line.
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
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
bool more()
Returns true if there is a line() to be had.