#ifndef TCP_CLIENT_HPP #define TCP_CLIENT_HPP #include "Logger.hpp" #include "Connection.hpp" #include "Thread.hpp" #include "Poll.hpp" #include #include // size_t template class TcpClient { private: template class PollerThread : public Thread , public Poll { public: PollerThread( TcpClient &data ) : Poll( &(data.m_connection) ) , m_tcpClient(data) { TRACE; } void stopPoller() { TRACE; Poll::stopPolling(); stop(); } protected: // overridig poll's behaviour virtual void acceptClient() { TRACE; m_tcpClient.m_connection.receive(); Poll::stopPolling(); } // overridig poll's behaviour virtual void handleClient( const int ) { TRACE; LOG( Logger::DEBUG, "Server closed the connection." ); Poll::stopPolling(); } private: void* run() { TRACE; Poll::startPolling(); return 0; } TcpClient &m_tcpClient; }; // class PollerThread public: TcpClient ( const std::string host, const std::string port, void *msgParam = 0 ) : m_connection (host, port, msgParam) , m_watcher(*this) { TRACE; } virtual ~TcpClient() { TRACE; disconnect(); } bool connect() { TRACE; if ( !m_connection.connectToHost() ) return false; m_watcher.start(); return true; } void disconnect() { TRACE; if ( m_watcher.isRunning() ) { m_watcher.stopPoller(); m_watcher.join(); } m_connection.closeConnection(); } bool send( const void* msg, const size_t msgLen ) { TRACE; return m_connection.send(msg, msgLen); } bool isPolling() const { TRACE; return m_watcher.isPolling(); } private: TcpClient(const TcpClient& ); TcpClient& operator=(const TcpClient& ); Connection m_connection; PollerThread m_watcher; }; #endif // TCP_CLIENT_HPP