diff --git a/include/Connection.hpp b/include/Connection.hpp index a95c768..20b9fc4 100644 --- a/include/Connection.hpp +++ b/include/Connection.hpp @@ -26,7 +26,7 @@ public: , m_host() , m_port() , m_status(CLOSED) - , m_message(msgParam) + , m_message(this, msgParam) , m_buffer(0) , m_bufferLength(bufferLength) @@ -45,7 +45,7 @@ public: , m_host(host) , m_port(port) , m_status(CLOSED) - , m_message(msgParam) + , m_message(this, msgParam) , m_buffer(0) , m_bufferLength(bufferLength) { @@ -73,6 +73,12 @@ public: return m_socket.bindToHost(m_host, m_port); } + bool listen( const int maxPendingQueueLen = 64 ) + { + TRACE; + return m_socket.listen( maxPendingQueueLen ); + } + void closeConnection() { TRACE; @@ -118,6 +124,12 @@ public: return m_socket.getSocket(); } + std::string getHost() const + { + TRACE; + return m_host; + } + private: diff --git a/include/Message.hpp b/include/Message.hpp index 8aeacda..9f858ed 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -1,6 +1,8 @@ #ifndef MESSAGE_HPP #define MESSAGE_HPP +#include "Connection.hpp" + #include #include // size_t @@ -9,13 +11,17 @@ * getExpectedLength(). */ +template class Message { public: - Message( void * msgParam = 0 ) - : m_buffer() - , m_param(msgParam) {}; + Message( Connection *connection, + void *msgParam = 0 ) + : m_connection(connection) + , m_param(msgParam) + , m_buffer() + {}; virtual ~Message() {}; @@ -27,10 +33,12 @@ protected: virtual size_t getExpectedLength() = 0; - /// @todo shall i use dinamic array? - std::string m_buffer; - void *m_param; + Connection *m_connection; + void *m_param; + + /// @todo shall i use dinamic array? + std::string m_buffer; }; diff --git a/include/Poll.hpp b/include/Poll.hpp index 90a6f4e..66ac5e3 100644 --- a/include/Poll.hpp +++ b/include/Poll.hpp @@ -15,10 +15,8 @@ class Poll { public: - Poll( Connection &connection, - const nfds_t maxClient = 10, - void *MessageParam ) - ) + Poll( Connection *connection, + const nfds_t maxClient = 10 ) : m_connection(connection) , m_polling(false) , m_connectionPool() @@ -28,7 +26,7 @@ public: { TRACE; m_fds = new pollfd[m_maxclients]; - addFd( m_connection.getSocket(), POLLIN | POLLPRI ); + addFd( m_connection->getSocket(), POLLIN | POLLPRI ); } virtual ~Poll() @@ -60,7 +58,7 @@ public: for ( nfds_t i = 0; i < m_num_of_fds; ++i ) if ( m_fds[i].revents != 0 ) - m_fds[i].fd == m_connection.getSocket() ? + m_fds[i].fd == m_connection->getSocket() ? acceptClient(m_fds[i].fd) : handleClient(m_fds[i].fd); @@ -82,7 +80,7 @@ protected: sockaddr clientAddr; socklen_t clientAddrLen; - int client_socket = accept( m_connection.getSocket(), + int client_socket = accept( m_connection->getSocket(), &clientAddr, &clientAddrLen ) ; if ( client_socket == -1 ) { @@ -165,7 +163,7 @@ private: typedef typename std::map< int, Connection* > ConnectionPool; - Connection &m_connection; + Connection *m_connection; bool m_polling; ConnectionPool m_connectionPool; diff --git a/include/Socket.hpp b/include/Socket.hpp index 97f8bb4..a0ab797 100644 --- a/include/Socket.hpp +++ b/include/Socket.hpp @@ -11,21 +11,23 @@ class Socket { public: - Socket(const int domain, - const int type, - const int protocol = 0); + Socket( const int domain, + const int type, + const int protocol = 0 ); - Socket(const int socket ); + Socket( const int socket ); virtual ~Socket(); bool createSocket(); void closeSocket(); - bool connectToHost(const std::string host, - const std::string port); - bool bindToHost(const std::string host, - const std::string port ); + bool connectToHost( const std::string host, + const std::string port ); + bool bindToHost( const std::string host, + const std::string port ); + + bool listen( const int maxPendingQueueLen = 64 ); void getPeerName(std::string &host, std::string &port); diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index 077162d..416ca4e 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -23,7 +23,7 @@ private: { public: PollerThread( TcpClient &data ) - : Poll(data.m_connection) + : Poll( &(data.m_connection) ) , m_tcpClient(data) { TRACE; diff --git a/include/TcpServer.hpp b/include/TcpServer.hpp index 0cec2b0..685d5dc 100644 --- a/include/TcpServer.hpp +++ b/include/TcpServer.hpp @@ -1,39 +1,67 @@ #ifndef TCP_SERVER_HPP #define TCP_SERVER_HPP -#include "Socket.hpp" +#include "Logger.hpp" + +#include "Connection.hpp" #include "Poll.hpp" + + #include -class TcpServer : public Socket - , public Poll +template +class TcpServer { public: TcpServer ( const std::string host, const std::string port, - const int maxClients = 5 ); + const int maxClients = 5, + const int maxPendingQueueLen = 10 ) + : m_connection(host, port) + , m_poll( &m_connection, maxClients) + , m_maxPendingQueueLen(maxPendingQueueLen) + { + TRACE; + } + + virtual ~TcpServer() + { + TRACE; + } + + bool start() + { + TRACE; + + if ( !m_connection.bindToHost() ) + return false; - virtual ~TcpServer(); + if ( m_connection.listen( m_maxPendingQueueLen ) == -1 ) { + return false; + } - bool start(); - void stop(); + m_poll.startPolling(); + return true; + } - // implements Poll::receive - bool receive( const int fd ); + void stop() + { + TRACE; + m_poll.stopPolling(); + m_connection.closeConnection(); + } - virtual void msgArrived(const int clientSocket, - const unsigned char* msg, - const int msgLen ) = 0; private: TcpServer(const TcpServer&); TcpServer& operator=(const TcpServer&); - std::string m_host; - std::string m_port; + Connection m_connection; + Poll m_poll; + const int m_maxPendingQueueLen; }; #endif // TCP_SERVER_HPP diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index bdd541c..8906e4f 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -3,17 +3,21 @@ #include "Logger.hpp" #include "TcpClient.hpp" +#include "Connection.hpp" #include "Message.hpp" + #include #include -class SimpleMessage : public Message + +class SimpleMessage : public Message { public: - SimpleMessage(void* msgParam = 0) - : Message(msgParam) + SimpleMessage( Connection *connection, + void *msgParam = 0) + : Message(connection, msgParam) { TRACE; } diff --git a/other/tcpserver_main.cpp b/other/tcpserver_main.cpp index 1edbbff..ce0c305 100644 --- a/other/tcpserver_main.cpp +++ b/other/tcpserver_main.cpp @@ -1,43 +1,56 @@ // gpp tcpServer_main.cpp -o client -I../include ../src/Logger.cpp ../src/TcpClient.cpp -#include "TcpServer.hpp" - #include "Logger.hpp" #include "Common.hpp" +#include "TcpServer.hpp" +#include "Message.hpp" + #include #include -class EchoTcpServer : public TcpServer +class EchoMessage : public Message { public: - EchoTcpServer ( const std::string host, - const std::string port, - const int maxClients = 5 ) - : TcpServer(host, port, maxClients) + EchoMessage( Connection *connection, + void *msgParam = 0) + : Message(connection, msgParam) { TRACE; } - void msgArrived(const int clientSocket, - const unsigned char*msg, - const int msgLen ) + bool buildMessage( const void *msgPart, + const size_t msgLen ) { TRACE; + m_buffer = std::string( (const char*) msgPart, msgLen ); + onMessageReady(); + return true; + } - std::string message((char*)msg, msgLen); - LOG( Logger::DEBUG, std::string("Got msg: ").append(message).c_str() ); + void onMessageReady() + { + TRACE; - std::string reply("Got your msg, buddy: \""); - reply.append(message).append("\" see you!"); + LOG( Logger::INFO, std::string("Got message: \""). + append(m_buffer).append("\" from: "). + append(m_connection->getHost()).c_str() ); - ssize_t n = write(clientSocket,message.c_str(), message.length()); - if (n == -1) { - LOG( Logger::ERR, errnoToString("ERROR writing to socket. ").c_str() ); - } + std::string reply("Got your message, "); + reply.append(m_connection->getHost()). + append(" \"").append(m_buffer).append("\""); + + m_connection->send( reply.c_str(), reply.length() ); } +protected: + + size_t getExpectedLength() + { + TRACE; + return 0; + } }; @@ -47,7 +60,7 @@ int main( int argc, char * argv[] ) Logger::init(std::cout); Logger::setLogLevel(Logger::FINEST); - EchoTcpServer tcpServer("localhost", "4455"); + TcpServer tcpServer("localhost", "4455"); tcpServer.start(); @@ -55,7 +68,6 @@ int main( int argc, char * argv[] ) sleep(1); tcpServer.stop(); - Logger::destroy(); return 0; } \ No newline at end of file diff --git a/src/Socket.cpp b/src/Socket.cpp index 1f93a7e..0c979c0 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -106,6 +106,18 @@ bool Socket::bindToHost( const std::string host, } +bool Socket::listen ( const int maxPendingQueueLen ) +{ + TRACE; + + if ( ::listen(m_socket, maxPendingQueueLen) == -1 ) { + LOG( Logger::ERR, errnoToString("ERROR listening. ").c_str() ); + return false; + } + return true; +} + + bool Socket::send ( const void *message, const int length ) { TRACE;