00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <ctype.h>
00034 #include <errno.h>
00035 #include <sys/types.h>
00036 #include <sys/socket.h>
00037
00038 #include "SocketListener.h"
00039 #include "SocketException.h"
00040 #include "Socket.h"
00041 #include "SocketConfig.h"
00042
00043 SocketListener::SocketListener()
00044 : _accepting( false )
00045 {
00046 }
00047
00048 SocketListener::~SocketListener()
00049 {
00050 }
00051
00052 void
00053 SocketListener::listen( Socket *s )
00054 {
00055 if( _accepting )
00056 {
00057 string err( "Already accepting connections ... no more sockets can be added" ) ;
00058 throw SocketException( err, __FILE__, __LINE__ ) ;
00059 }
00060
00061 if( s && !s->isConnected() && !s->isListening() )
00062 {
00063 s->listen() ;
00064 _socket_list[s->getSocketDescriptor()] = s ;
00065 }
00066 else
00067 {
00068 if( !s )
00069 {
00070 throw SocketException( "null socket passed", __FILE__, __LINE__ ) ;
00071 }
00072 else if( s->isConnected() )
00073 {
00074 string err( "socket already connected, cannot listen" ) ;
00075 throw SocketException( err, __FILE__, __LINE__ ) ;
00076 }
00077 else if( s->isListening() )
00078 {
00079 string err( "socket already listening" ) ;
00080 throw SocketException( err, __FILE__, __LINE__ ) ;
00081 }
00082 }
00083 }
00084
00086 Socket *
00087 SocketListener::accept()
00088 {
00089 int msgsock ;
00090
00091 fd_set read_fd ;
00092 struct timeval timeout ;
00093
00094 int maxfd ;
00095
00096 for(;;)
00097 {
00098 timeout.tv_sec = 120 ;
00099 timeout.tv_usec = 0 ;
00100
00101 FD_ZERO( &read_fd ) ;
00102
00103 maxfd = 0 ;
00104 Socket_citer iter = _socket_list.begin() ;
00105 for( ; iter != _socket_list.end(); iter++ )
00106 {
00107 Socket *s_ptr = (*iter).second ;
00108 int s = s_ptr->getSocketDescriptor() ;
00109 if( s > maxfd ) maxfd = s ;
00110 FD_SET( s, &read_fd ) ;
00111 }
00112
00113 if( select( maxfd+1, &read_fd,
00114 (fd_set*)NULL, (fd_set*)NULL, &timeout) < 0 )
00115 {
00116 string err( "selecting sockets " ) ;
00117 const char *error_info = strerror( errno ) ;
00118 if( error_info )
00119 err += " " + (string)error_info ;
00120 throw SocketException( err, __FILE__, __LINE__ ) ;
00121 }
00122
00123 iter = _socket_list.begin() ;
00124 for( ; iter != _socket_list.end(); iter++ )
00125 {
00126 Socket *s_ptr = (*iter).second ;
00127 int s = s_ptr->getSocketDescriptor() ;
00128 if ( FD_ISSET( s, &read_fd ) )
00129 {
00130 struct sockaddr_in from ;
00131 int len_from = sizeof( from ) ;
00132 #ifdef _ACCEPT_USES_SOCKLEN_T
00133 msgsock = ::accept( s, (struct sockaddr *)&from,
00134 (socklen_t *)&len_from ) ;
00135 #else
00136 msgsock = ::accept( s, (struct sockaddr *)&from,
00137 &len_from ) ;
00138 #endif
00139 return s_ptr->newSocket( msgsock, from ) ;
00140 }
00141 }
00142 }
00143 return 0 ;
00144 }
00145
00152 void
00153 SocketListener::dump( ostream &strm ) const
00154 {
00155 strm << BESIndent::LMarg << "SocketListener::dump - ("
00156 << (void *)this << ")" << endl ;
00157 BESIndent::Indent() ;
00158 if( _socket_list.size() )
00159 {
00160 strm << BESIndent::LMarg << "registered sockets:" << endl ;
00161 Socket_citer i = _socket_list.begin() ;
00162 Socket_citer ie = _socket_list.end() ;
00163 for( ; i != ie; i++ )
00164 {
00165 strm << BESIndent::LMarg << "socket: " << (*i).first ;
00166 Socket *s_ptr = (*i).second ;
00167 s_ptr->dump( strm ) ;
00168 }
00169 }
00170 else
00171 {
00172 strm << BESIndent::LMarg << "registered sockets: none" << endl ;
00173 }
00174 BESIndent::UnIndent() ;
00175 }
00176