new class: TimedTcpConnection. And SslConnection now uses it instead of TcpConnection.

master
Denes Matetelki 12 years ago
parent 6cfd5c2d7f
commit 7ad4ebe136

@ -3,14 +3,10 @@
#include "StreamConnection.hpp" #include "StreamConnection.hpp"
#include "TcpConnection.hpp" #include "TimedTcpConnection.hpp"
#include "Message.hpp"
#include <string> #include <string>
// #include <openssl/rand.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
// #include <openssl/err.h>
@ -22,10 +18,6 @@ public:
static void init(); static void init();
static void destroy(); static void destroy();
SslConnection ( const int socket,
Message *message,
const size_t bufferLength = 1024 );
SslConnection ( const std::string host, SslConnection ( const std::string host,
const std::string port, const std::string port,
Message *message, Message *message,
@ -47,12 +39,17 @@ public:
bool bind(); bool bind();
bool listen( const int maxPendingQueueLen = 64 ); bool listen( const int maxPendingQueueLen = 64 );
int accept(); bool accept(int &client_socket);
bool closed() const;
int getSocket() const; int getSocket() const;
private: private:
SslConnection ( TimedTcpConnection *timedTcpConnection,
Message *message,
const size_t bufferLength = 1024 );
SslConnection(const SslConnection&); SslConnection(const SslConnection&);
SslConnection& operator=(const SslConnection&); SslConnection& operator=(const SslConnection&);
@ -64,7 +61,7 @@ private:
void showCertificates(); void showCertificates();
TcpConnection m_tcpConnection; TimedTcpConnection *m_timedTcpConnection;
Message *m_message; Message *m_message;
unsigned char *m_buffer; unsigned char *m_buffer;
size_t m_bufferLength; size_t m_bufferLength;

@ -0,0 +1,69 @@
#ifndef TIMED_TCP_CONNECTION_HPP
#define TIMED_TCP_CONNECTION_HPP
#include "StreamConnection.hpp"
#include "TimerUser.hpp"
#include "TcpConnection.hpp"
/** @brief Inactivity monitored TCP connection.
*
* The timer is created at:
* - ctor, clone
*
* The timer is restarted after:
* - connect, send, receive
*
* The timer is destroyed at:
* - dtor, disconnect
*/
class TimedTcpConnection : public StreamConnection
, public TimerUser
{
public:
TimedTcpConnection(const std::string host,
const std::string port,
Message *message,
const size_t bufferLength = 1024,
const unsigned long timeOutSec = 30);
virtual ~TimedTcpConnection();
// inherited from TimerUser
virtual void timerExpired();
Connection* clone(const int socket);
/// @todo mention inheritance
bool connect();
bool disconnect();
bool send( const void* message, const size_t length );
bool receive();
int getSocket() const;
bool bind();
bool listen( const int maxPendingQueueLen = 64 );
bool accept(int &client_socket);
bool closed() const;
private:
TimedTcpConnection(TcpConnection *tcpConnection,
const unsigned long timeOutSec = 30);
TimedTcpConnection(const TimedTcpConnection&);
TimedTcpConnection& operator=(const TimedTcpConnection&);
TcpConnection *m_tcpConnection;
unsigned long m_timeOutSec;
};
#endif // TIMED_TCP_CONNECTION_HPP

@ -26,33 +26,12 @@ void SslConnection::destroy()
} }
SslConnection::SslConnection ( const int socket,
Message *message,
const size_t bufferLength )
: StreamConnection("invalid", "invalid")
, m_tcpConnection(socket, message, 0)
, m_message(message)
, m_buffer(0)
, m_bufferLength(bufferLength)
, m_sslHandle(0)
, m_sslContext(0)
{
TRACE;
setHost(m_tcpConnection.getHost());
setPort(m_tcpConnection.getPort());
m_buffer = new unsigned char[m_bufferLength];
m_message->setConnection(this);
}
SslConnection::SslConnection ( const std::string host, SslConnection::SslConnection ( const std::string host,
const std::string port, const std::string port,
Message *message, Message *message,
const size_t bufferLength ) const size_t bufferLength )
: StreamConnection(host, port) : StreamConnection(host, port)
, m_tcpConnection(host, port, message, 0) , m_timedTcpConnection(new TimedTcpConnection(host, port, message, 0))
, m_message(message) , m_message(message)
, m_buffer(0) , m_buffer(0)
, m_bufferLength(bufferLength) , m_bufferLength(bufferLength)
@ -70,6 +49,7 @@ SslConnection::~SslConnection()
TRACE; TRACE;
disconnect(); disconnect();
delete m_buffer; delete m_buffer;
delete m_timedTcpConnection;
} }
@ -77,9 +57,14 @@ Connection* SslConnection::clone(const int socket)
{ {
TRACE; TRACE;
SslConnection *conn = new SslConnection( socket, m_message->clone(), m_bufferLength ); Connection* conn = m_timedTcpConnection->clone(socket);
conn->setHandle(m_sslHandle);
return conn; SslConnection *sslConn = new SslConnection(
dynamic_cast<TimedTcpConnection*>(conn),
m_message->clone(),
m_bufferLength);
sslConn->setHandle(m_sslHandle);
return sslConn;
} }
@ -87,10 +72,10 @@ bool SslConnection::connect()
{ {
TRACE; TRACE;
if ( !m_tcpConnection.connect() ) if ( !m_timedTcpConnection->connect() )
return false; return false;
if ( SSL_set_fd(m_sslHandle, m_tcpConnection.getSocket() ) == 0 ) { if ( SSL_set_fd(m_sslHandle, m_timedTcpConnection->getSocket() ) == 0 ) {
LOG( Logger::ERR, getSslError("SSL set connection socket failed. ").c_str() ); LOG( Logger::ERR, getSslError("SSL set connection socket failed. ").c_str() );
return -1; return -1;
} }
@ -110,36 +95,35 @@ bool SslConnection::bind()
{ {
TRACE; TRACE;
return m_tcpConnection.bind(); return m_timedTcpConnection->bind();
} }
bool SslConnection::listen( const int maxPendingQueueLen ) bool SslConnection::listen( const int maxPendingQueueLen )
{ {
TRACE; TRACE;
return m_tcpConnection.listen(maxPendingQueueLen); return m_timedTcpConnection->listen(maxPendingQueueLen);
} }
int SslConnection::accept() bool SslConnection::accept(int &client_socket)
{ {
TRACE; TRACE;
int client_socket = m_tcpConnection.accept(); if(!m_timedTcpConnection->accept(client_socket))
if ( client_socket == -1) return false;
return client_socket;
if ( SSL_set_fd(m_sslHandle, client_socket) == 0 ) { if ( SSL_set_fd(m_sslHandle, client_socket) == 0 ) {
LOG( Logger::ERR, getSslError("SSL set connection socket failed. ").c_str() ); LOG( Logger::ERR, getSslError("SSL set connection socket failed. ").c_str() );
return -1; return false;
} }
if ( SSL_accept(m_sslHandle) == -1 ) { if ( SSL_accept(m_sslHandle) == -1 ) {
LOG( Logger::ERR, getSslError("SSL accept failed. ").c_str() ); LOG( Logger::ERR, getSslError("SSL accept failed. ").c_str() );
return -1; return false;
} }
return client_socket; return true;
} }
/// @todo this function shall be refactored /// @todo this function shall be refactored
@ -148,8 +132,8 @@ bool SslConnection::disconnect()
TRACE; TRACE;
/// @note do I have to call this? /// @note do I have to call this?
if ( m_tcpConnection.getSocket() != -1 ) if ( m_timedTcpConnection->getSocket() != -1 )
m_tcpConnection.disconnect(); m_timedTcpConnection->disconnect();
if ( m_sslHandle == 0 || m_sslContext == 0 ) if ( m_sslHandle == 0 || m_sslContext == 0 )
return false; return false;
@ -253,10 +237,38 @@ bool SslConnection::receive()
} }
bool SslConnection::closed() const
{
TRACE;
return m_timedTcpConnection->closed();
}
int SslConnection::getSocket() const int SslConnection::getSocket() const
{ {
TRACE; TRACE;
return m_tcpConnection.getSocket(); return m_timedTcpConnection->getSocket();
}
SslConnection::SslConnection(TimedTcpConnection *timedTcpConnection,
Message *message,
const size_t bufferLength)
: StreamConnection("invalid", "invalid")
, m_timedTcpConnection(timedTcpConnection)
, m_message(message)
, m_buffer(0)
, m_bufferLength(bufferLength)
, m_sslHandle(0)
, m_sslContext(0)
{
TRACE;
setHost(m_timedTcpConnection->getHost());
setPort(m_timedTcpConnection->getPort());
m_buffer = new unsigned char[m_bufferLength];
m_message->setConnection(this);
} }

@ -0,0 +1,128 @@
#include "TimedTcpConnection.hpp"
#include "Logger.hpp"
TimedTcpConnection::TimedTcpConnection(const std::string host,
const std::string port,
Message *message,
const size_t bufferLength,
const unsigned long timeOutSec)
: StreamConnection(host, port)
, TimerUser()
, m_tcpConnection(new TcpConnection(host, port, message, bufferLength))
, m_timeOutSec(timeOutSec)
{
TRACE;
}
TimedTcpConnection::~TimedTcpConnection()
{
TRACE;
delete m_tcpConnection;
}
void TimedTcpConnection::timerExpired()
{
TRACE;
m_tcpConnection->setState(TcpConnection::CLOSED);
}
Connection* TimedTcpConnection::clone(const int socket)
{
Connection *conn = m_tcpConnection->clone(socket);
TimedTcpConnection *timedTcpConnection = new TimedTcpConnection(
dynamic_cast<TcpConnection*>(conn),
m_timeOutSec);
return timedTcpConnection;
}
bool TimedTcpConnection::connect()
{
TRACE;
startTimer(m_timeOutSec);
return m_tcpConnection->connect();
}
bool TimedTcpConnection::bind()
{
TRACE;
return m_tcpConnection->bind();
}
bool TimedTcpConnection::listen( const int maxPendingQueueLen )
{
TRACE;
return m_tcpConnection->listen(maxPendingQueueLen);
}
bool TimedTcpConnection::accept(int &client_socket)
{
TRACE;
return m_tcpConnection->accept(client_socket);
}
bool TimedTcpConnection::disconnect()
{
TRACE;
stopTimer();
return m_tcpConnection->disconnect();
}
bool TimedTcpConnection::send(const void* message, const size_t length)
{
TRACE;
startTimer(m_timeOutSec);
return m_tcpConnection->send(message, length);
}
bool TimedTcpConnection::receive()
{
TRACE;
startTimer(m_timeOutSec);
return m_tcpConnection->receive();
}
int TimedTcpConnection::getSocket() const
{
TRACE;
return m_tcpConnection->getSocket();
}
bool TimedTcpConnection::closed() const
{
TRACE;
return m_tcpConnection->closed();
}
TimedTcpConnection::TimedTcpConnection(TcpConnection *tcpConnection,
const unsigned long timeOutSec)
: StreamConnection("invalid", "invalid")
, TimerUser()
, m_tcpConnection(tcpConnection)
, m_timeOutSec(timeOutSec)
{
TRACE;
setHost(m_tcpConnection->getHost());
setPort(m_tcpConnection->getPort());
}
Loading…
Cancel
Save