TcpServer uses class Poll

master
Denes Matetelki 13 years ago
parent 31afbb660d
commit e85a403f44

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

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

@ -2,11 +2,13 @@
#define TCP_SERVER_HPP
#include "Socket.hpp"
#include "Poll.hpp"
#include <string>
#include <poll.h>
class TcpServer : public Socket
class TcpServer : public Socket,
public Poll
{
public:
@ -19,6 +21,9 @@ public:
bool start();
void stop();
// implements Poll::receive
bool receive( const int fd );
virtual void msgArrived(const int clientSocket,
const std::string msg) = 0;
@ -27,19 +32,8 @@ private:
TcpServer(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_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

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

@ -10,24 +10,17 @@ TcpServer::TcpServer( const std::string host,
const std::string port,
const int maxClients )
: Socket(AF_INET, SOCK_STREAM)
, Poll(m_socket, maxClients)
, m_host(host)
, m_port(port)
, m_maxclients(maxClients)
, m_running(false)
, m_fds(0)
, m_num_of_fds(0)
, m_addr()
, m_addrLen(0)
{
TRACE;
m_fds = (pollfd*) malloc (sizeof(struct pollfd)*m_maxclients);
}
TcpServer::~TcpServer()
{
TRACE;
free(m_fds);
}
@ -46,59 +39,8 @@ bool TcpServer::start()
return false;
}
addFd( m_socket, POLLIN | POLLPRI ) ;
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);
}
}
}
}
}
startPolling();
return true;
}
@ -108,8 +50,7 @@ void TcpServer::stop()
{
TRACE;
m_running = false;
stopPolling();
closeSocket();
}
@ -136,35 +77,3 @@ bool TcpServer::receive(const int clientSocket)
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