- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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
42 GNet::Client::~Client()
43 {
44 }
45
46 void GNet::Client::onConnecting()
47 {
48 m_event_signal.emit( "connecting" , resolverInfo().displayString() ) ;
49 }
50
51 void GNet::Client::onConnectImp()
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
69 void GNet::Client::onSendImp()
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
85 G::Signal2<std::string,bool> & GNet::Client::doneSignal()
86 {
87 return m_done_signal ;
88 }
89
90 G::Signal2<std::string,std::string> & GNet::Client::eventSignal()
91 {
92 return m_event_signal ;
93 }
94
95 G::Signal0 & GNet::Client::connectedSignal()
96 {
97 return m_connected_signal ;
98 }
99
100 G::Signal0 & GNet::Client::secureSignal()
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 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
122 void GNet::Client::clearInput()
123 {
124 while( m_line_buffer.more() )
125 m_line_buffer.discard() ;
126 }
127
GNet::Client::Client
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
GNet::SimpleClient::size_type
std::string::size_type size_type
Definition: gsimpleclient.h:86
G::Signal2< std::string, bool >
GNet::Client::onSendImp
virtual void onSendImp()
Final override from GNet::SimpleClient.
Definition: gclient.cpp:69
GNet::Client::connectedSignal
G::Signal0 & connectedSignal()
Returns a signal that incidcates that the client has successfully connected to the server...
Definition: gclient.cpp:95
GNet::Address
The Address class encapsulates an IP transport address.
Definition: gaddress.h:48
GNet::Client::doneSignal
G::Signal2< std::string, bool > & doneSignal()
Returns a signal that indicates that client processing is complete.
Definition: gclient.cpp:85
gclient.h
GNet::ResolverInfo
A class that holds a host/service name pair and optionally the results of a name-to-address lookup...
Definition: gresolverinfo.h:48
GNet::Client::onDeleteImp
virtual void onDeleteImp(const std::string &, bool)
Override from GNet::HeapClient.
Definition: gclient.cpp:61
GNet::Client::eventSignal
G::Signal2< std::string, std::string > & eventSignal()
Returns a signal that indicates that something interesting has happened.
Definition: gclient.cpp:90
GNet::Client::~Client
virtual ~Client()
Destructor.
Definition: gclient.cpp:42
GNet::Client::onData
virtual void onData(const char *, SimpleClient::size_type)
Final override from GNet::SocketProtocolSink.
Definition: gclient.cpp:105
GNet::Client::clearInput
void clearInput()
Clears any pending input from the server.
Definition: gclient.cpp:122
GNet::HeapClient
A SimpleClient class for client objects that manage their own lifetime on the heap.
Definition: gheapclient.h:54
G::Signal0
Part of the slot/signal system.
Definition: gslot.h:138
GNet::Client::secureSignal
G::Signal0 & secureSignal()
Returns a signal that incidcates that the security layer has been successfully established.
Definition: gclient.cpp:100
GNet::Client::onConnectImp
virtual void onConnectImp()
Final override from GNet::SimpleClient.
Definition: gclient.cpp:51
GNet::Client
A HeapClient class that adds slot/signal signalling, connection/response timeouts, and input line buffering.
Definition: gclient.h:51
GNet::Client::onConnecting
virtual void onConnecting()
Final override from GNet::HeapClient.
Definition: gclient.cpp:46