You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
3.0 KiB
158 lines
3.0 KiB
#ifndef CONNECTION_HPP
|
|
#define CONNECTION_HPP
|
|
|
|
#include "Logger.hpp"
|
|
#include "Common.hpp"
|
|
|
|
#include "Socket.hpp"
|
|
|
|
#include <string>
|
|
|
|
|
|
template <typename T>
|
|
class Connection
|
|
{
|
|
public:
|
|
|
|
enum Status {
|
|
OPENED,
|
|
CLOSED
|
|
};
|
|
|
|
Connection ( const int socket,
|
|
void *msgParam = 0,
|
|
const size_t bufferLength = 1024 )
|
|
: m_socket(socket)
|
|
, m_host()
|
|
, m_port()
|
|
, m_status(CLOSED)
|
|
, m_message(this, msgParam)
|
|
, m_buffer(0)
|
|
, m_bufferLength(bufferLength)
|
|
|
|
{
|
|
TRACE;
|
|
|
|
m_socket.getPeerName(m_host, m_port);
|
|
m_buffer = new unsigned char[m_bufferLength];
|
|
}
|
|
|
|
Connection ( const std::string host,
|
|
const std::string port,
|
|
void *msgParam = 0,
|
|
const size_t bufferLength = 1024 )
|
|
: m_socket(AF_INET, SOCK_STREAM)
|
|
, m_host(host)
|
|
, m_port(port)
|
|
, m_status(CLOSED)
|
|
, m_message(this, msgParam)
|
|
, m_buffer(0)
|
|
, m_bufferLength(bufferLength)
|
|
{
|
|
TRACE;
|
|
m_socket.createSocket();
|
|
m_buffer = new unsigned char[m_bufferLength];
|
|
}
|
|
|
|
virtual ~Connection()
|
|
{
|
|
TRACE;
|
|
m_socket.closeSocket();
|
|
delete[] m_buffer;
|
|
}
|
|
|
|
bool connectToHost()
|
|
{
|
|
TRACE;
|
|
return m_socket.connectToHost(m_host, m_port);
|
|
}
|
|
|
|
bool bindToHost()
|
|
{
|
|
TRACE;
|
|
return m_socket.bindToHost(m_host, m_port);
|
|
}
|
|
|
|
bool listen( const int maxPendingQueueLen = 64 )
|
|
{
|
|
TRACE;
|
|
return m_socket.listen( maxPendingQueueLen );
|
|
}
|
|
|
|
void closeConnection()
|
|
{
|
|
TRACE;
|
|
m_socket.closeSocket();
|
|
}
|
|
|
|
bool send( const void* message, const size_t length )
|
|
{
|
|
TRACE;
|
|
return m_socket.send( message, length );
|
|
}
|
|
|
|
bool receive()
|
|
{
|
|
TRACE;
|
|
|
|
// LOG ( Logger::DEBUG, std::string("receving on socket: ").
|
|
// append(TToStr(m_socket.getSocket())).c_str() );
|
|
|
|
ssize_t len = recv(m_socket.getSocket(), m_buffer, m_bufferLength, 0);
|
|
|
|
LOG ( Logger::DEBUG, std::string("Received: ").
|
|
append(TToStr(len)).append(" bytes from: ").
|
|
append(m_host).append(":").append(m_port).c_str() );
|
|
|
|
if (len == -1) {
|
|
LOG( Logger::ERR, errnoToString("ERROR reading from socket. ").c_str() );
|
|
return false;
|
|
}
|
|
|
|
if (len == 0) {
|
|
LOG( Logger::INFO, std::string("Connection closed by ").
|
|
append(m_host).append(":").append(m_port).c_str() );
|
|
return false;
|
|
}
|
|
|
|
return m_message.buildMessage( (void*)m_buffer, (size_t)len);
|
|
}
|
|
|
|
|
|
int getSocket() const
|
|
{
|
|
TRACE;
|
|
return m_socket.getSocket();
|
|
}
|
|
|
|
std::string getHost() const
|
|
{
|
|
TRACE;
|
|
return m_host;
|
|
}
|
|
|
|
std::string getPort() const
|
|
{
|
|
TRACE;
|
|
return m_port;
|
|
}
|
|
|
|
|
|
private:
|
|
|
|
Connection(const Connection&);
|
|
Connection& operator=(const Connection&);
|
|
|
|
Socket m_socket;
|
|
std::string m_host;
|
|
std::string m_port;
|
|
Status m_status;
|
|
T m_message;
|
|
|
|
unsigned char *m_buffer;
|
|
size_t m_bufferLength;
|
|
};
|
|
|
|
|
|
#endif // CONNECTION_HPP
|