parent
6cfd5c2d7f
commit
7ad4ebe136
@ -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
|
@ -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…
Reference in new issue