From e4b28fcdbe815fee7dd6dd60e61fbba1dfa6fd46 Mon Sep 17 00:00:00 2001 From: Denes Matetelki Date: Wed, 16 Nov 2011 13:20:22 +0100 Subject: [PATCH] TcpClient passes msgParam to Connection, which passes it to the Message --- include/Connection.hpp | 16 +++--- include/Message.hpp | 8 ++- include/TcpClient.hpp | 7 ++- other/tcpclient_main.cpp | 11 ++++ src/Connection.cpp | 120 --------------------------------------- src/MessageBuilder.cpp | 41 ------------- src/TcpClient.cpp | 118 -------------------------------------- 7 files changed, 30 insertions(+), 291 deletions(-) delete mode 100644 src/Connection.cpp delete mode 100644 src/MessageBuilder.cpp delete mode 100644 src/TcpClient.cpp diff --git a/include/Connection.hpp b/include/Connection.hpp index d544bf2..a95c768 100644 --- a/include/Connection.hpp +++ b/include/Connection.hpp @@ -19,13 +19,14 @@ public: CLOSED }; - Connection ( const int socket, - const size_t bufferLength = 1024 ) + Connection ( const int socket, + void *msgParam = 0, + const size_t bufferLength = 1024 ) : m_socket(socket) , m_host() , m_port() , m_status(CLOSED) - , m_message() + , m_message(msgParam) , m_buffer(0) , m_bufferLength(bufferLength) @@ -36,14 +37,15 @@ public: m_buffer = new unsigned char[m_bufferLength]; } - Connection ( const std::string host, - const std::string port, - const size_t bufferLength = 1024 ) + 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() + , m_message(msgParam) , m_buffer(0) , m_bufferLength(bufferLength) { diff --git a/include/Message.hpp b/include/Message.hpp index 54114fd..8aeacda 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -13,7 +13,10 @@ class Message { public: - Message() : m_buffer() {}; + Message( void * msgParam = 0 ) + : m_buffer() + , m_param(msgParam) {}; + virtual ~Message() {}; virtual bool buildMessage( const void *msgPart, @@ -25,7 +28,8 @@ protected: virtual size_t getExpectedLength() = 0; /// @todo shall i use dinamic array? - std::string m_buffer; + std::string m_buffer; + void *m_param; }; diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index 7c6a21d..077162d 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -76,9 +76,10 @@ private: public: - TcpClient ( const std::string host, - const std::string port ) - : m_connection (host, port) + TcpClient ( const std::string host, + const std::string port, + void *msgParam = 0 ) + : m_connection (host, port, msgParam) , m_watcher(*this) { TRACE; diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index 59fe7bf..bdd541c 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -12,6 +12,12 @@ class SimpleMessage : public Message { public: + SimpleMessage(void* msgParam = 0) + : Message(msgParam) + { + TRACE; + } + bool buildMessage( const void *msgPart, const size_t msgLen ) { @@ -45,6 +51,9 @@ int main( int argc, char * argv[] ) Logger::init(std::cout); Logger::setLogLevel(Logger::FINEST); + int *a = new int; + *a=2; + TcpClient tcpclient("localhost", "4455"); tcpclient.connect(); @@ -60,6 +69,8 @@ int main( int argc, char * argv[] ) tcpclient.disconnect(); + delete a; + Logger::destroy(); return 0; } \ No newline at end of file diff --git a/src/Connection.cpp b/src/Connection.cpp deleted file mode 100644 index 71e121d..0000000 --- a/src/Connection.cpp +++ /dev/null @@ -1,120 +0,0 @@ -#include "TcpConnection.hpp" - -#include "Logger.hpp" -#include "Common.hpp" - -#include -#include - - -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); -} diff --git a/src/MessageBuilder.cpp b/src/MessageBuilder.cpp deleted file mode 100644 index 5bdbb97..0000000 --- a/src/MessageBuilder.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "MessageBuilder.hpp" - -#include "Logger.hpp" - -#include // 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; -} diff --git a/src/TcpClient.cpp b/src/TcpClient.cpp deleted file mode 100644 index 6118bb4..0000000 --- a/src/TcpClient.cpp +++ /dev/null @@ -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; -}