ACE_SOCK_Acceptor Class Reference

Defines a factory that creates new s passively. More...

#include <SOCK_Acceptor.h>

Inheritance diagram for ACE_SOCK_Acceptor:

Inheritance graph
[legend]
Collaboration diagram for ACE_SOCK_Acceptor:

Collaboration graph
[legend]
List of all members.

Public Types

typedef ACE_INET_Addr PEER_ADDR
typedef ACE_SOCK_Stream PEER_STREAM

Public Member Functions

 ACE_SOCK_Acceptor (void)
 Default constructor.

 ACE_SOCK_Acceptor (const ACE_Addr &local_sap, int reuse_addr=0, int protocol_family=PF_UNSPEC, int backlog=ACE_DEFAULT_BACKLOG, int protocol=0)
 ACE_SOCK_Acceptor (const ACE_Addr &local_sap, ACE_Protocol_Info *protocolinfo, ACE_SOCK_GROUP g, u_long flags, int reuse_addr, int protocol_family=PF_UNSPEC, int backlog=ACE_DEFAULT_BACKLOG, int protocol=0)
int open (const ACE_Addr &local_sap, int reuse_addr=0, int protocol_family=PF_UNSPEC, int backlog=ACE_DEFAULT_BACKLOG, int protocol=0)
int open (const ACE_Addr &local_sap, ACE_Protocol_Info *protocolinfo, ACE_SOCK_GROUP g, u_long flags, int reuse_addr, int protocol_family=PF_UNSPEC, int backlog=ACE_DEFAULT_BACKLOG, int protocol=0)
int close (void)
 Close the socket. Returns 0 on success and -1 on failure.

 ~ACE_SOCK_Acceptor (void)
 Default dtor.

int accept (ACE_SOCK_Stream &new_stream, ACE_Addr *remote_addr=0, ACE_Time_Value *timeout=0, int restart=1, int reset_new_handle=0) const
int accept (ACE_SOCK_Stream &new_stream, ACE_Accept_QoS_Params qos_params, ACE_Addr *remote_addr=0, ACE_Time_Value *timeout=0, int restart=1, int reset_new_handle=0) const
void dump (void) const
 Dump the state of an object.


Public Attributes

 ACE_ALLOC_HOOK_DECLARE
 Declare the dynamic allocation hooks.


Protected Member Functions

int shared_accept_start (ACE_Time_Value *timeout, int restart, int &in_blocking_mode) const
int shared_accept_finish (ACE_SOCK_Stream new_stream, int in_blocking_mode, int reset_new_handle) const
int shared_open (const ACE_Addr &local_sap, int protocol_family, int backlog)

Private Member Functions

int get_remote_addr (ACE_Addr &) const
 Do not allow this function to percolate up to this interface...


Detailed Description

Defines a factory that creates new s passively.

The ACE_SOCK_Acceptor has its own "passive-mode" socket. This serves as a factory to create so-called "data-mode" sockets, which are what the ACE_SOCK_Stream encapsulates. Therefore, by inheriting from ACE_SOCK, ACE_SOCK_Acceptor gets its very own socket.

Definition at line 39 of file SOCK_Acceptor.h.


Member Typedef Documentation

typedef ACE_INET_Addr ACE_SOCK_Acceptor::PEER_ADDR
 

Reimplemented in ACE_LSOCK_Acceptor, and ACE_MEM_Acceptor.

Definition at line 135 of file SOCK_Acceptor.h.

typedef ACE_SOCK_Stream ACE_SOCK_Acceptor::PEER_STREAM
 

Reimplemented in ACE_LSOCK_Acceptor, and ACE_MEM_Acceptor.

Definition at line 136 of file SOCK_Acceptor.h.


Constructor & Destructor Documentation

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_SOCK_Acceptor::ACE_SOCK_Acceptor void   ) 
 

Default constructor.

Definition at line 27 of file SOCK_Acceptor.cpp.

References ACE_TRACE.

00028 {
00029   ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00030 }

ACE_SOCK_Acceptor::ACE_SOCK_Acceptor const ACE_Addr local_sap,
int  reuse_addr = 0,
int  protocol_family = PF_UNSPEC,
int  backlog = ACE_DEFAULT_BACKLOG,
int  protocol = 0
 

Initialize a passive-mode BSD-style acceptor socket (no QoS). local_sap is the address that we're going to listen for connections on. If reuse_addr is 1 then we'll use the SO_REUSEADDR to reuse this address.

Definition at line 385 of file SOCK_Acceptor.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TRACE, LM_ERROR, and open().

00390 {
00391   ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00392   if (this->open (local_sap,
00393                   reuse_addr,
00394                   protocol_family,
00395                   backlog,
00396                   protocol) == -1)
00397     ACE_ERROR ((LM_ERROR,
00398                 ACE_LIB_TEXT ("%p\n"),
00399                 ACE_LIB_TEXT ("ACE_SOCK_Acceptor")));
00400 }

ACE_SOCK_Acceptor::ACE_SOCK_Acceptor const ACE_Addr local_sap,
ACE_Protocol_Info protocolinfo,
ACE_SOCK_GROUP  g,
u_long  flags,
int  reuse_addr,
int  protocol_family = PF_UNSPEC,
int  backlog = ACE_DEFAULT_BACKLOG,
int  protocol = 0
 

Initialize a passive-mode QoS-enabled acceptor socket. Returns 0 on success and -1 on failure.

Definition at line 327 of file SOCK_Acceptor.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_SOCK_GROUP, ACE_TRACE, LM_ERROR, and open().

00335 {
00336   ACE_TRACE ("ACE_SOCK_Acceptor::ACE_SOCK_Acceptor");
00337   if (this->open (local_sap,
00338                   protocolinfo,
00339                   g,
00340                   flags,
00341                   reuse_addr,
00342                   protocol_family,
00343                   backlog,
00344                   protocol) == -1)
00345     ACE_ERROR ((LM_ERROR,
00346                 ACE_LIB_TEXT ("%p\n"),
00347                 ACE_LIB_TEXT ("ACE_SOCK_Acceptor")));
00348 }

ACE_BEGIN_VERSIONED_NAMESPACE_DECL ACE_INLINE ACE_SOCK_Acceptor::~ACE_SOCK_Acceptor void   ) 
 

Default dtor.

Definition at line 8 of file SOCK_Acceptor.inl.

References ACE_TRACE.

00009 {
00010   ACE_TRACE ("ACE_SOCK_Acceptor::~ACE_SOCK_Acceptor");
00011 }


Member Function Documentation

int ACE_SOCK_Acceptor::accept ACE_SOCK_Stream new_stream,
ACE_Accept_QoS_Params  qos_params,
ACE_Addr remote_addr = 0,
ACE_Time_Value timeout = 0,
int  restart = 1,
int  reset_new_handle = 0
const
 

Accept a new connection using the QoS information in . A of 0 means block forever, a of {0, 0} means poll. == 1 means "restart if interrupted," i.e., if errno == EINTR. Note that inherits the "blocking mode" of , i.e., if acceptor factory is in non-blocking mode, the will be in non-blocking mode and vice versa.

Reimplemented in ACE_MEM_Acceptor.

Definition at line 160 of file SOCK_Acceptor.cpp.

References ACE_TRACE, ACE_Addr::get_addr(), ACE_IPC_SAP::get_handle(), ACE_Addr::get_size(), ACE_IPC_SAP::set_handle(), ACE_Addr::set_size(), shared_accept_finish(), and shared_accept_start().

00166 {
00167   ACE_TRACE ("ACE_SOCK_Acceptor::accept");
00168 
00169   int in_blocking_mode = 0;
00170   if (this->shared_accept_start (timeout,
00171                                  restart,
00172                                  in_blocking_mode) == -1)
00173     return -1;
00174   else
00175     {
00176       // On Win32 the third parameter to <accept> must be a NULL
00177       // pointer if we want to ignore the client's address.
00178       int *len_ptr = 0;
00179       int len = 0;
00180       sockaddr *addr = 0;
00181 
00182       if (remote_addr != 0)
00183         {
00184           len = remote_addr->get_size ();
00185           len_ptr = &len;
00186           addr = (sockaddr *) remote_addr->get_addr ();
00187         }
00188 
00189       do
00190         new_stream.set_handle (ACE_OS::accept (this->get_handle (),
00191                                                addr,
00192                                                len_ptr,
00193                                                qos_params));
00194       while (new_stream.get_handle () == ACE_INVALID_HANDLE
00195              && restart != 0
00196              && errno == EINTR
00197              && timeout == 0);
00198 
00199       // Reset the size of the addr, which is only necessary for UNIX
00200       // domain sockets.
00201       if (new_stream.get_handle () != ACE_INVALID_HANDLE
00202           && remote_addr != 0)
00203         remote_addr->set_size (len);
00204     }
00205 
00206   return this->shared_accept_finish (new_stream,
00207                                      in_blocking_mode,
00208                                      reset_new_handle);
00209 }

int ACE_SOCK_Acceptor::accept ACE_SOCK_Stream new_stream,
ACE_Addr remote_addr = 0,
ACE_Time_Value timeout = 0,
int  restart = 1,
int  reset_new_handle = 0
const
 

Accept a new connection. A of 0 means block forever, a of {0, 0} means poll. == 1 means "restart if interrupted," i.e., if errno == EINTR. Note that inherits the "blocking mode" of , i.e., if acceptor factory is in non-blocking mode, the will be in non-blocking mode and vice versa.

Reimplemented in ACE_LOCK_SOCK_Acceptor< ACE_LOCK >, and ACE_MEM_Acceptor.

Definition at line 105 of file SOCK_Acceptor.cpp.

References ACE_TRACE, ACE_Addr::get_addr(), ACE_IPC_SAP::get_handle(), ACE_Addr::get_size(), ACE_IPC_SAP::set_handle(), ACE_Addr::set_size(), ACE_Addr::set_type(), shared_accept_finish(), and shared_accept_start().

Referenced by ACE_LOCK_SOCK_Acceptor< ACE_LOCK >::accept(), ACE_Service_Manager::handle_input(), and ACE_Pipe::open().

00110 {
00111   ACE_TRACE ("ACE_SOCK_Acceptor::accept");
00112 
00113   int in_blocking_mode = 0;
00114   if (this->shared_accept_start (timeout,
00115                                  restart,
00116                                  in_blocking_mode) == -1)
00117     return -1;
00118   else
00119     {
00120       // On Win32 the third parameter to <accept> must be a NULL
00121       // pointer if we want to ignore the client's address.
00122       int *len_ptr = 0;
00123       sockaddr *addr = 0;
00124       int len = 0;
00125 
00126       if (remote_addr != 0)
00127         {
00128           len = remote_addr->get_size ();
00129           len_ptr = &len;
00130           addr = (sockaddr *) remote_addr->get_addr ();
00131         }
00132 
00133       do
00134         new_stream.set_handle (ACE_OS::accept (this->get_handle (),
00135                                                addr,
00136                                                len_ptr));
00137       while (new_stream.get_handle () == ACE_INVALID_HANDLE
00138              && restart != 0
00139              && errno == EINTR
00140              && timeout == 0);
00141 
00142       // Reset the size of the addr, so the proper UNIX/IPv4/IPv6 family
00143       // is known.
00144       if (new_stream.get_handle () != ACE_INVALID_HANDLE
00145           && remote_addr != 0)
00146         {
00147           remote_addr->set_size (len);
00148           if (addr)
00149             remote_addr->set_type (addr->sa_family);
00150         }
00151     }
00152 
00153   return this->shared_accept_finish (new_stream,
00154                                      in_blocking_mode,
00155                                      reset_new_handle);
00156 }

int ACE_SOCK_Acceptor::close void   ) 
 

Close the socket. Returns 0 on success and -1 on failure.

Reimplemented from ACE_SOCK.

Definition at line 403 of file SOCK_Acceptor.cpp.

References ACE_SOCK::close().

Referenced by ACE_Service_Manager::handle_close(), ACE_Pipe::open(), ACE_LSOCK_Acceptor::remove(), and shared_open().

00404 {
00405   return ACE_SOCK::close ();
00406 }

void ACE_SOCK_Acceptor::dump void   )  const
 

Dump the state of an object.

Reimplemented from ACE_SOCK.

Reimplemented in ACE_LSOCK_Acceptor, and ACE_MEM_Acceptor.

Definition at line 213 of file SOCK_Acceptor.cpp.

References ACE_TRACE.

00214 {
00215 #if defined (ACE_HAS_DUMP)
00216   ACE_TRACE ("ACE_SOCK_Acceptor::dump");
00217 #endif /* ACE_HAS_DUMP */
00218 }

int ACE_SOCK_Acceptor::get_remote_addr ACE_Addr  )  const [private]
 

Do not allow this function to percolate up to this interface...

Reimplemented from ACE_SOCK.

int ACE_SOCK_Acceptor::open const ACE_Addr local_sap,
ACE_Protocol_Info protocolinfo,
ACE_SOCK_GROUP  g,
u_long  flags,
int  reuse_addr,
int  protocol_family = PF_UNSPEC,
int  backlog = ACE_DEFAULT_BACKLOG,
int  protocol = 0
 

Initialize a passive-mode QoS-enabled acceptor socket. Returns 0 on success and -1 on failure.

Reimplemented in ACE_MEM_Acceptor.

Definition at line 299 of file SOCK_Acceptor.cpp.

References ACE_SOCK_GROUP, ACE_TRACE, ACE_Addr::get_type(), ACE_SOCK::open(), and shared_open().

00307 {
00308   ACE_TRACE ("ACE_SOCK_Acceptor::open");
00309 
00310   if (protocol_family == PF_UNSPEC)
00311     protocol_family = local_sap.get_type ();
00312 
00313   if (ACE_SOCK::open (SOCK_STREAM,
00314                       protocol_family,
00315                       protocol,
00316                       protocolinfo,
00317                       g,
00318                       flags,
00319                       reuse_addr) == -1)
00320     return -1;
00321   else
00322     return this->shared_open (local_sap,
00323                               protocol_family,
00324                               backlog);
00325 }

int ACE_SOCK_Acceptor::open const ACE_Addr local_sap,
int  reuse_addr = 0,
int  protocol_family = PF_UNSPEC,
int  backlog = ACE_DEFAULT_BACKLOG,
int  protocol = 0
 

Initialize a passive-mode BSD-style acceptor socket (no QoS). is the address that we're going to listen for connections on. If is 1 then we'll use the to reuse this address. Returns 0 on success and -1 on failure.

Reimplemented in ACE_LSOCK_Acceptor, and ACE_MEM_Acceptor.

Definition at line 353 of file SOCK_Acceptor.cpp.

References ACE_TRACE, ACE_Addr::get_type(), ACE::ipv6_enabled(), ACE_SOCK::open(), and shared_open().

Referenced by ACE_SOCK_Acceptor(), ACE_Service_Manager::open(), ACE_Pipe::open(), ACE_MEM_Acceptor::open(), and ACE_LSOCK_Acceptor::open().

00358 {
00359   ACE_TRACE ("ACE_SOCK_Acceptor::open");
00360 
00361   if (local_sap != ACE_Addr::sap_any)
00362     protocol_family = local_sap.get_type ();
00363   else if (protocol_family == PF_UNSPEC)
00364     {
00365 #if defined (ACE_HAS_IPV6)
00366       protocol_family = ACE::ipv6_enabled () ? PF_INET6 : PF_INET;
00367 #else
00368       protocol_family = PF_INET;
00369 #endif /* ACE_HAS_IPV6 */
00370     }
00371 
00372   if (ACE_SOCK::open (SOCK_STREAM,
00373                       protocol_family,
00374                       protocol,
00375                       reuse_addr) == -1)
00376     return -1;
00377   else
00378     return this->shared_open (local_sap,
00379                               protocol_family,
00380                               backlog);
00381 }

int ACE_SOCK_Acceptor::shared_accept_finish ACE_SOCK_Stream  new_stream,
int  in_blocking_mode,
int  reset_new_handle
const [protected]
 

Perform operations that must occur after <ACE_OS::accept> is called.

Definition at line 67 of file SOCK_Acceptor.cpp.

References ACE_NONBLOCK, ACE_TRACE, ACE::clr_flags(), and ACE_IPC_SAP::get_handle().

Referenced by accept(), and ACE_LSOCK_Acceptor::accept().

00070 {
00071   ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_finish ()");
00072 
00073   ACE_HANDLE new_handle = new_stream.get_handle ();
00074 
00075   // Check to see if we were originally in blocking mode, and if so,
00076   // set the <new_stream>'s handle and <this> handle to be in blocking
00077   // mode.
00078   if (in_blocking_mode)
00079     {
00080       // Save/restore errno.
00081       ACE_Errno_Guard error (errno);
00082 
00083       // Only disable ACE_NONBLOCK if we weren't in non-blocking mode
00084       // originally.
00085       ACE::clr_flags (this->get_handle (),
00086                       ACE_NONBLOCK);
00087       ACE::clr_flags (new_handle,
00088                       ACE_NONBLOCK);
00089     }
00090 
00091 #if defined (ACE_HAS_WINSOCK2) && (ACE_HAS_WINSOCK2 != 0)
00092   if (reset_new_handle)
00093     // Reset the event association inherited by the new handle.
00094     ::WSAEventSelect ((SOCKET) new_handle, 0, 0);
00095 #else
00096   ACE_UNUSED_ARG (reset_new_handle);
00097 #endif /* ACE_WIN32 */
00098 
00099   return new_handle == ACE_INVALID_HANDLE ? -1 : 0;
00100 }

int ACE_SOCK_Acceptor::shared_accept_start ACE_Time_Value timeout,
int  restart,
int &  in_blocking_mode
const [protected]
 

Perform operations that must occur before <ACE_OS::accept> is called.

Definition at line 35 of file SOCK_Acceptor.cpp.

References ACE_BIT_DISABLED, ACE_NONBLOCK, ACE_TRACE, ACE_IPC_SAP::get_handle(), ACE::handle_timed_accept(), and ACE::set_flags().

Referenced by accept(), ACE_MEM_Acceptor::accept(), and ACE_LSOCK_Acceptor::accept().

00038 {
00039   ACE_TRACE ("ACE_SOCK_Acceptor::shared_accept_start");
00040 
00041   ACE_HANDLE handle = this->get_handle ();
00042 
00043   // Handle the case where we're doing a timed <accept>.
00044   if (timeout != 0)
00045     {
00046       if (ACE::handle_timed_accept (handle,
00047                                     timeout,
00048                                     restart) == -1)
00049         return -1;
00050       else
00051         {
00052           in_blocking_mode = ACE_BIT_DISABLED (ACE::get_flags (handle),
00053                                                ACE_NONBLOCK);
00054           // Set the handle into non-blocking mode if it's not already
00055           // in it.
00056           if (in_blocking_mode
00057               && ACE::set_flags (handle,
00058                                  ACE_NONBLOCK) == -1)
00059             return -1;
00060         }
00061     }
00062 
00063   return 0;
00064 }

int ACE_SOCK_Acceptor::shared_open const ACE_Addr local_sap,
int  protocol_family,
int  backlog
[protected]
 

This method factors out the common code and is called by both the QoS-enabled method and the BSD-style method.

Definition at line 221 of file SOCK_Acceptor.cpp.

References ACE_ASSERT, ACE_NTOHL, ACE_TRACE, ACE_OS::bind(), ACE::bind_port(), close(), ACE_Addr::get_addr(), ACE_Addr::get_size(), ACE_OS::listen(), and ACE_OS::memset().

Referenced by open().

00224 {
00225   ACE_TRACE ("ACE_SOCK_Acceptor::shared_open");
00226   int error = 0;
00227 
00228 #if defined (ACE_HAS_IPV6)
00229   ACE_ASSERT (protocol_family == PF_INET || protocol_family == PF_INET6);
00230 
00231   if (protocol_family == PF_INET6)
00232     {
00233       sockaddr_in6 local_inet6_addr;
00234       ACE_OS::memset (reinterpret_cast<void *> (&local_inet6_addr),
00235                       0,
00236                       sizeof local_inet6_addr);
00237 
00238       if (local_sap == ACE_Addr::sap_any)
00239         {
00240           local_inet6_addr.sin6_family = AF_INET6;
00241           local_inet6_addr.sin6_port = 0;
00242           local_inet6_addr.sin6_addr = in6addr_any;
00243         }
00244       else
00245         local_inet6_addr = *reinterpret_cast<sockaddr_in6 *> (local_sap.get_addr ());
00246 
00247       // We probably don't need a bind_port written here.
00248       // There are currently no supported OS's that define
00249       // ACE_LACKS_WILDCARD_BIND.
00250       if (ACE_OS::bind (this->get_handle (),
00251                         reinterpret_cast<sockaddr *> (&local_inet6_addr),
00252                         sizeof local_inet6_addr) == -1)
00253         error = 1;
00254     }
00255   else
00256 #endif
00257   if (protocol_family == PF_INET)
00258     {
00259       sockaddr_in local_inet_addr;
00260       ACE_OS::memset (reinterpret_cast<void *> (&local_inet_addr),
00261                       0,
00262                       sizeof local_inet_addr);
00263 
00264       if (local_sap == ACE_Addr::sap_any)
00265         {
00266           local_inet_addr.sin_port = 0;
00267         }
00268       else
00269         local_inet_addr = *reinterpret_cast<sockaddr_in *> (local_sap.get_addr ());
00270       if (local_inet_addr.sin_port == 0)
00271         {
00272           if (ACE::bind_port (this->get_handle (),
00273                               ACE_NTOHL (ACE_UINT32 (local_inet_addr.sin_addr.s_addr))) == -1)
00274             error = 1;
00275         }
00276       else if (ACE_OS::bind (this->get_handle (),
00277                              reinterpret_cast<sockaddr *> (&local_inet_addr),
00278                              sizeof local_inet_addr) == -1)
00279         error = 1;
00280     }
00281   else if (ACE_OS::bind (this->get_handle (),
00282                          (sockaddr *) local_sap.get_addr (),
00283                          local_sap.get_size ()) == -1)
00284     error = 1;
00285 
00286   if (error != 0
00287       || ACE_OS::listen (this->get_handle (),
00288                          backlog) == -1)
00289     {
00290       ACE_Errno_Guard g (errno);    // Preserve across close() below.
00291       error = 1;
00292       this->close ();
00293     }
00294 
00295   return error ? -1 : 0;
00296 }


Member Data Documentation

ACE_SOCK_Acceptor::ACE_ALLOC_HOOK_DECLARE
 

Declare the dynamic allocation hooks.

Reimplemented from ACE_SOCK.

Reimplemented in ACE_LSOCK_Acceptor, and ACE_MEM_Acceptor.

Definition at line 142 of file SOCK_Acceptor.h.


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:29:46 2006 for ACE by doxygen 1.3.6