From e0e7932a122614a0ff566fbfd8de5776de8b9f6d Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 17 May 2007 05:56:26 +0000 Subject: Lots of cool new stuff, the Server class actually works for everything except actually interacting with clients, and the Client class is almost there, except that it doesn't really do anything yet. --- src/server.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/server.cpp (limited to 'src/server.cpp') diff --git a/src/server.cpp b/src/server.cpp new file mode 100644 index 0000000..f93238c --- /dev/null +++ b/src/server.cpp @@ -0,0 +1,73 @@ +#include "server.h" +#include + +Bu::Server::Server() : + nTimeoutSec( 0 ), + nTimeoutUSec( 0 ) +{ + FD_ZERO( &fdActive ); +} + +Bu::Server::~Server() +{ +} + +void Bu::Server::addPort( int nPort, int nPoolSize ) +{ + ServerSocket *s = new ServerSocket( nPort, nPoolSize ); + int nSocket = s->getSocket(); + FD_SET( nSocket, &fdActive ); + hServers.insert( nSocket, s ); +} + +void Bu::Server::addPort( const FString &sAddr, int nPort, int nPoolSize ) +{ + ServerSocket *s = new ServerSocket( sAddr, nPort, nPoolSize ); + int nSocket = s->getSocket(); + FD_SET( nSocket, &fdActive ); + hServers.insert( nSocket, s ); +} + +void Bu::Server::setTimeout( int nTimeoutSec, int nTimeoutUSec ) +{ + this->nTimeoutSec = nTimeoutSec; + this->nTimeoutUSec = nTimeoutUSec; +} + +void Bu::Server::scan() +{ + struct timeval xTimeout = { nTimeoutSec, nTimeoutUSec }; + + fd_set fdRead = fdActive; + fd_set fdWrite = fdActive; + fd_set fdException = fdActive; + + if( TEMP_FAILURE_RETRY( select( FD_SETSIZE, &fdRead, NULL, &fdException, &xTimeout ) ) < 0 ) + { + throw ExceptionBase("Error attempting to scan open connections."); + } + + for( int j = 0; j < FD_SETSIZE; j++ ) + { + if( FD_ISSET( j, &fdRead ) ) + { + if( hServers.has( j ) ) + { + addClient( hServers.get( j )->accept() ); + } + else + { + + } + } + } +} + +void Bu::Server::addClient( int nSocket ) +{ + FD_SET( nSocket, &fdActive ); + + Client *c = new Client(); + hClients.insert( nSocket, c ); +} + -- cgit v1.2.3