parent
5f5d376b4b
commit
cff3093cad
@ -0,0 +1,23 @@
|
||||
#ifndef MESSAGE_BUILDER_HPP
|
||||
#define MESSAGE_BUILDER_HPP
|
||||
|
||||
class MessageBuilder
|
||||
{
|
||||
public:
|
||||
|
||||
bool buildMessage( const unsigned char* message, unsigned int length ) ;
|
||||
|
||||
protected:
|
||||
|
||||
MessageBuilder( const int bufferLength ) ;
|
||||
virtual ~MessageBuilder() ;
|
||||
virtual void onMessageReady( const unsigned char* message, unsigned int length ) = 0 ;
|
||||
|
||||
private:
|
||||
|
||||
unsigned char *m_buffer;
|
||||
int m_bufferLength;
|
||||
int m_bufferUsed;
|
||||
};
|
||||
|
||||
#endif // MESSAGE_BUILDER_HPP
|
@ -0,0 +1,32 @@
|
||||
#ifndef MESSAGE_BUILDER_HPP
|
||||
#define MESSAGE_BUILDER_HPP
|
||||
|
||||
class TcpConnection;
|
||||
|
||||
|
||||
class MessageBuilder
|
||||
{
|
||||
public:
|
||||
|
||||
MessageBuilder( TcpConnection *connection,
|
||||
const int bufferLength = 1024 );
|
||||
|
||||
virtual ~MessageBuilder();
|
||||
|
||||
bool buildMessage( const unsigned char* message,
|
||||
const int length ) ;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
MessageBuilder(const MessageBuilder&);
|
||||
MessageBuilder& operator=(const MessageBuilder&);
|
||||
|
||||
|
||||
TcpConnection *m_connection;
|
||||
unsigned char *m_buffer;
|
||||
int m_bufferLength;
|
||||
int m_bufferUsed;
|
||||
};
|
||||
|
||||
#endif // MESSAGE_BUILDER_HPP
|
@ -0,0 +1,58 @@
|
||||
#ifndef TCP_CONNECTION_HPP
|
||||
#define TCP_CONNECTION_HPP
|
||||
|
||||
#include "MessageBuilder.hpp"
|
||||
#include "Socket.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
class TcpConnection
|
||||
{
|
||||
public:
|
||||
|
||||
enum Status {
|
||||
OPENED,
|
||||
CLOSED
|
||||
};
|
||||
|
||||
TcpConnection ( const int socket,
|
||||
const MessageBuilder *m_builder,
|
||||
const int bufferLenght = 1024 );
|
||||
|
||||
TcpConnection ( const std::string host,
|
||||
const std::string port,
|
||||
MessageBuilder *builder,
|
||||
const int bufferLenght = 1024 );
|
||||
|
||||
virtual ~TcpConnection();
|
||||
|
||||
bool connectToHost();
|
||||
bool bindToHost();
|
||||
|
||||
void closeConnection();
|
||||
|
||||
bool sendMessage( const void* message, const int length );
|
||||
bool readFromSocket();
|
||||
virtual void onMessageReady ( const unsigned char * message, const int length ) = 0;
|
||||
|
||||
|
||||
int getSocket() const;
|
||||
|
||||
|
||||
private:
|
||||
|
||||
TcpConnection(const TcpConnection&);
|
||||
TcpConnection& operator=(const TcpConnection&);
|
||||
|
||||
Socket m_socket;
|
||||
std::string m_host;
|
||||
std::string m_port;
|
||||
MessageBuilder *m_builder;
|
||||
Status m_status;
|
||||
|
||||
unsigned char *m_buffer;
|
||||
int m_bufferLength;
|
||||
};
|
||||
|
||||
|
||||
#endif // TCP_CONNECTION_HPP
|
@ -0,0 +1,41 @@
|
||||
#include "MessageBuilder.hpp"
|
||||
|
||||
#include "Logger.hpp"
|
||||
|
||||
#include <string.h> // memcpy
|
||||
|
||||
|
||||
|
||||
MessageBuilder::MessageBuilder( TcpConnection *connection,
|
||||
const int bufferLength )
|
||||
: m_connection(connection)
|
||||
, m_buffer(0)
|
||||
, m_bufferLength(bufferLength)
|
||||
, m_bufferUsed(0)
|
||||
{
|
||||
TRACE;
|
||||
|
||||
m_buffer = new unsigned char[bufferLength];
|
||||
}
|
||||
|
||||
|
||||
MessageBuilder::~MessageBuilder()
|
||||
{
|
||||
TRACE;
|
||||
|
||||
delete [] m_buffer ;
|
||||
}
|
||||
|
||||
|
||||
bool MessageBuilder::buildMessage( const unsigned char *message,
|
||||
const int length )
|
||||
{
|
||||
TRACE;
|
||||
|
||||
/// @todo implement composing the message
|
||||
memcpy(message, m_buffer, length );
|
||||
m_bufferUsed = length;
|
||||
|
||||
m_connection->onMessageReady ( m_buffer, m_bufferUsed );
|
||||
return true;
|
||||
}
|
@ -0,0 +1,120 @@
|
||||
#include "TcpConnection.hpp"
|
||||
|
||||
#include "Logger.hpp"
|
||||
#include "Common.hpp"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
|
||||
TcpConnection::TcpConnection ( const int socket,
|
||||
const int bufferLenght,
|
||||
MessageBuilder* builder
|
||||
)
|
||||
: m_socket(socket)
|
||||
, m_host()
|
||||
, m_port()
|
||||
, m_status(CLOSED)
|
||||
, m_builder(builder)
|
||||
, m_buffer(0)
|
||||
, m_bufferLength(bufferLenght)
|
||||
|
||||
{
|
||||
TRACE;
|
||||
|
||||
m_buffer = new unsigned char[m_bufferLength];
|
||||
m_socket.getPeerName(m_host, m_port);
|
||||
}
|
||||
|
||||
|
||||
TcpConnection::TcpConnection ( const std::string host,
|
||||
const std::string port,
|
||||
const int bufferLenght,
|
||||
MessageBuilder* builder )
|
||||
: m_socket(AF_INET, SOCK_STREAM)
|
||||
, m_host(host)
|
||||
, m_port(port)
|
||||
, m_status(CLOSED)
|
||||
, m_builder(builder)
|
||||
, m_buffer(0)
|
||||
, m_bufferLength(bufferLenght)
|
||||
{
|
||||
TRACE;
|
||||
|
||||
m_buffer = new unsigned char[m_bufferLength];
|
||||
m_socket.createSocket();
|
||||
}
|
||||
|
||||
|
||||
TcpConnection::~TcpConnection()
|
||||
{
|
||||
TRACE;
|
||||
|
||||
delete[] m_buffer;
|
||||
m_socket.closeSocket();
|
||||
}
|
||||
|
||||
|
||||
bool TcpConnection::connectToHost()
|
||||
{
|
||||
TRACE;
|
||||
|
||||
return m_socket.connectToHost(m_host, m_port);
|
||||
}
|
||||
|
||||
|
||||
bool TcpConnection::bindToHost()
|
||||
{
|
||||
TRACE;
|
||||
|
||||
return m_socket.bindToHost(m_host, m_port);
|
||||
}
|
||||
|
||||
|
||||
void TcpConnection::closeConnection()
|
||||
{
|
||||
TRACE;
|
||||
|
||||
m_socket.closeSocket();
|
||||
}
|
||||
|
||||
|
||||
bool TcpConnection::sendMessage( const void* message, const int length )
|
||||
{
|
||||
TRACE;
|
||||
|
||||
return m_socket.send( message, length );
|
||||
}
|
||||
|
||||
|
||||
int& TcpConnection::getSocket() const
|
||||
{
|
||||
TRACE;
|
||||
|
||||
return m_socket.getSocket();
|
||||
}
|
||||
|
||||
|
||||
bool TcpConnection::readFromSocket()
|
||||
{
|
||||
TRACE;
|
||||
|
||||
int len = recv(m_socket, m_buffer, m_bufferLength, 0);
|
||||
|
||||
if (len == -1) {
|
||||
LOG( Logger::ERR, errnoToString("ERROR reading from socket. ").c_str() );
|
||||
return false;
|
||||
}
|
||||
|
||||
if (len == 0) {
|
||||
LOG( Logger::DEBUG, "Connection closed by peer." );
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( !m_builder ) {
|
||||
onMessageReady(m_buffer, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
return m_builder->buildMessage(m_buffer, len);
|
||||
}
|
Loading…
Reference in new issue