Logger fix. Some warnings resolved. Poll.src deleted

master
Denes Matetelki 13 years ago
parent 9c347897b3
commit d3a28084af

@ -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:

@ -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

@ -39,6 +39,11 @@ protected:
/// @todo shall i use dinamic array?
std::string m_buffer;
private:
Message<T>(const Message<T> &);
Message<T>& operator=(const Message<T> &);
};

@ -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<T>(client_socket);
addFd( client_socket, POLLIN | POLLPRI );
return;
}
Connection<T> *connection = new Connection<T>(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<T>(client_socket);
addFd( client_socket, POLLIN | POLLPRI );
}
virtual void handleClient( const int socket )

@ -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." );

@ -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<SimpleMessage> tcpclient("localhost", "4455");
TcpClient<SimpleMessage> tcpclient("127.0.0.1", "4455");
tcpclient.connect();

@ -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);

@ -1,151 +0,0 @@
#include "Poll.hpp"
#include "Logger.hpp"
#include "Common.hpp"
#include "Socket.hpp"
#include <stdlib.h> // 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<int, Connection>( 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;
}

@ -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);
}

Loading…
Cancel
Save