TcpServer uses class Poll

master
Denes Matetelki 13 years ago
parent 31afbb660d
commit e85a403f44

@ -7,26 +7,34 @@ class Poll
{ {
public: public:
Poll( const int socket, const nfds_t maxClient ); Poll( int &socket, const nfds_t maxClient );
virtual ~Poll(); virtual ~Poll();
void startPolling(); void startPolling();
void stopPolling(); void stopPolling();
virtual bool acceptClient(); virtual void acceptClient();
virtual bool handleClient( const int fd ); virtual void handleClient( const int fd );
bool receive( const int fd ); virtual bool receive( const int fd ) = 0;
bool addFd( const int fd, const short events ); bool addFd( const int fd, const short events );
bool removeFd( const int fd ); bool removeFd( const int fd );
protected:
bool m_polling;
private: private:
int m_socket; Poll(const Poll&);
Poll& operator=(const Poll&);
int &m_pollSocket;
nfds_t m_maxclients; nfds_t m_maxclients;
pollfd *m_fds; pollfd *m_fds;
nfds_t m_num_of_fds; nfds_t m_num_of_fds;
bool m_polling;
}; };

@ -24,6 +24,10 @@ public:
bool bindToHost(const std::string host, bool bindToHost(const std::string host,
const std::string port ); const std::string port );
static bool convertNameInfo( sockaddr* addr,
socklen_t addrLen,
std::string &retAddr,
std::string &retService);
protected: protected:
@ -36,10 +40,6 @@ protected:
sockaddr m_addr; sockaddr m_addr;
socklen_t m_addrLen; socklen_t m_addrLen;
static bool convertNameInfo( sockaddr* addr,
socklen_t addrLen,
std::string &retAddr,
std::string &retService);
private: private:

@ -2,11 +2,13 @@
#define TCP_SERVER_HPP #define TCP_SERVER_HPP
#include "Socket.hpp" #include "Socket.hpp"
#include "Poll.hpp"
#include <string> #include <string>
#include <poll.h> #include <poll.h>
class TcpServer : public Socket class TcpServer : public Socket,
public Poll
{ {
public: public:
@ -19,6 +21,9 @@ public:
bool start(); bool start();
void stop(); void stop();
// implements Poll::receive
bool receive( const int fd );
virtual void msgArrived(const int clientSocket, virtual void msgArrived(const int clientSocket,
const std::string msg) = 0; const std::string msg) = 0;
@ -27,19 +32,8 @@ private:
TcpServer(const TcpServer&); TcpServer(const TcpServer&);
TcpServer& operator=(const TcpServer&); TcpServer& operator=(const TcpServer&);
bool receive(const int clientSocket);
void addFd( int fd, short events );
void removeFd( int fd );
std::string m_host; std::string m_host;
std::string m_port; std::string m_port;
nfds_t m_maxclients;
bool m_running;
pollfd *m_fds;
nfds_t m_num_of_fds;
sockaddr m_addr;
socklen_t m_addrLen;
}; };
#endif // TCP_SERVER_HPP #endif // TCP_SERVER_HPP

@ -11,12 +11,12 @@
#include <stdlib.h> #include <stdlib.h>
Poll::Poll ( const int socket, const nfds_t maxClient ) Poll::Poll ( int &socket, const nfds_t maxClient )
: m_socket(socket) : m_polling(false)
, m_maxclients() , m_pollSocket(socket)
, m_maxclients(maxClient)
, m_fds(0) , m_fds(0)
, m_num_of_fds(0) , m_num_of_fds(0)
, m_polling(false)
{ {
TRACE; TRACE;
@ -36,6 +36,7 @@ void Poll::startPolling()
{ {
TRACE; TRACE;
m_polling = true;
struct timespec tm = {0,1000}; struct timespec tm = {0,1000};
while ( m_polling ) { while ( m_polling ) {
@ -45,24 +46,21 @@ void Poll::startPolling()
if ( ret == -1 ) { if ( ret == -1 ) {
LOG( Logger::ERR, errnoToString("ERROR polling. ").c_str() ); LOG( Logger::ERR, errnoToString("ERROR polling. ").c_str() );
return false; /// @todo shall we handle this?
return;
} }
if ( ret == 0 ) // timeout if ( ret == 0 ) // timeout
continue; continue;
for ( nfds_t i = 0; i < m_num_of_fds; ++i ) { for ( nfds_t i = 0; i < m_num_of_fds; ++i )
if ( m_fds[i].revents != 0 ) { if ( m_fds[i].revents != 0 )
m_fds[i].fd == m_pollSocket ?
if ( m_fds[i].fd == m_socket ) { acceptClient() :
acceptClient(m_fds[i].fd);
}
else {
handleClient(m_fds[i].fd); handleClient(m_fds[i].fd);
}
}
} } // while
}
} }
@ -74,13 +72,13 @@ void Poll::stopPolling()
} }
bool Poll::acceptClient() void Poll::acceptClient()
{ {
TRACE; TRACE;
sockaddr clientAddr; sockaddr clientAddr;
socklen_t clientAddrLen; socklen_t clientAddrLen;
int client_socket = accept( m_socket, &clientAddr, &clientAddrLen ) ; int client_socket = accept( m_pollSocket, &clientAddr, &clientAddrLen ) ;
if ( client_socket == -1 ) { if ( client_socket == -1 ) {
LOG( Logger::ERR, errnoToString("ERROR accepting. ").c_str() ); LOG( Logger::ERR, errnoToString("ERROR accepting. ").c_str() );
@ -109,13 +107,6 @@ void Poll::handleClient( const int fd )
} }
bool Poll::receive( const int fd)
{
TRACE;
}
bool Poll::addFd( const int fd, short events ) bool Poll::addFd( const int fd, short events )
{ {
TRACE; TRACE;

@ -10,24 +10,17 @@ TcpServer::TcpServer( const std::string host,
const std::string port, const std::string port,
const int maxClients ) const int maxClients )
: Socket(AF_INET, SOCK_STREAM) : Socket(AF_INET, SOCK_STREAM)
, Poll(m_socket, maxClients)
, m_host(host) , m_host(host)
, m_port(port) , m_port(port)
, m_maxclients(maxClients)
, m_running(false)
, m_fds(0)
, m_num_of_fds(0)
, m_addr()
, m_addrLen(0)
{ {
TRACE; TRACE;
m_fds = (pollfd*) malloc (sizeof(struct pollfd)*m_maxclients);
} }
TcpServer::~TcpServer() TcpServer::~TcpServer()
{ {
TRACE; TRACE;
free(m_fds);
} }
@ -46,59 +39,8 @@ bool TcpServer::start()
return false; return false;
} }
addFd( m_socket, POLLIN | POLLPRI ) ; addFd( m_socket, POLLIN | POLLPRI ) ;
startPolling();
m_running = true;
struct timespec tm = {0,1000};
while ( m_running ) {
nanosleep(&tm, &tm) ;
int ret = poll( m_fds , m_maxclients, 1000);
if ( ret == -1 ) {
LOG( Logger::ERR, errnoToString("ERROR polling. ").c_str() );
return false;
}
if ( ret == 0 ) // timeout
continue;
for ( nfds_t i = 0; i < m_num_of_fds; ++i ) {
if ( m_fds[i].revents != 0 ) {
if ( m_fds[i].fd == m_socket ) {
sockaddr clientAddr;
socklen_t clientAddrLen;
int client_socket = accept( m_socket, &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() );
}
addFd( client_socket, POLLIN | POLLPRI );
}
}
else {
if ( !receive( m_fds[i].fd ) ) {
removeFd(m_fds[i].fd);
}
}
}
}
}
return true; return true;
} }
@ -108,8 +50,7 @@ void TcpServer::stop()
{ {
TRACE; TRACE;
m_running = false; stopPolling();
closeSocket(); closeSocket();
} }
@ -136,35 +77,3 @@ bool TcpServer::receive(const int clientSocket)
return true; return true;
} }
void TcpServer::addFd( int fd, short events )
{
TRACE;
if (m_num_of_fds < m_maxclients )
{
m_fds[m_num_of_fds].fd = fd ;
m_fds[m_num_of_fds].events = events ;
m_fds[m_num_of_fds].revents = 0 ;
++m_num_of_fds ;
}
}
void TcpServer::removeFd( int fd )
{
TRACE;
unsigned int i = 0 ;
while (i < m_maxclients && m_fds[i].fd != fd ) ++i ;
if ( i != m_maxclients ) {
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 ;
}
}
Loading…
Cancel
Save