diff --git a/include/Connection.hpp b/include/Connection.hpp index 20b9fc4..22d9952 100644 --- a/include/Connection.hpp +++ b/include/Connection.hpp @@ -95,14 +95,14 @@ public: { TRACE; - LOG ( Logger::DEBUG, std::string("receving on socket: "). - append(TToStr(m_socket.getSocket())).c_str() ); +// 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("len: "). - append(TToStr(len)).append(" errno: "). - append(TToStr(errno)).c_str() ); + 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() ); @@ -130,6 +130,12 @@ public: return m_host; } + std::string getPort() const + { + TRACE; + return m_port; + } + private: diff --git a/include/Logger.hpp b/include/Logger.hpp index 907f639..77fd53f 100644 --- a/include/Logger.hpp +++ b/include/Logger.hpp @@ -78,7 +78,7 @@ private: else (void)0 #define LOG(level, msg) \ - if ( Logger::getInstance()->getLoglevel() >= Logger::FINEST ) \ + if ( Logger::getInstance()->getLoglevel() >= level ) \ Logger::getInstance()->log_string( \ level, msg, __FILE__, __LINE__, __PRETTY_FUNCTION__); \ else (void)0 diff --git a/include/Message.hpp b/include/Message.hpp index 9f858ed..3f280f1 100644 --- a/include/Message.hpp +++ b/include/Message.hpp @@ -39,6 +39,11 @@ protected: /// @todo shall i use dinamic array? std::string m_buffer; + +private: + + Message(const Message &); + Message& operator=(const Message &); }; diff --git a/include/Poll.hpp b/include/Poll.hpp index 66ac5e3..8a0ed55 100644 --- a/include/Poll.hpp +++ b/include/Poll.hpp @@ -59,7 +59,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() ? - acceptClient(m_fds[i].fd) : + acceptClient() : handleClient(m_fds[i].fd); } // while @@ -74,7 +74,7 @@ public: protected: - virtual void acceptClient( const int socket ) + virtual void acceptClient() { TRACE; @@ -85,19 +85,17 @@ protected: if ( client_socket == -1 ) { LOG( Logger::ERR, errnoToString("ERROR accepting. ").c_str() ); - } else { - - std::string clientAddress, clientService; - if ( Socket::convertNameInfo(&clientAddr, clientAddrLen, - clientAddress, clientService ) ) { - LOG( Logger::DEBUG, std::string("New client connected: "). - append(clientAddress).append(":"). - append(clientService).c_str() ); - } - - m_connectionPool[client_socket] = new Connection(client_socket); - addFd( client_socket, POLLIN | POLLPRI ); + return; } + + Connection *connection = new Connection(client_socket); + + LOG( Logger::DEBUG, std::string("New client connected: "). + append(connection->getHost()).append(":"). + append(connection->getPort()).c_str() ); + + m_connectionPool[client_socket] = new Connection(client_socket); + addFd( client_socket, POLLIN | POLLPRI ); } virtual void handleClient( const int socket ) diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index 416ca4e..9ab5958 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -39,21 +39,16 @@ private: protected: /// @todo this is unclear and nasty hack - virtual void acceptClient( const int socket ) + virtual void acceptClient() { TRACE; - LOG( Logger::DEBUG, std::string("own socket: "). - append( TToStr(m_tcpClient.m_connection.getSocket())). - append( " param socket: "). - append( TToStr( socket) ).c_str() ); - m_tcpClient.m_connection.receive(); stopPolling(); } /// @todo this is unclear and nasty hack - virtual void handleClient( const int socket ) + virtual void handleClient( const int ) { TRACE; LOG( Logger::DEBUG, "Server closed the connection." ); diff --git a/other/tcpclient_main.cpp b/other/tcpclient_main.cpp index 8906e4f..86106b6 100644 --- a/other/tcpclient_main.cpp +++ b/other/tcpclient_main.cpp @@ -49,16 +49,16 @@ protected: }; -int main( int argc, char * argv[] ) +int main() { Logger::createInstance(); Logger::init(std::cout); - Logger::setLogLevel(Logger::FINEST); + Logger::setLogLevel(Logger::DEBUG); int *a = new int; *a=2; - TcpClient tcpclient("localhost", "4455"); + TcpClient tcpclient("127.0.0.1", "4455"); tcpclient.connect(); diff --git a/other/tcpserver_main.cpp b/other/tcpserver_main.cpp index ce0c305..362deb8 100644 --- a/other/tcpserver_main.cpp +++ b/other/tcpserver_main.cpp @@ -1,4 +1,5 @@ -// gpp tcpServer_main.cpp -o client -I../include ../src/Logger.cpp ../src/TcpClient.cpp +// gpp tcpserver_main.cpp -o server -I../include ../src/Logger.cpp ../src/Socket.cpp + #include "Logger.hpp" #include "Common.hpp" @@ -54,7 +55,7 @@ protected: }; -int main( int argc, char * argv[] ) +int main() { Logger::createInstance(); Logger::init(std::cout); diff --git a/src/Poll.cpp b/src/Poll.cpp deleted file mode 100644 index c0d102f..0000000 --- a/src/Poll.cpp +++ /dev/null @@ -1,151 +0,0 @@ -#include "Poll.hpp" - -#include "Logger.hpp" -#include "Common.hpp" - -#include "Socket.hpp" - -#include // malloc, free - - -Poll::Poll ( Connection &connection, const nfds_t maxClient ) - : m_connection(connection) - , m_polling(false) - , m_maxclients(maxClient) - , m_fds(0) - , m_num_of_fds(0) -{ - TRACE; - - m_fds = new pollfd[m_maxclients]; -} - - -Poll::~Poll() -{ - TRACE; - - delete[] m_fds; -} - - -void Poll::startPolling() -{ - TRACE; - - m_polling = true; - struct timespec tm = {0,1000}; - - while ( m_polling ) { - - nanosleep(&tm, &tm) ; - int ret = poll( m_fds , m_maxclients, 1000); - - if ( ret == -1 ) { - LOG( Logger::ERR, errnoToString("ERROR polling. ").c_str() ); - /// @todo reconnect - return; - } - - if ( ret == 0 ) // timeout - continue; - - for ( nfds_t i = 0; i < m_num_of_fds; ++i ) - if ( m_fds[i].revents != 0 ) - m_fds[i].fd == m_connection.getSocket() ? - acceptClient() : - handleClient(m_fds[i].fd); - - } // while -} - - -void Poll::stopPolling() -{ - TRACE; - - m_polling = false; -} - - -void Poll::acceptClient() -{ - TRACE; - - sockaddr clientAddr; - socklen_t clientAddrLen; - int client_socket = accept( m_connection.getSocket(), &clientAddr, &clientAddrLen ) ; - - if ( client_socket == -1 ) { - LOG( Logger::ERR, errnoToString("ERROR accepting. ").c_str() ); - } else { - - std::string clientAddress, clientService; - if ( Socket::convertNameInfo(&clientAddr, clientAddrLen, - clientAddress, clientService ) ) { - LOG( Logger::DEBUG, std::string("New client connected: "). - append(clientAddress).append(":"). - append(clientService).c_str() ); - } - - m_connectionPool.insert ( - std::pair( client_socket, Connection(client_socket)) ); - addFd( client_socket, POLLIN | POLLPRI ); - } -} - - -void Poll::handleClient( const int socket ) -{ - TRACE; - - ConnectionPool::iterator it = m_connectionPool.find(socket); - - if ( it == m_connectionPool.end() || !it->second.receive() ) { - m_connectionPool.erase(socket); - removeFd(socket); - } -} - - -bool Poll::addFd( const int socket, const short events ) -{ - TRACE; - LOG( Logger::DEBUG, std::string("Adding socket: "). - append(TToStr(socket)).c_str() ); - - if (m_num_of_fds >= m_maxclients ) - return false; - - m_fds[m_num_of_fds].fd = socket; - m_fds[m_num_of_fds].events = events; - m_fds[m_num_of_fds].revents = 0; - m_num_of_fds++; - - return true; -} - - -bool Poll::removeFd( const int socket ) -{ - TRACE; - LOG( Logger::DEBUG, std::string("Removing socket: "). - append(TToStr(socket)).c_str() ); - - unsigned int i = 0 ; - while (i < m_maxclients && m_fds[i].fd != socket ) - i++; - - if ( i == m_maxclients ) - return false; - - for ( ; i < m_maxclients - 1; ++i ) - m_fds[i] = m_fds[i+1] ; - - m_fds[i].fd = 0 ; - m_fds[i].events = 0 ; - m_fds[i].revents = 0 ; - m_num_of_fds--; - - return true; -} \ No newline at end of file diff --git a/src/Socket.cpp b/src/Socket.cpp index 0c979c0..048cacd 100644 --- a/src/Socket.cpp +++ b/src/Socket.cpp @@ -194,7 +194,7 @@ void Socket::getPeerName( std::string &host, char tmp[INET_ADDRSTRLEN]; host = inet_ntop(AF_INET, &address.sin_addr, tmp, INET_ADDRSTRLEN); - port = address.sin_port; + port = TToStr(address.sin_port); }