TcpConnection checks state at the beginning of each function

master
Denes Matetelki 12 years ago
parent 91a1193543
commit 8dff446dd8

4
.gitignore vendored

@ -29,8 +29,8 @@ test/testCppUtils.out
html/* html/*
# standalone binaries # standalone binaries
other/client other/tcpclient
other/server other/tcpserver
other/sslclient other/sslclient
other/sslserver other/sslserver

@ -6,6 +6,7 @@
#include <string> #include <string>
#include <list> #include <list>
/// @todo shall inherit connection?
/// @note Call init/destroy before/after usage /// @note Call init/destroy before/after usage
class MysqlClient class MysqlClient

@ -49,6 +49,11 @@ bool TcpConnection::connect()
{ {
TRACE; TRACE;
if (m_state == OPEN) {
LOG(Logger::ERR, "Connection is open already.");
return false;
}
AddrInfo addrInfo; AddrInfo addrInfo;
if (!addrInfo.getHostInfo(m_host, m_port)) if (!addrInfo.getHostInfo(m_host, m_port))
return false; return false;
@ -76,6 +81,11 @@ bool TcpConnection::bind()
{ {
TRACE; TRACE;
if (m_state == OPEN) {
LOG(Logger::ERR, "Connection is open already.");
return false;
}
AddrInfo addrInfo; AddrInfo addrInfo;
if (!addrInfo.getHostInfo(m_host, m_port)) if (!addrInfo.getHostInfo(m_host, m_port))
return false; return false;
@ -100,6 +110,11 @@ bool TcpConnection::listen( const int maxPendingQueueLen )
{ {
TRACE; TRACE;
if (m_state == OPEN) {
LOG(Logger::ERR, "Connection is open already.");
return false;
}
if (m_socket.listen(maxPendingQueueLen)) { if (m_socket.listen(maxPendingQueueLen)) {
m_state = OPEN; m_state = OPEN;
return true; return true;
@ -112,6 +127,9 @@ bool TcpConnection::listen( const int maxPendingQueueLen )
bool TcpConnection::accept(int& client_socket) bool TcpConnection::accept(int& client_socket)
{ {
TRACE; TRACE;
if (m_state == CLOSED)
return false;
return m_socket.accept(client_socket); return m_socket.accept(client_socket);
} }
@ -119,6 +137,9 @@ bool TcpConnection::accept(int& client_socket)
bool TcpConnection::disconnect() bool TcpConnection::disconnect()
{ {
TRACE; TRACE;
if (m_state == CLOSED)
return false;
m_state = CLOSED; m_state = CLOSED;
return m_socket.closeSocket(); return m_socket.closeSocket();
} }
@ -127,6 +148,9 @@ bool TcpConnection::disconnect()
bool TcpConnection::send( const void* message, const size_t length ) bool TcpConnection::send( const void* message, const size_t length )
{ {
TRACE; TRACE;
if (m_state == CLOSED)
return false;
return m_socket.send( message, length ); return m_socket.send( message, length );
} }
@ -135,6 +159,9 @@ bool TcpConnection::receive()
{ {
TRACE; TRACE;
if (m_state == CLOSED)
return false;
ssize_t length; ssize_t length;
if (!m_socket.receive(m_buffer, m_bufferLength, &length)) if (!m_socket.receive(m_buffer, m_bufferLength, &length))
return false; return false;
@ -202,7 +229,7 @@ TcpConnection::TcpConnection ( const int socket,
, m_message(message) , m_message(message)
, m_buffer(0) , m_buffer(0)
, m_bufferLength(bufferLength) , m_bufferLength(bufferLength)
, m_state(OPEN) , m_state(OPEN) /// @todo can clone only open ones?
{ {
TRACE; TRACE;

Loading…
Cancel
Save