SocketListener.cc
Go to the documentation of this file.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 <sys/types.h>
00035 #include <sys/socket.h>
00036
00037 #include <cstring>
00038 #include <cerrno>
00039
00040 #include "SocketListener.h"
00041 #include "BESInternalError.h"
00042 #include "Socket.h"
00043 #include "SocketConfig.h"
00044
00045 SocketListener::SocketListener()
00046 : _accepting( false )
00047 {
00048 }
00049
00050 SocketListener::~SocketListener()
00051 {
00052 }
00053
00054 void
00055 SocketListener::listen( Socket *s )
00056 {
00057 if( _accepting )
00058 {
00059 string err = (string)"Already accepting connections, "
00060 + "no more sockets can be added" ;
00061 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00062 }
00063
00064 if( s && !s->isConnected() && !s->isListening() )
00065 {
00066 s->listen() ;
00067 _socket_list[s->getSocketDescriptor()] = s ;
00068 }
00069 else
00070 {
00071 if( !s )
00072 {
00073 throw BESInternalError( "null socket passed", __FILE__, __LINE__ ) ;
00074 }
00075 else if( s->isConnected() )
00076 {
00077 string err( "socket already connected, cannot listen" ) ;
00078 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00079 }
00080 else if( s->isListening() )
00081 {
00082 string err( "socket already listening" ) ;
00083 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00084 }
00085 }
00086 }
00087
00089 Socket *
00090 SocketListener::accept()
00091 {
00092 int msgsock ;
00093
00094 fd_set read_fd ;
00095 struct timeval timeout ;
00096
00097 int maxfd ;
00098
00099 for(;;)
00100 {
00101 timeout.tv_sec = 120 ;
00102 timeout.tv_usec = 0 ;
00103
00104 FD_ZERO( &read_fd ) ;
00105
00106 maxfd = 0 ;
00107 Socket_citer iter = _socket_list.begin() ;
00108 for( ; iter != _socket_list.end(); iter++ )
00109 {
00110 Socket *s_ptr = (*iter).second ;
00111 int s = s_ptr->getSocketDescriptor() ;
00112 if( s > maxfd ) maxfd = s ;
00113 FD_SET( s, &read_fd ) ;
00114 }
00115
00116 if( select( maxfd+1, &read_fd,
00117 (fd_set*)NULL, (fd_set*)NULL, &timeout) < 0 )
00118 {
00119 string err( "selecting sockets " ) ;
00120 const char *error_info = strerror( errno ) ;
00121 if( error_info )
00122 err += " " + (string)error_info ;
00123 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00124 }
00125
00126 iter = _socket_list.begin() ;
00127 for( ; iter != _socket_list.end(); iter++ )
00128 {
00129 Socket *s_ptr = (*iter).second ;
00130 int s = s_ptr->getSocketDescriptor() ;
00131 if ( FD_ISSET( s, &read_fd ) )
00132 {
00133 struct sockaddr from ;
00134 int len_from = sizeof( from ) ;
00135 #ifdef _ACCEPT_USES_SOCKLEN_T
00136 msgsock = ::accept( s, (struct sockaddr *)&from,
00137 (socklen_t *)&len_from ) ;
00138 #else
00139 msgsock = ::accept( s, (struct sockaddr *)&from,
00140 &len_from ) ;
00141 #endif
00142 return s_ptr->newSocket( msgsock, (struct sockaddr *)&from );
00143 }
00144 }
00145 }
00146 return 0 ;
00147 }
00148
00155 void
00156 SocketListener::dump( ostream &strm ) const
00157 {
00158 strm << BESIndent::LMarg << "SocketListener::dump - ("
00159 << (void *)this << ")" << endl ;
00160 BESIndent::Indent() ;
00161 if( _socket_list.size() )
00162 {
00163 strm << BESIndent::LMarg << "registered sockets:" << endl ;
00164 Socket_citer i = _socket_list.begin() ;
00165 Socket_citer ie = _socket_list.end() ;
00166 for( ; i != ie; i++ )
00167 {
00168 strm << BESIndent::LMarg << "socket: " << (*i).first ;
00169 Socket *s_ptr = (*i).second ;
00170 s_ptr->dump( strm ) ;
00171 }
00172 }
00173 else
00174 {
00175 strm << BESIndent::LMarg << "registered sockets: none" << endl ;
00176 }
00177 BESIndent::UnIndent() ;
00178 }
00179