From 8dff446dd8dfca50627178e4bb29d8355e8dadd4 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Sat, 27 Jul 2013 13:38:05 +0200 Subject: [PATCH] TcpConnection checks state at the beginning of each function --- .gitignore | 4 ++-- lib/cpp_utils/MysqlClient.hpp | 1 + lib/cpp_utils/TcpConnection.cpp | 29 ++++++++++++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index c4589ea..fa8396a 100644 --- a/.gitignore +++ b/.gitignore @@ -29,8 +29,8 @@ test/testCppUtils.out html/* # standalone binaries -other/client -other/server +other/tcpclient +other/tcpserver other/sslclient other/sslserver diff --git a/lib/cpp_utils/MysqlClient.hpp b/lib/cpp_utils/MysqlClient.hpp index 540a3ac..d82ca39 100644 --- a/lib/cpp_utils/MysqlClient.hpp +++ b/lib/cpp_utils/MysqlClient.hpp @@ -6,6 +6,7 @@ #include #include +/// @todo shall inherit connection? /// @note Call init/destroy before/after usage class MysqlClient diff --git a/lib/cpp_utils/TcpConnection.cpp b/lib/cpp_utils/TcpConnection.cpp index 2ffa97a..9c944ff 100644 --- a/lib/cpp_utils/TcpConnection.cpp +++ b/lib/cpp_utils/TcpConnection.cpp @@ -49,6 +49,11 @@ bool TcpConnection::connect() { TRACE; + if (m_state == OPEN) { + LOG(Logger::ERR, "Connection is open already."); + return false; + } + AddrInfo addrInfo; if (!addrInfo.getHostInfo(m_host, m_port)) return false; @@ -76,6 +81,11 @@ bool TcpConnection::bind() { TRACE; + if (m_state == OPEN) { + LOG(Logger::ERR, "Connection is open already."); + return false; + } + AddrInfo addrInfo; if (!addrInfo.getHostInfo(m_host, m_port)) return false; @@ -100,6 +110,11 @@ bool TcpConnection::listen( const int maxPendingQueueLen ) { TRACE; + if (m_state == OPEN) { + LOG(Logger::ERR, "Connection is open already."); + return false; + } + if (m_socket.listen(maxPendingQueueLen)) { m_state = OPEN; return true; @@ -112,6 +127,9 @@ bool TcpConnection::listen( const int maxPendingQueueLen ) bool TcpConnection::accept(int& client_socket) { TRACE; + if (m_state == CLOSED) + return false; + return m_socket.accept(client_socket); } @@ -119,6 +137,9 @@ bool TcpConnection::accept(int& client_socket) bool TcpConnection::disconnect() { TRACE; + if (m_state == CLOSED) + return false; + m_state = CLOSED; return m_socket.closeSocket(); } @@ -127,6 +148,9 @@ bool TcpConnection::disconnect() bool TcpConnection::send( const void* message, const size_t length ) { TRACE; + if (m_state == CLOSED) + return false; + return m_socket.send( message, length ); } @@ -135,6 +159,9 @@ bool TcpConnection::receive() { TRACE; + if (m_state == CLOSED) + return false; + ssize_t length; if (!m_socket.receive(m_buffer, m_bufferLength, &length)) return false; @@ -202,7 +229,7 @@ TcpConnection::TcpConnection ( const int socket, , m_message(message) , m_buffer(0) , m_bufferLength(bufferLength) - , m_state(OPEN) + , m_state(OPEN) /// @todo can clone only open ones? { TRACE;