From 76dd8a2ca66f05e5f37d7f3a10c66b59eb50afda Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Thu, 17 Nov 2011 14:14:26 +0100 Subject: [PATCH] small code cleanups, Connection hides Socket --- include/Connection.hpp | 34 ++++++++++++++++------------------ include/Message.hpp | 2 -- include/Poll.hpp | 5 ++++- include/Socket.hpp | 2 ++ include/TcpClient.hpp | 11 ++--------- other/tcpclient_main.cpp | 18 ++++++++++++++---- other/tcpserver_main.cpp | 6 +++++- src/Socket.cpp | 10 ++++++++++ 8 files changed, 53 insertions(+), 35 deletions(-) diff --git a/include/Connection.hpp b/include/Connection.hpp index 91022cf..718291d 100644 --- a/include/Connection.hpp +++ b/include/Connection.hpp @@ -8,6 +8,8 @@ #include +/** @todo make connection an iface and this class shall be a TcpConnection, + * inherited from connection */ template class Connection @@ -95,27 +97,23 @@ public: { TRACE; -// LOG ( Logger::DEBUG, std::string("receving on socket: "). -// append(TToStr(m_socket.getSocket())).c_str() ); - - ssize_t len = recv(m_socket.getSocket(), m_buffer, m_bufferLength, 0); - - LOG ( Logger::DEBUG, std::string("Received: "). - append(TToStr(len)).append(" bytes from: "). - append(m_host).append(":").append(m_port).c_str() ); - - if (len == -1) { - LOG( Logger::ERR, errnoToString("ERROR reading from socket. ").c_str() ); - return false; + ssize_t length; + if ( !m_socket.receive(m_buffer, m_bufferLength, &length) ) { + if (length == -1) { + LOG( Logger::ERR, errnoToString("ERROR reading from socket. ").c_str() ); + } + else if (length == 0) { + LOG( Logger::INFO, std::string("Connection closed by "). + append(m_host).append(":").append(m_port).c_str() ); + } + return false; } - if (len == 0) { - LOG( Logger::INFO, std::string("Connection closed by "). - append(m_host).append(":").append(m_port).c_str() ); - return false; - } + LOG ( Logger::DEBUG, std::string("Received: "). + append(TToStr(length)).append(" bytes from: "). + append(m_host).append(":").append(m_port).c_str() ); - return m_message.buildMessage( (void*)m_buffer, (size_t)len); + return m_message.buildMessage( (void*)m_buffer, (size_t)length); } diff --git a/include/Message.hpp b/include/Message.hpp index 3f280f1..b3c73a7 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -36,8 +36,6 @@ protected: Connection *m_connection; void *m_param; - - /// @todo shall i use dinamic array? std::string m_buffer; private: diff --git a/include/Poll.hpp b/include/Poll.hpp index 49e0f87..5731b4c 100644 --- a/include/Poll.hpp +++ b/include/Poll.hpp @@ -25,7 +25,7 @@ public: , m_num_of_fds(0) { TRACE; - m_fds = new pollfd[m_maxclients]; + m_fds = new pollfd[m_maxclients+1]; // plus the server socket addFd( m_connection->getSocket(), POLLIN | POLLPRI ); } @@ -74,6 +74,7 @@ public: protected: + // can be overriden: behaviour alters in server/client virtual void acceptClient() { TRACE; @@ -98,6 +99,8 @@ protected: addFd( client_socket, POLLIN | POLLPRI ); } + + // can be overriden: behaviour alters in server/client virtual void handleClient( const int socket ) { TRACE; diff --git a/include/Socket.hpp b/include/Socket.hpp index a0ab797..21c10eb 100644 --- a/include/Socket.hpp +++ b/include/Socket.hpp @@ -33,6 +33,8 @@ public: std::string &port); bool send( const void *message, const int lenght ); + bool receive ( void* buffer, const int bufferLen, ssize_t *msgLen ); + int getSocket() const; static bool convertNameInfo( sockaddr* addr, diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index 9ab5958..07f1b37 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -38,7 +38,7 @@ private: protected: - /// @todo this is unclear and nasty hack + // overridig poll's behaviour virtual void acceptClient() { TRACE; @@ -47,7 +47,7 @@ private: stopPolling(); } - /// @todo this is unclear and nasty hack + // overridig poll's behaviour virtual void handleClient( const int ) { TRACE; @@ -120,11 +120,6 @@ private: TcpClient(const TcpClient& ); TcpClient& operator=(const TcpClient& ); - Connection& getConnection() - { - TRACE; - return m_connection; - } Connection m_connection; PollerThread m_watcher; @@ -132,5 +127,3 @@ private: }; #endif // TCP_CLIENT_HPP - - diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index 184ff27..4e082bb 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -70,11 +70,22 @@ int main(int argc, char* argv[] ) TcpClient tcpclient(argv[1], argv[2], &finished); - tcpclient.connect(); - sleep(1); // wait for thread creation + if ( !tcpclient.connect() ) { + LOG( Logger::ERR, "Couldn't connect to server, exiting..." ); + Logger::destroy(); + return 1; + } + + // wait for thread creation + sleep(1); + // send message to server std::string msg1(argv[3]); - tcpclient.send( msg1.c_str(), msg1.length()); + if ( !tcpclient.send( msg1.c_str(), msg1.length()) ) { + LOG( Logger::ERR, "Couldn't send message to server, exiting..." ); + Logger::destroy(); + return 1; + } // wait for the complate &handled reply struct timespec tm = {0,1000}; @@ -82,7 +93,6 @@ int main(int argc, char* argv[] ) nanosleep(&tm, &tm) ; tcpclient.disconnect(); - Logger::destroy(); return 0; } \ No newline at end of file diff --git a/other/tcpserver_main.cpp b/other/tcpserver_main.cpp index 653a6b6..2675bc5 100644 --- a/other/tcpserver_main.cpp +++ b/other/tcpserver_main.cpp @@ -66,7 +66,11 @@ int main() TcpServer tcpServer("localhost", "4455"); - tcpServer.start(); + if ( !tcpServer.start() ) { + LOG( Logger::ERR, "Failed to start TCP server, exiting..."); + Logger::destroy(); + return 1; + } // never reached sleep(1); diff --git a/src/Socket.cpp b/src/Socket.cpp index 4fdfa7f..361b560 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -131,6 +131,16 @@ bool Socket::send ( const void *message, const int length ) } +bool Socket::receive( void *buffer, const int bufferLen, ssize_t *msgLen ) +{ + TRACE; + + *msgLen = recv(m_socket, buffer, bufferLen, 0); + + return (*msgLen > 0); +} + + int Socket::getSocket() const { TRACE;