TcpClient passes msgParam to Connection, which passes it to the Message

master
Denes Matetelki 13 years ago
parent 8816e1989f
commit e4b28fcdbe

@ -19,13 +19,14 @@ public:
CLOSED CLOSED
}; };
Connection ( const int socket, Connection ( const int socket,
const size_t bufferLength = 1024 ) void *msgParam = 0,
const size_t bufferLength = 1024 )
: m_socket(socket) : m_socket(socket)
, m_host() , m_host()
, m_port() , m_port()
, m_status(CLOSED) , m_status(CLOSED)
, m_message() , m_message(msgParam)
, m_buffer(0) , m_buffer(0)
, m_bufferLength(bufferLength) , m_bufferLength(bufferLength)
@ -36,14 +37,15 @@ public:
m_buffer = new unsigned char[m_bufferLength]; m_buffer = new unsigned char[m_bufferLength];
} }
Connection ( const std::string host, Connection ( const std::string host,
const std::string port, const std::string port,
const size_t bufferLength = 1024 ) void *msgParam = 0,
const size_t bufferLength = 1024 )
: m_socket(AF_INET, SOCK_STREAM) : m_socket(AF_INET, SOCK_STREAM)
, m_host(host) , m_host(host)
, m_port(port) , m_port(port)
, m_status(CLOSED) , m_status(CLOSED)
, m_message() , m_message(msgParam)
, m_buffer(0) , m_buffer(0)
, m_bufferLength(bufferLength) , m_bufferLength(bufferLength)
{ {

@ -13,7 +13,10 @@ class Message
{ {
public: public:
Message() : m_buffer() {}; Message( void * msgParam = 0 )
: m_buffer()
, m_param(msgParam) {};
virtual ~Message() {}; virtual ~Message() {};
virtual bool buildMessage( const void *msgPart, virtual bool buildMessage( const void *msgPart,
@ -25,7 +28,8 @@ protected:
virtual size_t getExpectedLength() = 0; virtual size_t getExpectedLength() = 0;
/// @todo shall i use dinamic array? /// @todo shall i use dinamic array?
std::string m_buffer; std::string m_buffer;
void *m_param;
}; };

@ -76,9 +76,10 @@ private:
public: public:
TcpClient ( const std::string host, TcpClient ( const std::string host,
const std::string port ) const std::string port,
: m_connection (host, port) void *msgParam = 0 )
: m_connection (host, port, msgParam)
, m_watcher(*this) , m_watcher(*this)
{ {
TRACE; TRACE;

@ -12,6 +12,12 @@ class SimpleMessage : public Message
{ {
public: public:
SimpleMessage(void* msgParam = 0)
: Message(msgParam)
{
TRACE;
}
bool buildMessage( const void *msgPart, bool buildMessage( const void *msgPart,
const size_t msgLen ) const size_t msgLen )
{ {
@ -45,6 +51,9 @@ int main( int argc, char * argv[] )
Logger::init(std::cout); Logger::init(std::cout);
Logger::setLogLevel(Logger::FINEST); Logger::setLogLevel(Logger::FINEST);
int *a = new int;
*a=2;
TcpClient<SimpleMessage> tcpclient("localhost", "4455"); TcpClient<SimpleMessage> tcpclient("localhost", "4455");
tcpclient.connect(); tcpclient.connect();
@ -60,6 +69,8 @@ int main( int argc, char * argv[] )
tcpclient.disconnect(); tcpclient.disconnect();
delete a;
Logger::destroy(); Logger::destroy();
return 0; return 0;
} }

@ -1,120 +0,0 @@
#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);
}

@ -1,41 +0,0 @@
#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;
}

@ -1,118 +0,0 @@
#include "TcpClient.hpp"
#include "Logger.hpp"
#include "Common.hpp"
TcpClient::TcpClient( const std::string host,
const std::string port )
: m_connection (host, port)
, m_watcher(*this)
{
TRACE;
}
TcpClient::~TcpClient()
{
TRACE;
disconnect();
}
bool TcpClient::connect()
{
TRACE;
if ( !m_connection.connectToHost() )
return false;
m_watcher.setOwnSocket(m_connection.getSocket());
m_watcher.start();
return true;
}
void TcpClient::disconnect()
{
TRACE;
m_connection.closeConnection();
if ( m_watcher.isRunning() ) {
m_watcher.stopPolling();
m_watcher.stop();
m_watcher.join();
}
}
bool TcpClient::send( const void* message, const int length )
{
TRACE;
return m_connection.sendMessage(message, length);
}
// WatcherThread
TcpClient::WatcherThread::WatcherThread( TcpClient &data )
: Poll(data.m_connection.getSocket())
, m_tcpClient(data)
{
TRACE;
}
void TcpClient::WatcherThread::acceptClient()
{
TRACE;
// not accepting anything
// receive( m_tcpClient.m_socket );
}
void TcpClient::WatcherThread::handleClient( const int fd )
{
TRACE;
LOG( Logger::DEBUG, "Server closed the connection." );
stopPolling();
}
// bool TcpClient::WatcherThread::receive( const int fd)
// {
// TRACE;
// char buffer[14];
// int len = recv( fd, buffer , 14, 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." );
// stopPolling();
// return false;
// }
//
// std::string msg(buffer, len);
// m_tcpClient.msgArrived(msg);
/*
return true;
}*/
void* TcpClient::WatcherThread::run()
{
TRACE;
startPolling();
return 0;
}
Loading…
Cancel
Save