#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; stopPolling(); stop(); } protected: /// @todo this is unclear and nasty hack virtual void acceptClient() { TRACE; m_tcpClient.m_connection.receive(); stopPolling(); } /// @todo this is unclear and nasty hack virtual void handleClient( const int ) { TRACE; LOG( Logger::DEBUG, "Server closed the connection." ); stopPolling(); } private: void* run() { TRACE; 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); } private: TcpClient(const TcpClient& ); TcpClient& operator=(const TcpClient& ); Connection& getConnection() { TRACE; return m_connection; } Connection m_connection; PollerThread m_watcher; }; #endif // TCP_CLIENT_HPP