21 #ifndef GV_CLIENT_HOLDER__H
22 #define GV_CLIENT_HOLDER__H
39 template <
typename TFactory,
typename TClient>
43 explicit ClientHolder( TFactory & factory ,
bool once =
false ,
44 bool (*quit_test_fn)(std::string) =
nullptr ,
unsigned int reconnect_timeout = 1U ) ;
63 virtual void onTimeout()
override ;
64 virtual void onException( std::exception & )
override ;
65 void onDone( std::string ) ;
69 TFactory & m_factory ;
71 bool m_once_connected ;
72 bool (*m_quit_test_fn)(std::string) ;
73 unsigned int m_reconnect_timeout ;
77 template <
typename TFactory,
typename TClient>
81 m_once_connected(false) ,
82 m_quit_test_fn(quit_test_fn) ,
83 m_reconnect_timeout(reconnect_timeout)
85 m_ptr.reset( m_factory.create() ) ;
86 m_ptr.doneSignal().connect(
G::Slot::slot(*
this,&ClientHolder::onDone) ) ;
87 m_ptr.connectedSignal().connect(
G::Slot::slot(*
this,&ClientHolder::onConnected) ) ;
90 template <
typename TFactory,
typename TClient>
93 m_ptr.doneSignal().disconnect() ;
94 m_ptr.connectedSignal().disconnect() ;
97 template <
typename TFactory,
typename TClient>
100 G_WARNING(
"ClientHolder::onDone: session ended: " << reason ) ;
101 if( (m_once&&m_once_connected) || (m_quit_test_fn!=
nullptr && m_quit_test_fn(reason)) )
104 startTimer( m_reconnect_timeout ) ;
107 template <
typename TFactory,
typename TClient>
108 void ClientHolder<TFactory,TClient>::onConnected()
110 m_once_connected = true ;
113 template <
typename TFactory,
typename TClient>
114 void ClientHolder<TFactory,TClient>::onTimeout()
116 G_LOG(
"ClientHolder::onTimeout: reconnecting to " << m_factory.url().summary() ) ;
117 m_ptr.reset( m_factory.create() ) ;
120 template <
typename TFactory,
typename TClient>
121 void ClientHolder<TFactory,TClient>::onException( std::exception & )
Slot0 slot(T &object, void(T::*fn)())
A slot factory function overloaded for a zero-parameter callback.
~ClientHolder()
Destructor.
A smart pointer class for GNet::HeapClient that keeps track of when the contained instance deletes it...
An interface used by GNet::TimerList to keep track of pending timeouts and to deliver timeout events...
Manages a network client object so that the network client is automatically re-created from a factory...
ClientHolder(TFactory &factory, bool once=false, bool(*quit_test_fn)(std::string)=nullptr, unsigned int reconnect_timeout=1U)
Constructor that creates a network client object from a factory.
static EventLoop & instance()
Returns a reference to an instance of the class, if any.
virtual void quit(std::string reason)=0
Causes run() to return (once the call stack has unwound).