From f5dbb22807f77af777e33cccea574482a64e6db0 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Fri, 19 Jul 2013 12:56:15 +0200 Subject: [PATCH] accept takes a int& since it _modifies_ it --- lib/cpp_utils/Poll.cpp | 35 +++++++++++++++++----------- lib/cpp_utils/Poll.hpp | 2 +- lib/cpp_utils/Socket.cpp | 8 ++----- lib/cpp_utils/Socket.hpp | 2 +- lib/cpp_utils/SslConnection.cpp | 2 +- lib/cpp_utils/SslConnection.hpp | 2 +- lib/cpp_utils/StreamConnection.hpp | 2 +- lib/cpp_utils/TcpConnection.cpp | 2 +- lib/cpp_utils/TcpConnection.hpp | 2 +- lib/cpp_utils/TimedTcpConnection.cpp | 2 +- lib/cpp_utils/TimedTcpConnection.hpp | 2 +- 11 files changed, 32 insertions(+), 29 deletions(-) diff --git a/lib/cpp_utils/Poll.cpp b/lib/cpp_utils/Poll.cpp index d0dc921..9202369 100644 --- a/lib/cpp_utils/Poll.cpp +++ b/lib/cpp_utils/Poll.cpp @@ -117,28 +117,35 @@ void Poll::handleClient( const int socket ) void Poll::removeTimeoutedConnections() { - TRACE; - - if (m_connections.empty()) - return; - - ConnectionMap::iterator it; - for (it = m_connections.begin(); it != m_connections.end(); ) - if (it->second->disconnect()) { - it = removeConnection(it->second->getSocket(), it++); - } else { - ++it; - } +// TRACE; +// +// if (m_connections.empty()) +// return; +// +// ConnectionMap::iterator it; +// for (it = m_connections.begin(); it != m_connections.end(); ) +// +// /// @bug pull up closed() from TcpConnection to StreamConnection? +// if (it->second->closed()) { +// it = removeConnection(it->second->getSocket(), it); +// } else { +// ++it; +// } } -Poll::ConnectionMap::iterator Poll::removeConnection(int socket, ConnectionMap::iterator it) +Poll::ConnectionMap::iterator Poll::removeConnection(int socket, std::map< int, StreamConnection* >::iterator it) { TRACE; + ConnectionMap::iterator next = it; + next++; + removeFd(socket); delete it->second; - return m_connections.erase(it); + m_connections.erase(it); + + return next; } diff --git a/lib/cpp_utils/Poll.hpp b/lib/cpp_utils/Poll.hpp index 175ee58..db2b9a0 100644 --- a/lib/cpp_utils/Poll.hpp +++ b/lib/cpp_utils/Poll.hpp @@ -38,7 +38,7 @@ private: Poll(const Poll&); Poll& operator=(const Poll&); - typedef typename std::map< int, StreamConnection* > ConnectionMap; + typedef std::map< int, StreamConnection* > ConnectionMap; // can be overriden: behaviour alters in server/client virtual void removeTimeoutedConnections(); diff --git a/lib/cpp_utils/Socket.cpp b/lib/cpp_utils/Socket.cpp index 809fc93..a83a48c 100644 --- a/lib/cpp_utils/Socket.cpp +++ b/lib/cpp_utils/Socket.cpp @@ -108,6 +108,7 @@ bool Socket::bind(struct addrinfo *servinfo ) if (servinfo == 0) return false; + /// @bug Not error message on quick re-bind error. if (::bind(m_socket, servinfo->ai_addr, servinfo->ai_addrlen) == -1) { LOG_BEGIN(Logger::ERR) LOG_PROP("Error message", strerror(errno)) @@ -132,17 +133,12 @@ bool Socket::listen(const int maxPendingQueueLen) } -bool Socket::accept(int client_socket) +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) diff --git a/lib/cpp_utils/Socket.hpp b/lib/cpp_utils/Socket.hpp index d5d231d..253c986 100644 --- a/lib/cpp_utils/Socket.hpp +++ b/lib/cpp_utils/Socket.hpp @@ -25,7 +25,7 @@ public: bool connect(addrinfo *servinfo); bool bind(addrinfo *servinfo); bool listen( const int maxPendingQueueLen = 64 ); - bool accept( int client_socket ); + 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/lib/cpp_utils/SslConnection.cpp b/lib/cpp_utils/SslConnection.cpp index 2fb93ae..260ca18 100644 --- a/lib/cpp_utils/SslConnection.cpp +++ b/lib/cpp_utils/SslConnection.cpp @@ -106,7 +106,7 @@ bool SslConnection::listen( const int maxPendingQueueLen ) } -bool SslConnection::accept(int client_socket) +bool SslConnection::accept( int& client_socket ) { TRACE; diff --git a/lib/cpp_utils/SslConnection.hpp b/lib/cpp_utils/SslConnection.hpp index 6758522..ebed1fc 100644 --- a/lib/cpp_utils/SslConnection.hpp +++ b/lib/cpp_utils/SslConnection.hpp @@ -39,7 +39,7 @@ public: bool bind(); bool listen( const int maxPendingQueueLen = 64 ); - bool accept(int client_socket); + bool accept( int& client_socket ); bool closed() const; int getSocket() const; diff --git a/lib/cpp_utils/StreamConnection.hpp b/lib/cpp_utils/StreamConnection.hpp index 4d35d3c..1f00d42 100644 --- a/lib/cpp_utils/StreamConnection.hpp +++ b/lib/cpp_utils/StreamConnection.hpp @@ -19,7 +19,7 @@ public: virtual bool listen( const int maxPendingQueueLen = 64 ) = 0; /// @todo move accept and poll here - virtual bool accept(int socket) = 0; + virtual bool accept(int& socket) = 0; // virtual bool poll() = 0; diff --git a/lib/cpp_utils/TcpConnection.cpp b/lib/cpp_utils/TcpConnection.cpp index 8402d58..2ffa97a 100644 --- a/lib/cpp_utils/TcpConnection.cpp +++ b/lib/cpp_utils/TcpConnection.cpp @@ -109,7 +109,7 @@ bool TcpConnection::listen( const int maxPendingQueueLen ) } -bool TcpConnection::accept(int client_socket) +bool TcpConnection::accept(int& client_socket) { TRACE; return m_socket.accept(client_socket); diff --git a/lib/cpp_utils/TcpConnection.hpp b/lib/cpp_utils/TcpConnection.hpp index bb0bd84..4c2f936 100644 --- a/lib/cpp_utils/TcpConnection.hpp +++ b/lib/cpp_utils/TcpConnection.hpp @@ -36,7 +36,7 @@ public: bool bind(); bool listen( const int maxPendingQueueLen = 64 ); - bool accept(int client_socket); + bool accept(int& client_socket); int getSocket() const; void setState(const State state); diff --git a/lib/cpp_utils/TimedTcpConnection.cpp b/lib/cpp_utils/TimedTcpConnection.cpp index 74a38ad..2a1e321 100644 --- a/lib/cpp_utils/TimedTcpConnection.cpp +++ b/lib/cpp_utils/TimedTcpConnection.cpp @@ -66,7 +66,7 @@ bool TimedTcpConnection::listen( const int maxPendingQueueLen ) } -bool TimedTcpConnection::accept(int client_socket) +bool TimedTcpConnection::accept(int& client_socket) { TRACE; return m_tcpConnection->accept(client_socket); diff --git a/lib/cpp_utils/TimedTcpConnection.hpp b/lib/cpp_utils/TimedTcpConnection.hpp index c12d226..a480ed9 100644 --- a/lib/cpp_utils/TimedTcpConnection.hpp +++ b/lib/cpp_utils/TimedTcpConnection.hpp @@ -48,7 +48,7 @@ public: bool bind(); bool listen( const int maxPendingQueueLen = 64 ); - bool accept(int client_socket); + bool accept(int& client_socket); bool closed() const;