diff --git a/CMakeLists.txt b/CMakeLists.txt index 8daaeff..6353a2a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ project (CPP_UTILS_LIB) add_subdirectory (build) add_subdirectory (test EXCLUDE_FROM_ALL) +add_subdirectory (other EXCLUDE_FROM_ALL) find_package(Doxygen) if(DOXYGEN_FOUND) 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 a79e46c..db4f6fd 100644 --- a/lib/cpp_utils/Socket.cpp +++ b/lib/cpp_utils/Socket.cpp @@ -101,6 +101,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)) @@ -125,7 +126,7 @@ bool Socket::listen(const int maxPendingQueueLen) } -bool Socket::accept(int client_socket) +bool Socket::accept(int& client_socket) { TRACE; sockaddr clientAddr; 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 516af69..ee35faa 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; diff --git a/other/CMakeLists.txt b/other/CMakeLists.txt new file mode 100644 index 0000000..bf70152 --- /dev/null +++ b/other/CMakeLists.txt @@ -0,0 +1,28 @@ +set (CXX_FLAGS "-Wall -Wextra -pedantic -Weffc++ -Wshadow " + "-Wpointer-arith -Wcast-qual " + "-ggdb -fprofile-arcs -ftest-coverage --std=c++0x " ) + +add_definitions( ${CXX_FLAGS} ) + +include_directories (. ../lib) + +add_executable ( tcpserver tcpserver_main.cpp ) +target_link_libraries ( tcpserver CppUtils ) + +add_executable ( tcpclient tcpclient_main.cpp ) +target_link_libraries ( tcpclient CppUtils ) + +add_executable ( sslserver sslserver_main.cpp ) +target_link_libraries ( sslserver CppUtils ssl pthread rt ) + +add_executable ( sslclient sslclient_main.cpp ) +target_link_libraries ( sslclient CppUtils ssl pthread rt ) + +add_executable ( mysqlclient mysqlclient_main.cpp ) +add_library ( lib_mysql_client SHARED IMPORTED ) +# TODO use find_library +set_target_properties ( lib_mysql_client PROPERTIES IMPORTED_LOCATION /usr/lib/x86_64-linux-gnu/libmysqlclient.so ) +target_link_libraries ( mysqlclient CppUtils lib_mysql_client ) + + +add_custom_target( other DEPENDS tcpserver tcpclient sslserver sslclient mysqlclient ) diff --git a/other/compile.sh b/other/compile.sh index e97e8e5..ebb1f65 100755 --- a/other/compile.sh +++ b/other/compile.sh @@ -1,12 +1,15 @@ #!/bin/bash -INCLUDE_DIR="../include" +WORKING_DIR=`pwd` + +INCLUDE_DIR="$WORKING_DIR/../lib" +LIB_DIR=/home/denes/projects/cpp_utils/cpp_utils/build GCC_OPTIONS="-Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-qual -ggdb -Weffc++ -std=c++0x" -GCC="/usr/lib/colorgcc/bin/g++" +GCC="g++" -BUILD_DIR="tmp/" +BUILD_DIR="$WORKING_DIR/tmp" -for SRC_FILE in $(ls ../src/*.cpp) +for SRC_FILE in $(ls $INCLUDE_DIR/cpp_utils/*.cpp) do echo "Compiling $SRC_FILE" $GCC -c $SRC_FILE -I$INCLUDE_DIR $GCC_OPTIONS @@ -25,16 +28,16 @@ cd $BUILD_DIR echo "Linking tcpclient_main.o" -$GCC tcpclient_main.o Logger.o SocketClient.o TcpConnection.o Socket.o AddrInfo.o Connection.o Thread.o Poll.o -lpthread -o tcpclient +$GCC tcpclient_main.o -L$LIB_DIR -lCppUtils -lpthread -o tcpclient echo "Linking tcpserver_main.o" -$GCC tcpserver_main.o Logger.o SocketServer.o TcpConnection.o Socket.o AddrInfo.o Connection.o Thread.o Poll.o -lpthread -o tcpserver +$GCC tcpserver_main.o -L$LIB_DIR -lCppUtils -lpthread -o tcpserver echo "Linking sslclient_main.o" -$GCC sslclient_main.o Logger.o SocketClient.o TimerUser.o Timer.o TimedTcpConnection.o TcpConnection.o Socket.o AddrInfo.o Connection.o Thread.o Poll.o SslConnection.o -lpthread -lssl -lrt -o sslclient +$GCC sslclient_main.o -L$LIB_DIR -lCppUtils -lpthread -lssl -lrt -o sslclient echo "Linking sslserver_main.o" -$GCC sslserver_main.o Logger.o SocketServer.o TimerUser.o Timer.o TimedTcpConnection.o TcpConnection.o Socket.o AddrInfo.o Connection.o Thread.o Poll.o SslConnection.o -lpthread -lssl -lrt -o sslserver +$GCC sslserver_main.o -L$LIB_DIR -lCppUtils -lpthread -lssl -lrt -o sslserver echo "Linking mysqlclient_main.o" -$GCC mysqlclient_main.o Logger.o ArgParse.o ConditionVariable.o ScopedLock.o MysqlClient.o Mutex.o MysqlConnectionPool.o -lrt -lpthread -L/usr/lib/mysql -lmysqlclient -o mysqlclient +$GCC mysqlclient_main.o -L$LIB_DIR -lCppUtils -lrt -lpthread -L/usr/lib/mysql -lmysqlclient -o mysqlclient diff --git a/other/mysqlclient_main.cpp b/other/mysqlclient_main.cpp index 2977683..1c0c57e 100644 --- a/other/mysqlclient_main.cpp +++ b/other/mysqlclient_main.cpp @@ -1,12 +1,12 @@ // g++ mysqlclient_main.cpp src/Logger.cpp src/MysqlClient.cpp src/ArgParse.cpp -I./include -lmysqlclient -#include "Logger.hpp" -#include "Common.hpp" +#include +#include -#include "ArgParse.hpp" -#include "MysqlClient.hpp" -#include "MysqlConnectionPool.hpp" +#include +#include +#include #include #include diff --git a/other/sslclient_main.cpp b/other/sslclient_main.cpp index 24511ae..152ae0e 100644 --- a/other/sslclient_main.cpp +++ b/other/sslclient_main.cpp @@ -1,10 +1,10 @@ -#include "Logger.hpp" +#include -#include "Message.hpp" -#include "SslConnection.hpp" -#include "SocketClient.hpp" +#include +#include +#include -#include "../test/SimpleMessage.hpp" +#include "../test/cpp_utils/SimpleMessage.hpp" #include @@ -12,7 +12,8 @@ #include #include // nanosleep -#include + +// #include int main(int argc, char* argv[] ) diff --git a/other/sslserver_main.cpp b/other/sslserver_main.cpp index 149bc77..5a7ed9a 100644 --- a/other/sslserver_main.cpp +++ b/other/sslserver_main.cpp @@ -1,11 +1,11 @@ // gpp sslserver_main.cpp -o sslserver -I../include ../src/Logger.cpp ../src/Socket.cpp -ggdb ../src/SocketServer.cpp ../src/Connection.cpp ../src/Poll.cpp ../src/TcpConnection.cpp ../src/SslConnection.cpp -lssl -lcrypto ../src/Addrinfo.cpp -#include "Logger.hpp" -#include "Common.hpp" +#include +#include -#include "Message.hpp" -#include "SslConnection.hpp" -#include "SocketServer.hpp" +#include +#include +#include #include diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index de34472..7d55e02 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -1,9 +1,9 @@ -#include "Logger.hpp" -#include "Common.hpp" +#include +#include -#include "Message.hpp" -#include "TcpConnection.hpp" -#include "SocketClient.hpp" +#include +#include +#include #include diff --git a/other/tcpserver_main.cpp b/other/tcpserver_main.cpp index 3579f13..56adb82 100644 --- a/other/tcpserver_main.cpp +++ b/other/tcpserver_main.cpp @@ -1,9 +1,9 @@ -#include "Logger.hpp" -#include "Common.hpp" +#include +#include -#include "Message.hpp" -#include "TcpConnection.hpp" -#include "SocketServer.hpp" +#include +#include +#include #include