diff --git a/include/Socket.hpp b/include/Socket.hpp index 5cf3054..dc12637 100644 --- a/include/Socket.hpp +++ b/include/Socket.hpp @@ -25,6 +25,7 @@ public: bool connect(addrinfo *servinfo); bool bind(addrinfo *servinfo); bool listen( const int maxPendingQueueLen = 64 ); + bool accept( int &client_socket ); bool send( const void *message, const int lenght ); bool receive ( void* buffer, const int bufferLen, ssize_t *msgLen ); diff --git a/include/Socket.hpp.gch b/include/Socket.hpp.gch new file mode 100644 index 0000000..80b414b Binary files /dev/null and b/include/Socket.hpp.gch differ diff --git a/include/SocketClient.hpp b/include/SocketClient.hpp index 705d23b..9927842 100644 --- a/include/SocketClient.hpp +++ b/include/SocketClient.hpp @@ -27,16 +27,19 @@ private: protected: // overridig poll's behaviour - virtual void acceptClient(); + void acceptClient(); // overridig poll's behaviour - virtual void handleClient( const int ); + void handleClient( const int ); private: PollerThread(const PollerThread&); PollerThread& operator=(const PollerThread&); + // overridig poll's behaviour + void removeTimeoutedConnections(); + void* run(); SocketClient *m_tcpClient; diff --git a/src/Socket.cpp b/src/Socket.cpp index 6b6513b..b0f1f0b 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -10,6 +10,8 @@ #include // inet_ntop #include +#include + #include // strerror #include // errno @@ -63,11 +65,21 @@ bool Socket::closeSocket() { TRACE; - /// @note are the return values of shutdown and close meaningful? - shutdown(m_socket, SHUT_RDWR); - close(m_socket); - m_socket = -1; + if (shutdown(m_socket, SHUT_RDWR) == -1) { + LOG_BEGIN(Logger::ERR) + LOG_PROP("Error message", strerror(errno)) + LOG_END("Could not shutdown socket."); + return false; + } + + if (close(m_socket) == -1) { + LOG_BEGIN(Logger::ERR) + LOG_PROP("Error message", strerror(errno)) + LOG_END("Could not close socket."); + return false; + } + m_socket = -1; return true; } @@ -85,14 +97,6 @@ bool Socket::connect(struct addrinfo *servinfo) LOG_END("Could not connect to peer."); return false; } - - std::string address, service; - if ( AddrInfo::convertNameInfo( servinfo, address, service) ) { - LOG_BEGIN(Logger::INFO) - LOG_PROP("Host", address) - LOG_PROP("Port", service) - LOG_END("Connected to peer."); - } return true; } @@ -110,19 +114,11 @@ bool Socket::bind(struct addrinfo *servinfo ) LOG_END("Could not bind name to socket."); return false; } - - std::string address, service; - if ( AddrInfo::convertNameInfo( servinfo, address, service) ) { - LOG_BEGIN(Logger::INFO) - LOG_PROP("Host", address) - LOG_PROP("Port", service) - LOG_END("Binded to socket."); - } return true; } -bool Socket::listen ( const int maxPendingQueueLen ) +bool Socket::listen(const int maxPendingQueueLen) { TRACE; @@ -136,6 +132,28 @@ bool Socket::listen ( const int maxPendingQueueLen ) } +bool Socket::accept(int &client_socket) +{ + TRACE; + sockaddr clientAddr; + socklen_t clientAddrLen; + + /// @bug This needs to be investigated ASAP: if the m_socket is not used before accept, it fails. + LOG_BEGIN(Logger::INFO) + LOG_SPROP(m_socket) + LOG_END("Accept mystery."); + + client_socket = ::accept( m_socket, &clientAddr, &clientAddrLen ) ; + if ( client_socket == -1 ) { + LOG_BEGIN(Logger::ERR) + LOG_PROP("Error message", strerror(errno)) + LOG_END("Could not accept connection on socket."); + return false; + } + return true; +} + + bool Socket::send ( const void *message, const int length ) { TRACE; diff --git a/src/SocketClient.cpp b/src/SocketClient.cpp index 800377f..b6a3507 100644 --- a/src/SocketClient.cpp +++ b/src/SocketClient.cpp @@ -38,6 +38,11 @@ void SocketClient::PollerThread::handleClient( const int ) } +void SocketClient::PollerThread::removeTimeoutedConnections() +{ +} + + void* SocketClient::PollerThread::run() { TRACE;